Skip to content

Commit

Permalink
feat: support --install flag
Browse files Browse the repository at this point in the history
  • Loading branch information
antfu committed Jan 9, 2025
1 parent 6875220 commit 96a4754
Show file tree
Hide file tree
Showing 7 changed files with 46 additions and 4 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@
"escalade": "^3.2.0",
"js-yaml": "^4.1.0",
"jsonc-parser": "^3.3.1",
"package-manager-detector": "^0.2.8",
"prompts": "^2.4.2",
"semver": "^7.6.3",
"tinyexec": "^0.3.2",
Expand Down
11 changes: 7 additions & 4 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions src/cli/parse-args.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ export async function parseArgs(): Promise<ParsedArgs> {
noGitCheck: args.noGitCheck,
confirm: !args.yes,
noVerify: !args.verify,
install: args.install,
files: [...(args['--'] || []), ...resultArgs],
ignoreScripts: args.ignoreScripts,
currentVersion: args.currentVersion,
Expand Down Expand Up @@ -83,6 +84,7 @@ export function loadCliArgs(argv = process.argv) {
.option('-t, --tag [tag]', 'Tag name', { default: true })
.option('--no-tag', 'Skip tag', { default: false })
.option('--sign', 'Sign commit and tag')
.option('--install', `Run 'npm install' after bumping version (default: ${bumpConfigDefaults.install})`, { default: false })
.option('-p, --push', `Push to remote (default: ${bumpConfigDefaults.push})`)
.option('-y, --yes', `Skip confirmation (default: ${!bumpConfigDefaults.confirm})`)
.option('-r, --recursive', `Bump package.json files recursively (default: ${bumpConfigDefaults.recursive})`)
Expand Down
1 change: 1 addition & 0 deletions src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ export const bumpConfigDefaults: VersionBumpOptions = {
push: true,
tag: true,
sign: false,
install: false,
recursive: false,
noVerify: false,
confirm: true,
Expand Down
3 changes: 3 additions & 0 deletions src/normalize-options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ export interface NormalizedOptions {
push: boolean
files: string[]
cwd: string
install: boolean
interface: Interface
ignoreScripts: boolean
execute?: string | ((config: Operation) => void | PromiseLike<void>)
Expand All @@ -77,6 +78,7 @@ export async function normalizeOptions(raw: VersionBumpOptions): Promise<Normali
const sign = Boolean(raw.sign)
const push = Boolean(raw.push)
const all = Boolean(raw.all)
const install = Boolean(raw.install)
const noVerify = Boolean(raw.noVerify)
const cwd = raw.cwd || process.cwd()
const ignoreScripts = Boolean(raw.ignoreScripts)
Expand Down Expand Up @@ -181,6 +183,7 @@ export async function normalizeOptions(raw: VersionBumpOptions): Promise<Normali
push,
files,
cwd,
install,
interface: ui,
ignoreScripts,
execute,
Expand Down
7 changes: 7 additions & 0 deletions src/types/version-bump-options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,13 @@ export interface VersionBumpOptions {
*/
push?: boolean

/**
* Run `npm install` after bumping the version number.
*
* Defaults to `false`.
*/
install?: boolean

/**
* Indicates whether the git commit should include ALL files (`git commit --all`)
* rather than just the files that were modified by `versionBump()`.
Expand Down
25 changes: 25 additions & 0 deletions src/version-bump.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,29 @@ export async function versionBump(arg: (VersionBumpOptions) | string = {}): Prom
// Update the version number in all files
await updateFiles(operation)

if (operation.options.install) {
const { detect } = await import('package-manager-detector/detect')
const pm = await detect()
if (!pm?.name) {
throw new Error('Could not detect package manager, failed to run npm install')
}

const { COMMANDS, constructCommand } = await import('package-manager-detector/commands')
const command = constructCommand(COMMANDS[pm.name].install, [])
if (!command) {
throw new Error('Could not find install command for package manager')
}
console.log(symbols.info, 'Installing dependencies with', `${command.command} ${command.args.join(' ')}`)
await x(command.command, command.args, {
throwOnError: true,
nodeOptions: {
stdio: 'inherit',
cwd: operation.options.cwd,
},
})
console.log(symbols.success, 'Dependencies installed')
}

if (operation.options.execute) {
if (typeof operation.options.execute === 'function') {
await operation.options.execute(operation)
Expand Down Expand Up @@ -117,6 +140,8 @@ function printSummary(operation: Operation) {
console.log(` execute ${c.bold(typeof operation.options.execute === 'function' ? 'function' : operation.options.execute)}`)
if (operation.options.push)
console.log(` push ${c.cyan(c.bold('yes'))}`)
if (operation.options.install)
console.log(` install ${c.cyan(c.bold('yes'))}`)
console.log()
console.log(` from ${c.bold(operation.state.currentVersion)}`)
console.log(` to ${c.green(c.bold(operation.state.newVersion))}`)
Expand Down

0 comments on commit 96a4754

Please sign in to comment.