@eljs/release
Version:
Release npm package easily.
138 lines (136 loc) • 5.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/git.ts
var git_exports = {};
__export(git_exports, {
default: () => git_default
});
module.exports = __toCommonJS(git_exports);
var import_utils = require("../../utils");
var import_utils2 = require("@eljs/utils");
var import_node_os = require("node:os");
var import_node_path = __toESM(require("node:path"));
var git_default = (api) => {
api.onCheck(async () => {
const { requireClean, requireBranch } = api.config.git;
if (requireClean) {
api.step("Checking git ...");
if (!await (0, import_utils2.isGitClean)({
cwd: api.cwd,
verbose: true
})) {
throw new import_utils.AppError("Git working tree is not clean.");
}
if (await (0, import_utils2.isGitBehindRemote)({
cwd: api.cwd,
verbose: true
})) {
throw new import_utils.AppError("Git working tree is behind remote.");
}
}
if (requireBranch && api.appData.branch !== requireBranch) {
throw new import_utils.AppError(
`Require branch ${requireBranch}\`, but got ${import_utils2.chalk.cyan(api.appData.branch)}.`
);
}
});
api.getChangelog(
async () => {
const { changelog, independent } = api.config.git;
if (!changelog) {
return "";
}
return (0, import_utils.getChangelog)({
cwd: api.cwd,
independent,
...changelog
});
},
{
stage: 10
}
);
api.onBeforeRelease(async ({ changelog }) => {
if (!changelog || !api.config.git.changelog) {
return;
}
const { filename, placeholder } = api.config.git.changelog;
const changelogFile = import_node_path.default.join(api.cwd, filename);
api.step(`Writing changelog to ${changelogFile} ...`);
if (changelog.indexOf("###") === -1) {
changelog = changelog.replace(new RegExp(import_node_os.EOL, "g"), "");
changelog += `${import_node_os.EOL}${import_node_os.EOL}${placeholder}`;
}
if (await (0, import_utils2.isPathExists)(changelogFile)) {
const remain = (await (0, import_utils2.readFile)(changelogFile)).trim();
changelog = remain.length ? remain.replace(
/# Change\s?Log/,
`# ChangeLog ${import_node_os.EOL}${import_node_os.EOL}${changelog}`
) : `# ChangeLog ${import_node_os.EOL}${import_node_os.EOL}${changelog}`;
} else {
changelog = `# ChangeLog ${import_node_os.EOL}${import_node_os.EOL}${changelog}`;
}
await (0, import_utils2.writeFile)(changelogFile, changelog);
});
api.onRelease(async ({ version }) => {
const { independent, commit, commitMessage, commitArgs, push, pushArgs } = api.config.git;
const { pkgNames, latestTag } = api.appData;
if (!commit) {
return;
}
api.step("Committing changes ...");
const commitMsg = commitMessage.replace("${version}", version);
await (0, import_utils2.gitCommit)(commitMsg, [...(0, import_utils2.normalizeArgs)(commitArgs)].filter(Boolean), {
cwd: api.cwd,
verbose: true
});
const tags = independent ? pkgNames.map((pkgName) => `${pkgName}@${version}`) : [`v${version}`];
for await (const tagName of tags) {
try {
await (0, import_utils2.gitTag)(tagName, {
cwd: api.cwd,
verbose: true
});
} catch (error) {
const err = error;
if ((/tag '.+' already exists/.test(err.message) || /标签 '.+' 已存在/.test(err.message)) && latestTag === tagName) {
import_utils2.logger.warn(`Tag ${import_utils2.chalk.cyan(tagName)} already exists.`);
} else {
throw err;
}
}
}
if (!push) {
return;
}
await (0, import_utils2.gitPush)([...(0, import_utils2.normalizeArgs)(pushArgs)].filter(Boolean), {
cwd: api.cwd,
verbose: true
});
});
};