Skip to content

Commit

Permalink
feat: support npm_config_user_agent env (#323)
Browse files Browse the repository at this point in the history
  • Loading branch information
fengmk2 authored Jan 14, 2020
1 parent ed586f9 commit 9a2dde5
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 5 deletions.
2 changes: 2 additions & 0 deletions bin/install.js
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,8 @@ const env = {
cooked: orignalArgv,
original: orignalArgv,
}),
// user-agent
npm_config_user_agent: globalConfig.userAgent,
};
// https://github.com/npm/npm/blob/2005f4ce11f6cdf142f8a77f4f7ee4996000fb57/lib/utils/lifecycle.js#L67
env.npm_node_execpath = env.NODE = process.env.NODE || process.execPath;
Expand Down
5 changes: 4 additions & 1 deletion lib/config.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
'use strict';

const pkg = require('../package.json');

const config = {
env: {
// show node-pre-gyp http info
// like "node-pre-gyp http GET https://npm.taobao.org/mirrors/fsevents/v1.0.6/fse-v1.0.6-node-v46-darwin-x64.tar.gz"
npm_config_loglevel: 'http',
},
chineseMirrorUrl: 'https://npm.taobao.org/mirrors',
chineseRegistry: 'https://r.npm.taobao.org',
chineseRegistry: 'https://registry.npm.taobao.org',
userAgent: `npminstall/${pkg.version} npm/? node/${process.version} ${process.platform} ${process.arch}`,
};

module.exports = config;
13 changes: 9 additions & 4 deletions test/npm_package_env.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
const assert = require('assert');
const rimraf = require('mz-modules/rimraf');
const path = require('path');
const npminstall = require('..');
const coffee = require('coffee');
const pkg = require('../package.json');
const helper = require('./helper');

describe('test/npm_package_env.test.js', () => {
Expand All @@ -18,9 +19,12 @@ describe('test/npm_package_env.test.js', () => {
afterEach(cleanup);

it('should set npm_package_* env on preinstall, postinstall', async () => {
await npminstall({
root,
});
await coffee.fork(helper.npminstall, [], {
cwd: root,
})
.debug()
.expect('code', 0)
.end();
const preinstallEnv = await helper.readJSON(path.join(root, '.tmp_preinstall'));
assert(preinstallEnv.npm_package_engines_node === '~0.10.0 || ~0.12.0 || ^4.2.0');
assert(preinstallEnv.npm_package_engines_foo_bar_0 === '1111');
Expand All @@ -31,5 +35,6 @@ describe('test/npm_package_env.test.js', () => {
assert(postinstallEnv.npm_package_engines_foo_bar_0 === '1111');
assert(postinstallEnv.npm_package_greenkeeper_ignore_0 === 'glob');
assert(postinstallEnv.npm_package_greenkeeper_ignore_4 === 'showdown-ghost');
assert(postinstallEnv.npm_config_user_agent === `npminstall/${pkg.version} npm/? node/${process.version} ${process.platform} ${process.arch}`);
});
});

0 comments on commit 9a2dde5

Please sign in to comment.