UNPKG

1.04 kBJavaScriptView Raw
1var npm, pkg, unlessGloballyInstalled;
2
3npm = require("npm");
4
5pkg = process.env.npm_package_name;
6
7if (!pkg) {
8 console.error("Must use via package.json's scripts.install - defaults to name.");
9 process.exit(1);
10}
11
12unlessGloballyInstalled = function(pkg, cb) {
13 return npm.load(function(err, npm) {
14 if (err != null) {
15 console.error(err);
16 }
17 npm.config.set("global", true);
18 npm.config.set("json", true);
19 npm.config.set("depth", 0);
20 return npm.commands.ls([pkg], true, function(err, data, lite) {
21 if (err != null) {
22 console.error(err);
23 }
24 if (Object.keys(lite).length === 0) {
25 return cb(npm);
26 }
27 });
28 });
29};
30
31unlessGloballyInstalled(pkg, function(npm) {
32 console.log("Package " + pkg + " insists to be globally installed, please wait...");
33 npm.config.set("global", true);
34 npm.config.set("json", false);
35 return npm.commands.install([pkg], function(err, data, lite) {
36 if (err != null) {
37 console.error(err);
38 }
39 return console.log();
40 });
41});