@eljs/release
Version:
Release npm package easily.
318 lines (316 loc) • 11.2 kB
JavaScript
var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
// If the importer is in node compatibility mode or this is not an ESM
// file that has been converted to a CommonJS file using a Babel-
// compatible transform (i.e. "__esModule" has not been set), then set
// "default" to the CommonJS "module.exports" for node compatibility.
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
mod
));
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
// src/internal/plugins/version.ts
var version_exports = {};
__export(version_exports, {
default: () => version_default
});
module.exports = __toCommonJS(version_exports);
var import_constants = require("../../constants");
var import_utils = require("../../utils");
var import_utils2 = require("@eljs/utils");
var import_node_os = require("node:os");
var import_semver = __toESM(require("semver"));
var debug = (0, import_utils2.createDebugger)("release:version");
var version_default = (api) => {
api.onCheck(async ({ releaseTypeOrVersion }) => {
if (releaseTypeOrVersion && !(0, import_utils.isVersionValid)(releaseTypeOrVersion, true)) {
throw new import_utils.AppError(
`Invalid semantic version ${import_utils2.chalk.cyan(releaseTypeOrVersion)}.`
);
}
});
api.getIncrementVersion(
async ({ releaseTypeOrVersion }) => {
const version = await getIncrementVersion(api, releaseTypeOrVersion);
if (!api.config.npm.confirm) {
return version;
}
return confirmVersion(api, version);
},
{
stage: 10
}
);
api.onBeforeBumpVersion(
async ({ version, isPrerelease, prereleaseId: preid }) => {
const { prerelease, prereleaseId } = api.config.npm;
if (prereleaseId && prereleaseId !== preid) {
throw new import_utils.AppError(
`Expected a ${prereleaseId} tag, but got ${import_utils2.chalk.cyan(version)}.`
);
}
if ((prereleaseId || prerelease === true) && !isPrerelease) {
throw new import_utils.AppError(
`Expected a prerelease type, but got ${import_utils2.chalk.cyan(version)}.`
);
}
if (!prereleaseId && prerelease === false && isPrerelease) {
throw new import_utils.AppError(
`Expected a release type, but got ${import_utils2.chalk.cyan(version)}.`
);
}
await checkVersion(
api.appData.validPkgNames[0],
version,
api.appData.registry
);
}
);
api.onBumpVersion(async ({ version }) => {
const { projectPkgJsonPath, projectPkg, pkgNames, pkgJsonPaths, pkgs } = api.appData;
debug == null ? void 0 : debug(pkgNames);
for (let i = 0; i < pkgNames.length; i++) {
await (0, import_utils.updatePackageVersion)(pkgJsonPaths[i], pkgs[i], version, pkgNames);
}
if (pkgJsonPaths[0] !== projectPkgJsonPath) {
await (0, import_utils.updatePackageVersion)(projectPkgJsonPath, projectPkg, version);
}
});
api.onAfterBumpVersion(async () => {
api.step("Updating Lockfile ...");
await (0, import_utils.updatePackageLock)(api.appData.packageManager, {
cwd: api.cwd,
verbose: true
});
});
};
async function getIncrementVersion(api, releaseTypeOrVersion) {
const { prerelease, prereleaseId, canary } = api.config.npm;
const { registry, projectPkg, validPkgNames } = api.appData;
api.step("Incrementing version ...");
if (releaseTypeOrVersion && !import_semver.RELEASE_TYPES.includes(releaseTypeOrVersion)) {
return releaseTypeOrVersion;
}
const localVersion = projectPkg.version;
const {
latest: remoteLatestVersion,
alpha: remoteAlphaVersion,
beta: remoteBetaVersion,
rc: remoteRcVersion
} = await (0, import_utils.getRemoteDistTag)(validPkgNames, {
cwd: api.cwd,
registry
});
const referenceVersionMap = {
latest: (0, import_utils.getMaxVersion)(localVersion, remoteLatestVersion),
alpha: (0, import_utils.getMaxVersion)(localVersion, remoteLatestVersion, remoteAlphaVersion),
beta: (0, import_utils.getMaxVersion)(localVersion, remoteLatestVersion, remoteBetaVersion),
rc: (0, import_utils.getMaxVersion)(localVersion, remoteLatestVersion, remoteRcVersion)
};
if (releaseTypeOrVersion) {
return (0, import_utils.getReleaseVersion)(
getReferenceVersion(prereleaseId),
releaseTypeOrVersion,
prereleaseId
);
}
if (canary) {
return (0, import_utils.getCanaryVersion)(referenceVersionMap.latest, api.cwd);
} else {
import_utils2.logger.info(`Local version: ${import_utils2.chalk.cyan(localVersion)}`);
if (remoteLatestVersion) {
import_utils2.logger.info(`Remote latest version: ${import_utils2.chalk.cyan(remoteLatestVersion)}`);
}
if (remoteAlphaVersion && (!prereleaseId || prereleaseId === "alpha")) {
import_utils2.logger.info(`Remote alpha version: ${import_utils2.chalk.cyan(remoteAlphaVersion)}`);
}
if (remoteBetaVersion && (!prereleaseId || prereleaseId === "beta")) {
import_utils2.logger.info(`Remote beta version: ${import_utils2.chalk.cyan(remoteBetaVersion)}`);
}
if (remoteRcVersion && (!prereleaseId || prereleaseId === "rc")) {
import_utils2.logger.info(`Remote rc version: ${import_utils2.chalk.cyan(remoteRcVersion)}`);
}
}
console.log();
const patchVersion = (0, import_utils.getReleaseVersion)(referenceVersionMap.latest, "patch");
const minorVersion = (0, import_utils.getReleaseVersion)(referenceVersionMap.latest, "minor");
const majorVersion = (0, import_utils.getReleaseVersion)(referenceVersionMap.latest, "major");
let answer = {
value: ""
};
if (prereleaseId) {
answer = await (0, import_utils2.prompts)(
getPrereleaseChoices(getReferenceVersion(prereleaseId), prereleaseId),
{
onCancel: import_utils.onCancel
}
);
return answer.value;
}
const choices = getReleaseChoices(
patchVersion,
minorVersion,
majorVersion,
prerelease
);
let releaseType = "";
if (!prereleaseId) {
answer = await (0, import_utils2.prompts)(
[
{
name: "value",
type: "select",
message: "Please select the release type to bump:",
choices
}
],
{
onCancel: import_utils.onCancel
}
);
releaseType = answer.value;
if (releaseType === "canary") {
return (0, import_utils.getCanaryVersion)(referenceVersionMap.latest, api.cwd);
}
if (releaseType === "custom") {
answer = await (0, import_utils2.prompts)({
name: "value",
type: "text",
message: "Please input the custom version:"
});
return answer.value;
}
if (!["alpha", "beta", "rc"].includes(releaseType)) {
return answer.value;
}
}
answer = await (0, import_utils2.prompts)(
getPrereleaseChoices(getReferenceVersion(answer.value), answer.value),
{
onCancel: import_utils.onCancel
}
);
return answer.value;
function getReferenceVersion(prereleaseId2) {
return referenceVersionMap[prereleaseId2] || referenceVersionMap.latest;
}
}
function getPrereleaseChoices(referenceVersion, prereleaseId) {
return {
name: "value",
type: "select",
message: `Please select the ${prereleaseId} version to bump:`,
choices: import_constants.prereleaseTypes.map((releaseType) => {
const version = (0, import_utils.getReleaseVersion)(
referenceVersion,
releaseType,
prereleaseId
);
return {
title: `${releaseType === "prerelease" ? (0, import_utils2.pascalCase)(releaseType) : (0, import_utils2.pascalCase)(releaseType) + " "} (${import_utils2.chalk.cyan(version)})`,
value: version
};
})
};
}
function getReleaseChoices(patchVersion, minorVersion, majorVersion, prerelease) {
let choices = [
{
title: `Patch (${patchVersion})`,
value: patchVersion,
description: import_utils2.chalk.grey(`Bug Fix`)
},
{
title: `Minor (${minorVersion})`,
value: minorVersion,
description: import_utils2.chalk.grey(`New Feature`)
},
{
title: `Major (${majorVersion})`,
value: majorVersion,
description: import_utils2.chalk.grey(`Breaking Change`)
},
{
title: `Alpha`,
value: "alpha",
description: import_utils2.chalk.grey(`Internal Test Version`)
},
{
title: `Beta`,
value: "beta",
description: import_utils2.chalk.grey(`External Test Version`)
},
{
title: `Rc`,
value: "rc",
description: import_utils2.chalk.grey(`Release Candidate Version`)
},
{
title: `Canary`,
value: "canary",
description: import_utils2.chalk.grey(`Canary Deployment Version`)
}
];
if (prerelease === true) {
choices = choices.slice(3);
}
if (prerelease === false) {
choices = choices.slice(0, 3);
}
return choices.concat({
title: `Custom`,
value: "custom",
description: import_utils2.chalk.grey(`Custom version`)
});
}
async function checkVersion(pkgName, version, registry) {
if (!import_semver.default.valid(version)) {
throw new import_utils.AppError(`Invalid semantic version ${import_utils2.chalk.cyan(version)}.`);
}
if (await (0, import_utils.isVersionExist)(pkgName, version, registry)) {
throw new import_utils.AppError(
`Package ${import_utils2.chalk.cyan(`${pkgName}@${version}`)} has been published already.`
);
}
}
async function confirmVersion(api, version) {
const { validPkgNames } = api.appData;
if (!validPkgNames.length) {
return version;
}
let confirmMessage = "";
if (validPkgNames.length === 1) {
confirmMessage = `Are you sure to bump version to ${import_utils2.chalk.cyan(version)}`;
} else {
console.log(`The packages will be bumped are as follows:${import_node_os.EOL}`);
for (const pkgName of validPkgNames) {
console.log(` - ${import_utils2.chalk.cyan(`${pkgName}@${version}`)}`);
}
console.log();
confirmMessage = "Are you sure to bump?";
}
const answer = await (0, import_utils2.confirm)(confirmMessage);
console.log();
if (answer) {
return version;
} else {
const version2 = await getIncrementVersion(api);
return confirmVersion(api, version2);
}
}