Skip to content

Commit 2656c73

Browse files
Fix first-time package publishing, fixes #11
1 parent 5a905d1 commit 2656c73

File tree

2 files changed

+53
-2
lines changed

2 files changed

+53
-2
lines changed

src/npm.ts

+9-2
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,20 @@ export const npm = {
2727
options.debug(`Running command: npm view ${name} version`);
2828

2929
// Run NPM to get the latest published version of the package
30-
let { stdout } = await ezSpawn.async("npm", ["view", name, "version"], { env });
30+
let { stdout, stderr } = await ezSpawn.async("npm", ["view", name, "version"], { env });
31+
32+
// If the package was not previously published, return version 0.0.0.
33+
if (stderr && stderr.indexOf("E404") !== -1) {
34+
options.debug(`The latest version of ${name} is at v0.0.0, as it was never published.`);
35+
return new SemVer("0.0.0");
36+
}
37+
3138
let version = stdout.trim();
3239

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

36-
options.debug(`The local version of ${name} is at v${semver}`);
43+
options.debug(`The latest version of ${name} is at v${semver}`);
3744
return semver;
3845
}
3946
catch (error) {

test/specs/lib/success.spec.js

+44
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,50 @@ describe("NPM package - success tests", () => {
6363
npm.assert.ran(4);
6464
});
6565

66+
it("should publish a new version to NPM if no package exists", async () => {
67+
files.create([
68+
{ path: "workspace/package.json", contents: { name: "my-lib", version: "2.0.0" }},
69+
]);
70+
71+
npm.mock({
72+
args: ["config", "get", "userconfig"],
73+
stdout: `${paths.npmrc}${EOL}`,
74+
});
75+
76+
npm.mock({
77+
args: ["view", "my-lib", "version"],
78+
stdout: `${EOL}`,
79+
stderr: `npm ERR! code E404${EOL}`
80+
});
81+
82+
npm.mock({
83+
args: ["config", "get", "userconfig"],
84+
stdout: `${paths.npmrc}${EOL}`,
85+
});
86+
87+
npm.mock({
88+
args: ["publish"],
89+
stdout: `my-lib 2.0.0${EOL}`,
90+
});
91+
92+
let results = await npmPublish({ quiet: true });
93+
94+
expect(results).to.deep.equal({
95+
type: "major",
96+
package: "my-lib",
97+
version: "2.0.0",
98+
oldVersion: "0.0.0",
99+
dryRun: false,
100+
});
101+
102+
files.assert.contents("home/.npmrc",
103+
`//registry.npmjs.org/:_authToken=\${INPUT_TOKEN}${EOL}` +
104+
`registry=https://registry.npmjs.org/${EOL}`
105+
);
106+
107+
npm.assert.ran(4);
108+
});
109+
66110
it("should not publish a new version to NPM if the version number hasn't changed", async () => {
67111
files.create([
68112
{ path: "workspace/package.json", contents: { name: "my-lib", version: "1.0.0" }},

0 commit comments

Comments
 (0)