|
1 | 1 | #!/usr/bin/env node |
2 | 2 | "use strict"; |
3 | | - |
4 | | -const program = require("commander"); |
5 | | -const SemVer = require("semver"); |
6 | | -const inquirer = require("inquirer"); |
7 | | -const chalk = require("chalk"); |
8 | | -const api = require("../"); |
9 | | - |
10 | | -program |
11 | | - .version(require("../package").version) |
12 | | - .option("--major", "Increase major version") |
13 | | - .option("--minor", "Increase minor version") |
14 | | - .option("--patch", "Increase patch version") |
15 | | - .option("--premajor", "Increase major version, pre-release") |
16 | | - .option("--preminor", "Increase preminor version, pre-release") |
17 | | - .option("--prepatch", "Increase prepatch version, pre-release") |
18 | | - .option("--prerelease", "Increase prerelease version") |
19 | | - .option("--prompt", "Prompt for type of bump (patch, minor, major, premajor, prerelase, etc.)") |
20 | | - .option("--preid <name>", 'The identifier for prerelease versions (default is "beta")') |
21 | | - .option("--commit [message]", 'Commit changed files to Git (default message is "release vX.X.X")') |
22 | | - .option("--no-verify", "Bypasses the pre-commit and commit-msg hooks") |
23 | | - .option("--tag", "Tag the commit in Git") |
24 | | - .option("--push", "Push the Git commit") |
25 | | - .option("--all", "Commit/tag/push ALL pending files, not just the ones changed by bump") |
26 | | - .option("--grep <filespec...>", "Files and/or globs to do a text-replace of the old version number with the new one") |
27 | | - .option("--lock", "Also update the package-lock.json") |
28 | | - .on("--help", () => { |
29 | | - console.log( |
30 | | - "\n Examples:\n" + |
31 | | - "\n" + |
32 | | - " $ bump --patch\n" + |
33 | | - " $ bump --major --tag\n" + |
34 | | - " $ bump --patch --tag --all --grep README.md\n" + |
35 | | - " $ bump --prompt --tag --push --all\n" |
36 | | - ); |
37 | | - }) |
38 | | - .parse(process.argv); |
39 | | - |
40 | | -// Show help if no options were given |
41 | | -if (program.rawArgs.length < 3) { |
42 | | - program.help(); |
43 | | -} |
44 | | -else { |
45 | | - let options = program; |
46 | | - |
47 | | - if (options.grep && program.args) { |
48 | | - // If multiple --grep files are specified, then they are parsed as separate args |
49 | | - options.grep = program.args.concat(options.grep); |
50 | | - } |
51 | | - |
52 | | - if (typeof options.commit === "string") { |
53 | | - options.commitMessage = options.commit; |
54 | | - options.commit = true; |
55 | | - } |
56 | | - |
57 | | - let manifests = api.manifests(options.lock); |
58 | | - bumpManifests(manifests, options) |
59 | | - .then(() => { |
60 | | - api.grep(manifests, options); |
61 | | - manifests.forEach((manifest) => { |
62 | | - api.runNpmScriptIfExists(manifest, "version"); |
63 | | - }); |
64 | | - }) |
65 | | - .then(() => { |
66 | | - if (options.commit || options.tag || options.push) { |
67 | | - api.git(manifests, options); |
68 | | - } |
69 | | - else { |
70 | | - manifests.forEach((manifest) => { |
71 | | - api.runNpmScriptIfExists(manifest, "postversion"); |
72 | | - }); |
73 | | - } |
74 | | - }) |
75 | | - .catch((err) => { |
76 | | - console.error(chalk.red(err.message)); |
77 | | - process.exit(err.status || 1); |
78 | | - }); |
79 | | -} |
80 | | - |
81 | | -/** |
82 | | - * Bumps each manifest sequentially |
83 | | - * |
84 | | - * @param {string[]} manifests - An array of manifest files to bump |
85 | | - * @param {object} options - CLI options |
86 | | - * @returns {Promise} |
87 | | - */ |
88 | | -function bumpManifests (manifests, options) { |
89 | | - let i = 0; |
90 | | - |
91 | | - return bumpNext("patch"); |
92 | | - |
93 | | - function bumpNext (defaultBumpType) { |
94 | | - let manifest = manifests[i++]; |
95 | | - if (manifest) { |
96 | | - return bumpManifest(manifest, defaultBumpType, options).then(bumpNext); |
97 | | - } |
98 | | - else { |
99 | | - return Promise.resolve(defaultBumpType); |
100 | | - } |
101 | | - } |
102 | | -} |
103 | | - |
104 | | -/** |
105 | | - * Bumps the given manifest |
106 | | - * |
107 | | - * @param {string} manifest - The manifest name (e.g. "package.json", "bower.json", etc.) |
108 | | - * @param {string} defaultBumpType - The default type of bump to perform |
109 | | - * @param {object} options - CLI options |
110 | | - * @returns {Promise} |
111 | | - */ |
112 | | -function bumpManifest (manifest, defaultBumpType, options) { |
113 | | - return new Promise((resolve, reject) => { |
114 | | - api.runNpmScriptIfExists(manifest, "preversion"); |
115 | | - |
116 | | - if (options.prompt) { |
117 | | - // Prompt the user for the type of bump to perform |
118 | | - let version = api.versionInfo(manifest, options); |
119 | | - console.log("\nCurrent version in %s is %s", manifest, version.current); |
120 | | - |
121 | | - inquirer.prompt([ |
122 | | - { |
123 | | - type: "list", |
124 | | - name: "bumpType", |
125 | | - message: "How would you like to bump it?", |
126 | | - default: defaultBumpType, |
127 | | - pageSize: 9, |
128 | | - choices: [ |
129 | | - { value: "major", name: "major (" + version.nextMajor + ")" }, |
130 | | - { value: "minor", name: "minor (" + version.nextMinor + ")" }, |
131 | | - { value: "patch", name: "patch (" + version.nextPatch + ")" }, |
132 | | - { value: "premajor", name: "pre-release major (" + version.nextPreMajor + ")" }, |
133 | | - { value: "preminor", name: "pre-release minor (" + version.nextPreMinor + ")" }, |
134 | | - { value: "prepatch", name: "pre-release patch (" + version.nextPrePatch + ")" }, |
135 | | - { value: "prerelease", name: "pre-release (" + version.nextPreRelease + ")" }, |
136 | | - new inquirer.Separator(), |
137 | | - { value: "custom", name: "custom..." }, |
138 | | - ] |
139 | | - }, |
140 | | - { |
141 | | - type: "input", |
142 | | - name: "newVersion", |
143 | | - message: "Enter the new version number:", |
144 | | - default: version.current, |
145 | | - when: answers => answers.bumpType === "custom", |
146 | | - filter: SemVer.clean, |
147 | | - validate: answer => { |
148 | | - return SemVer.valid(answer) ? true : "That's not a valid version number"; |
149 | | - }, |
150 | | - } |
151 | | - ]) |
152 | | - .then((answers) => { |
153 | | - bump(answers.bumpType, answers.newVersion); |
154 | | - }); |
155 | | - } |
156 | | - else { |
157 | | - let bumpType = |
158 | | - options.major ? "major" |
159 | | - : options.minor ? "minor" |
160 | | - : options.patch ? "patch" |
161 | | - : options.premajor ? "premajor" |
162 | | - : options.preminor ? "preminor" |
163 | | - : options.prepatch ? "prepatch" |
164 | | - : options.prerelease ? "prerelease" |
165 | | - : defaultBumpType; |
166 | | - |
167 | | - bump(bumpType); |
168 | | - } |
169 | | - |
170 | | - function bump (bumpType, newVersion) { |
171 | | - try { |
172 | | - options.newVersion = newVersion; |
173 | | - api.bump(manifest, bumpType, options); |
174 | | - } |
175 | | - catch (ex) { |
176 | | - reject(ex); |
177 | | - } |
178 | | - resolve(bumpType); |
179 | | - } |
180 | | - }); |
181 | | -} |
| 3 | +const { main } = require("../lib/cli"); |
| 4 | +main(process.argv.slice(2)); |
0 commit comments