Permalink
Browse files

install-npm command & min NPM version check

  • Loading branch information...
1 parent b673374 commit 8972a6f2b1b5694542f3988d13296fa3824738bb @cadorn cadorn committed Oct 19, 2012
Showing with 379 additions and 5 deletions.
  1. +36 −5 lib/cli.js
  2. +33 −0 lib/commands/install-npm.js
  3. +310 −0 lib/npm-install.sh
View
@@ -1,14 +1,16 @@
var PATH = require("path");
var FS = require("fs");
+var EXEC = require("child_process").exec;
+var SEMVER = require("semver");
var TERM = require("sourcemint-util-js/lib/term");
var UTIL = require("sourcemint-util-js/lib/util");
var ERROR = require("sourcemint-util-js/lib/error");
var ARGS_PARSER = require("sourcemint-util-js/lib/args").Parser;
-
//require("sourcemint-util-js/lib/console").enableFileLineInfo();
+const NPM_MIN_VERSION = "1.1.63";
//TODO: Only enable this in debug mode.
process.on("uncaughtException", function (err)
@@ -48,12 +50,41 @@ function main() {
return;
}
- var cliOptions = optParser.parse(process.argv.splice(1));
+ ensureMinNpmVersion(function(err) {
+ if (err) return exports.failAndExit(err);
- if (cliOptions.help === true) {
- optParser.printHelp(cliOptions);
- return;
+ var cliOptions = optParser.parse(process.argv.splice(1));
+
+ if (cliOptions.help === true) {
+ optParser.printHelp(cliOptions);
+ return;
+ }
+ });
+}
+
+
+function ensureMinNpmVersion(callback) {
+
+ // Skip if we are trying to install `npm`.
+ if (process.argv[process.argv.length-1] === "install-npm") {
+ return callback(null);
}
+
+ EXEC("npm -v", function(error, stdout, stderr) {
+ if (error || stderr) {
+ TERM.stdout.writenl("\0red(" + stderr + "\0)");
+ TERM.stdout.writenl("\0red([sm] ERROR: While calling `npm` command. Is `npm` installed? It should be on your `PATH`. To install run: \0bold(sm install-npm\0)\0)");
+ return callback(true);
+ }
+ // Check if installed version is newer than latest.
+ var version = stdout.replace(/[\n\s]*$/, "");
+ if (SEMVER.compare(version, NPM_MIN_VERSION) < 0) {
+ TERM.stdout.writenl("\0red([sm] ERROR: Found `npm@" + version + "` but we need at least version '" + NPM_MIN_VERSION + "'. To install run: \0bold(sm install-npm\0)\0)");
+ return callback(true);
+ } else {
+ return callback(null);
+ }
+ });
}
@@ -0,0 +1,33 @@
+
+const PATH = require("path");
+const SPAWN = require("child_process").spawn;
+const ARGS_PARSER = require("sourcemint-util-js/lib/args").Parser;
+const TERM = require("sourcemint-util-js/lib/term");
+const CLI = require("../cli");
+
+
+var command = exports["install-npm"] = new ARGS_PARSER();
+
+command.help("Install latest `npm`.");
+command.helpful();
+
+command.action(function (options) {
+ var proc = SPAWN("sh", [
+ PATH.join(__dirname, "../npm-install.sh")
+ ]);
+ proc.on("error", function(err) {
+ CLI.failAndExit(err);
+ });
+ proc.stdout.on("data", function(data) {
+ process.stdout.write(data);
+ });
+ proc.stderr.on("data", function(data) {
+ TERM.stderr.write("\0red(");
+ process.stderr.write(data);
+ TERM.stderr.write("\0)");
+ });
+ proc.on("exit", function(code) {
+ if (code !== 0) return CLI.failAndExit(new Error("`npm` install error"));
+ process.exit(0);
+ });
+});
Oops, something went wrong.

0 comments on commit 8972a6f

Please sign in to comment.