Skip to content

Commit 0288a8f

Browse files
Fix: Use the configured NPM registry and token for ALL npm commands, not just for publishing
1 parent 26ebb24 commit 0288a8f

File tree

7 files changed

+192
-74
lines changed

7 files changed

+192
-74
lines changed

src/npm.ts

Lines changed: 27 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -15,18 +15,24 @@ export const npm = {
1515
/**
1616
* Gets the latest published version of the specified package.
1717
*/
18-
async getLatestVersion(name: string, { debug }: NormalizedOptions): Promise<SemVer> {
18+
async getLatestVersion(name: string, options: NormalizedOptions): Promise<SemVer> {
19+
// Update the NPM config with the specified registry and token
20+
await setNpmConfig(options);
21+
1922
try {
20-
debug(`Running command: npm view ${name} version`);
23+
// Get the environment variables to pass to NPM
24+
let env = getNpmEnvironment(options);
25+
26+
options.debug(`Running command: npm view ${name} version`);
2127

2228
// Run NPM to get the latest published versiono of the package
23-
let process = await ezSpawn.async("npm", "view", name, "version");
24-
let version = process.stdout.trim();
29+
let { stdout } = await ezSpawn.async("npm", ["view", name, "version"], { env });
30+
let version = stdout.trim();
2531

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

29-
debug(`The local version of ${name} is at v${semver}`);
35+
options.debug(`The local version of ${name} is at v${semver}`);
3036
return semver;
3137
}
3238
catch (error) {
@@ -49,20 +55,29 @@ export const npm = {
4955
// Determine whether to suppress NPM's output
5056
let stdio: StdioOptions = options.quiet ? "pipe" : "inherit";
5157

52-
// Only pass environment variables if we need to set the NPM token
53-
let env = Boolean(options.token && process.env.INPUT_TOKEN !== options.token);
58+
// Get the environment variables to pass to NPM
59+
let env = getNpmEnvironment(options);
5460

5561
options.debug("Running command: npm publish", { stdio, cwd, env });
5662

5763
// Run NPM to publish the package
58-
await ezSpawn.async("npm", ["publish"], {
59-
cwd,
60-
stdio,
61-
env: env ? { ...process.env, INPUT_TOKEN: options.token } : undefined
62-
});
64+
await ezSpawn.async("npm", ["publish"], { cwd, stdio, env });
6365
}
6466
catch (error) {
6567
throw ono(error, `Unable to publish ${name} v${version} to NPM.`);
6668
}
6769
},
6870
};
71+
72+
73+
/**
74+
* Returns the environment variables that should be passed to NPM, based on the given options.
75+
*/
76+
function getNpmEnvironment(options: NormalizedOptions): NodeJS.ProcessEnv | undefined {
77+
// Determine if we need to set the NPM token
78+
let needsToken = Boolean(options.token && process.env.INPUT_TOKEN !== options.token);
79+
80+
if (needsToken) {
81+
return { ...process.env, INPUT_TOKEN: options.token };
82+
}
83+
}

test/specs/action/failure.spec.js

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,11 @@ describe("GitHub Action - failure tests", () => {
126126
{ path: "workspace/package.json", contents: { name: "my-lib", version: "2.0.0" }},
127127
]);
128128

129+
npm.mock({
130+
args: ["config", "get", "userconfig"],
131+
stdout: `${paths.npmrc}${EOL}`,
132+
});
133+
129134
npm.mock({
130135
args: ["view", "my-lib", "version"],
131136
stderr: "BOOM!",
@@ -144,7 +149,7 @@ describe("GitHub Action - failure tests", () => {
144149
expect(cli).stdout.to.include("BOOM!");
145150
expect(cli).to.have.exitCode(1);
146151

147-
npm.assert.ran(1);
152+
npm.assert.ran(2);
148153
});
149154

150155
it("should fail if the .npmrc file is invalid", () => {
@@ -153,11 +158,6 @@ describe("GitHub Action - failure tests", () => {
153158
{ path: "home/.npmrc/file.txt", contents: "~/.npmrc is a directory, not a file" },
154159
]);
155160

156-
npm.mock({
157-
args: ["view", "my-lib", "version"],
158-
stdout: `1.0.0${EOL}`,
159-
});
160-
161161
npm.mock({
162162
args: ["config", "get", "userconfig"],
163163
stdout: `${paths.npmrc}${EOL}`,
@@ -174,19 +174,14 @@ describe("GitHub Action - failure tests", () => {
174174
expect(cli).stdout.to.include("Error: EISDIR: illegal operation on a directory, read");
175175
expect(cli).to.have.exitCode(1);
176176

177-
npm.assert.ran(2);
177+
npm.assert.ran(1);
178178
});
179179

180180
it('should fail if the "npm config" command errors', () => {
181181
files.create([
182182
{ path: "workspace/package.json", contents: { name: "my-lib", version: "2.0.0" }},
183183
]);
184184

185-
npm.mock({
186-
args: ["view", "my-lib", "version"],
187-
stdout: `1.0.0${EOL}`,
188-
});
189-
190185
npm.mock({
191186
args: ["config", "get", "userconfig"],
192187
stderr: "BOOM!",
@@ -205,14 +200,19 @@ describe("GitHub Action - failure tests", () => {
205200
expect(cli).stdout.to.include("BOOM!");
206201
expect(cli).to.have.exitCode(1);
207202

208-
npm.assert.ran(2);
203+
npm.assert.ran(1);
209204
});
210205

211206
it('should fail if the "npm publish" command errors', () => {
212207
files.create([
213208
{ path: "workspace/package.json", contents: { name: "my-lib", version: "2.0.0" }},
214209
]);
215210

211+
npm.mock({
212+
args: ["config", "get", "userconfig"],
213+
stdout: `${paths.npmrc}${EOL}`,
214+
});
215+
216216
npm.mock({
217217
args: ["view", "my-lib", "version"],
218218
stdout: `1.0.0${EOL}`,
@@ -241,7 +241,7 @@ describe("GitHub Action - failure tests", () => {
241241
expect(cli).stdout.not.to.include("BOOM!");
242242
expect(cli).to.have.exitCode(1);
243243

244-
npm.assert.ran(3);
244+
npm.assert.ran(4);
245245
});
246246

247247
});

test/specs/action/success.spec.js

Lines changed: 37 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,11 @@ describe("GitHub Action - success tests", () => {
1515
{ path: "workspace/package.json", contents: { name: "my-lib", version: "2.0.0" }},
1616
]);
1717

18+
npm.mock({
19+
args: ["config", "get", "userconfig"],
20+
stdout: `${paths.npmrc}${EOL}`,
21+
});
22+
1823
npm.mock({
1924
args: ["view", "my-lib", "version"],
2025
stdout: `1.0.0${EOL}`,
@@ -50,14 +55,19 @@ describe("GitHub Action - success tests", () => {
5055
`registry=https://registry.npmjs.org/${EOL}`
5156
);
5257

53-
npm.assert.ran(3);
58+
npm.assert.ran(4);
5459
});
5560

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

66+
npm.mock({
67+
args: ["config", "get", "userconfig"],
68+
stdout: `${paths.npmrc}${EOL}`,
69+
});
70+
6171
npm.mock({
6272
args: ["view", "my-lib", "version"],
6373
stdout: `1.0.0${EOL}`,
@@ -76,8 +86,12 @@ describe("GitHub Action - success tests", () => {
7686
expect(cli).stdout.to.include("::set-output name=old-version::1.0.0");
7787
expect(cli).to.have.exitCode(0);
7888

79-
files.assert.doesNotExist("home/.npmrc");
80-
npm.assert.ran(1);
89+
files.assert.contents("home/.npmrc",
90+
`//registry.npmjs.org/:_authToken=\${INPUT_TOKEN}${EOL}` +
91+
`registry=https://registry.npmjs.org/${EOL}`
92+
);
93+
94+
npm.assert.ran(2);
8195
});
8296

8397
it("should append to an existing .npmrc file", () => {
@@ -86,6 +100,11 @@ describe("GitHub Action - success tests", () => {
86100
{ path: "home/.npmrc", contents: "This is my NPM config.\nThere are many like it,\nbut this one is mine." },
87101
]);
88102

103+
npm.mock({
104+
args: ["config", "get", "userconfig"],
105+
stdout: `${paths.npmrc}${EOL}`,
106+
});
107+
89108
npm.mock({
90109
args: ["view", "my-lib", "version"],
91110
stdout: `1.0.0${EOL}`,
@@ -120,11 +139,12 @@ describe("GitHub Action - success tests", () => {
120139
`This is my NPM config.${EOL}` +
121140
`There are many like it,${EOL}` +
122141
`but this one is mine.${EOL}` +
142+
`${EOL}` +
123143
`//registry.npmjs.org/:_authToken=\${INPUT_TOKEN}${EOL}` +
124144
`registry=https://registry.npmjs.org/${EOL}`
125145
);
126146

127-
npm.assert.ran(3);
147+
npm.assert.ran(4);
128148
});
129149

130150
it("should update an existing .npmrc file's settings", () => {
@@ -145,6 +165,11 @@ describe("GitHub Action - success tests", () => {
145165
},
146166
]);
147167

168+
npm.mock({
169+
args: ["config", "get", "userconfig"],
170+
stdout: `${paths.npmrc}${EOL}`,
171+
});
172+
148173
npm.mock({
149174
args: ["view", "my-lib", "version"],
150175
stdout: `1.0.0${EOL}`,
@@ -182,18 +207,24 @@ describe("GitHub Action - success tests", () => {
182207
`${EOL}` +
183208
`# Use some other package registry${EOL}` +
184209
`${EOL}` +
210+
`${EOL}` +
185211
`//registry.npmjs.org/:_authToken=\${INPUT_TOKEN}${EOL}` +
186212
`registry=https://registry.npmjs.org/${EOL}`
187213
);
188214

189-
npm.assert.ran(3);
215+
npm.assert.ran(4);
190216
});
191217

192218
it("should publish a package that's not in the root of the workspace directory", () => {
193219
files.create([
194220
{ path: "workspace/subdir/my-lib/package.json", contents: { name: "my-lib", version: "1.0.0-beta" }},
195221
]);
196222

223+
npm.mock({
224+
args: ["config", "get", "userconfig"],
225+
stdout: `${paths.npmrc}${EOL}`,
226+
});
227+
197228
npm.mock({
198229
args: ["view", "my-lib", "version"],
199230
stdout: `1.0.0${EOL}`,
@@ -231,7 +262,7 @@ describe("GitHub Action - success tests", () => {
231262
`registry=https://registry.npmjs.org/${EOL}`
232263
);
233264

234-
npm.assert.ran(3);
265+
npm.assert.ran(4);
235266
});
236267

237268
});

test/specs/cli/failure.spec.js

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,11 @@ describe("CLI - failure tests", () => {
7171
{ path: "workspace/package.json", contents: { name: "my-lib", version: "2.0.0" }},
7272
]);
7373

74+
npm.mock({
75+
args: ["config", "get", "userconfig"],
76+
stdout: `${paths.npmrc}${EOL}`,
77+
});
78+
7479
npm.mock({
7580
args: ["view", "my-lib", "version"],
7681
stderr: "BOOM!",
@@ -87,7 +92,7 @@ describe("CLI - failure tests", () => {
8792
);
8893
expect(cli).to.have.exitCode(1);
8994

90-
npm.assert.ran(1);
95+
npm.assert.ran(2);
9196
});
9297

9398
it("should fail if the .npmrc file is invalid", () => {
@@ -96,11 +101,6 @@ describe("CLI - failure tests", () => {
96101
{ path: "home/.npmrc/file.txt", contents: "~/.npmrc is a directory, not a file" },
97102
]);
98103

99-
npm.mock({
100-
args: ["view", "my-lib", "version"],
101-
stdout: `1.0.0${EOL}`,
102-
});
103-
104104
npm.mock({
105105
args: ["config", "get", "userconfig"],
106106
stdout: `${paths.npmrc}${EOL}`,
@@ -113,19 +113,14 @@ describe("CLI - failure tests", () => {
113113
expect(cli).stderr.to.include("EISDIR: illegal operation on a directory, read");
114114
expect(cli).to.have.exitCode(1);
115115

116-
npm.assert.ran(2);
116+
npm.assert.ran(1);
117117
});
118118

119119
it('should fail if the "npm config" command errors', () => {
120120
files.create([
121121
{ path: "workspace/package.json", contents: { name: "my-lib", version: "2.0.0" }},
122122
]);
123123

124-
npm.mock({
125-
args: ["view", "my-lib", "version"],
126-
stdout: `1.0.0${EOL}`,
127-
});
128-
129124
npm.mock({
130125
args: ["config", "get", "userconfig"],
131126
stderr: "BOOM!",
@@ -140,14 +135,19 @@ describe("CLI - failure tests", () => {
140135
expect(cli).stderr.to.include("BOOM!");
141136
expect(cli).to.have.exitCode(1);
142137

143-
npm.assert.ran(2);
138+
npm.assert.ran(1);
144139
});
145140

146141
it('should fail if the "npm publish" command errors', () => {
147142
files.create([
148143
{ path: "workspace/package.json", contents: { name: "my-lib", version: "2.0.0" }},
149144
]);
150145

146+
npm.mock({
147+
args: ["config", "get", "userconfig"],
148+
stdout: `${paths.npmrc}${EOL}`,
149+
});
150+
151151
npm.mock({
152152
args: ["view", "my-lib", "version"],
153153
stdout: `1.0.0${EOL}`,
@@ -172,7 +172,7 @@ describe("CLI - failure tests", () => {
172172
expect(cli).stderr.to.include("BOOM!");
173173
expect(cli).to.have.exitCode(1);
174174

175-
npm.assert.ran(3);
175+
npm.assert.ran(4);
176176
});
177177

178178
});

0 commit comments

Comments
 (0)