|
| 1 | +/* eslint-disable no-console */ |
| 2 | + |
| 3 | +const fs = require('fs') |
| 4 | +const childProcess = require('child_process') |
| 5 | + |
| 6 | +const writeFileAndGitAdd = (p, content) => { |
| 7 | + fs.writeFileSync(p, content) |
| 8 | + if (childProcess.spawnSync('git', ['add', p]).status !== 0) { |
| 9 | + throw new Error(`failed to execute git add on ${p}`) |
| 10 | + } |
| 11 | +} |
| 12 | + |
| 13 | +// check arguments |
| 14 | +const version = process.argv[2] |
| 15 | +if (!version) { |
| 16 | + throw new Error('version not given in argv') |
| 17 | +} |
| 18 | +if (!/[0-9]+\.[0-9]+\.[0-9]+/.test(version)) { |
| 19 | + throw new Error('version illegal') |
| 20 | +} |
| 21 | + |
| 22 | +// avoid eslint warnings |
| 23 | +;['vscode-extension'].forEach((p) => { |
| 24 | + console.info(`Run eslint on ${p}`) |
| 25 | + if ( |
| 26 | + childProcess.spawnSync('npx', ['eslint', '-c', '.eslintrc.js', 'src'], { |
| 27 | + cwd: p, |
| 28 | + stdio: 'inherit', |
| 29 | + }).status !== 0 |
| 30 | + ) { |
| 31 | + throw new Error('failed to lint modules (are there eslint warnings or errors?)') |
| 32 | + } |
| 33 | +}) |
| 34 | + |
| 35 | +// check git status |
| 36 | +const gitStatusRes = childProcess.spawnSync('git', ['diff', '--name-only'], { encoding: 'utf8' }) |
| 37 | +if (gitStatusRes.status !== 0 || gitStatusRes.stdout.length > 0) { |
| 38 | + throw new Error('failed to check git status (are there uncommitted changes?)') |
| 39 | +} |
| 40 | + |
| 41 | +// change npm version |
| 42 | +;['package.json'].forEach((p) => { |
| 43 | + let content = fs.readFileSync(p, { encoding: 'utf8' }) |
| 44 | + let oldVersion |
| 45 | + const refVersions = [] |
| 46 | + content = content.replace(/"version": "(.+)"/, (_, v) => { |
| 47 | + oldVersion = v |
| 48 | + return `"version": "${version}"` |
| 49 | + }) |
| 50 | + if (!oldVersion) { |
| 51 | + throw new Error(`version segment not found in ${p}`) |
| 52 | + } |
| 53 | + console.info(`Update ${p} version from "${oldVersion}" to "${version}"`) |
| 54 | + refVersions.forEach(({ mod, v }) => { |
| 55 | + console.info(` + dependency ${mod} version from "${v}" to "${version}"`) |
| 56 | + }) |
| 57 | + writeFileAndGitAdd(p, content) |
| 58 | +}) |
| 59 | + |
| 60 | +// npm test |
| 61 | +console.info(`Run npm test`) |
| 62 | +if (childProcess.spawnSync('npm', ['test'], { stdio: 'inherit' }).status !== 0) { |
| 63 | + throw new Error('failed to run npm test') |
| 64 | +} |
| 65 | + |
| 66 | +// add lock files |
| 67 | +;['package.json'].forEach((p) => { |
| 68 | + if (childProcess.spawnSync('git', ['add', p]).status !== 0) { |
| 69 | + throw new Error(`failed to execute git add on ${p}`) |
| 70 | + } |
| 71 | +}) |
| 72 | + |
| 73 | +// git commit |
| 74 | +if ( |
| 75 | + childProcess.spawnSync('git', ['commit', '--message', `version: ${version}`], { |
| 76 | + stdio: 'inherit', |
| 77 | + }).status !== 0 |
| 78 | +) { |
| 79 | + throw new Error('failed to execute git commit') |
| 80 | +} |
| 81 | + |
| 82 | +// add a git tag and push |
| 83 | +console.info('Push to git origin') |
| 84 | +if (childProcess.spawnSync('git', ['tag', `v${version}`]).status !== 0) { |
| 85 | + throw new Error('failed to execute git tag') |
| 86 | +} |
| 87 | +if (childProcess.spawnSync('git', ['push'], { stdio: 'inherit' }).status !== 0) { |
| 88 | + throw new Error('failed to execute git push') |
| 89 | +} |
| 90 | +if (childProcess.spawnSync('git', ['push', '--tags'], { stdio: 'inherit' }).status !== 0) { |
| 91 | + throw new Error('failed to execute git push --tags') |
| 92 | +} |
| 93 | + |
| 94 | +console.info('Version updated! Wait the remote actions to build and publish.') |
0 commit comments