|
| 1 | +// udpate to latest built files of Vue |
| 2 | +require('./sync-sponsors') |
| 3 | + |
| 4 | +const fs = require('fs') |
| 5 | +const zlib = require('zlib') |
| 6 | +const axios = require('axios') |
| 7 | +const execSync = require('child_process').execSync |
| 8 | + |
| 9 | +const themeconfPath = 'themes/vue/_config.yml' |
| 10 | +const installPath = 'src/v2/guide/installation.md' |
| 11 | +const themeconfig = fs.readFileSync(themeconfPath, 'utf-8') |
| 12 | +const installation = fs.readFileSync(installPath, 'utf-8') |
| 13 | + |
| 14 | +// get latest Vue version |
| 15 | +console.log(`Checking latest Vue version...`) |
| 16 | +const localVersion = themeconfig.match(/vue_version: (.*)/)[1] |
| 17 | +const version = execSync('npm view vue@v2-latest version').toString().trim() |
| 18 | + |
| 19 | +if (localVersion === version) { |
| 20 | + console.log(`Version is up-to-date.`) |
| 21 | + process.exit(0) |
| 22 | +} |
| 23 | + |
| 24 | +console.log(`Latest version: ${version}. Downloading dist files...`) |
| 25 | + |
| 26 | +// replace version in theme config |
| 27 | +fs.writeFileSync( |
| 28 | + themeconfPath, |
| 29 | + themeconfig.replace(/vue_version: .*/, 'vue_version: ' + version) |
| 30 | +) |
| 31 | + |
| 32 | +// grab it from unpkg |
| 33 | +Promise.all([download(`vue.js`), download(`vue.min.js`)]) |
| 34 | + .then(([devSize, prodSize]) => { |
| 35 | + // replace installation page version and size |
| 36 | + fs.writeFileSync( |
| 37 | + installPath, |
| 38 | + installation |
| 39 | + .replace(/vue_version: .*/, 'vue_version: ' + version) |
| 40 | + .replace(/gz_size:.*/g, `gz_size: "${prodSize}"`) |
| 41 | + .replace(/\/vue@[\d\.]+/g, `/vue@${version}`) |
| 42 | + ) |
| 43 | + console.log( |
| 44 | + `\nSuccessfully updated Vue version (${version}) and gzip file size (${prodSize}kb).\n` |
| 45 | + ) |
| 46 | + }) |
| 47 | + .catch((err) => { |
| 48 | + console.error(err) |
| 49 | + process.exit(1) |
| 50 | + }) |
| 51 | + |
| 52 | +function download(file) { |
| 53 | + return axios({ |
| 54 | + url: `http://unpkg.com/vue@${version}/dist/${file}`, |
| 55 | + method: 'get' |
| 56 | + }).then((res) => { |
| 57 | + fs.writeFileSync(`themes/vue/source/js/${file}`, res.data) |
| 58 | + const zipped = zlib.gzipSync(Buffer.from(res.data)) |
| 59 | + return (zipped.length / 1024).toFixed(2) |
| 60 | + }) |
| 61 | +} |
0 commit comments