@process-engine/ci_tools
Version:
CI tools for process-engine.io
157 lines (144 loc) • 6.84 kB
JavaScript
Object.defineProperty(exports, "__esModule", { value: true });
exports.printHelp = exports.getShortDoc = exports.run = void 0;
const chalk = require("chalk");
const yargsParser = require("yargs-parser");
const git_1 = require("../git/git");
const git_helpers_1 = require("../versions/git_helpers");
const package_version_1 = require("../versions/package_version");
const retry_run_1 = require("../versions/retry_run");
const printMultiLineString_1 = require("../cli/printMultiLineString");
const shell_1 = require("../cli/shell");
const COMMAND_NAME = 'prepare-version';
const BADGE = `[${COMMAND_NAME}]\t`;
const DEFAULT_MODE = 'node';
const DOC = `
Adjusts the pre-version in your project file automatically (\`package.json\` for Node, \`*.csproj\` for C# .NET
or \`setup.py\` for Python).
OPTIONS
--allow-dirty-workdir allows for a "dirty" Git workdir
--dry performs a dry which does not changes any files
--force overrides all plausibility checks and increments the pre-version
--mode sets the package mode [dotnet, node, python] (default: node)
EXAMPLES
Your package.json's version field is
1.2.0-alpha13
if you push to develop again, it gets incremented:
1.2.0-alpha14
If you merge into \`beta\`, the suffix is automatically changed and incremented with each subsequent merge/commit:
1.2.0-beta1
1.2.0-beta2
1.2.0-beta3
IMPORTANT: This script always keeps the "base" of the version and never changes that!
1.2.0-alpha14
1.2.0-beta2
1.2.0
^^^^^ base version
For alpha and beta releases, it adds the suffix, if not present.
For stable releases, it removes the suffix to the version, if present.
It then writes package.json, commits, tags and pushes it
(when on one of the applicable branches).
`;
// DOC: see above
async function run(...args) {
const argv = yargsParser(args, { alias: { help: ['h'] }, default: { mode: DEFAULT_MODE } });
const allowDirtyWorkdir = args.indexOf('--allow-dirty-workdir') !== -1;
const isDryRun = args.indexOf('--dry') !== -1;
const isForced = process.env.CI_TOOLS_FORCE_PUBLISH === 'true' || args.indexOf('--force') !== -1;
const mode = argv.mode;
let nextVersion = await (0, git_helpers_1.getNextVersion)(mode);
let nextVersionTag = (0, git_helpers_1.getVersionTag)(nextVersion);
await printInfo(mode, nextVersion, isDryRun, isForced);
if (await (0, retry_run_1.isRetryRunForPartiallySuccessfulBuild)(mode)) {
console.error(chalk.yellow(`${BADGE}This seems to be a retry run for a partially successful build.`));
nextVersion = await (0, retry_run_1.getPartiallySuccessfulBuildVersion)(mode);
nextVersionTag = (0, git_helpers_1.getVersionTag)(nextVersion);
console.log('');
console.log(`${BADGE}resetting nextVersionTag:`, nextVersionTag);
}
await abortIfRetryRun(mode);
abortIfDirtyWorkdir(allowDirtyWorkdir, isForced);
await abortIfTagAlreadyExistsAndIsNoRetryRun(mode, nextVersionTag, isForced);
abortIfDryRun(nextVersion, isDryRun, isForced);
(0, package_version_1.setPackageVersion)(mode, nextVersion);
return true;
}
exports.run = run;
function getShortDoc() {
return DOC.trim().split('\n')[0];
}
exports.getShortDoc = getShortDoc;
function printHelp() {
console.log(`Usage: ci_tools ${COMMAND_NAME} [--allow-dirty-workdir] [--dry] [--force] [--mode <MODE>]`);
console.log('');
console.log(DOC.trim());
}
exports.printHelp = printHelp;
async function abortIfRetryRun(mode) {
if (await (0, retry_run_1.isRedundantRunTriggeredBySystemUserPush)(mode)) {
const currentVersionTag = await (0, package_version_1.getPackageVersionTag)(mode);
console.error(chalk.yellow(`${BADGE}Current commit is tagged with "${currentVersionTag}".`));
console.error(chalk.yellowBright(`${BADGE}Nothing to do here, since this is the current package version!`));
process.exit(0);
}
}
function abortIfDirtyWorkdir(allowDirtyWorkdir, isForced) {
if ((0, git_1.isDirty)() && !allowDirtyWorkdir) {
const workdirState = (0, shell_1.sh)('git status --porcelain --untracked-files=no').trim();
if (isForced) {
console.error(chalk.yellow(`${BADGE}Git workdir is dirty:`));
(0, printMultiLineString_1.printMultiLineString)(workdirState);
console.error(chalk.yellowBright(`${BADGE}Resuming since --force was provided.`));
console.log('');
}
else {
console.error(chalk.red(`${BADGE}Can not proceed due to dirty git workdir:`));
(0, printMultiLineString_1.printMultiLineString)(workdirState);
process.exit(1);
}
}
}
async function abortIfTagAlreadyExistsAndIsNoRetryRun(mode, nextVersionTag, isForced) {
const isRetryRun = await (0, retry_run_1.isRetryRunForPartiallySuccessfulBuild)(mode);
if ((0, git_1.isExistingTag)(nextVersionTag) && !isRetryRun) {
console.error(chalk.red(`${BADGE}Sanity check failed!`));
console.error(chalk.red(`${BADGE}Tag "${nextVersionTag}" already exists!`));
if (isForced) {
console.error(chalk.yellowBright(`${BADGE}Resuming since --force was provided.`));
console.log('');
}
else {
console.error(chalk.yellow(`${BADGE}Aborting!`));
process.exit(1);
}
}
}
function abortIfDryRun(nextVersion, isDryRun, isForced) {
if (isDryRun) {
console.log(chalk.yellow(`${BADGE}I would write version ${nextVersion} to package.json.`));
console.log(chalk.yellow(`${BADGE}Aborting due to --dry.`));
if (isForced) {
console.error(chalk.yellow(`${BADGE}Even though --force was provided, --dry takes precedence.`));
}
process.exit(0);
}
}
async function printInfo(mode, nextVersion, isDryRun, isForced) {
const packageVersion = await (0, package_version_1.getPackageVersion)(mode);
const packageVersionTag = (0, git_helpers_1.getVersionTag)(packageVersion);
const branchName = (0, git_1.getGitBranch)();
const gitTagList = (0, git_1.getGitTagList)();
console.log(`${BADGE}isDryRun:`, isDryRun);
console.log(`${BADGE}isForced:`, isForced);
console.log('');
console.log(`${BADGE}packageVersion:`, packageVersion);
console.log(`${BADGE}packageVersionTag:`, packageVersionTag);
console.log(`${BADGE}branchName:`, branchName);
console.log(`${BADGE}gitTagList:`);
(0, printMultiLineString_1.printMultiLineString)(gitTagList);
console.log(`${BADGE}tagsForHEAD:`);
(0, printMultiLineString_1.printMultiLineString)((0, git_1.getGitTagsFromCommit)('HEAD'));
console.log(`${BADGE}nextVersionTag:`, (0, git_helpers_1.getVersionTag)(nextVersion));
console.log('');
}
//# sourceMappingURL=prepare-version.js.map
;