standard-commit
Version:
conventional commit
132 lines (123 loc) • 3.99 kB
JavaScript
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const meow_1 = __importDefault(require("meow"));
const fs_extra_1 = __importDefault(require("fs-extra"));
const read_pkg_up_1 = __importDefault(require("read-pkg-up"));
const write_pkg_1 = __importDefault(require("write-pkg"));
const ansi_colors_1 = __importDefault(require("ansi-colors"));
const lib_1 = require("../lib");
const cli = (0, meow_1.default)(`
Usage: standard-commit [options...]
Where <options> is one of:
-i --init
initialize a standard-commit config file
-a --all
Automatically stage files that have been modified.
-s --signoff
Add Signed-off-by at the end of the commit log message.
-n --no-verify
Bypasses the pre-commit and commit-msg hooks.
-e --edit
further edit the message.
Alias: git cc <option> with:
git config --global alias.cc '!standard-commit'
`, {
description: 'standard-commit',
flags: {
all: {
type: 'boolean',
alias: 'a',
},
signoff: {
type: 'boolean',
alias: 's',
},
noVerify: {
type: 'boolean',
alias: 'n',
},
verify: {
type: 'boolean',
default: true,
},
edit: {
type: 'boolean',
alias: 'e',
},
dryRun: {
type: 'boolean',
},
},
});
const { flags } = cli;
if (flags.init) {
init();
}
else {
commit({
all: flags.all,
signoff: flags.signoff,
noVerify: flags.noVerify || !flags.verify,
dryRun: flags.dryRun,
edit: flags.edit,
});
}
async function commit(flags) {
try {
// commit args
const commitArgs = [];
if (flags.all)
commitArgs.push('-a');
if (flags.signoff)
commitArgs.push('-s');
if (flags.noVerify)
commitArgs.push('-n');
if (flags.dryRun)
commitArgs.push('--dry-run');
// exit if can not commit
if (!(await (0, lib_1.gitCanCommit)(...commitArgs))) {
process.exitCode = 1;
return;
}
// prompt for commit message
const config = await (0, lib_1.loadConfig)();
const commitmsg = await (0, lib_1.promptCommitMessage)({}, config);
// display formated commit
const message = (0, lib_1.formatMessage)(commitmsg);
const [header, ...lines] = message.split('\n');
const symbol = ansi_colors_1.default.gray('·');
process.stdout.write('\n');
process.stdout.write(`${symbol} ${ansi_colors_1.default.whiteBright(header.trim())}\n`);
for (const line of lines.slice(0, lines.length - 1)) {
process.stdout.write(`${symbol} ${ansi_colors_1.default.white(line).trim()}\n`);
}
process.stdout.write('\n');
// confirm commit
const confirm = await (0, lib_1.promptConfirmCommit)(config);
// commit
if (confirm) {
process.exitCode = await (0, lib_1.gitCommit)(message, ...commitArgs);
}
}
catch (err) {
console.error('Internal Error:', err.message);
process.exitCode = err.code;
}
}
async function init() {
const config = await (0, lib_1.promptConfig)();
const { updatePackage } = await (0, lib_1.promptPackageUpdate)();
if (updatePackage) {
const pkgUp = await (0, read_pkg_up_1.default)();
const pkg = pkgUp.packageJson || {};
const path = pkgUp.path || 'package.json';
pkg['standard-commit'] = config;
await (0, write_pkg_1.default)(path, pkg);
}
else {
await fs_extra_1.default.outputJSON('.standard-commitrc.json', config, { spaces: 2 });
}
}