|
| 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 rust warnings |
| 23 | +console.info('Run cargo check') |
| 24 | +if ( |
| 25 | + childProcess.spawnSync('cargo', ['check'], { |
| 26 | + env: { RUSTFLAGS: '-D warnings', ...process.env }, |
| 27 | + stdio: 'inherit', |
| 28 | + }).status !== 0 |
| 29 | +) { |
| 30 | + throw new Error('failed to check rust modules (are there rust warnings or errors?)') |
| 31 | +} |
| 32 | + |
| 33 | +// force rust formatting |
| 34 | +console.info('Run cargo fmt --check') |
| 35 | +if ( |
| 36 | + childProcess.spawnSync('cargo', ['fmt', '--check'], { |
| 37 | + stdio: 'inherit', |
| 38 | + }).status !== 0 |
| 39 | +) { |
| 40 | + throw new Error('failed to check formatting of rust modules') |
| 41 | +} |
| 42 | + |
| 43 | +// avoid eslint warnings |
| 44 | +;['vscode-extension'].forEach((p) => { |
| 45 | + console.info(`Run eslint on ${p}`) |
| 46 | + if ( |
| 47 | + childProcess.spawnSync('npx', ['eslint', '-c', '.eslintrc.js', 'src'], { |
| 48 | + cwd: p, |
| 49 | + stdio: 'inherit', |
| 50 | + }).status !== 0 |
| 51 | + ) { |
| 52 | + throw new Error('failed to lint modules (are there eslint warnings or errors?)') |
| 53 | + } |
| 54 | +}) |
| 55 | + |
| 56 | +// check git status |
| 57 | +const gitStatusRes = childProcess.spawnSync('git', ['diff', '--name-only'], { encoding: 'utf8' }) |
| 58 | +if (gitStatusRes.status !== 0 || gitStatusRes.stdout.length > 0) { |
| 59 | + throw new Error('failed to check git status (are there uncommitted changes?)') |
| 60 | +} |
| 61 | + |
| 62 | +// change npm version |
| 63 | +;['vscode-extension/package.json'].forEach((p) => { |
| 64 | + let content = fs.readFileSync(p, { encoding: 'utf8' }) |
| 65 | + let oldVersion |
| 66 | + const refVersions = [] |
| 67 | + content = content.replace(/"version": "(.+)"/, (_, v) => { |
| 68 | + oldVersion = v |
| 69 | + return `"version": "${version}"` |
| 70 | + }) |
| 71 | + if (!oldVersion) { |
| 72 | + throw new Error(`version segment not found in ${p}`) |
| 73 | + } |
| 74 | + console.info(`Update ${p} version from "${oldVersion}" to "${version}"`) |
| 75 | + refVersions.forEach(({ mod, v }) => { |
| 76 | + console.info(` + dependency ${mod} version from "${v}" to "${version}"`) |
| 77 | + }) |
| 78 | + writeFileAndGitAdd(p, content) |
| 79 | +}) |
| 80 | + |
| 81 | +// change cargo version |
| 82 | +;['Cargo.toml'].forEach( |
| 83 | + (p) => { |
| 84 | + let content = fs.readFileSync(p, { encoding: 'utf8' }) |
| 85 | + let oldVersion |
| 86 | + content = content.replace(/\nversion = "(.+)"/, (_, v) => { |
| 87 | + oldVersion = v |
| 88 | + return `\nversion = "${version}"` |
| 89 | + }) |
| 90 | + if (!oldVersion) { |
| 91 | + throw new Error(`version segment not found in ${p}`) |
| 92 | + } |
| 93 | + console.info(`Update ${p} version from "${oldVersion}" to "${version}"`) |
| 94 | + writeFileAndGitAdd(p, content) |
| 95 | + }, |
| 96 | +) |
| 97 | + |
| 98 | +// npm test |
| 99 | +;['vscode-extension'].forEach((p) => { |
| 100 | + console.info(`Run npm test in ${p}`) |
| 101 | + if (childProcess.spawnSync('npm', ['test'], { cwd: p, stdio: 'inherit' }).status !== 0) { |
| 102 | + throw new Error('failed to run npm test') |
| 103 | + } |
| 104 | +}) |
| 105 | + |
| 106 | +// add lock files |
| 107 | +;['Cargo.lock', 'vscode-extension/package.json'].forEach((p) => { |
| 108 | + if (childProcess.spawnSync('git', ['add', p]).status !== 0) { |
| 109 | + throw new Error(`failed to execute git add on ${p}`) |
| 110 | + } |
| 111 | +}) |
| 112 | + |
| 113 | +// git commit |
| 114 | +if ( |
| 115 | + childProcess.spawnSync('git', ['commit', '--message', `version: ${version}`], { |
| 116 | + stdio: 'inherit', |
| 117 | + }).status !== 0 |
| 118 | +) { |
| 119 | + throw new Error('failed to execute git commit') |
| 120 | +} |
| 121 | + |
| 122 | +// add a git tag and push |
| 123 | +console.info('Push to git origin') |
| 124 | +if (childProcess.spawnSync('git', ['tag', `v${version}`]).status !== 0) { |
| 125 | + throw new Error('failed to execute git tag') |
| 126 | +} |
| 127 | +if (childProcess.spawnSync('git', ['push'], { stdio: 'inherit' }).status !== 0) { |
| 128 | + throw new Error('failed to execute git push') |
| 129 | +} |
| 130 | +if (childProcess.spawnSync('git', ['push', '--tags'], { stdio: 'inherit' }).status !== 0) { |
| 131 | + throw new Error('failed to execute git push --tags') |
| 132 | +} |
| 133 | + |
| 134 | +console.info('Version updated! Wait the remote actions to build and publish.') |
0 commit comments