Skip to content

Commit 7ac97fa

Browse files
Added "tag" and "scope" options
1 parent c37331b commit 7ac97fa

File tree

5 files changed

+77
-16
lines changed

5 files changed

+77
-16
lines changed

src/normalize-options.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { URL } from "url";
2-
import { Debug, Options } from "./options";
2+
import { Access, Debug, Options } from "./options";
33

44
/**
55
* Normalized and sanitized options
@@ -9,8 +9,10 @@ export interface NormalizedOptions {
99
token: string;
1010
registry: URL;
1111
package: string;
12-
checkVersion: boolean;
12+
tag: string;
13+
access?: Access;
1314
dryRun: boolean;
15+
checkVersion: boolean;
1416
quiet: boolean;
1517
debug: Debug;
1618
}
@@ -26,8 +28,10 @@ export function normalizeOptions(options: Options): NormalizedOptions {
2628
token: options.token || "",
2729
registry: registryURL || new URL("https://registry.npmjs.org/"),
2830
package: options.package || "package.json",
29-
checkVersion: options.checkVersion === undefined ? true : Boolean(options.checkVersion),
31+
tag: options.tag || "latest",
32+
access: options.access,
3033
dryRun: options.dryRun || false,
34+
checkVersion: options.checkVersion === undefined ? true : Boolean(options.checkVersion),
3135
quiet: options.quiet || false,
3236
debug: options.debug || (() => undefined),
3337
};

src/npm-publish.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ export async function npmPublish(opts: Options = {}): Promise<Results> {
2828
type: diff || "none",
2929
version: manifest.version.raw,
3030
oldVersion: publishedVersion.raw,
31+
tag: options.tag,
32+
access: options.access || (manifest.name.startsWith("@") ? "restricted" : "public"),
3133
dryRun: options.dryRun
3234
};
3335

src/npm.ts

Lines changed: 30 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,16 +21,26 @@ export const npm = {
2121
await setNpmConfig(options);
2222

2323
try {
24+
let command = ["npm", "view"];
25+
26+
if (options.tag === "latest") {
27+
command.push(name);
28+
}
29+
else {
30+
command.push(`${name}@${options.tag}`);
31+
}
32+
33+
command.push("version");
34+
2435
// Get the environment variables to pass to NPM
2536
let env = getNpmEnvironment(options);
2637

27-
options.debug(`Running command: npm view ${name} version`);
28-
2938
// Run NPM to get the latest published version of the package
30-
let { stdout, stderr } = await ezSpawn.async("npm", ["view", name, "version"], { env });
39+
options.debug(`Running command: npm view ${name} version`, { command, env });
40+
let { stdout, stderr } = await ezSpawn.async(command, { env });
3141

3242
// If the package was not previously published, return version 0.0.0.
33-
if (stderr && stderr.indexOf("E404") !== -1) {
43+
if (stderr && stderr.includes("E404")) {
3444
options.debug(`The latest version of ${name} is at v0.0.0, as it was never published.`);
3545
return new SemVer("0.0.0");
3646
}
@@ -57,6 +67,20 @@ export const npm = {
5767
await setNpmConfig(options);
5868

5969
try {
70+
let command = ["npm", "publish"];
71+
72+
if (options.tag !== "latest") {
73+
command.push("--tag", options.tag);
74+
}
75+
76+
if (options.access) {
77+
command.push("--access", options.access);
78+
}
79+
80+
if (options.dryRun) {
81+
command.push("--dry-run");
82+
}
83+
6084
// Run "npm publish" in the package.json directory
6185
let cwd = resolve(dirname(options.package));
6286

@@ -66,12 +90,9 @@ export const npm = {
6690
// Get the environment variables to pass to NPM
6791
let env = getNpmEnvironment(options);
6892

69-
options.debug("Running command: npm publish", { stdio, cwd, env });
70-
71-
let command = options.dryRun ? ["publish", "--dry-run"] : ["publish"];
72-
7393
// Run NPM to publish the package
74-
await ezSpawn.async("npm", command, { cwd, stdio, env });
94+
options.debug("Running command: npm publish", { command, stdio, cwd, env });
95+
await ezSpawn.async(command, { cwd, stdio, env });
7596
}
7697
catch (error) {
7798
throw ono(error, `Unable to publish ${name} v${version} to NPM.`);

src/options.ts

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,21 @@ export interface Options {
2424
package?: string;
2525

2626
/**
27-
* Only publish the package if the version number in package.json
28-
* differs from the latest on NPM.
27+
* The tag to publish to. This allows people to install the package
28+
* using "npm install <package-name>@<tag>".
2929
*
30-
* Defaults to `true`
30+
* Defaults to "latest"
3131
*/
32-
checkVersion?: boolean;
32+
tag?: string;
33+
34+
/**
35+
* Determines whether the published package should be publicly visible,
36+
* or restricted to members of your NPM organization. This only applies
37+
* to scoped packages.
38+
*
39+
* Defaults to "restricted" for scoped packages and "public" for non-scoped packages.
40+
*/
41+
access?: Access;
3342

3443
/**
3544
* If true, run npm publish with the --dry-run flag
@@ -40,6 +49,14 @@ export interface Options {
4049
*/
4150
dryRun?: boolean;
4251

52+
/**
53+
* Only publish the package if the version number in package.json
54+
* differs from the latest on NPM.
55+
*
56+
* Defaults to `true`
57+
*/
58+
checkVersion?: boolean;
59+
4360
/**
4461
* Suppress console output from NPM and npm-publish.
4562
*
@@ -55,6 +72,11 @@ export interface Options {
5572
debug?: Debug;
5673
}
5774

75+
/**
76+
* The possible access levels for an NPM package
77+
*/
78+
export type Access = "public" | "restricted";
79+
5880
/**
5981
* A function that receives debug messages
6082
*/

src/results.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { ReleaseType } from "semver";
2+
import { Access } from "./options";
23

34
export { ReleaseType };
45

@@ -26,6 +27,17 @@ export interface Results {
2627
*/
2728
oldVersion: string;
2829

30+
/**
31+
* The tag that the package was published to.
32+
*/
33+
tag: string;
34+
35+
/**
36+
* Indicates whether the published package is publicly visible
37+
* or restricted to members of your NPM organization.
38+
*/
39+
access: Access;
40+
2941
/**
3042
* Whether this was a dry run (not published to NPM)
3143
*/

0 commit comments

Comments
 (0)