Skip to content

Commit 8cbe0fb

Browse files
author
Trevor Brindle
committed
update to 2.0.0
- shows install progress - option to prefer npm, else defaults to yarn
1 parent 5895372 commit 8cbe0fb

File tree

3 files changed

+55
-63
lines changed

3 files changed

+55
-63
lines changed

README.md

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,21 +15,21 @@ Worse, sometimes you have to build a native binding like node-sass or couchbase,
1515
- battery
1616
- patience
1717

18-
With install subsets, you can cleverly avoid some devDependencies in certain contexts. For example, on a CI server where you are simply building an artifact, you do not need to install dependencies like eslint, karma, or other testing frameworks. With a busy or sharedserver, you can shave precious time off of each build this way.
18+
You can cleverly avoid some devDependencies in certain contexts. For example, on a CI server where you are simply building an artifact, you do not need to install dependencies like eslint, karma, or other testing frameworks. With a busy or shared server, you can shave precious time off of each build this way.
1919

2020
## Installation
2121

22-
`npm install -g install-subset`
22+
`npm install -g install-subset`
2323

2424
## Usage
2525

2626
Add something like to your package.json:
2727
```
28-
"installSubsets": {
28+
"subsets": {
2929
"build": {
3030
"whitelist": [
31-
"dotenv",
32-
"prettier"
31+
"babel-cli",
32+
"dotenv"
3333
]
3434
},
3535
"test": {
@@ -42,15 +42,14 @@ Add something like to your package.json:
4242
}
4343
```
4444

45-
then call:
46-
`install-subset install build`
45+
Then call `subset install test`
4746

4847
## Info
4948

5049
- 'build' and 'test' are subset names
5150
- 'whitelist' and 'blacklist' are methods of install
52-
- 'whitelist' allows that subset of your devDependencies to install
53-
- 'blacklist' disallows that subset of your devDependencies to install
51+
- 'whitelist' allows that subset of your devDependencies to install
52+
- 'blacklist' disallows that subset of your devDependencies to install
5453
- package.json will be temporarily changed, and restored after install
5554

5655

index.js

Lines changed: 40 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -3,53 +3,59 @@
33

44
var fs = require("fs");
55
var _ = require("lodash");
6+
var shelljs = require("shelljs");
67
var cli = require("commander");
7-
var originalPackage = require(process.cwd() + "/package.json");
8-
var tempPackage = _.clone(originalPackage);
9-
var exec = require("child_process").exec;
8+
var cwd = process.cwd();
9+
var packageJson = require(cwd + "/package.json");
10+
var tempPackage = _.clone(packageJson);
11+
var spawn = require("child_process").spawn;
1012

11-
var run = function(cmd, cb) {
12-
var child = exec(cmd, function(error, stdout, stderr) {
13-
if (stderr !== null) {
14-
console.log("" + stderr);
15-
}
16-
if (stdout !== null) {
17-
console.log("" + stdout);
18-
}
19-
if (error !== null) {
20-
console.log("" + error);
21-
}
22-
if (cb) {
23-
cb();
24-
}
25-
});
26-
};
27-
28-
// only works for dev dependencies
2913
cli
3014
.command("install [input_string]")
31-
.description("Check for word or phrase in translation memory")
32-
.action(function(input_string) {
15+
.alias("i")
16+
.option("-c, --clean", "remove node_modules first")
17+
.option("--npm", "use npm to install")
18+
.description("install a given subset defined in package.json")
19+
.action(function(input_string, options) {
3320
if (input_string) {
34-
if (tempPackage.installSubsets) {
35-
if (tempPackage.installSubsets[input_string]) {
36-
const subset = tempPackage.installSubsets[input_string];
21+
if (packageJson.subsets) {
22+
if (packageJson.subsets[input_string]) {
23+
const subset = packageJson.subsets[input_string];
3724
let devDependencies;
3825

3926
if (subset.whitelist) {
40-
devDependencies = _.pick(tempPackage.devDependencies, subset.whitelist);
27+
devDependencies = _.pick(packageJson.devDependencies, subset.whitelist);
4128
} else if (subset.blacklist) {
42-
devDependencies = _.omit(tempPackage.devDependencies, subset.blacklist);
29+
devDependencies = _.omit(packageJson.devDependencies, subset.blacklist);
4330
} else {
4431
throw "No valid subset actions found";
4532
}
4633

4734
tempPackage.devDependencies = devDependencies;
4835

49-
fs.writeFile(process.cwd() + "/package.json", JSON.stringify(tempPackage, null, " "), function(err) {
36+
if (options.clean) {
37+
shelljs.rm("-rf", cwd + "/yarn.lock");
38+
shelljs.rm("-rf", cwd + "/package-lock.json ");
39+
shelljs.rm("-rf", cwd + "/node_modules");
40+
}
41+
42+
fs.writeFile(cwd + "/package.json", JSON.stringify(tempPackage, null, " "), function(err) {
5043
if (err) throw err;
51-
run("type yarn 2>/dev/null && yarn || npm set depth=0; npm install --silent", function() {
52-
fs.writeFile(process.cwd() + "/package.json", JSON.stringify(originalPackage, null, " "), function(err) {
44+
45+
if (!options.npm && shelljs.which('yarn')){
46+
var installer = spawn("yarn", [], { stdio: "inherit" });
47+
} else {
48+
var installer = spawn("npm", ['install'], { stdio: "inherit" });
49+
}
50+
51+
installer.on("close", function(code) {
52+
if (code !== 0) {
53+
throw "Error code " + code;
54+
} else {
55+
installer.unref();
56+
console.log("Installation of subset " + input_string + " successful");
57+
}
58+
fs.writeFile(cwd + "/package.json", JSON.stringify(packageJson, null, " "), function(err) {
5359
if (err) throw err;
5460
});
5561
});
@@ -71,11 +77,10 @@ cli.parse(process.argv);
7177

7278
if (cli.args.length === 0) cli.help();
7379

74-
process.on("unhandledRejection", (reason, promise) => {
75-
console.log("Unhandled Rejection at:", promise, "reason:", reason);
76-
});
77-
7880
process.on("uncaughtException", err => {
7981
console.log("ERROR: " + err);
82+
fs.writeFile(cwd + "/package.json", JSON.stringify(packageJson, null, " "), function(err) {
83+
if (err) throw err;
84+
});
8085
process.exit();
8186
});

package.json

Lines changed: 7 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,28 @@
11
{
22
"name": "install-subset",
3-
"version": "1.0.0",
3+
"version": "2.0.0",
44
"description": "Install a subset of npm dependencies based on given contexts",
55
"main": "index.js",
66
"scripts": {
77
"test": "echo \"Error: no test specified\" && exit 1"
88
},
9-
"bin": "index.js",
9+
"bin": {
10+
"subset": "index.js"
11+
},
1012
"keywords": [
1113
"npm",
1214
"install",
1315
"subset",
1416
"dependency"
1517
],
1618
"author": "[email protected]",
17-
"license": "ISC",
18-
"babel": {
19-
"presets": [
20-
[
21-
"env",
22-
{
23-
"targets": {
24-
"node": 4.3
25-
}
26-
}
27-
]
28-
],
29-
"plugins": [
30-
"transform-runtime"
31-
]
32-
},
19+
"license": "MIT",
3320
"devDependencies": {
3421
"prettier": "^1.3.1"
3522
},
3623
"dependencies": {
3724
"commander": "^2.9.0",
38-
"lodash": "^4.17.4"
25+
"lodash": "^4.17.4",
26+
"shelljs": "^0.7.7"
3927
}
4028
}

0 commit comments

Comments
 (0)