diff options
author | isaacs <i@foohack.com> | 2009-09-30 02:01:06 +0400 |
---|---|---|
committer | isaacs <i@foohack.com> | 2009-09-30 02:01:06 +0400 |
commit | a05d4ae4b9bd77b3b8e531e4dff78d1e1e3d3a00 (patch) | |
tree | b0a451efe0e4ee8a13f01db6436812a3e4fad0a5 /npm.js | |
parent | 39f1b773f5af33cc658e08fc8dba151db2aba2a8 (diff) |
Kludge some command-line curl in there to get around the fact that httpClient seems to be borked for some reason right now. With that, implement refreshSource and writeCatalog appropriately.
Diffstat (limited to 'npm.js')
-rwxr-xr-x | npm.js | 113 |
1 files changed, 94 insertions, 19 deletions
@@ -11,18 +11,42 @@ npm.install = function npm_install () { }; npm.refresh = function npm_refresh () { - // return dummyPromise() // get my list of sources. var p = new node.Promise(); npm.getSources().addCallback(function (srcArr) { queue(srcArr, function (src) { return npm.refreshSource(src) }).addCallback(function () { - p.emitSuccess(); - }).addErrback(function () { - p.emitError(); - }); + node.fs.unlink(".npm.catalog.tmp"); + npm.writeCatalog() + .addCallback(function () { p.emitSuccess(); }) + .addErrback(function () { p.emitError(); }); + }).addErrback(function () { p.emitError(); }); + }); + return p; +}; + +var CATALOG = {}; +npm.writeCatalog = function npm_writeCatalog () { + var p = new node.Promise(); + + node.fs.open( + node.path.join(ENV.HOME, ".npm", "catalog.json"), + node.O_WRONLY | node.O_TRUNC | node.O_CREAT, + 0666 + ).addErrback(fail(p, + "couldn't open "+node.path.join(ENV.HOME, ".npm", "catalog.json")+" for writing" + )).addCallback(function (fd) { + try { + var data = JSON.stringify(CATALOG); + } catch (ex) { + return fail(p, "Couldn't stringify catalog data")(); + } + node.fs.write(fd, data, 0).addErrback(fail(p, + "couldn't write to "+node.path.join(ENV.HOME, ".npm", "catalog.json") + )).addCallback(function () { p.emitSuccess() }); }); + return p; }; @@ -44,27 +68,74 @@ npm.getSources = function npm_getSources () { }; npm.refreshSource = function npm_refreshSource (src) { - debug("refresh the source: "+src); + // debug("refresh the source: "+src); var p = new node.Promise(); - // var u = http.parseUri(src); - // var httpClient = http.createClient(uri.port || 80, uri.host); - // client.get(uri.path || "/", headers || {}) + var uri = http.parseUri(src); - http.cat(src, "utf-8", { "User-Agent" : "nodejs" }) - .addCallback(function (data) { - debug("do something"); - p.emitSuccess(data); - }) - .addErrback(function (er) { - debug("error "+JSON.stringify(er)+ " " + JSON.stringify(res)); - p.emitError(er); - }); + // TODO: Replace this nonportable kludge with http.createClient. + var data = ""; + exec("curl "+src+" > .npm.catalog.tmp").addCallback(function (stdout, stderr) { + // debug("it worked!" + stdout); + // now read the file, and parse it. + node.fs.cat(".npm.catalog.tmp").addCallback(function (data) { + try { + data = JSON.parse(data); + merge(CATALOG, data); + p.emitSuccess(data); + } catch (ex) { p.emitError(ex); return; } + }).addErrback(function () { p.emitError() }); + }); + + // var uri = http.parseUri(src); + // debug("parsed uri "+JSON.stringify(uri)); + // var client = http.createClient(uri.port || 80, uri.host); + // var request = client.get(uri.path || "/"); + // request.finish(function (response) { + // puts("STATUS: " + response.statusCode); + // puts("HEADERS: " + JSON.stringify(response.headers)); + // response.setBodyEncoding("utf8"); + // response.addListener("body", function (chunk) { + // puts("BODY: " + chunk); + // }); + // }); + // + + // var client = http.createClient(uri.port || 80, uri.host); + // debug("created client"); + // client.get(uri.path || "/").finish(function (response) { + // debug("STATUS: " + response.statusCode); + // debug("HEADERS: " + JSON.stringify(response.headers)); + // response.setBodyEncoding("utf8"); + // response.addListener("body", function (chunk) { + // debug("BODY: " + chunk); + // }); + // response.addListener("complete", function () { + // debug("finished"); + // p.emitSuccess(); + // }); + // }); + + + // http.cat("http://example.com/", "utf-8") + // .addCallback(function (data) { + // debug("do something"); + // p.emitSuccess(data); + // }) + // .addErrback(function (er) { + // debug("error "+JSON.stringify(er)); + // p.emitError(er); + // }); + return p; }; +function merge (dest, src) { + for (var i in src) dest[i] = src[i]; +}; + function dummyPromise (name) { var promise = new node.Promise(); name = name || arguments.callee.caller.name; @@ -74,4 +145,8 @@ function dummyPromise (name) { }); return promise; }; -
\ No newline at end of file + +function fail (p, msg) { return function () { + p.emitError(); + debug("npm failed: "+msg); +}}; |