-
Notifications
You must be signed in to change notification settings - Fork 110
/
set-npmrc-auth.js
37 lines (32 loc) · 1.18 KB
/
set-npmrc-auth.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
import path from "path";
import rc from "rc";
import fs from "fs-extra";
import getAuthToken from "registry-auth-token";
import nerfDart from "nerf-dart";
import AggregateError from "aggregate-error";
import getError from "./get-error.js";
export default async function (npmrc, registry, { cwd, env: { NPM_TOKEN, NPM_CONFIG_USERCONFIG }, logger }) {
logger.log("Verify authentication for registry %s", registry);
const { configs, ...rcConfig } = rc(
"npm",
{ registry: "https://registry.npmjs.org/" },
{ config: NPM_CONFIG_USERCONFIG || path.resolve(cwd, ".npmrc") }
);
if (configs) {
logger.log("Reading npm config from %s", configs.join(", "));
}
const currentConfig = configs ? (await Promise.all(configs.map((config) => fs.readFile(config)))).join("\n") : "";
if (getAuthToken(registry, { npmrc: rcConfig })) {
await fs.outputFile(npmrc, currentConfig);
return;
}
if (NPM_TOKEN) {
await fs.outputFile(
npmrc,
`${currentConfig ? `${currentConfig}\n` : ""}${nerfDart(registry)}:_authToken = \${NPM_TOKEN}`
);
logger.log(`Wrote NPM_TOKEN to ${npmrc}`);
} else {
throw new AggregateError([getError("ENONPMTOKEN", { registry })]);
}
}