Skip to content

Commit c6e3dbc

Browse files
authored
fix(npm): Fix NPM_TOKEN usage again (#143)
This is a retake on #130. Although npm/cli#8 claims to have support for `npm_config_//registry.npmjs.org/:_authToken=` usage, my tests and the reports on the internet says this still doesn't work, even with the latest npm (7.0.15 at the time). The only way to pass the token is to have the `authToken` line in an `.npmrc` file. The quick&dirty way would have been to create one in the project directory but that may collide with a potentially pre-existing project `.npmrc`. Trying to merge these seems more trouble than it is worth: https://github.com/actions/setup-node/blob/59e61b89511ed136a0b17773f07c349fa5c01e8b/src/authutil.ts (even worse as you'd need to revert these changes after the fact) The "better" solution I found is: 1. Create a temporary file as your npmrc 2. Put the token/registry line there 3. Tell npm to use that file as the user config 4. Use the `npm_config_userconfig` for the above to support yarn too
1 parent e798120 commit c6e3dbc

File tree

1 file changed

+34
-15
lines changed

1 file changed

+34
-15
lines changed

src/targets/npm.ts

Lines changed: 34 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ import {
1212
BaseArtifactProvider,
1313
RemoteArtifact,
1414
} from '../artifact_providers/base';
15+
import { withTempFile } from '../utils/files';
16+
import { writeFileSync } from 'fs';
1517

1618
const logger = loggerRaw.withScope('[npm]');
1719

@@ -24,6 +26,8 @@ export const YARN_BIN = process.env.YARN_BIN || 'yarn';
2426
const NPM_MIN_MAJOR = 5;
2527
const NPM_MIN_MINOR = 6;
2628

29+
const NPM_TOKEN_ENV_VAR = 'NPM_TOKEN';
30+
2731
/** A regular expression used to find the package tarball */
2832
const DEFAULT_PACKAGE_REGEX = /^.*\d\.\d.*\.tgz$/;
2933

@@ -43,6 +47,8 @@ export interface NpmTargetOptions extends TargetConfig {
4347
useOtp?: boolean;
4448
/** Do we use Yarn instead of NPM? */
4549
useYarn: boolean;
50+
/** Value of NPM_TOKEN so we can pass it to npm executable */
51+
token: string;
4652
}
4753

4854
/** Options for running the NPM publish command */
@@ -125,8 +131,14 @@ export class NpmTarget extends BaseTarget {
125131
* Extracts NPM target options from the raw configuration
126132
*/
127133
protected getNpmConfig(): NpmTargetOptions {
134+
const token = process.env.NPM_TOKEN;
135+
if (!token) {
136+
throw new Error('NPM target: NPM_TOKEN not found in the environment');
137+
}
138+
128139
const npmConfig: NpmTargetOptions = {
129140
useYarn: !!process.env.USE_YARN || !hasExecutable(NPM_BIN),
141+
token,
130142
};
131143
if (this.config.access) {
132144
if (Object.values(NpmPackageAccess).includes(this.config.access)) {
@@ -180,21 +192,28 @@ export class NpmTarget extends BaseTarget {
180192
args.push('--tag=next');
181193
}
182194

183-
// Pass OTP if configured
184-
const spawnOptions: SpawnOptions = {};
185-
if (options.otp) {
186-
spawnOptions.env = {
187-
...process.env,
188-
NPM_CONFIG_OTP: options.otp,
189-
};
190-
}
191-
192-
// The path has to be pushed always as the last arg
193-
args.push(path);
194-
195-
// Disable output buffering because NPM/Yarn can ask us for one-time passwords
196-
return spawnProcess(bin, args, spawnOptions, {
197-
showStdout: true,
195+
return withTempFile(filePath => {
196+
// Pass OTP if configured
197+
const spawnOptions: SpawnOptions = {};
198+
spawnOptions.env = { ...process.env };
199+
if (options.otp) {
200+
spawnOptions.env.NPM_CONFIG_OTP = options.otp;
201+
}
202+
spawnOptions.env[NPM_TOKEN_ENV_VAR] = this.npmConfig.token;
203+
// NOTE(byk): Use npm_config_userconfig instead of --userconfig for yarn compat
204+
spawnOptions.env.npm_config_userconfig = filePath;
205+
writeFileSync(
206+
filePath,
207+
`//registry.npmjs.org/:_authToken=\${${NPM_TOKEN_ENV_VAR}}`
208+
);
209+
210+
// The path has to be pushed always as the last arg
211+
args.push(path);
212+
213+
// Disable output buffering because NPM/Yarn can ask us for one-time passwords
214+
return spawnProcess(bin, args, spawnOptions, {
215+
showStdout: true,
216+
});
198217
});
199218
}
200219

0 commit comments

Comments
 (0)