Skip to content

Commit 5895372

Browse files
committed
initial commit
0 parents  commit 5895372

File tree

5 files changed

+200
-0
lines changed

5 files changed

+200
-0
lines changed

.editorconfig

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# http://editorconfig.org
2+
3+
root = true
4+
5+
[*]
6+
charset = utf-8
7+
end_of_line = lf
8+
indent_size = 2
9+
indent_style = space
10+
insert_final_newline = true
11+
insert_final_newline = true
12+
trim_trailing_whitespace = true
13+
14+
[Makefile]
15+
indent_style = tab
16+
17+
[*.md]
18+
trim_trailing_whitespace = false
19+
indent_size = 4

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/node_modules

README.md

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
# install-subset
2+
3+
Create npm installation subsets to save time
4+
5+
Even with npm 5 and yarn, installing node modules can be a long and painful task. You are at the mercy of:
6+
- your connection speed/latency
7+
- the registry servers
8+
- the number of dependencies
9+
- the disk space available
10+
- etc
11+
12+
Worse, sometimes you have to build a native binding like node-sass or couchbase, which requires:
13+
- time
14+
- processor power
15+
- battery
16+
- patience
17+
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.
19+
20+
## Installation
21+
22+
`npm install -g install-subset`
23+
24+
## Usage
25+
26+
Add something like to your package.json:
27+
```
28+
"installSubsets": {
29+
"build": {
30+
"whitelist": [
31+
"dotenv",
32+
"prettier"
33+
]
34+
},
35+
"test": {
36+
"blacklist": [
37+
"eslint",
38+
"lint-rules",
39+
"prettier"
40+
]
41+
}
42+
}
43+
```
44+
45+
then call:
46+
`install-subset install build`
47+
48+
## Info
49+
50+
- 'build' and 'test' are subset names
51+
- '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
54+
- package.json will be temporarily changed, and restored after install
55+
56+
57+
## Caution
58+
59+
This is very beta. Do not blame me if your project no longer works, you mess up your package.json, or your coworkers do not understand what you are doing. If you break something with a subset, remove and reinstall your node_modules in the normal way.

index.js

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
#!/usr/bin/env node
2+
"use strict";
3+
4+
var fs = require("fs");
5+
var _ = require("lodash");
6+
var cli = require("commander");
7+
var originalPackage = require(process.cwd() + "/package.json");
8+
var tempPackage = _.clone(originalPackage);
9+
var exec = require("child_process").exec;
10+
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
29+
cli
30+
.command("install [input_string]")
31+
.description("Check for word or phrase in translation memory")
32+
.action(function(input_string) {
33+
if (input_string) {
34+
if (tempPackage.installSubsets) {
35+
if (tempPackage.installSubsets[input_string]) {
36+
const subset = tempPackage.installSubsets[input_string];
37+
let devDependencies;
38+
39+
if (subset.whitelist) {
40+
devDependencies = _.pick(tempPackage.devDependencies, subset.whitelist);
41+
} else if (subset.blacklist) {
42+
devDependencies = _.omit(tempPackage.devDependencies, subset.blacklist);
43+
} else {
44+
throw "No valid subset actions found";
45+
}
46+
47+
tempPackage.devDependencies = devDependencies;
48+
49+
fs.writeFile(process.cwd() + "/package.json", JSON.stringify(tempPackage, null, " "), function(err) {
50+
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) {
53+
if (err) throw err;
54+
});
55+
});
56+
});
57+
} else {
58+
throw "No install subset with that name";
59+
}
60+
} else {
61+
throw "No install subsets in package.json";
62+
}
63+
} else {
64+
throw "Please provide an install subset name";
65+
}
66+
});
67+
68+
cli.command("*").action(() => cli.help());
69+
70+
cli.parse(process.argv);
71+
72+
if (cli.args.length === 0) cli.help();
73+
74+
process.on("unhandledRejection", (reason, promise) => {
75+
console.log("Unhandled Rejection at:", promise, "reason:", reason);
76+
});
77+
78+
process.on("uncaughtException", err => {
79+
console.log("ERROR: " + err);
80+
process.exit();
81+
});

package.json

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
{
2+
"name": "install-subset",
3+
"version": "1.0.0",
4+
"description": "Install a subset of npm dependencies based on given contexts",
5+
"main": "index.js",
6+
"scripts": {
7+
"test": "echo \"Error: no test specified\" && exit 1"
8+
},
9+
"bin": "index.js",
10+
"keywords": [
11+
"npm",
12+
"install",
13+
"subset",
14+
"dependency"
15+
],
16+
"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+
},
33+
"devDependencies": {
34+
"prettier": "^1.3.1"
35+
},
36+
"dependencies": {
37+
"commander": "^2.9.0",
38+
"lodash": "^4.17.4"
39+
}
40+
}

0 commit comments

Comments
 (0)