@speedy-js/mono
Version:
Monorepo development & continuous integration tooling.
182 lines • 5.92 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.release = exports.changelog = exports.patch = void 0;
/**
* Module dependencies
*/
const chalk_1 = __importDefault(require("chalk"));
const execa_1 = __importDefault(require("execa"));
const semver_1 = __importDefault(require("semver"));
const inquirer_1 = __importDefault(require("inquirer"));
const patch_1 = require("./patch");
Object.defineProperty(exports, "patch", { enumerable: true, get: function () { return patch_1.patch; } });
const shared_1 = require("./shared");
const changelog_1 = require("./changelog");
Object.defineProperty(exports, "changelog", { enumerable: true, get: function () { return changelog_1.changelog; } });
/**
* Select version and tag to be released.
*/
async function selectVersionAndTag(currentVersion) {
const customItem = { name: 'Custom', value: 'custom' };
const bumps = [
'patch',
'minor',
'major',
'prerelease',
'premajor',
];
const versions = bumps.reduce((memo, bump) => {
memo[bump] = semver_1.default.inc(currentVersion, bump);
return memo;
}, {});
const bumpChoices = bumps.map((b) => ({
name: `${b} (${versions[b]})`,
value: b,
}));
function getVersion(answers) {
return answers.customVersion || versions[answers.bump];
}
function getNpmTags(version) {
if (isPreRelease(version)) {
return ['next', 'latest', 'beta', customItem];
}
return ['latest', 'next', 'beta', customItem];
}
function isPreRelease(version) {
return Boolean(semver_1.default.prerelease(version));
}
const { bump, customVersion, npmTag, customNpmTag } = await inquirer_1.default.prompt([
{
name: 'bump',
message: 'Select release type:',
type: 'list',
choices: [...bumpChoices, customItem],
},
{
name: 'customVersion',
message: 'Input version:',
type: 'input',
when: (answers) => answers.bump === 'custom',
},
{
name: 'npmTag',
message: 'Input npm tag:',
type: 'list',
default: (answers) => getNpmTags(getVersion(answers))[0],
choices: (answers) => getNpmTags(getVersion(answers)),
},
{
name: 'customNpmTag',
message: 'Input customized npm tag:',
type: 'input',
when: (answers) => answers.npmTag === 'custom',
},
]);
const version = customVersion || versions[bump];
const tag = customNpmTag || npmTag;
return {
tag,
version,
};
}
async function release(options) {
var _a;
const cwd = (_a = options.cwd) !== null && _a !== void 0 ? _a : process.cwd();
const config = (0, shared_1.resolveLernaConfig)(cwd);
if (!(config === null || config === void 0 ? void 0 : config.path)) {
throw new Error('[MONO] "mono.json" or "lerna.json" doesn\'t exist');
}
const lernaConfig = config.data;
if (lernaConfig.version === 'independent') {
throw new Error('[MONO] "release" cannot be executed under "independent" mode');
}
if (typeof options.ready === 'function') {
await options.ready({ cwd });
}
shared_1.logger.info(`${chalk_1.default.gray('Current version: ')}${lernaConfig.version}`);
const { version, tag } = await selectVersionAndTag(lernaConfig.version);
const { yes } = await inquirer_1.default.prompt([
{
name: 'yes',
message: `Confirm releasing ${version} (${tag})?`,
type: 'list',
choices: ['N', 'Y'],
},
]);
if (yes === 'N') {
console.log('[MONO] cancelled.');
return;
}
/**
* Set this env for subsequent build process.
*/
process.env.MONO_RELEASE_VERSION = version;
/**
* Execute custom build script before release.
*/
if (options.build) {
const buildScript = typeof options.build === 'string' ? options.build : 'npm run build';
const [command, ...args] = buildScript.split(' ');
await (0, execa_1.default)(command, args, {
shell: true,
cwd: options.cwd,
stdio: 'inherit',
env: process.env,
});
}
let releaseArguments = [
'publish',
version,
'--exact',
'--force-publish',
'--dist-tag',
tag,
];
if (typeof options.modifyReleaseArguments === 'function') {
releaseArguments = options.modifyReleaseArguments({
arguments: releaseArguments,
version,
tag,
});
}
if (options.ignoreScripts) {
releaseArguments.push('--ignore-scripts');
}
(0, shared_1.ensureLernaConfig)(cwd);
try {
await (0, execa_1.default)(require.resolve('lerna/cli.js', {
paths: [cwd, __dirname],
}), releaseArguments, {
stdio: 'inherit',
});
}
catch (e) {
console.log(e);
await (0, patch_1.patch)({
cwd,
version,
tag,
runInBand: options.runInBand,
ignoreScripts: options.ignoreScripts,
});
}
if (typeof options.released === 'function') {
await options.released({ cwd, version, tag });
}
(0, shared_1.cleanLernaConfig)(cwd);
if (options.changelog) {
await (0, changelog_1.changelog)({
cwd,
beautify: true,
commit: true,
gitPush: true,
attachAuthor: true,
authorNameType: 'email',
});
}
}
exports.release = release;
//# sourceMappingURL=index.js.map