Skip to content

Commit cd925b0

Browse files
Added support for npm "preversion", "version", and "postversion" scripts
1 parent d9d7c8f commit cd925b0

File tree

5 files changed

+70
-5
lines changed

5 files changed

+70
-5
lines changed

src/cli/index.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ export async function main(args: string[]): Promise<void> {
4444
}
4545
}
4646

47-
function progress({ event, updatedFiles, skippedFiles, newVersion }: VersionBumpProgress): void {
47+
function progress({ event, script, updatedFiles, skippedFiles, newVersion }: VersionBumpProgress): void {
4848
// tslint:disable-next-line: switch-default
4949
switch (event) {
5050
case ProgressEvent.FileUpdated:
@@ -66,6 +66,10 @@ function progress({ event, updatedFiles, skippedFiles, newVersion }: VersionBump
6666
case ProgressEvent.GitPush:
6767
console.log(success, "Git push");
6868
break;
69+
70+
case ProgressEvent.NpmScript:
71+
console.log(success, `Npm run ${script}`);
72+
break;
6973
}
7074
}
7175

src/operation.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { NormalizedOptions, normalizeOptions } from "./normalize-options";
22
import { ReleaseType } from "./release-type";
33
import { VersionBumpOptions } from "./types/version-bump-options";
4-
import { ProgressEvent, VersionBumpProgress } from "./types/version-bump-progress";
4+
import { NpmScript, ProgressEvent, VersionBumpProgress } from "./types/version-bump-progress";
55
import { VersionBumpResults } from "./types/version-bump-results";
66

77
type ProgressCallback = (progress: VersionBumpProgress) => void;
@@ -19,6 +19,7 @@ interface OperationState {
1919

2020
interface UpdateOperationState extends Partial<OperationState> {
2121
event?: ProgressEvent;
22+
script?: NpmScript;
2223
}
2324

2425
/**
@@ -89,13 +90,13 @@ export class Operation {
8990
/**
9091
* Updates the operation state and results, and reports the updated progress to the user.
9192
*/
92-
public update({ event, ...newState }: UpdateOperationState): this {
93+
public update({ event, script, ...newState }: UpdateOperationState): this {
9394
// Update the operation state
9495
Object.assign(this.state, newState);
9596

9697
if (event && this._progress) {
9798
// Report the progress to the user
98-
this._progress({ event, ...this.results });
99+
this._progress({ event, script, ...this.results });
99100
}
100101

101102
return this;

src/run-npm-script.ts

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import * as ezSpawn from "ez-spawn";
2+
import { readJsonFile } from "./fs";
3+
import { isManifest, Manifest } from "./manifest";
4+
import { Operation } from "./operation";
5+
import { NpmScript, ProgressEvent } from "./types/version-bump-progress";
6+
7+
/**
8+
* Runs the specified NPM script in the package.json file.
9+
*/
10+
export async function runNpmScript(script: NpmScript, operation: Operation): Promise<Operation> {
11+
let { cwd } = operation.options;
12+
13+
let { data: manifest } = await readJsonFile("package.json", cwd);
14+
15+
if (isManifest(manifest) && hasScript(manifest, script)) {
16+
await ezSpawn.async("npm", ["run", script, "--silent"], { stdio: "inherit" });
17+
operation.update({ event: ProgressEvent.NpmScript, script });
18+
}
19+
20+
return operation;
21+
}
22+
23+
/**
24+
* Determines whether the specified NPM script exists in the given manifest.
25+
*/
26+
function hasScript(manifest: Manifest, script: NpmScript): boolean {
27+
let scripts = manifest.scripts as Record<NpmScript, string> | undefined;
28+
29+
if (scripts && typeof scripts === "object") {
30+
return Boolean(scripts[script]);
31+
}
32+
33+
return false;
34+
}

src/types/version-bump-progress.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,24 @@ export const enum ProgressEvent {
99
GitCommit = "git commit",
1010
GitTag = "git tag",
1111
GitPush = "git push",
12+
NpmScript = "npm script",
13+
}
14+
15+
/**
16+
* The NPM version scripts
17+
*
18+
* @see https://docs.npmjs.com/cli/version.html
19+
*/
20+
export const enum NpmScript {
21+
PreVersion = "preversion",
22+
Version = "version",
23+
PostVersion = "postversion",
1224
}
1325

1426
/**
1527
* Information about the progress of the `versionBump()` function.
1628
*/
1729
export interface VersionBumpProgress extends VersionBumpResults {
1830
event: ProgressEvent;
31+
script?: NpmScript;
1932
}

src/version-bump.ts

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@ import { getNewVersion } from "./get-new-version";
22
import { getOldVersion } from "./get-old-version";
33
import { gitCommit, gitPush, gitTag } from "./git";
44
import { Operation } from "./operation";
5+
import { runNpmScript } from "./run-npm-script";
56
import { VersionBumpOptions } from "./types/version-bump-options";
7+
import { NpmScript } from "./types/version-bump-progress";
68
import { VersionBumpResults } from "./types/version-bump-results";
79
import { updateFiles } from "./update-files";
810

@@ -46,12 +48,23 @@ export async function versionBump(arg: VersionBumpOptions | string = {}): Promis
4648
await getOldVersion(operation);
4749
await getNewVersion(operation);
4850

51+
// Run npm preversion script, if any
52+
await runNpmScript(NpmScript.PreVersion, operation);
53+
4954
// Update the version number in all files
5055
await updateFiles(operation);
5156

52-
// Git commit, tag, push (if enabled)
57+
// Run npm version script, if any
58+
await runNpmScript(NpmScript.Version, operation);
59+
60+
// Git commit and tag, if enabled
5361
await gitCommit(operation);
5462
await gitTag(operation);
63+
64+
// Run npm postversion script, if any
65+
await runNpmScript(NpmScript.PostVersion, operation);
66+
67+
// Push the git commit and tag, if enabled
5568
await gitPush(operation);
5669

5770
return operation.results;

0 commit comments

Comments
 (0)