Skip to content

Commit 96a4754

Browse files
committed
feat: support --install flag
1 parent 6875220 commit 96a4754

File tree

7 files changed

+46
-4
lines changed

7 files changed

+46
-4
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@
6565
"escalade": "^3.2.0",
6666
"js-yaml": "^4.1.0",
6767
"jsonc-parser": "^3.3.1",
68+
"package-manager-detector": "^0.2.8",
6869
"prompts": "^2.4.2",
6970
"semver": "^7.6.3",
7071
"tinyexec": "^0.3.2",

pnpm-lock.yaml

Lines changed: 7 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/cli/parse-args.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ export async function parseArgs(): Promise<ParsedArgs> {
3939
noGitCheck: args.noGitCheck,
4040
confirm: !args.yes,
4141
noVerify: !args.verify,
42+
install: args.install,
4243
files: [...(args['--'] || []), ...resultArgs],
4344
ignoreScripts: args.ignoreScripts,
4445
currentVersion: args.currentVersion,
@@ -83,6 +84,7 @@ export function loadCliArgs(argv = process.argv) {
8384
.option('-t, --tag [tag]', 'Tag name', { default: true })
8485
.option('--no-tag', 'Skip tag', { default: false })
8586
.option('--sign', 'Sign commit and tag')
87+
.option('--install', `Run 'npm install' after bumping version (default: ${bumpConfigDefaults.install})`, { default: false })
8688
.option('-p, --push', `Push to remote (default: ${bumpConfigDefaults.push})`)
8789
.option('-y, --yes', `Skip confirmation (default: ${!bumpConfigDefaults.confirm})`)
8890
.option('-r, --recursive', `Bump package.json files recursively (default: ${bumpConfigDefaults.recursive})`)

src/config.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ export const bumpConfigDefaults: VersionBumpOptions = {
99
push: true,
1010
tag: true,
1111
sign: false,
12+
install: false,
1213
recursive: false,
1314
noVerify: false,
1415
confirm: true,

src/normalize-options.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ export interface NormalizedOptions {
6060
push: boolean
6161
files: string[]
6262
cwd: string
63+
install: boolean
6364
interface: Interface
6465
ignoreScripts: boolean
6566
execute?: string | ((config: Operation) => void | PromiseLike<void>)
@@ -77,6 +78,7 @@ export async function normalizeOptions(raw: VersionBumpOptions): Promise<Normali
7778
const sign = Boolean(raw.sign)
7879
const push = Boolean(raw.push)
7980
const all = Boolean(raw.all)
81+
const install = Boolean(raw.install)
8082
const noVerify = Boolean(raw.noVerify)
8183
const cwd = raw.cwd || process.cwd()
8284
const ignoreScripts = Boolean(raw.ignoreScripts)
@@ -181,6 +183,7 @@ export async function normalizeOptions(raw: VersionBumpOptions): Promise<Normali
181183
push,
182184
files,
183185
cwd,
186+
install,
184187
interface: ui,
185188
ignoreScripts,
186189
execute,

src/types/version-bump-options.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,13 @@ export interface VersionBumpOptions {
6464
*/
6565
push?: boolean
6666

67+
/**
68+
* Run `npm install` after bumping the version number.
69+
*
70+
* Defaults to `false`.
71+
*/
72+
install?: boolean
73+
6774
/**
6875
* Indicates whether the git commit should include ALL files (`git commit --all`)
6976
* rather than just the files that were modified by `versionBump()`.

src/version-bump.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,29 @@ export async function versionBump(arg: (VersionBumpOptions) | string = {}): Prom
7272
// Update the version number in all files
7373
await updateFiles(operation)
7474

75+
if (operation.options.install) {
76+
const { detect } = await import('package-manager-detector/detect')
77+
const pm = await detect()
78+
if (!pm?.name) {
79+
throw new Error('Could not detect package manager, failed to run npm install')
80+
}
81+
82+
const { COMMANDS, constructCommand } = await import('package-manager-detector/commands')
83+
const command = constructCommand(COMMANDS[pm.name].install, [])
84+
if (!command) {
85+
throw new Error('Could not find install command for package manager')
86+
}
87+
console.log(symbols.info, 'Installing dependencies with', `${command.command} ${command.args.join(' ')}`)
88+
await x(command.command, command.args, {
89+
throwOnError: true,
90+
nodeOptions: {
91+
stdio: 'inherit',
92+
cwd: operation.options.cwd,
93+
},
94+
})
95+
console.log(symbols.success, 'Dependencies installed')
96+
}
97+
7598
if (operation.options.execute) {
7699
if (typeof operation.options.execute === 'function') {
77100
await operation.options.execute(operation)
@@ -117,6 +140,8 @@ function printSummary(operation: Operation) {
117140
console.log(` execute ${c.bold(typeof operation.options.execute === 'function' ? 'function' : operation.options.execute)}`)
118141
if (operation.options.push)
119142
console.log(` push ${c.cyan(c.bold('yes'))}`)
143+
if (operation.options.install)
144+
console.log(` install ${c.cyan(c.bold('yes'))}`)
120145
console.log()
121146
console.log(` from ${c.bold(operation.state.currentVersion)}`)
122147
console.log(` to ${c.green(c.bold(operation.state.newVersion))}`)

0 commit comments

Comments
 (0)