Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for nvm for Windows #3748

Merged
merged 1 commit into from
Jul 4, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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