Skip to content

Commit

Permalink
feat: set config per env (#280)
Browse files Browse the repository at this point in the history
  • Loading branch information
fengmk2 committed Oct 24, 2018
1 parent 7a06391 commit a472b39
Show file tree
Hide file tree
Showing 4 changed files with 115 additions and 0 deletions.
30 changes: 30 additions & 0 deletions bin/install.js
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,36 @@ co(function* () {
if (pkg.config.npminstall.disableDedupe === true) {
config.disableDedupe = true;
}
// env config
// {
// "config": {
// "npminstall": {
// "env:production": {
// "disableDedupe": true
// }
// }
// }
// }
// production
if (config.production && pkg.config.npminstall['env:production']) {
const envConfig = pkg.config.npminstall['env:production'];
if (envConfig.prune === true) {
config.prune = true;
}
if (envConfig.disableDedupe === true) {
config.disableDedupe = true;
}
}
// development
if (!config.production && pkg.config.npminstall['env:development']) {
const envConfig = pkg.config.npminstall['env:development'];
if (envConfig.prune === true) {
config.prune = true;
}
if (envConfig.disableDedupe === true) {
config.disableDedupe = true;
}
}
}
}
yield installLocal(config);
Expand Down
59 changes: 59 additions & 0 deletions test/disable-dedupe.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,63 @@ describe('test/disable-dedupe.test.js', () => {
assert(names.length === 1);
assert(names[0] === 'koa');
});

it('should install config.npminstall[env:production].disableDedupe=true work', function* () {
const root = path.join(__dirname, 'fixtures', 'disable-dedupe-production-config');
rimraf.sync(path.join(root, 'node_modules'));
yield coffee.fork(npminstall, { cwd: root })
.debug()
.expect('code', 0)
.expect('stderr', /Linked \d+ latest versions/)
.end();
let names = fs.readdirSync(path.join(root, 'node_modules'))
.filter(n => !/^[\.\_]/.test(n));
assert(names.length > 1);

rimraf.sync(path.join(root, 'node_modules'));
yield coffee.fork(npminstall, { cwd: root, env: Object.assign({}, process.env, { NODE_ENV: 'production' }) })
.debug()
.expect('code', 0)
// .expect('stderr', /disable dedupe mode/)
.end();
names = fs.readdirSync(path.join(root, 'node_modules'))
.filter(n => !/^[\.\_]/.test(n));
assert(names.length === 1);
assert(names[0] === 'koa');

rimraf.sync(path.join(root, 'node_modules'));
yield coffee.fork(npminstall, [ '--production' ], { cwd: root })
.debug()
.expect('code', 0)
// .expect('stderr', /disable dedupe mode/)
.end();
names = fs.readdirSync(path.join(root, 'node_modules'))
.filter(n => !/^[\.\_]/.test(n));
assert(names.length === 1);
assert(names[0] === 'koa');
});

it('should install config.npminstall[env:development].disableDedupe=true work', function* () {
const root = path.join(__dirname, 'fixtures', 'disable-dedupe-development-config');
rimraf.sync(path.join(root, 'node_modules'));
yield coffee.fork(npminstall, { cwd: root })
.debug()
.expect('code', 0)
// .expect('stderr', /disable dedupe mode/)
.end();
let names = fs.readdirSync(path.join(root, 'node_modules'))
.filter(n => !/^[\.\_]/.test(n));
assert(names.length === 1);
assert(names[0] === 'koa');

rimraf.sync(path.join(root, 'node_modules'));
yield coffee.fork(npminstall, { cwd: root, env: Object.assign({}, process.env, { NODE_ENV: 'production' }) })
.debug()
.expect('code', 0)
// .expect('stderr', /Linked \d+ latest versions/)
.end();
names = fs.readdirSync(path.join(root, 'node_modules'))
.filter(n => !/^[\.\_]/.test(n));
assert(names.length > 1);
});
});
13 changes: 13 additions & 0 deletions test/fixtures/disable-dedupe-development-config/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"name": "disable-dedupe-demo",
"dependencies": {
"koa": "2"
},
"config": {
"npminstall": {
"env:development": {
"disableDedupe": true
}
}
}
}
13 changes: 13 additions & 0 deletions test/fixtures/disable-dedupe-production-config/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"name": "disable-dedupe-demo",
"dependencies": {
"koa": "2"
},
"config": {
"npminstall": {
"env:production": {
"disableDedupe": true
}
}
}
}

0 comments on commit a472b39

Please sign in to comment.