Skip to content

Commit

Permalink
put OS check to prevent windows people from getting cryptic errors
Browse files Browse the repository at this point in the history
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
barbados-clemens committed May 24, 2024
1 parent 07735a2 commit 1be76d3
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 72 deletions.
3 changes: 3 additions & 0 deletions workflow-steps/install-node/README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
## Usage

> [!WARNING]
> This step current does not support Windows yet.
```yaml
- name: Install Node
uses: 'nrwl/nx-cloud-workflows/v4/workflow-steps/install-node/main.yaml'
Expand Down
149 changes: 77 additions & 72 deletions workflow-steps/install-node/main.js
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 || ''}`,
);
}
}
}

0 comments on commit 1be76d3

Please sign in to comment.