Skip to content

Commit 3de858a

Browse files
committedJan 2, 2023
Add target option
1 parent e42e372 commit 3de858a

File tree

10 files changed

+76
-1
lines changed

10 files changed

+76
-1
lines changed
 

‎README.md

+2
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ You can set any or all of the following input parameters:
7070
|`package` |string |./package.json |The path of your package.json file
7171
|`tag` |string |"latest" |The tag to publish to. This allows people to install the package using `npm install <package-name>@<tag>`.
7272
|`access` |string |"public" for non-scoped packages. "restricted" for scoped packages.|Determines whether the published package should be publicly visible, or restricted to members of your NPM organization.
73+
|`target` |string |"" |The package to publish (the first argument to npm publish).
7374
|`dry-run` |boolean |false |Run NPM publish with the `--dry-run` flag to prevent publication
7475
|`check-version` |boolean |true |Only publish to NPM if the version number in `package.json` differs from the latest on NPM
7576
|`greater-version-only`|boolean |false |Only publish to NPM if the version number in `package.json` is greater than the latest on NPM |
@@ -135,6 +136,7 @@ As shown in the example above, you can pass options to the `npmPublish()` functi
135136
| `tag` | string | "latest" | The tag to publish to. This allows people to install the package using `npm install <package-name>@<tag>`. |
136137
| `access` | string | "public" for non-scoped packages. "restricted" for scoped packages. | Determines whether the published package should be publicly visible, or restricted to members of your NPM organization. |
137138
| `dryRun` | boolean | false | Run NPM publish with the `--dry-run` flag to prevent publication |
139+
| `target` | string | "" | The package to publish (the first argument to npm publish) |
138140
| `checkVersion` | boolean | true | Only publish to NPM if the version number in `package.json` differs from the latest on NPM |
139141
| `greaterVersionOnly` | boolean | false | Only publish to NPM if the version number in `package.json` is greater then the latest on NPM |
140142
| `quiet` | boolean | false | Suppress console output from NPM and npm-publish |

‎action.yml

+5
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,11 @@ inputs:
3535
This only applies to scoped packages.
3636
required: false
3737

38+
target:
39+
description: The package to publish (the first argument to npm publish).
40+
required: false
41+
default: ""
42+
3843
dry-run:
3944
description: If true, run with the --dry-run flag
4045
required: false

‎dist/index.js

+5
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎dist/index.js.map

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎src/action/index.ts

+1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ async function main(): Promise<void> {
2222
getInput("check-version", { required: true }).toLowerCase() === "true",
2323
tag: getInput("tag"),
2424
access: getInput("access") as Access,
25+
target: getInput("target"),
2526
dryRun: getInput("dry-run").toLowerCase() === "true",
2627
greaterVersionOnly: getInput("greater-version-only").toLowerCase() === "true",
2728
debug: debugHandler,

‎src/cli/parse-args.ts

+1
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ export function parseArgs(argv: string[]): ParsedArgs {
5252
package: args.package as string,
5353
tag: args.tag as string,
5454
access: args.access as Access,
55+
target: args.target as string,
5556
dryRun: args["dry-run"] as boolean,
5657
debug: args.debug ? console.debug : undefined,
5758
quiet: args.quiet as boolean,

‎src/normalize-options.ts

+2
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ export interface NormalizedOptions {
1111
package: string;
1212
tag: string;
1313
access?: Access;
14+
target: string;
1415
dryRun: boolean;
1516
checkVersion: boolean;
1617
greaterVersionOnly: boolean;
@@ -34,6 +35,7 @@ export function normalizeOptions(options: Options): NormalizedOptions {
3435
package: options.package || "package.json",
3536
tag: options.tag || "latest",
3637
access: options.access,
38+
target: options.target || "",
3739
dryRun: options.dryRun || false,
3840
checkVersion:
3941
options.checkVersion === undefined ? true : Boolean(options.checkVersion),

‎src/npm.ts

+4
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,10 @@ export const npm = {
9595
command.push("--dry-run");
9696
}
9797

98+
if (options.target) {
99+
command.push(options.target);
100+
}
101+
98102
// Run "npm publish" in the package.json directory
99103
let cwd = resolve(dirname(options.package));
100104

‎src/options.ts

+8
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,14 @@ export interface Options {
4040
*/
4141
access?: Access;
4242

43+
/**
44+
* The package to publish.
45+
* This is the first argument to npm publish.
46+
*
47+
* Defaults to "" which is the default behavior of npm publish.
48+
*/
49+
target?: string;
50+
4351
/**
4452
* If true, run npm publish with the --dry-run flag
4553
* so that the package is not published. Used for

‎test/specs/lib/success.spec.js

+47
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,53 @@ describe("NPM package - success tests", () => {
192192
npm.assert.ran(2);
193193
});
194194

195+
it("should pass target option as argument to npm publish", async () => {
196+
files.create([
197+
{ path: "workspace/package.json", contents: { name: "my-lib", version: "2.0.0" }},
198+
]);
199+
200+
npm.mock({
201+
args: ["config", "get", "userconfig"],
202+
stdout: `${paths.npmrc}${EOL}`,
203+
});
204+
205+
npm.mock({
206+
args: ["view", "my-lib", "version"],
207+
stdout: `1.0.0${EOL}`,
208+
});
209+
210+
npm.mock({
211+
args: ["config", "get", "userconfig"],
212+
stdout: `${paths.npmrc}${EOL}`,
213+
});
214+
215+
npm.mock({
216+
args: ["publish my-lib-2.0.0.tgz"],
217+
stdout: `my-lib 2.0.0${EOL}`,
218+
});
219+
220+
let results = await npmPublish({ target: "my-lib-2.0.0.tgz", quiet: true });
221+
222+
expect(results).to.deep.equal({
223+
type: "major",
224+
package: "my-lib",
225+
registry: new URL("https://registry.npmjs.org/"),
226+
version: "2.0.0",
227+
oldVersion: "1.0.0",
228+
tag: "latest",
229+
access: "public",
230+
dryRun: false,
231+
});
232+
233+
files.assert.contents("home/.npmrc",
234+
`//registry.npmjs.org/:_authToken=\${INPUT_TOKEN}${EOL}` +
235+
`registry=https://registry.npmjs.org/${EOL}`
236+
);
237+
238+
npm.assert.ran(4);
239+
});
240+
241+
195242
it("should use the specified NPM token to publish the package", async () => {
196243
files.create([
197244
{ path: "workspace/package.json", contents: { name: "my-lib", version: "1.0.0-beta.1" }},

0 commit comments

Comments
 (0)
Failed to load comments.