@@ -310,6 +310,44 @@ Common.sink.determineExecMode = function(app) {
310310 }
311311} ;
312312
313+ var resolveNodeInterpreter = function ( app ) {
314+ if ( app . exec_mode . indexOf ( 'cluster' ) > - 1 ) {
315+ Common . printError ( cst . PREFIX_MSG_WARNING + chalk . bold . yellow ( 'Choosing the Node.js version in cluster mode is not supported' ) ) ;
316+ return false ;
317+ }
318+ if ( ! process . env . NVM_DIR ) {
319+ Common . printError ( cst . PREFIX_MSG_ERR + chalk . red ( 'NVM is not available in PATH' ) ) ;
320+ Common . printError ( cst . PREFIX_MSG_ERR + chalk . red ( 'Fallback to node in PATH' ) ) ;
321+ Common . printOut ( cst . PREFIX_MSG_ERR + chalk . bold ( 'Install NVM:\n$ curl https://raw.githubusercontent.com/creationix/nvm/master/install.sh | bash' ) ) ;
322+ }
323+ else {
324+ var node_version = app . exec_interpreter . split ( '@' ) [ 1 ] ;
325+ var nvm_node_path ;
326+
327+ if ( semver . satisfies ( node_version , '>= 0.12.0' ) )
328+ nvm_node_path = path . join ( process . env . NVM_DIR , '/versions/node/v' + node_version + '/bin/node' ) ;
329+ else
330+ nvm_node_path = path . join ( process . env . NVM_DIR , '/v' + node_version + '/bin/node' ) ;
331+
332+ try {
333+ fs . accessSync ( nvm_node_path ) ;
334+ } catch ( e ) {
335+ Common . printOut ( cst . PREFIX_MSG + 'Installing Node v%s' , node_version ) ;
336+ var nvm_bin = path . join ( process . env . NVM_DIR , 'nvm.sh' ) ;
337+ var nvm_cmd = '. ' + nvm_bin + ' ; nvm install ' + node_version ;
338+
339+ Common . printOut ( cst . PREFIX_MSG + 'Executing: %s' , nvm_cmd ) ;
340+ shelljs . exec ( nvm_cmd ) ;
341+ }
342+
343+ Common . printOut ( cst . PREFIX_MSG + chalk . green . bold ( 'Setting Node to v%s (path=%s)' ) ,
344+ node_version ,
345+ nvm_node_path ) ;
346+
347+ app . exec_interpreter = nvm_node_path ;
348+ }
349+ } ;
350+
313351/**
314352 * Resolve interpreter
315353 */
@@ -318,49 +356,14 @@ Common.sink.resolveInterpreter = function(app) {
318356 extName = path . extname ( app . pm_exec_path ) ,
319357 betterInterpreter = extItps [ extName ] ;
320358
321- var thereIsNVMInstalled = false ;
322-
323359 // No interpreter defined and correspondance in schema hashmap
324360 if ( noInterpreter && betterInterpreter )
325361 app . exec_interpreter = betterInterpreter ;
326362 // Else if no Interpreter detect if process is binary
327363 else if ( noInterpreter )
328364 app . exec_interpreter = isBinary ( app . pm_exec_path ) ? 'none' : 'node' ;
329- else if ( app . exec_interpreter . indexOf ( 'node@' ) > - 1 ||
330- app . node_version && thereIsNVMInstalled ) {
331-
332- if ( ! process . env . NVM_DIR ) {
333- Common . printError ( cst . PREFIX_MSG_ERR + chalk . red ( 'NVM is not available in PATH' ) ) ;
334- Common . printError ( cst . PREFIX_MSG_ERR + chalk . red ( 'Fallback to node in PATH' ) ) ;
335- Common . printOut ( cst . PREFIX_MSG_ERR + chalk . bold ( 'Install NVM:\n$ curl https://raw.githubusercontent.com/creationix/nvm/master/install.sh | bash' ) ) ;
336- }
337- else {
338- var node_version = app . exec_interpreter . split ( '@' ) [ 1 ] ;
339- var nvm_node_path ;
340-
341- if ( semver . satisfies ( node_version , '>= 0.12.0' ) )
342- nvm_node_path = path . join ( process . env . NVM_DIR , '/versions/node/v' + node_version + '/bin/node' ) ;
343- else
344- nvm_node_path = path . join ( process . env . NVM_DIR , '/v' + node_version + '/bin/node' ) ;
345-
346- try {
347- fs . accessSync ( nvm_node_path ) ;
348- } catch ( e ) {
349- Common . printOut ( cst . PREFIX_MSG + 'Installing Node v%s' , node_version ) ;
350- var nvm_bin = path . join ( process . env . NVM_DIR , 'nvm.sh' ) ;
351- var nvm_cmd = '. ' + nvm_bin + ' ; nvm install ' + node_version ;
352-
353- Common . printOut ( cst . PREFIX_MSG + 'Executing: %s' , nvm_cmd ) ;
354- shelljs . exec ( nvm_cmd ) ;
355- }
356-
357- Common . printOut ( cst . PREFIX_MSG + chalk . green . bold ( 'Setting Node to v%s (path=%s)' ) ,
358- node_version ,
359- nvm_node_path ) ;
360-
361- app . exec_interpreter = nvm_node_path ;
362- }
363- }
365+ else if ( app . exec_interpreter . indexOf ( 'node@' ) > - 1 )
366+ resolveNodeInterpreter ( app ) ;
364367
365368 /**
366369 * Specific installed JS transpilers
@@ -496,6 +499,10 @@ Common.mergeEnvironmentVariables = function(app_env, env_name, deploy_conf) {
496499 delete current_conf . env ;
497500 app . env . current_conf = current_conf ;
498501
502+ // #2541 force resolution of node interpreter
503+ if ( app . env . current_conf . exec_interpreter . indexOf ( '@' ) > - 1 )
504+ resolveNodeInterpreter ( app . env . current_conf ) ;
505+
499506 return app . env ;
500507}
501508
0 commit comments