UNPKG

2.81 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3const path = require("path");
4const git_1 = require("./git");
5const utils_1 = require("./utils");
6async function getVersionTagData() {
7 const projectPkgPath = path.join(process.cwd(), 'package.json');
8 const projectPkgData = await utils_1.readJSONFile(projectPkgPath);
9 if (utils_1.isPkgData(projectPkgData)) {
10 const { version } = projectPkgData;
11 /**
12 * @see https://github.com/sindresorhus/np/blob/v5.1.3/source/util.js#L51-L65
13 * @see https://github.com/npm/cli/blob/v6.13.0/lib/version.js#L311
14 * @see https://github.com/yarnpkg/yarn/blob/v1.19.1/src/cli/commands/version.js#L194
15 */
16 const prefix = await utils_1.getConfig({
17 npm: 'tag-version-prefix',
18 yarn: 'version-tag-prefix',
19 });
20 /**
21 * @see https://github.com/npm/cli/blob/v6.13.0/lib/version.js#L311
22 * @see https://github.com/yarnpkg/yarn/blob/v1.19.1/src/cli/commands/version.js#L206
23 */
24 const tagName = `${prefix}${version}`;
25 /**
26 * @see https://github.com/npm/cli/blob/v6.13.0/lib/version.js#L296
27 * @see https://github.com/yarnpkg/yarn/blob/v1.19.1/src/cli/commands/version.js#L191
28 */
29 const message = (await utils_1.getConfig({ npm: 'message', yarn: 'version-git-message' })).replace(/%s/g, version);
30 return { tagName, message };
31 }
32 throw new Error('Failed to find version tag name.');
33}
34async function gitTagAlreadyExists(versionTagName, tagMessage, opts) {
35 if (!(await git_1.isHeadTag(versionTagName))) {
36 throw new Error(`Git tag '${versionTagName}' already exists`);
37 }
38 if (opts.verbose) {
39 await git_1.setTag(versionTagName, {
40 message: tagMessage,
41 debug: (commandText) => `> #${commandText}\n # tag '${versionTagName}' already exists`,
42 dryRun: true,
43 });
44 }
45}
46async function main(opts) {
47 if (opts.dryRun) {
48 console.error('Dry Run enabled');
49 opts.verbose = true;
50 }
51 const { tagName: versionTagName, message } = await getVersionTagData();
52 if (await git_1.tagExists(versionTagName)) {
53 await gitTagAlreadyExists(versionTagName, message, opts);
54 }
55 else {
56 await git_1.setTag(versionTagName, {
57 message,
58 debug: opts.verbose,
59 dryRun: opts.dryRun,
60 });
61 }
62 if (opts.push) {
63 await git_1.push(versionTagName, {
64 debug: opts.verbose,
65 dryRun: opts.dryRun,
66 });
67 }
68}
69async function default_1(opts = {}) {
70 try {
71 await main(opts);
72 }
73 finally {
74 utils_1.endPrintVerbose();
75 }
76}
77exports.default = default_1;
78//# sourceMappingURL=index.js.map
\No newline at end of file