|
| 1 | +const resolvePath = require('path').resolve |
| 2 | +const readFileSync = require('fs').readFileSync |
| 3 | +const execSync = require('child_process').execSync |
| 4 | +const prompt = require('readline-sync').question |
| 5 | + |
| 6 | +const exec = (command) => |
| 7 | + execSync(command, { stdio: 'inherit' }) |
| 8 | + |
| 9 | +const getPackageVersion = () => |
| 10 | + JSON.parse(readFileSync(resolvePath(__dirname, '../package.json'))).version |
| 11 | + |
| 12 | +if (process.cwd() !== resolvePath(__dirname, '..')) { |
| 13 | + console.error('The release script must be run from the repo root') |
| 14 | + process.exit(1) |
| 15 | +} |
| 16 | + |
| 17 | +// Get the next version, which may be specified as a semver |
| 18 | +// version number or anything `npm version` recognizes. This |
| 19 | +// is a "pre-release" if nextVersion is premajor, preminor, |
| 20 | +// prepatch, or prerelease |
| 21 | +const nextVersion = prompt(`Next version (current version is ${getPackageVersion()})? `) |
| 22 | +const isPrerelease = nextVersion.substr(0, 3) === 'pre' || nextVersion.indexOf('-') !== -1 |
| 23 | + |
| 24 | +// 1) Make sure the tests pass |
| 25 | +exec('npm test -- --single-run') |
| 26 | + |
| 27 | +// 2) Increment the package version in package.json |
| 28 | +// 3) Create a new commit |
| 29 | +// 4) Create a v* tag that points to that commit |
| 30 | +exec(`npm version ${nextVersion} -m "Version %s"`) |
| 31 | + |
| 32 | +// 5) Push to the same branch on the git remote (GitHub). |
| 33 | +// Do this before we publish in case anyone has pushed |
| 34 | +// since we last pulled |
| 35 | +exec('git push origin') |
| 36 | + |
| 37 | +// 6) Publish to npm. Use the "next" tag for pre-releases, |
| 38 | +// "latest" for all others |
| 39 | +exec(`npm publish --tag ${isPrerelease ? 'next' : 'latest'}`) |
| 40 | + |
| 41 | +// 7) Push the v* tag to GitHub |
| 42 | +exec(`git push -f origin v${getPackageVersion()}`) |
| 43 | + |
| 44 | +// 8) Push the "latest" tag to GitHub |
| 45 | +if (!isPrerelease) { |
| 46 | + exec('git tag -f latest') |
| 47 | + exec('git push -f origin latest') |
| 48 | +} |
0 commit comments