Skip to content

Commit

Permalink
Merge pull request #3748 from JimiC/support_nvm4win
Browse files Browse the repository at this point in the history
Add support for nvm for Windows
  • Loading branch information
wallet77 committed Jul 4, 2018
2 parents d4e66e3 + 6b52514 commit 2dac235
Showing 1 changed file with 23 additions and 10 deletions.
33 changes: 23 additions & 10 deletions lib/Common.js
Original file line number Diff line number Diff line change
Expand Up @@ -343,29 +343,42 @@ var resolveNodeInterpreter = function(app) {
Common.printError(cst.PREFIX_MSG_WARNING + chalk.bold.yellow('Choosing the Node.js version in cluster mode is not supported'));
return false;
}
if (!process.env.NVM_DIR) {

var nvm_path = cst.IS_WINDOWS ? process.env.NVM_HOME : process.env.NVM_DIR;
if (!nvm_path) {
Common.printError(cst.PREFIX_MSG_ERR + chalk.red('NVM is not available in PATH'));
Common.printError(cst.PREFIX_MSG_ERR + chalk.red('Fallback to node in PATH'));
Common.printOut(cst.PREFIX_MSG_ERR + chalk.bold('Install NVM:\n$ curl https://raw.githubusercontent.com/creationix/nvm/master/install.sh | bash'));
var msg = cst.IS_WINDOWS
? 'https://github.com/coreybutler/nvm-windows/releases/'
: '$ curl https://raw.githubusercontent.com/creationix/nvm/master/install.sh | bash';
Common.printOut(cst.PREFIX_MSG_ERR + chalk.bold('Install NVM:\n' + msg));
}
else {
var node_version = app.exec_interpreter.split('@')[1];
var nvm_node_path;

if (semver.satisfies(node_version, '>= 0.12.0'))
nvm_node_path = path.join(process.env.NVM_DIR, '/versions/node/v' + node_version + '/bin/node');
else
nvm_node_path = path.join(process.env.NVM_DIR, '/v' + node_version + '/bin/node');
var path_to_node = cst.IS_WINDOWS
? '/v' + node_version + '/node.exe'
: semver.satisfies(node_version, '>= 0.12.0')
? '/versions/node/v' + node_version + '/bin/node'
: '/v' + node_version + '/bin/node';
var nvm_node_path = path.join(nvm_path, path_to_node);

try {
fs.accessSync(nvm_node_path);
} catch(e) {
Common.printOut(cst.PREFIX_MSG + 'Installing Node v%s', node_version);
var nvm_bin = path.join(process.env.NVM_DIR, 'nvm.sh');
var nvm_cmd = '. ' + nvm_bin + ' ; nvm install ' + node_version;
var nvm_bin = path.join(nvm_path, 'nvm.' + (cst.IS_WINDOWS ? 'exe' : 'sh'));
var nvm_cmd = cst.IS_WINDOWS
? nvm_bin + ' install ' + node_version
: '. ' + nvm_bin + ' ; nvm install ' + node_version;

Common.printOut(cst.PREFIX_MSG + 'Executing: %s', nvm_cmd);
require('shelljs').exec(nvm_cmd);

// in order to support both arch, nvm for Windows renames 'node.exe' to:
// 'node32.exe' for x32 arch
// 'node64.exe' for x64 arch
if (cst.IS_WINDOWS)
nvm_node_path = nvm_node_path.replace(/node/, 'node' + process.arch.slice(1))
}

Common.printOut(cst.PREFIX_MSG + chalk.green.bold('Setting Node to v%s (path=%s)'),
Expand Down

0 comments on commit 2dac235

Please sign in to comment.