|
| 1 | +const { utils } = require('umi'); |
| 2 | +const { join } = require('path'); |
| 3 | +const exec = require('./utils/exec'); |
| 4 | +const getPackages = require('./utils/getPackages'); |
| 5 | +const isNextVersion = require('./utils/isNextVersion'); |
| 6 | + |
| 7 | +const { yParser, execa, chalk } = utils; |
| 8 | +const cwd = process.cwd(); |
| 9 | +const args = yParser(process.argv); |
| 10 | +const lernaCli = require.resolve('lerna/cli'); |
| 11 | + |
| 12 | +function printErrorAndExit(message) { |
| 13 | + console.error(chalk.red(message)); |
| 14 | + process.exit(1); |
| 15 | +} |
| 16 | + |
| 17 | +function logStep(name) { |
| 18 | + console.log(`${chalk.gray('>> Release:')} ${chalk.magenta.bold(name)}`); |
| 19 | +} |
| 20 | + |
| 21 | +async function release() { |
| 22 | + // Check git status |
| 23 | + if (!args.skipGitStatusCheck) { |
| 24 | + const gitStatus = execa.sync('git', ['status', '--porcelain']).stdout; |
| 25 | + if (gitStatus.length) { |
| 26 | + printErrorAndExit(`Your git status is not clean. Aborting.`); |
| 27 | + } |
| 28 | + } else { |
| 29 | + logStep( |
| 30 | + 'git status check is skipped, since --skip-git-status-check is supplied', |
| 31 | + ); |
| 32 | + } |
| 33 | + |
| 34 | + // Check npm registry |
| 35 | + logStep('check npm registry'); |
| 36 | + const userRegistry = execa.sync('npm', ['config', 'get', 'registry']).stdout; |
| 37 | + if (userRegistry.includes('https://registry.yarnpkg.com/')) { |
| 38 | + printErrorAndExit( |
| 39 | + `Release failed, please use ${chalk.blue('npm run release')}.`, |
| 40 | + ); |
| 41 | + } |
| 42 | + if (!userRegistry.includes('https://registry.npmjs.org/')) { |
| 43 | + const registry = chalk.blue('https://registry.npmjs.org/'); |
| 44 | + printErrorAndExit(`Release failed, npm registry must be ${registry}.`); |
| 45 | + } |
| 46 | + |
| 47 | + let updated = null; |
| 48 | + |
| 49 | + if (!args.publishOnly) { |
| 50 | + // Get updated packages |
| 51 | + logStep('check updated packages'); |
| 52 | + const updatedStdout = execa.sync(lernaCli, ['changed']).stdout; |
| 53 | + updated = updatedStdout |
| 54 | + .split('\n') |
| 55 | + .map(pkg => { |
| 56 | + return pkg.split('/')[1]; |
| 57 | + }) |
| 58 | + .filter(Boolean); |
| 59 | + if (!updated.length) { |
| 60 | + printErrorAndExit('Release failed, no updated package is updated.'); |
| 61 | + } |
| 62 | + |
| 63 | + // Clean |
| 64 | + logStep('clean'); |
| 65 | + |
| 66 | + // Build |
| 67 | + if (!args.skipBuild) { |
| 68 | + logStep('build'); |
| 69 | + await exec('npm', ['run', 'build']); |
| 70 | + } else { |
| 71 | + logStep('build is skipped, since args.skipBuild is supplied'); |
| 72 | + } |
| 73 | + |
| 74 | + // Bump version |
| 75 | + // Commit |
| 76 | + // Git Tag |
| 77 | + // Push |
| 78 | + logStep('bump version with lerna version'); |
| 79 | + const conventionalGraduate = args.conventionalGraduate |
| 80 | + ? ['--conventional-graduate'].concat( |
| 81 | + Array.isArray(args.conventionalGraduate) |
| 82 | + ? args.conventionalGraduate.join(',') |
| 83 | + : [], |
| 84 | + ) |
| 85 | + : []; |
| 86 | + const conventionalPrerelease = args.conventionalPrerelease |
| 87 | + ? ['--conventional-prerelease'].concat( |
| 88 | + Array.isArray(args.conventionalPrerelease) |
| 89 | + ? args.conventionalPrerelease.join(',') |
| 90 | + : [], |
| 91 | + ) |
| 92 | + : []; |
| 93 | + await exec( |
| 94 | + lernaCli, |
| 95 | + [ |
| 96 | + 'version', |
| 97 | + '--exact', |
| 98 | + // '--no-commit-hooks', |
| 99 | + // '--no-git-tag-version', |
| 100 | + // '--no-push', |
| 101 | + '--conventional-commits', |
| 102 | + ] |
| 103 | + .concat(conventionalGraduate) |
| 104 | + .concat(conventionalPrerelease), |
| 105 | + ); |
| 106 | + |
| 107 | + logStep('done'); |
| 108 | + return; |
| 109 | + } |
| 110 | + |
| 111 | + // Publish |
| 112 | + // Umi must be the latest. |
| 113 | + const pkgs = args.publishOnly ? getPackages() : updated; |
| 114 | + logStep(`publish packages: ${chalk.blue(pkgs.join(', '))}`); |
| 115 | + |
| 116 | + pkgs.forEach(pkg => { |
| 117 | + const pkgPath = join(cwd, 'packages', pkg); |
| 118 | + const { name, version } = require(join(pkgPath, 'package.json')); |
| 119 | + const isNext = isNextVersion(version); |
| 120 | + console.log(`Publish package ${name} ${isNext ? 'with next tag' : ''}`); |
| 121 | + const cliArgs = isNext ? ['publish', '--tag', 'next'] : ['publish']; |
| 122 | + const { stdout } = execa.sync('npm', cliArgs, { |
| 123 | + cwd: pkgPath, |
| 124 | + }); |
| 125 | + console.log(stdout); |
| 126 | + }); |
| 127 | +} |
| 128 | + |
| 129 | +release().catch(err => { |
| 130 | + console.error(err); |
| 131 | + process.exit(1); |
| 132 | +}); |
0 commit comments