Skip to content

Commit

Permalink
Merge branch 'auto-version-detection'
Browse files Browse the repository at this point in the history
  • Loading branch information
marcelklehr committed Apr 22, 2016
2 parents 13774da + e1e49f7 commit a2fcfc5
Show file tree
Hide file tree
Showing 7 changed files with 287 additions and 180 deletions.
Binary file modified bin/node.exe
Binary file not shown.
5 changes: 1 addition & 4 deletions bin/nodist.cmd
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,7 @@ goto next1

:env
call %0 + %2
if ERRORLEVEL 0 (
:: get version and set NODIST_VERSION
FOR /F "tokens=1 delims=" %%A in ('"%0" add %2') do @set "NODIST_VERSION=%%A"
)
@set "NODIST_VERSION=%2"
:: goto end
GOTO end

Expand Down
10 changes: 4 additions & 6 deletions bin/nodist.ps1
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
param($cmd,$ver,[Switch]$v)
if ($cmd -eq "use" -or $cmd -eq "env") {
$version = (nodist.cmd add $ver)
if($LastExitCode -eq 0) {
$env:NODIST_VERSION = $version
echo $env:NODIST_VERSION
}
nodist.cmd add $ver
$env:NODIST_VERSION = $ver
echo $ver
}
elseif ($v){
nodist.cmd -v
}
else {
nodist.cmd $cmd $ver ([string]$args)
}
}
110 changes: 65 additions & 45 deletions cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -128,36 +128,39 @@ if (!argv[0]) {
// LIST all installed buids
if (command.match(/^list|ls$/i)) {

n.getGlobal(function(err, global){
if(err) void(0);
n.getLocal(function(err, local, localFile){
if(err) void(0);
n.getEnv(function(err, env){
if(err) void(0);
n.listInstalled(function(err, ls) {
if(err) abort(err.message+'. Sorry.');
if(n.wantX64) console.log(' (x64)');
if(ls.length === 0) abort('No builds installed, yet.');
var current = env || local || global;
// display all versions
ls.forEach(function(version) {
var del = ' ';
var note = ' ';
if (version === env) {
note += ' (env)';
}
if (version === local) {
note += ' ('+localFile+')';
}
if (version === global) {
note += ' (global)';
}
if (version === current) del ='> ';// highlight current
n.getGlobal(function(err, globalSpec){
n.resolveVersionLocally(globalSpec, function(er, globalVersion) {
n.getLocal(function(err, localSpec, localFile){
n.resolveVersionLocally(localSpec, function(er, localVersion) {
n.getEnv(function(err, envSpec){
n.resolveVersionLocally(envSpec, function(er, envVersion) {
n.listInstalled(function(err, ls) {
if(err) abort(err.message+'. Sorry.');
if(n.wantX64) console.log(' (x64)');
if(ls.length === 0) abort('No builds installed, yet.');
var current = envVersion || localVersion || globalVersion;
// display all versions
ls.forEach(function(version) {
var del = ' ';
var note = ' ';
if (version === envVersion) {
note += ' (env)';
}
if (version === localVersion) {
note += ' ('+localFile+')';
}
if (version === globalVersion) {
note += ' (global)';
}
if (version === current) del ='> ';// highlight current

console.log(del + version+note);
});
exit();
});
console.log(del + version+note);
});
exit();
});
})
})
})
});
});
});
Expand Down Expand Up @@ -280,26 +283,43 @@ else if (command.match(/^args$/i) && argv[1]) {
}
// LOCAL use the specified version locally
else if (command.match(/^local$/i) && argv[1]) {
version = argv[1];
n.resolveVersion(version, function(er, v) {
if(er) abort(er.message+'. Sorry.');
n.setLocal(v, function(err, file) {
if(err) abort(err.message+'. Sorry.');
console.log(v, '(' + file + ')');
exit();
});
var spec = argv[1];
n.setLocal(spec, function(err, file) {
if(err) abort(err.message+'. Sorry.');
console.log(version, '(' + file + ')');
n.resolveVersionLocally(spec, function(er, found) {
if(found) {
exit();
}
n.resolveVersion(spec, function(er, version) {
console.log("Installing "+version)
n.install(version, function(er) {
if(er) return abort(er.message+'. Sorry.')
exit(0, 'Installation successfull.')
})
})
})
});
}
// GLOBAL globally use the specified node version
else if (command.match(/^global$/i) && argv[1] || argv[0] && !argv[1]) {
version = argv[1] || argv[0];
n.resolveVersion(version, function(er, v) {
if(er) abort(er.message+'. Sorry.');
n.setGlobal(v, function(err) {
if(err) abort(err.message+'. Sorry.');
console.log(v);
exit();
});
spec = argv[1] || argv[0];
n.setGlobal(spec, function(err) {
if(err) abort(err.message+'. Sorry.');
console.log(spec);
n.resolveVersionLocally(spec, function(er, found) {
if(found) {
exit();
}
n.resolveVersion(spec, function(er, version) {
console.log("Installing "+version)
n.install(version, function(er) {
if(er) return abort(er.message+'. Sorry.')
exit(0, 'Installation successfull.')
})
})
})

});
}
// HELP display help for unknown cli parameters
Expand Down
107 changes: 33 additions & 74 deletions lib/nodist.js
Original file line number Diff line number Diff line change
Expand Up @@ -404,16 +404,26 @@ nodist.prototype.resolveVersion = function resolveVersion(versionSpec, cb){
var that = this;
that.listAvailable(function(err, list) {
if(err) {
that.listInstalled(function(err, list) {
if(err) return cb(err);
vermanager.find(versionSpec, list, cb);
});
return;
that.resolveVersionLocally(versionSpec, cb)
return
}
vermanager.find(versionSpec, list, cb);
});
};

/**
* Resolves version specs given by the user against the installed versions only
* relative to available versions and installed versions
* @param {string} versionSpec such as '0.12.x'
* @param {function} cb in the form of err, versions
*/
nodist.prototype.resolveVersionLocally = function resolveVersion(versionSpec, cb){
if(!versionSpec) return cb() // Hack in order to make `nodist list` work
this.listInstalled(function(err, list) {
if(err) return cb(err);
vermanager.find(versionSpec, list, cb);
});
};

/**
* Install the passed version
Expand Down Expand Up @@ -662,20 +672,15 @@ nodist.prototype.fetch = function fetch(version, callback){
* @param {string} version
* @param {function} cb accepting (err)
*/
nodist.prototype.setGlobal = function setGlobal(version, cb){
var that = this;

that.install(version, function(err){
if(err) return cb(err);
var globalFile = that.getPathToGlobalVersion();
fs.writeFile(globalFile, version, function(er) {
if(er){
return cb(new Error(
'Could not activate version ' + version + ' (' + er.message + ')'
));
}
cb();
});
nodist.prototype.setGlobal = function setGlobal(versionSpec, cb){
var globalFile = this.getPathToGlobalVersion();
fs.writeFile(globalFile, versionSpec, function(er) {
if(er){
return cb(new Error(
'Could not set version ' + versionSpec + ' (' + er.message + ')'
));
}
cb();
});
};

Expand All @@ -688,7 +693,7 @@ nodist.prototype.getGlobal = function getGlobal(cb){
var globalFile = this.getPathToGlobalVersion();
fs.readFile(globalFile, function(er, version){
if(er) return cb(er);
cb(null, version.toString().trim());
cb(null, version.toString().trim(), cb);
});
};

Expand All @@ -698,17 +703,14 @@ nodist.prototype.getGlobal = function getGlobal(cb){
* @param {string} version
* @param {function} cb function accepting (err)
*/
nodist.prototype.setLocal = function setLocal(version, cb) {
this.install(version, function(err){
if(err) return cb(err);
fs.writeFile('./.node-version', version, function(er){
if(er){
return cb(new Error(
'Could not activate version ' + version + ' (' + er.message + ')'
));
}
cb(null, process.cwd() + '\\.node-version');
});
nodist.prototype.setLocal = function setLocal(versionSpec, cb) {
fs.writeFile('./.node-version', versionSpec, function(er){
if(er){
return cb(new Error(
'Could not set version ' + versionSpec + ' (' + er.message + ')'
));
}
cb(null, process.cwd() + '\\.node-version');
});
};

Expand Down Expand Up @@ -748,49 +750,6 @@ nodist.prototype.getEnv = function getEnv(cb) {
cb(null, this.envVersion);
};


/**
* Sets additional arguments to be used when running a specific version
* @param {string} version the version to be set
* @param {object} args arguments to pass
* @param {function} cb accepting (err)
*/
nodist.prototype.setArgsForVersion = function setArgsForVersion(
version,
args,
cb
){
var that = this;

this.install(version, function(err) {
if(err) return cb(err);
var argsFile = that.getVersionDir(version) + '/args';
fs.writeFile(argsFile, args, function(er){
if(er){
return cb(new Error(
'Could not write args to file ' + argsFile + ' (' + er.message + ')'
));
}
cb();
});
});
};


/**
* Gets additional arguments to be used for a specific version
* @param {string} version
* @param {function} cb accepting (err, args)
*/
nodist.prototype.getArgsForVersion = function getArgsForVersion(version, cb) {
var that = this;
fs.readFile(that.getVersionDir(version) + '/args', function(er, args){
if(er) return cb(er);
cb(null, args.toString().trim());
});
};


/**
* Emulates the passed node version with args
* @param {string} version
Expand Down
Loading

0 comments on commit a2fcfc5

Please sign in to comment.