-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
put OS check to prevent windows people from getting cryptic errors
we'll follow up with support windows when we figure out what toolchain makes the most sense to use for custom node versions in windows
- Loading branch information
1 parent
07735a2
commit 1be76d3
Showing
2 changed files
with
80 additions
and
72 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,89 +1,94 @@ | ||
const { platform } = require('os'); | ||
const { execSync } = require('child_process'); | ||
const { existsSync, readFileSync } = require('fs'); | ||
|
||
// Allow using inputs or env until we fully switch to inputs | ||
const nodeVersionInput = | ||
process.env.NX_CLOUD_INPUT_node_version || process.env.NODE_VERSION; | ||
if (platform === 'win32') { | ||
throw new Error('Windows is not supported with this reuseable step yet.'); | ||
} else { | ||
// Allow using inputs or env until we fully switch to inputs | ||
const nodeVersionInput = | ||
process.env.NX_CLOUD_INPUT_node_version || process.env.NODE_VERSION; | ||
|
||
// set defaults incase they are not set yet | ||
process.env.NVM_DIR ??= '/home/workflows/.nvm'; | ||
process.env.COREPACK_ENABLE_AUTO_PIN ??= 0; | ||
// set defaults incase they are not set yet | ||
process.env.NVM_DIR ??= '/home/workflows/.nvm'; | ||
process.env.COREPACK_ENABLE_AUTO_PIN ??= 0; | ||
|
||
const maybeVoltaNodeVersion = getVoltaNodeVersion(); | ||
const maybeVoltaNodeVersion = getVoltaNodeVersion(); | ||
|
||
if (nodeVersionInput) { | ||
runNvmInstall(nodeVersionInput); | ||
} else if (isUsingNvm()) { | ||
// nvm will auto detect version in .nvmrc, no need to pass version | ||
runNvmInstall(null); | ||
} else if (maybeVoltaNodeVersion) { | ||
runNvmInstall(maybeVoltaNodeVersion); | ||
} else { | ||
console.warn( | ||
`No node version specified. You can use the step inputs to define a node version.`, | ||
); | ||
console.log( | ||
`Falling back to the default node version in the base image. ${execSync( | ||
'node -v', | ||
)}`, | ||
); | ||
} | ||
if (nodeVersionInput) { | ||
runNvmInstall(nodeVersionInput); | ||
} else if (isUsingNvm()) { | ||
// nvm will auto detect version in .nvmrc, no need to pass version | ||
runNvmInstall(null); | ||
} else if (maybeVoltaNodeVersion) { | ||
runNvmInstall(maybeVoltaNodeVersion); | ||
} else { | ||
console.warn( | ||
`No node version specified. You can use the step inputs to define a node version.`, | ||
); | ||
console.log( | ||
`Falling back to the default node version in the base image. ${execSync( | ||
'node -v', | ||
)}`, | ||
); | ||
} | ||
|
||
function getVoltaNodeVersion() { | ||
try { | ||
if (existsSync('package.json')) { | ||
const packageJsonContents = | ||
JSON.parse(readFileSync('package.json')) ?? {}; | ||
function getVoltaNodeVersion() { | ||
try { | ||
if (existsSync('package.json')) { | ||
const packageJsonContents = | ||
JSON.parse(readFileSync('package.json')) ?? {}; | ||
|
||
return packageJsonContents['volta']?.['node']; | ||
return packageJsonContents['volta']?.['node']; | ||
} | ||
} catch (e) { | ||
return null; | ||
} | ||
} catch (e) { | ||
return null; | ||
} | ||
} | ||
|
||
function isUsingNvm() { | ||
try { | ||
return existsSync('.nvmrc'); | ||
} catch (e) { | ||
return false; | ||
function isUsingNvm() { | ||
try { | ||
return existsSync('.nvmrc'); | ||
} catch (e) { | ||
return false; | ||
} | ||
} | ||
} | ||
|
||
function runNvmInstall(version) { | ||
try { | ||
// enable nvm and then run the install command | ||
// nvm command isn't available since nx agents don't run the bash profile | ||
const installNodeWithNvm = `. $NVM_DIR/nvm.sh && nvm install ${ | ||
version || '' | ||
} --default`; | ||
const reenableCorePack = `corepack enable`; | ||
// install outside of the current directory, | ||
// otherwise corepack errors if a different package mangager is used than is defined in the workspace | ||
const reinstallPackageManagers = `cd .. && corepack prepare yarn@1 && corepack prepare pnpm@8`; | ||
const printVersions = ['node', 'npm', 'yarn', 'pnpm'] | ||
.map((cmd) => `echo "${cmd}: $(${cmd} -v)"`) | ||
.join(' && '); | ||
function runNvmInstall(version) { | ||
try { | ||
// enable nvm and then run the install command | ||
// nvm command isn't available since nx agents don't run the bash profile | ||
const installNodeWithNvm = `. $NVM_DIR/nvm.sh && nvm install ${ | ||
version || '' | ||
} --default`; | ||
const reenableCorePack = `corepack enable`; | ||
// install outside of the current directory, | ||
// otherwise corepack errors if a different package mangager is used than is defined in the workspace | ||
const reinstallPackageManagers = `cd .. && corepack prepare yarn@1 && corepack prepare pnpm@8`; | ||
const printVersions = ['node', 'npm', 'yarn', 'pnpm'] | ||
.map((cmd) => `echo "${cmd}: $(${cmd} -v)"`) | ||
.join(' && '); | ||
|
||
// path will be updated via nvm to include the new node versions, | ||
const saveEnvVars = `echo "PATH=$PATH\nNVM_DIR=${process.env.NVM_DIR}\nCOREPACK_ENABLE_AUTO_PIN=0" >> $NX_CLOUD_ENV`; | ||
// path will be updated via nvm to include the new node versions, | ||
const saveEnvVars = `echo "PATH=$PATH\nNVM_DIR=${process.env.NVM_DIR}\nCOREPACK_ENABLE_AUTO_PIN=0" >> $NX_CLOUD_ENV`; | ||
|
||
execSync( | ||
[ | ||
installNodeWithNvm, | ||
reenableCorePack, | ||
reinstallPackageManagers, | ||
printVersions, | ||
saveEnvVars, | ||
].join(' && '), | ||
{ | ||
stdio: 'inherit', | ||
}, | ||
); | ||
} catch (e) { | ||
console.error(e); | ||
throw new Error( | ||
`Failed to install node version using nvm ${version || ''}`, | ||
); | ||
execSync( | ||
[ | ||
installNodeWithNvm, | ||
reenableCorePack, | ||
reinstallPackageManagers, | ||
printVersions, | ||
saveEnvVars, | ||
].join(' && '), | ||
{ | ||
stdio: 'inherit', | ||
}, | ||
); | ||
} catch (e) { | ||
console.error(e); | ||
throw new Error( | ||
`Failed to install node version using nvm ${version || ''}`, | ||
); | ||
} | ||
} | ||
} |