|
| 1 | +const path = require('path'); |
| 2 | +const rc = require('rc'); |
1 | 3 | const {appendFile} = require('fs-extra'); |
2 | 4 | const getAuthToken = require('registry-auth-token'); |
3 | 5 | const nerfDart = require('nerf-dart'); |
4 | 6 | const AggregateError = require('aggregate-error'); |
5 | 7 | const getError = require('./get-error'); |
6 | 8 |
|
7 | | -module.exports = async (registry, logger) => { |
| 9 | +module.exports = async (registry, {cwd, env: {NPM_TOKEN, NPM_USERNAME, NPM_PASSWORD, NPM_EMAIL}, logger}) => { |
8 | 10 | logger.log('Verify authentication for registry %s', registry); |
9 | | - const {NPM_TOKEN, NPM_USERNAME, NPM_PASSWORD, NPM_EMAIL} = process.env; |
10 | | - |
11 | | - if (getAuthToken(registry)) { |
| 11 | + const config = path.resolve(cwd, '.npmrc'); |
| 12 | + if (getAuthToken(registry, {npmrc: rc('npm', {registry: 'https://registry.npmjs.org/'}, {config})})) { |
12 | 13 | return; |
13 | 14 | } |
14 | 15 | if (NPM_USERNAME && NPM_PASSWORD && NPM_EMAIL) { |
15 | | - await appendFile('./.npmrc', `\n_auth = ${Buffer.from(`\${LEGACY_TOKEN}\nemail = \${NPM_EMAIL}`)}`); |
16 | | - logger.log('Wrote NPM_USERNAME, NPM_PASSWORD and NPM_EMAIL to .npmrc.'); |
| 16 | + await appendFile(config, `\n_auth = ${Buffer.from(`\${LEGACY_TOKEN}\nemail = \${NPM_EMAIL}`)}`); |
| 17 | + logger.log(`Wrote NPM_USERNAME, NPM_PASSWORD and NPM_EMAIL to ${config}`); |
17 | 18 | } else if (NPM_TOKEN) { |
18 | | - await appendFile('./.npmrc', `\n${nerfDart(registry)}:_authToken = \${NPM_TOKEN}`); |
19 | | - logger.log('Wrote NPM_TOKEN to .npmrc.'); |
| 19 | + await appendFile(config, `\n${nerfDart(registry)}:_authToken = \${NPM_TOKEN}`); |
| 20 | + logger.log(`Wrote NPM_TOKEN to ${config}`); |
20 | 21 | } else { |
21 | 22 | throw new AggregateError([getError('ENONPMTOKEN', {registry})]); |
22 | 23 | } |
|
0 commit comments