Skip to content

Commit 071192b

Browse files
committed
Add support for npm publish --dry-run
Add an extra Option/command line argument to optionally run npm publish with the --dry-run flag enabled to prevent actual publishing. Intended for use in testing workflows. This option/argument defaults to false.
1 parent eba6c49 commit 071192b

File tree

14 files changed

+168
-5
lines changed

14 files changed

+168
-5
lines changed

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ You can set any or all of the following input parameters:
7878
|`registry` |string |no |https://registry.npmjs.org/ |The NPM registry URL to use
7979
|`package` |string |no |./package.json |The path of your package.json file
8080
|`check-version` |boolean |no |true |Only publish to NPM if the version number in `package.json` differs from the latest on NPM
81+
|`dry-run` |boolean |no |false |Run NPM publish with the `--dry-run` flag to prevent publication.
8182

8283

8384

@@ -194,6 +195,8 @@ options:
194195

195196
--help, -h Show help
196197

198+
--dry-run Pass the `--dry-run` flag to NPM
199+
197200
package_path The absolute or relative path of the NPM package to publish.
198201
Can be a directory path, or the path of a package.json file.
199202
Defaults to the current directory.

action.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,11 @@ inputs:
2929
required: false
3030
default: "true"
3131

32+
dry-run:
33+
description: If true, run with the --dry-run flag
34+
required: false
35+
default: "false"
36+
3237
outputs:
3338
type:
3439
description: The type of version change that occurred ("none", "major", "minor", "patch", etc.)

src/action/index.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,19 @@ async function main(): Promise<void> {
1818
registry: getInput("registry", { required: true }),
1919
package: getInput("package", { required: true }),
2020
checkVersion: getInput("check-version", { required: true }).toLowerCase() === "true",
21+
dryRun: getInput("dry-run").toLowerCase() === "true",
2122
debug: debugHandler,
2223
};
2324

24-
// Puglish to NPM
25+
// Publish to NPM
2526
let results = await npmPublish(options);
2627

2728
if (results.type === "none") {
2829
console.log(`\n📦 ${results.package} v${results.version} is already published to NPM`);
2930
}
31+
else if (results.type === "dry-run") {
32+
console.log(`\n📦 ${results.package} v${results.version} successfully built but not published to NPM`);
33+
}
3034
else {
3135
console.log(`\n📦 Successfully published ${results.package} v${results.version} to NPM`);
3236
}

src/cli/index.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,9 @@ export async function main(args: string[]): Promise<void> {
3737
if (results.type === "none") {
3838
console.log(`\n📦 ${results.package} v${results.version} is already published to NPM`);
3939
}
40+
else if (results.type === "dry-run") {
41+
console.log(`\n📦 ${results.package} v${results.version} successfully built but not published to NPM`);
42+
}
4043
else {
4144
console.log(`\n📦 Successfully published ${results.package} v${results.version} to NPM`);
4245
}

src/cli/parse-args.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ export function parseArgs(argv: string[]): ParsedArgs {
2828
{ name: "quiet", alias: "q", type: Boolean },
2929
{ name: "version", alias: "v", type: Boolean },
3030
{ name: "help", alias: "h", type: Boolean },
31+
{ name: "dry-run", type: Boolean, defaultOption: false }
3132
],
3233
{ argv }
3334
);
@@ -49,6 +50,7 @@ export function parseArgs(argv: string[]): ParsedArgs {
4950
package: args.package as string,
5051
debug: args.debug ? console.debug : undefined,
5152
quiet: args.quiet as boolean,
53+
dryRun: args["dry-run"] as boolean
5254
}
5355
};
5456

src/normalize-options.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ export interface NormalizedOptions {
1010
registry: URL;
1111
package: string;
1212
checkVersion: boolean;
13+
dryRun: boolean;
1314
quiet: boolean;
1415
debug: Debug;
1516
}
@@ -26,6 +27,7 @@ export function normalizeOptions(options: Options): NormalizedOptions {
2627
registry: registryURL || new URL("https://registry.npmjs.org/"),
2728
package: options.package || "package.json",
2829
checkVersion: options.checkVersion === undefined ? true : Boolean(options.checkVersion),
30+
dryRun: options.dryRun || false,
2931
quiet: options.quiet || false,
3032
debug: options.debug || (() => undefined),
3133
};

src/npm-publish.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ export async function npmPublish(opts: Options = {}): Promise<Results> {
2525

2626
let results: Results = {
2727
package: manifest.name,
28-
type: diff || "none",
28+
type: options.dryRun ? "dry-run" : (diff || "none"),
2929
version: manifest.version.raw,
3030
oldVersion: publishedVersion.raw,
3131
};

src/npm.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ export const npm = {
2525

2626
options.debug(`Running command: npm view ${name} version`);
2727

28-
// Run NPM to get the latest published versiono of the package
28+
// Run NPM to get the latest published version of the package
2929
let { stdout } = await ezSpawn.async("npm", ["view", name, "version"], { env });
3030
let version = stdout.trim();
3131

@@ -60,8 +60,10 @@ export const npm = {
6060

6161
options.debug("Running command: npm publish", { stdio, cwd, env });
6262

63+
let command = options.dryRun ? ["publish", "--dry-run"] : ["publish"];
64+
6365
// Run NPM to publish the package
64-
await ezSpawn.async("npm", ["publish"], { cwd, stdio, env });
66+
await ezSpawn.async("npm", command, { cwd, stdio, env });
6567
}
6668
catch (error) {
6769
throw ono(error, `Unable to publish ${name} v${version} to NPM.`);

src/options.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,15 @@ export interface Options {
3131
*/
3232
checkVersion?: boolean;
3333

34+
/**
35+
* If true, run npm publish with the --dry-run flag
36+
* so that the package is not published. Used for
37+
* testing workflows.
38+
*
39+
* Defaults to `false`
40+
*/
41+
dryRun?: boolean;
42+
3443
/**
3544
* Suppress console output from NPM and npm-publish.
3645
*

src/results.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ export interface Results {
99
/**
1010
* The type of version change that occurred
1111
*/
12-
type: ReleaseType | "none";
12+
type: ReleaseType | "none" | "dry-run";
1313

1414
/**
1515
* The name of the NPM package that was published

0 commit comments

Comments
 (0)