Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/npm/cli.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorisaacs <i@izs.me>2010-03-05 08:39:32 +0300
committerisaacs <i@izs.me>2010-03-05 08:40:23 +0300
commit8073b0b9838b36847bed1fa9d93ed6b7390cb2c7 (patch)
treec63fb7f56557a9e6e61d78376cbc12a3ecd88ebe
parentfbbdff345a6ac92ab47c820ff02148f9ad179f96 (diff)
Refactor out the utils/index.js file, and clean up some style issues.
-rwxr-xr-xcli.js2
-rw-r--r--lib/activate.js2
-rw-r--r--lib/build.js14
-rw-r--r--lib/deactivate.js2
-rw-r--r--lib/install.js12
-rw-r--r--lib/link.js3
-rw-r--r--lib/ls.js2
-rw-r--r--lib/utils/exec.js4
-rw-r--r--lib/utils/fetch.js26
-rw-r--r--lib/utils/get.js6
-rw-r--r--lib/utils/index.js68
-rw-r--r--lib/utils/lifecycle.js2
-rw-r--r--lib/utils/log.js10
-rw-r--r--lib/utils/mkdir-p.js4
-rw-r--r--lib/utils/read-json.js4
-rw-r--r--lib/utils/set.js14
-rwxr-xr-xnpm.js13
-rw-r--r--package.json1
-rwxr-xr-xscripts/bootstrap.js4
19 files changed, 77 insertions, 116 deletions
diff --git a/cli.js b/cli.js
index 5a12d06aa..6f31095eb 100755
--- a/cli.js
+++ b/cli.js
@@ -34,7 +34,7 @@ var commands =
, "deactivate"
, "link"
],
- log = require(npm.moduleName+"/../lib/utils").log;
+ log = require(npm.moduleName+"/../lib/utils/log");
var argv = process.argv, arg = "";
while (argv.shift() !== module.filename);
diff --git a/lib/activate.js b/lib/activate.js
index dcfaf7398..3f4e8bfcb 100644
--- a/lib/activate.js
+++ b/lib/activate.js
@@ -7,7 +7,7 @@
var mkdir = require("./utils/mkdir-p"),
npm = require("../npm"),
fs = require("fs"),
- log = require("./utils").log,
+ log = require("./utils/log"),
path = require("path"),
rm = require("./utils/rm-rf"),
chain = require("./utils/chain"),
diff --git a/lib/build.js b/lib/build.js
index f556662e4..b12c72e4c 100644
--- a/lib/build.js
+++ b/lib/build.js
@@ -9,10 +9,9 @@
// This runs AFTER install or link are completed.
var npm = require("../npm"),
- utils = require("./utils"),
+ log = require("./utils/log"),
rm = require("./utils/rm-rf"),
chain = require("./utils/chain"),
- log = utils.log,
fetch = require("./utils/fetch"),
fs = require("fs"),
path = require("path"),
@@ -71,7 +70,7 @@ function build (pkg, cb) {
cb
);
-};
+}
function readAndBuild (folder, cb) {
readJson(path.join(folder, "package.json"), function (er, data) {
@@ -170,7 +169,7 @@ function resolveDependencies (pkg, topCb) {
else cb();
});
});
- };
+ }
chain([link], [function (cb) {
from += ".js";
@@ -202,7 +201,7 @@ function createMain (pkg,cb) {
proxyFile = path.join(npm.dir, pkg.name, pkg.version, "main.js");
fs.writeFile(proxyFile, code, "ascii", cb);
-};
+}
// symlink ROOT/{name}-{version}/ to ROOT/.npm/{name}/{version}/{lib}
function linkLib (pkg, cb) {
@@ -244,7 +243,7 @@ function linkLib (pkg, cb) {
});
}
});
-};
+}
function linkMain (pkg, cb) {
if (!pkg.main) return;
@@ -257,4 +256,5 @@ function linkMain (pkg, cb) {
});
else fs.symlink(from, to, cb);
});
-};
+}
+
diff --git a/lib/deactivate.js b/lib/deactivate.js
index 31602c637..d5464cc76 100644
--- a/lib/deactivate.js
+++ b/lib/deactivate.js
@@ -2,7 +2,7 @@
var mkdir = require("./utils/mkdir-p"),
npm = require("../npm"),
fs = require("fs"),
- log = require("./utils").log,
+ log = require("./utils/log"),
path = require("path"),
rm = require("./utils/rm-rf"),
chain = require("./utils/chain"),
diff --git a/lib/install.js b/lib/install.js
index 45f5daaee..9aeefd0ab 100644
--- a/lib/install.js
+++ b/lib/install.js
@@ -4,7 +4,7 @@ var npm = require("../npm"),
rm = require("./utils/rm-rf"),
exec = require("./utils/exec"),
chain = require("./utils/chain"),
- log = require("./utils").log,
+ log = require("./utils/log"),
fetch = require("./utils/fetch"),
fs = require("fs"),
path = require("path"),
@@ -61,7 +61,7 @@ function install (tarball, cb) {
cb
);
-};
+}
// move to ROOT/.npm/{name}/{version}/package
function moveIntoPlace (dir, pkg, cb) {
@@ -83,7 +83,7 @@ function moveIntoPlace (dir, pkg, cb) {
} else {
log("unlinked "+target,"moveIntoPlace");
cb();
- };
+ }
});
else cb();
});
@@ -94,7 +94,7 @@ function moveIntoPlace (dir, pkg, cb) {
[function (cb) { log("done", "moveIntoPlace"); cb() }],
cb
);
-};
+}
function fetchAndInstall (tarball, cb) {
mkdir(npm.tmp, function (er, ok) {
@@ -113,8 +113,8 @@ function fetchAndInstall (tarball, cb) {
);
});
});
-};
+}
function unpackTar (tarball, unpackTarget, cb) {
exec("tar", ["xzvf", tarball, "--strip", "1", "-C", unpackTarget], cb);
-};
+}
diff --git a/lib/link.js b/lib/link.js
index 4956852b0..2fc2b0cfc 100644
--- a/lib/link.js
+++ b/lib/link.js
@@ -2,9 +2,8 @@
// link the supplied folder to .npm/{name}/{version}/package
var npm = require("../npm"),
- utils = require("./utils"),
chain = require("./utils/chain"),
- log = require("./utils").log,
+ log = require("./utils/log"),
fs = require("fs"),
readJson = require("./utils/read-json"),
rm = require("./utils/rm-rf"),
diff --git a/lib/ls.js b/lib/ls.js
index d7bc59c58..3c38fe32c 100644
--- a/lib/ls.js
+++ b/lib/ls.js
@@ -5,7 +5,7 @@ module.exports = exports = ls;
var npm = require("../npm"),
path = require("path"),
fs = require("fs"),
- log = require("./utils").log;
+ log = require("./utils/log");
function ls (pkg, cb) {
if (!cb) {
diff --git a/lib/utils/exec.js b/lib/utils/exec.js
index bd53aace9..1858823f5 100644
--- a/lib/utils/exec.js
+++ b/lib/utils/exec.js
@@ -1,5 +1,5 @@
-var log = require("../utils").log;
+var log = require("../utils/log");
module.exports = function exec (cmd, args, env, cb) {
if (!cb) {
@@ -18,4 +18,4 @@ module.exports = function exec (cmd, args, env, cb) {
if (code) cb(new Error("`"+cmd+"` failed with "+code));
else cb(null, code);
});
-};
+}
diff --git a/lib/utils/fetch.js b/lib/utils/fetch.js
index 0111e64c7..6e4eaa378 100644
--- a/lib/utils/fetch.js
+++ b/lib/utils/fetch.js
@@ -6,7 +6,9 @@ var http = require("http"),
url = require("url"),
sys = require("sys"),
fs = require("fs"),
- utils = require("./index"),
+ get = require("./get"),
+ set = require("./set"),
+ log = require("./log"),
Promise = require("events").Promise;
module.exports = function fetch (remote, local, headers, cb) {
@@ -14,7 +16,7 @@ module.exports = function fetch (remote, local, headers, cb) {
cb = headers;
headers = {};
}
- utils.log(remote+" to "+local, "fetch");
+ log(remote+" to "+local, "fetch");
headers.host = url.parse(remote).hostname;
fs.open(local, process.O_CREAT | process.O_WRONLY | process.O_TRUNC, 0755,
function (er, fd) {
@@ -22,10 +24,10 @@ module.exports = function fetch (remote, local, headers, cb) {
fetchAndWrite(remote, fd, headers, cb);
}
);
-};
+}
function fetchAndWrite (remote, fd, headers, maxRedirects, redirects, cb) {
- // utils.log("fetchAndWrite "+sys.inspect(Array.prototype.slice.call(arguments, 0)));
+ // log("fetchAndWrite "+sys.inspect(Array.prototype.slice.call(arguments, 0)));
if (!cb) {
cb = redirects;
@@ -36,27 +38,27 @@ function fetchAndWrite (remote, fd, headers, maxRedirects, redirects, cb) {
maxRedirects = 10;
}
if (!cb) throw new Error("No callback provided");
- utils.log(remote, "fetch");
+ log(remote, "fetch");
remote = url.parse(remote);
- utils.set(headers, "host", remote.hostname);
+ set(headers, "host", remote.hostname);
remote.path = remote.pathname+(remote.search||"")+(remote.hash||"");
http
.createClient(remote.port || (remote.protocol === "https:" ? 443 : 80), remote.hostname)
.request("GET", (remote.pathname||"/")+(remote.search||"")+(remote.hash||""), headers)
.addListener("response", function (response) {
// handle redirects.
- var loc = utils.get(response.headers, "location");
+ var loc = get(response.headers, "location");
if (Math.floor(response.statusCode / 100) === 3
&& loc && loc !== remote.href && redirects < maxRedirects) {
// This is a laughably naïve way to handle this situation.
// @TODO: Really need a full curl or wget style module that would
// do all this kind of stuff for us.
- var cookie = utils.get(response.headers, "Set-Cookie");
+ var cookie = get(response.headers, "Set-Cookie");
if (cookie) {
cookie = cookie.split(";").shift();
- utils.set(headers, "Cookie", cookie);
+ set(headers, "Cookie", cookie);
}
- utils.log(response.statusCode, "fetch");
+ log(response.statusCode, "fetch");
return fetchAndWrite(loc, fd, headers, maxRedirects, redirects + 1, cb);
}
if (response.statusCode !== 200) {
@@ -67,12 +69,12 @@ function fetchAndWrite (remote, fd, headers, maxRedirects, redirects, cb) {
var written = 0;
response.addListener("data", function (chunk) {
// write the chunk...
- utils.log((written += chunk.length) + " bytes", "fetch");
+ log((written += chunk.length) + " bytes", "fetch");
fs.write(fd, chunk, "binary", function (er) { if (er) cb(er) })
})
response.addListener("error", cb);
response.addListener("end", function () {
- utils.log("finished", "fetch");
+ log("finished", "fetch");
fs.close(fd, cb);
});
})
diff --git a/lib/utils/get.js b/lib/utils/get.js
new file mode 100644
index 000000000..fde89c760
--- /dev/null
+++ b/lib/utils/get.js
@@ -0,0 +1,6 @@
+
+module.exports = get;
+function get (obj, key) {
+ for (var i in obj) if (i.toLowerCase() === key.toLowerCase()) return obj[i];
+ return undefined;
+}
diff --git a/lib/utils/index.js b/lib/utils/index.js
deleted file mode 100644
index 2b52590be..000000000
--- a/lib/utils/index.js
+++ /dev/null
@@ -1,68 +0,0 @@
-module.exports = {
- set:set,
- get:get,
- has:has,
- bind:bind,
- method:method,
- log:log,
- array:array,
- succeed:succeed,
- fail:fail
-};
-
-function get (obj, key) {
- for (var i in obj) if (i.toLowerCase() === key.toLowerCase()) return obj[i];
- return undefined;
-};
-
-function set (obj, key, val) {
- for (var i in obj) if (i.toLowerCase() === key.toLowerCase()) return obj[i] = val;
- obj[key] = val;
- if (val && val.version && key.indexOf("-"+val.version) !== -1) {
- key = key.replace("-"+val.version, "");
- var reg = get(obj, key) || {};
- set(reg, val.version, val);
- set(obj, key, reg);
- reg._versions = get(reg, "_versions") || [];
- reg._versions.push(val.version);
- }
-};
-
-function has (obj, key) {
- for (var i in obj) if (obj.hasOwnProperty(i) && i.toLowerCase === key.toLowerCase()) return true;
- return false;
-};
-
-function bind (fn, o) {
- var args = array(arguments, 2);
- return function () {
- return fn.apply(o, args.concat(array(arguments)));
- };
-};
-
-function method (o, m) {
- var args = array(arguments, 2);
- if (!o) {
- throw new Error("invalid object: "+o);
- }
- return function () {
- return o[m].apply(o, args.concat(array(arguments)));
- };
-};
-
-function log (msg, pref) {
- pref = (pref && ": \033[35m" + pref+"\033[0m") || "";
- if (msg) msg = "\033[31mnpm\033[0m"+pref+": "+msg;
- process.stdio.writeError(msg+"\n");
-};
-
-function array (arr, n) {
- return Array.prototype.slice.call(arr,n || 0);
-};
-
-function succeed (p) {
- return method(p, "emitSuccess", array(arguments,1));
-};
-function fail (p) {
- return method(p, "emitError", array(arguments,1));
-} \ No newline at end of file
diff --git a/lib/utils/lifecycle.js b/lib/utils/lifecycle.js
index a38d09b1c..1a607124f 100644
--- a/lib/utils/lifecycle.js
+++ b/lib/utils/lifecycle.js
@@ -1,7 +1,7 @@
module.exports = lifecycle;
-var log = require("../utils").log,
+var log = require("../utils/log"),
exec = require("../utils/exec"),
npm = require("../../npm"),
path = require("path");
diff --git a/lib/utils/log.js b/lib/utils/log.js
new file mode 100644
index 000000000..9da0ec915
--- /dev/null
+++ b/lib/utils/log.js
@@ -0,0 +1,10 @@
+
+module.exports = log;
+var sys = require("sys");
+
+function log (msg, pref) {
+ pref = (pref && ": \033[35m" + pref+"\033[0m") || "";
+ if (msg) msg = "\033[31mnpm\033[0m"+pref+": "+msg;
+ sys.error(msg);
+}
+
diff --git a/lib/utils/mkdir-p.js b/lib/utils/mkdir-p.js
index 51d1b5872..3561bbf0c 100644
--- a/lib/utils/mkdir-p.js
+++ b/lib/utils/mkdir-p.js
@@ -1,5 +1,5 @@
-var log = require("../utils").log,
+var log = require("../utils/log"),
fs = require("fs"),
path = require("path");
@@ -31,4 +31,4 @@ function mkdir (ensure, chmod, cb) {
}
});
})(dirs.shift());
-};
+}
diff --git a/lib/utils/read-json.js b/lib/utils/read-json.js
index c75fc0377..5179cbdb9 100644
--- a/lib/utils/read-json.js
+++ b/lib/utils/read-json.js
@@ -3,7 +3,7 @@ module.exports = readJson;
var fs = require("fs"),
semver = require("./semver"),
- log = require("../utils").log;
+ log = require("../utils/log");
function readJson (jsonFile, cb) {
log(jsonFile, "readJson");
@@ -27,7 +27,7 @@ function readJson (jsonFile, cb) {
json._npmKey = key;
testEngine(json, cb);
});
-};
+}
function testEngine (json, cb) {
if (!json.engines) return cb(null, json);
diff --git a/lib/utils/set.js b/lib/utils/set.js
new file mode 100644
index 000000000..00171943f
--- /dev/null
+++ b/lib/utils/set.js
@@ -0,0 +1,14 @@
+
+module.exports = set;
+function set (obj, key, val) {
+ for (var i in obj) if (i.toLowerCase() === key.toLowerCase()) return obj[i] = val;
+ obj[key] = val;
+ if (val && val.version && key.indexOf("-"+val.version) !== -1) {
+ key = key.replace("-"+val.version, "");
+ var reg = get(obj, key) || {};
+ set(reg, val.version, val);
+ set(obj, key, reg);
+ reg._versions = get(reg, "_versions") || [];
+ reg._versions.push(val.version);
+ }
+}
diff --git a/npm.js b/npm.js
index 0a0414d01..32984ec53 100755
--- a/npm.js
+++ b/npm.js
@@ -7,9 +7,9 @@ if (module.id !== moduleName) {
return;
}
-var npm = exports;
-
-var utils = require("./lib/utils");
+var npm = exports,
+ set = require("./lib/utils/set"),
+ get = require("./lib/utils/get");
npm.moduleName = moduleName;
@@ -26,11 +26,8 @@ npm.list = npm.ls;
var registry = {};
-npm.set = set;
-npm.get = get;
-
-function set (name, data) { return utils.set(registry, name, data) };
-function get (name) { return utils.get(registry, name) };
+npm.set = function set (name, data) { return set(registry, name, data) };
+npm.get = function get (name) { return get(registry, name) };
var path = require("path");
diff --git a/package.json b/package.json
index b5351c3db..b70416a71 100644
--- a/package.json
+++ b/package.json
@@ -4,6 +4,7 @@
, "author" : "Isaac Z. Schlueter <i@izs.me>"
, "directories" : { "lib" : "lib" }
, "main" : "./npm"
+, "bin" : { "npm" : "./cli.js" }
, "scripts" :
{ "install" : "scripts/install.js"
, "uninstall" : "scripts/uninstall.js"
diff --git a/scripts/bootstrap.js b/scripts/bootstrap.js
index 7a6fa336c..0e3a9c7fc 100755
--- a/scripts/bootstrap.js
+++ b/scripts/bootstrap.js
@@ -2,11 +2,11 @@
var sys = require("sys");
-function print (m, cr) { process.stdio.writeError(m+(cr===false?"":"\n")); return print };
+function print (m, cr) { process.stdio.writeError(m+(cr===false?"":"\n")); return print }
require("../npm").install("http://github.com/isaacs/npm/tarball/master", function (er, ok) {
if (er) {
sys.error("\nFailed after "+ok.length+" step(s)\n");
throw er;
} else print("It worked!");
-})
+});