-
Notifications
You must be signed in to change notification settings - Fork 111
/
set-npmrc-auth.js
47 lines (42 loc) · 1.53 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
38
39
40
41
42
43
44
45
46
47
const path = require('path');
const rc = require('rc');
const {outputFile, readFile} = require('fs-extra');
const getAuthToken = require('registry-auth-token');
const nerfDart = require('nerf-dart');
const AggregateError = require('aggregate-error');
const getError = require('./get-error');
module.exports = async (
npmrc,
registry,
{cwd, env: {NPM_TOKEN, NPM_CONFIG_USERCONFIG, NPM_USERNAME, NPM_PASSWORD, NPM_EMAIL}, 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) => readFile(config)))).join('\n') : '';
if (getAuthToken(registry, {npmrc: rcConfig})) {
await outputFile(npmrc, currentConfig);
return;
}
if (NPM_USERNAME && NPM_PASSWORD && NPM_EMAIL) {
await outputFile(
npmrc,
`${currentConfig ? `${currentConfig}\n` : ''}_auth = \${LEGACY_TOKEN}\nemail = \${NPM_EMAIL}`
);
logger.log(`Wrote NPM_USERNAME, NPM_PASSWORD and NPM_EMAIL to ${npmrc}`);
} else if (NPM_TOKEN) {
await outputFile(
npmrc,
`${currentConfig ? `${currentConfig}\n` : ''}${nerfDart(registry)}:_authToken = \${NPM_TOKEN}`
);
logger.log(`Wrote NPM_TOKEN to ${npmrc}`);
} else {
throw new AggregateError([getError('ENONPMTOKEN', {registry})]);
}
};