Skip to content

Commit

Permalink
Added support for non-existent tags on existing packages
Browse files Browse the repository at this point in the history
Fixes #24
  • Loading branch information
JamesMessinger committed Jan 2, 2021
1 parent 4544e09 commit be95160
Show file tree
Hide file tree
Showing 4 changed files with 145 additions and 5 deletions.
8 changes: 5 additions & 3 deletions src/npm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,12 @@ export const npm = {
result = err as ezSpawn.ProcessError;
}

let version = result.stdout.trim();
let error = result.stderr.trim();
let status = result.status || 0;

// If the package was not previously published, return version 0.0.0.
if (result.stderr && result.stderr.includes("E404")) {
if ((status === 0 && !version) || error.includes("E404")) {
options.debug(`The latest version of ${name} is at v0.0.0, as it was never published.`);
return new SemVer("0.0.0");
}
Expand All @@ -57,8 +61,6 @@ export const npm = {
throw result;
}

let version = result.stdout.trim();

// Parse/validate the version number
let semver = new SemVer(version);

Expand Down
54 changes: 53 additions & 1 deletion test/specs/action/success.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,8 @@ describe("GitHub Action - success tests", () => {
npm.mock({
args: ["view", "my-lib", "version"],
stdout: `${EOL}`,
stderr: `npm ERR! code E404${EOL}`
stderr: `npm ERR! code E404${EOL}`,
exitCode: 1,
});

npm.mock({
Expand Down Expand Up @@ -112,6 +113,57 @@ describe("GitHub Action - success tests", () => {
npm.assert.ran(4);
});

it("should publish a new version to NPM if the tag does not exist", async () => {
files.create([
{ path: "workspace/package.json", contents: { name: "my-lib", version: "1.0.0" }},
]);

npm.mock({
args: ["config", "get", "userconfig"],
stdout: `${paths.npmrc}${EOL}`,
});

npm.mock({
args: ["view", "my-lib@my-tag", "version"],
stdout: `${EOL}`,
});

npm.mock({
args: ["config", "get", "userconfig"],
stdout: `${paths.npmrc}${EOL}`,
});

npm.mock({
args: ["publish", "--tag", "my-tag"],
stdout: `my-lib 1.0.0${EOL}`,
});

let cli = exec.action({
env: {
INPUT_TOKEN: "my-secret-token",
INPUT_TAG: "my-tag",
}
});

expect(cli).to.have.stderr("");
expect(cli).stdout.to.include("my-lib 1.0.0");
expect(cli).stdout.to.include("Successfully published my-lib v1.0.0 to NPM");
expect(cli).stdout.to.include("::set-output name=type::major");
expect(cli).stdout.to.include("::set-output name=version::1.0.0");
expect(cli).stdout.to.include("::set-output name=old-version::0.0.0");
expect(cli).stdout.to.include("::set-output name=tag::my-tag");
expect(cli).stdout.to.include("::set-output name=access::public");
expect(cli).stdout.to.include("::set-output name=dry-run::false");
expect(cli).to.have.exitCode(0);

files.assert.contents("home/.npmrc",
`//registry.npmjs.org/:_authToken=\${INPUT_TOKEN}${EOL}` +
`registry=https://registry.npmjs.org/${EOL}`
);

npm.assert.ran(4);
});

it("should not publish a new version to NPM if the version number hasn't changed", () => {
files.create([
{ path: "workspace/package.json", contents: { name: "my-lib", version: "1.0.0" }},
Expand Down
40 changes: 40 additions & 0 deletions test/specs/cli/success.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,46 @@ describe("CLI - success tests", () => {
npm.assert.ran(4);
});

it("should publish a new version to NPM if the tag does not exist", async () => {
files.create([
{ path: "workspace/package.json", contents: { name: "my-lib", version: "1.0.0" }},
]);

npm.mock({
args: ["config", "get", "userconfig"],
stdout: `${paths.npmrc}${EOL}`,
});

npm.mock({
args: ["view", "my-lib@my-tag", "version"],
stdout: `${EOL}`,
});

npm.mock({
args: ["config", "get", "userconfig"],
stdout: `${paths.npmrc}${EOL}`,
});

npm.mock({
args: ["publish", "--tag", "my-tag"],
stdout: `my-lib 1.0.0${EOL}`,
});

let cli = exec.cli("--tag", "my-tag");

expect(cli).to.have.stderr("");
expect(cli).stdout.to.include("my-lib 1.0.0");
expect(cli).stdout.to.include("Successfully published my-lib v1.0.0 to NPM");
expect(cli).to.have.exitCode(0);

files.assert.contents("home/.npmrc",
`//registry.npmjs.org/:_authToken=\${INPUT_TOKEN}${EOL}` +
`registry=https://registry.npmjs.org/${EOL}`
);

npm.assert.ran(4);
});

it("should not publish a new version to NPM if the version number hasn't changed", () => {
files.create([
{ path: "workspace/package.json", contents: { name: "my-lib", version: "1.0.0" }},
Expand Down
48 changes: 47 additions & 1 deletion test/specs/lib/success.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,8 @@ describe("NPM package - success tests", () => {
npm.mock({
args: ["view", "my-lib", "version"],
stdout: `${EOL}`,
stderr: `npm ERR! code E404${EOL}`
stderr: `npm ERR! code E404${EOL}`,
exitCode: 1,
});

npm.mock({
Expand Down Expand Up @@ -111,6 +112,51 @@ describe("NPM package - success tests", () => {
npm.assert.ran(4);
});

it("should publish a new version to NPM if the tag does not exist", async () => {
files.create([
{ path: "workspace/package.json", contents: { name: "my-lib", version: "1.0.0" }},
]);

npm.mock({
args: ["config", "get", "userconfig"],
stdout: `${paths.npmrc}${EOL}`,
});

npm.mock({
args: ["view", "my-lib@my-tag", "version"],
stdout: `${EOL}`,
});

npm.mock({
args: ["config", "get", "userconfig"],
stdout: `${paths.npmrc}${EOL}`,
});

npm.mock({
args: ["publish", "--tag", "my-tag"],
stdout: `my-lib 1.0.0${EOL}`,
});

let results = await npmPublish({ tag: "my-tag", quiet: true });

expect(results).to.deep.equal({
type: "major",
package: "my-lib",
version: "1.0.0",
oldVersion: "0.0.0",
tag: "my-tag",
access: "public",
dryRun: false,
});

files.assert.contents("home/.npmrc",
`//registry.npmjs.org/:_authToken=\${INPUT_TOKEN}${EOL}` +
`registry=https://registry.npmjs.org/${EOL}`
);

npm.assert.ran(4);
});

it("should not publish a new version to NPM if the version number hasn't changed", async () => {
files.create([
{ path: "workspace/package.json", contents: { name: "my-lib", version: "1.0.0" }},
Expand Down

0 comments on commit be95160

Please sign in to comment.