Skip to content

Commit

Permalink
fix: cnpm install error without user settings (#301)
Browse files Browse the repository at this point in the history
  • Loading branch information
Moudicat authored and fengmk2 committed May 8, 2019
1 parent ef1432a commit 2688fa8
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 2 deletions.
5 changes: 3 additions & 2 deletions lib/get.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,9 @@ function* get(url, options, globalOptions) {
// need auth
const registryUrl = cnpmConfig.get('registry');
const registryUri = registryUrl && registryUrl.replace(urlParser.parse(registryUrl).protocol, '') || '';
const authed = registryUri && url.indexOf(registryUri) !== -1 || false;
if (authed || cnpmConfig.get(registryUri + ':always-auth') || cnpmConfig.get('always-auth')) {
const authed = registryUri && url.indexOf(registryUri) !== -1;
const hasUserSettings = typeof cnpmConfig.get(registryUri + ':username') === 'string' && typeof cnpmConfig.get(registryUri + ':_password') === 'string';
if (hasUserSettings && (authed || cnpmConfig.get(registryUri + ':always-auth') || cnpmConfig.get('always-auth'))) {
const authToken = (`${cnpmConfig.get(registryUri + ':username')}:${Buffer.from(cnpmConfig.get(registryUri + ':_password'), 'base64').toString()}`);
options.headers.Authorization = `Basic ${Buffer.from(authToken).toString('base64')}`;
}
Expand Down
1 change: 1 addition & 0 deletions test/fixtures/initial-cnpmrc/.cnpmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
registry=https://registry.npmjs.com/
30 changes: 30 additions & 0 deletions test/install-without-userconfig.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
'use strict';

const path = require('path');
const rimraf = require('rimraf');
const coffee = require('coffee');
const npminstall = path.join(__dirname, '..', 'bin', 'install.js');

const cwd = path.join(__dirname, './fixtures/initial-cnpmrc/');

describe('test/install-without-userconfig.test.js', () => {
it('should run cnpm install successfully without cnpmrc userconfig', function* () {
function cleanup() {
rimraf.sync(path.join(cwd, 'node_modules'));
}

beforeEach(cleanup);
afterEach(cleanup);

yield coffee.fork(npminstall, ['webpack-parallel-uglify-plugin@1.0.0'], {
cwd: cwd,
env: Object.assign({}, process.env, {
USERPROFILE: cwd,
HOME: cwd
})
})
.debug()
.expect('code', 0)
.end();
});
});

0 comments on commit 2688fa8

Please sign in to comment.