Skip to content

Commit be95160

Browse files
Added support for non-existent tags on existing packages
Fixes #24
1 parent 4544e09 commit be95160

File tree

4 files changed

+145
-5
lines changed

4 files changed

+145
-5
lines changed

src/npm.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,12 @@ export const npm = {
4747
result = err as ezSpawn.ProcessError;
4848
}
4949

50+
let version = result.stdout.trim();
51+
let error = result.stderr.trim();
52+
let status = result.status || 0;
53+
5054
// If the package was not previously published, return version 0.0.0.
51-
if (result.stderr && result.stderr.includes("E404")) {
55+
if ((status === 0 && !version) || error.includes("E404")) {
5256
options.debug(`The latest version of ${name} is at v0.0.0, as it was never published.`);
5357
return new SemVer("0.0.0");
5458
}
@@ -57,8 +61,6 @@ export const npm = {
5761
throw result;
5862
}
5963

60-
let version = result.stdout.trim();
61-
6264
// Parse/validate the version number
6365
let semver = new SemVer(version);
6466

test/specs/action/success.spec.js

Lines changed: 53 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,8 @@ describe("GitHub Action - success tests", () => {
7474
npm.mock({
7575
args: ["view", "my-lib", "version"],
7676
stdout: `${EOL}`,
77-
stderr: `npm ERR! code E404${EOL}`
77+
stderr: `npm ERR! code E404${EOL}`,
78+
exitCode: 1,
7879
});
7980

8081
npm.mock({
@@ -112,6 +113,57 @@ describe("GitHub Action - success tests", () => {
112113
npm.assert.ran(4);
113114
});
114115

116+
it("should publish a new version to NPM if the tag does not exist", async () => {
117+
files.create([
118+
{ path: "workspace/package.json", contents: { name: "my-lib", version: "1.0.0" }},
119+
]);
120+
121+
npm.mock({
122+
args: ["config", "get", "userconfig"],
123+
stdout: `${paths.npmrc}${EOL}`,
124+
});
125+
126+
npm.mock({
127+
args: ["view", "my-lib@my-tag", "version"],
128+
stdout: `${EOL}`,
129+
});
130+
131+
npm.mock({
132+
args: ["config", "get", "userconfig"],
133+
stdout: `${paths.npmrc}${EOL}`,
134+
});
135+
136+
npm.mock({
137+
args: ["publish", "--tag", "my-tag"],
138+
stdout: `my-lib 1.0.0${EOL}`,
139+
});
140+
141+
let cli = exec.action({
142+
env: {
143+
INPUT_TOKEN: "my-secret-token",
144+
INPUT_TAG: "my-tag",
145+
}
146+
});
147+
148+
expect(cli).to.have.stderr("");
149+
expect(cli).stdout.to.include("my-lib 1.0.0");
150+
expect(cli).stdout.to.include("Successfully published my-lib v1.0.0 to NPM");
151+
expect(cli).stdout.to.include("::set-output name=type::major");
152+
expect(cli).stdout.to.include("::set-output name=version::1.0.0");
153+
expect(cli).stdout.to.include("::set-output name=old-version::0.0.0");
154+
expect(cli).stdout.to.include("::set-output name=tag::my-tag");
155+
expect(cli).stdout.to.include("::set-output name=access::public");
156+
expect(cli).stdout.to.include("::set-output name=dry-run::false");
157+
expect(cli).to.have.exitCode(0);
158+
159+
files.assert.contents("home/.npmrc",
160+
`//registry.npmjs.org/:_authToken=\${INPUT_TOKEN}${EOL}` +
161+
`registry=https://registry.npmjs.org/${EOL}`
162+
);
163+
164+
npm.assert.ran(4);
165+
});
166+
115167
it("should not publish a new version to NPM if the version number hasn't changed", () => {
116168
files.create([
117169
{ path: "workspace/package.json", contents: { name: "my-lib", version: "1.0.0" }},

test/specs/cli/success.spec.js

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,46 @@ describe("CLI - success tests", () => {
9292
npm.assert.ran(4);
9393
});
9494

95+
it("should publish a new version to NPM if the tag does not exist", async () => {
96+
files.create([
97+
{ path: "workspace/package.json", contents: { name: "my-lib", version: "1.0.0" }},
98+
]);
99+
100+
npm.mock({
101+
args: ["config", "get", "userconfig"],
102+
stdout: `${paths.npmrc}${EOL}`,
103+
});
104+
105+
npm.mock({
106+
args: ["view", "my-lib@my-tag", "version"],
107+
stdout: `${EOL}`,
108+
});
109+
110+
npm.mock({
111+
args: ["config", "get", "userconfig"],
112+
stdout: `${paths.npmrc}${EOL}`,
113+
});
114+
115+
npm.mock({
116+
args: ["publish", "--tag", "my-tag"],
117+
stdout: `my-lib 1.0.0${EOL}`,
118+
});
119+
120+
let cli = exec.cli("--tag", "my-tag");
121+
122+
expect(cli).to.have.stderr("");
123+
expect(cli).stdout.to.include("my-lib 1.0.0");
124+
expect(cli).stdout.to.include("Successfully published my-lib v1.0.0 to NPM");
125+
expect(cli).to.have.exitCode(0);
126+
127+
files.assert.contents("home/.npmrc",
128+
`//registry.npmjs.org/:_authToken=\${INPUT_TOKEN}${EOL}` +
129+
`registry=https://registry.npmjs.org/${EOL}`
130+
);
131+
132+
npm.assert.ran(4);
133+
});
134+
95135
it("should not publish a new version to NPM if the version number hasn't changed", () => {
96136
files.create([
97137
{ path: "workspace/package.json", contents: { name: "my-lib", version: "1.0.0" }},

test/specs/lib/success.spec.js

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,8 @@ describe("NPM package - success tests", () => {
7878
npm.mock({
7979
args: ["view", "my-lib", "version"],
8080
stdout: `${EOL}`,
81-
stderr: `npm ERR! code E404${EOL}`
81+
stderr: `npm ERR! code E404${EOL}`,
82+
exitCode: 1,
8283
});
8384

8485
npm.mock({
@@ -111,6 +112,51 @@ describe("NPM package - success tests", () => {
111112
npm.assert.ran(4);
112113
});
113114

115+
it("should publish a new version to NPM if the tag does not exist", async () => {
116+
files.create([
117+
{ path: "workspace/package.json", contents: { name: "my-lib", version: "1.0.0" }},
118+
]);
119+
120+
npm.mock({
121+
args: ["config", "get", "userconfig"],
122+
stdout: `${paths.npmrc}${EOL}`,
123+
});
124+
125+
npm.mock({
126+
args: ["view", "my-lib@my-tag", "version"],
127+
stdout: `${EOL}`,
128+
});
129+
130+
npm.mock({
131+
args: ["config", "get", "userconfig"],
132+
stdout: `${paths.npmrc}${EOL}`,
133+
});
134+
135+
npm.mock({
136+
args: ["publish", "--tag", "my-tag"],
137+
stdout: `my-lib 1.0.0${EOL}`,
138+
});
139+
140+
let results = await npmPublish({ tag: "my-tag", quiet: true });
141+
142+
expect(results).to.deep.equal({
143+
type: "major",
144+
package: "my-lib",
145+
version: "1.0.0",
146+
oldVersion: "0.0.0",
147+
tag: "my-tag",
148+
access: "public",
149+
dryRun: false,
150+
});
151+
152+
files.assert.contents("home/.npmrc",
153+
`//registry.npmjs.org/:_authToken=\${INPUT_TOKEN}${EOL}` +
154+
`registry=https://registry.npmjs.org/${EOL}`
155+
);
156+
157+
npm.assert.ran(4);
158+
});
159+
114160
it("should not publish a new version to NPM if the version number hasn't changed", async () => {
115161
files.create([
116162
{ path: "workspace/package.json", contents: { name: "my-lib", version: "1.0.0" }},

0 commit comments

Comments
 (0)