@clerc/plugin-version
Version:
Clerc plugin version
54 lines (50 loc) • 1.3 kB
JavaScript
import { definePlugin } from '@clerc/core';
import { gracefulVersion } from '@clerc/utils';
const locales = {
"en": {
"version.description": "Show CLI version",
"version.notes.1": 'The version string begins with a "v".'
},
"zh-CN": {
"version.description": "\u5C55\u793A CLI \u7248\u672C",
"version.notes.1": '\u7248\u672C\u53F7\u5F00\u5934\u5E26\u6709 "v"\u3002'
}
};
const versionPlugin = ({
command = true,
flag = true
} = {}) => definePlugin({
setup: (cli) => {
const { add, t } = cli.i18n;
add(locales);
const gracefullyVersion = gracefulVersion(cli._version);
if (command) {
cli = cli.command("version", t("version.description"), {
help: {
notes: [t("version.notes.1")]
}
}).on("version", () => {
process.stdout.write(gracefullyVersion);
});
}
if (flag) {
cli = cli.flag("version", t("version.description"), {
alias: "V",
type: Boolean,
default: false
});
cli.interceptor({
enforce: "pre",
fn: (ctx, next) => {
if (ctx.flags.version) {
process.stdout.write(gracefullyVersion);
} else {
next();
}
}
});
}
return cli;
}
});
export { versionPlugin };