Skip to content

Instantly share code, notes, and snippets.

@jsynowiec
Last active April 28, 2019 10:50
Show Gist options
  • Save jsynowiec/b67221be8f34ac531f17c69fdaf1efbe to your computer and use it in GitHub Desktop.
Save jsynowiec/b67221be8f34ac531f17c69fdaf1efbe to your computer and use it in GitHub Desktop.
[yarn with npm fallback] Try to install dependencies using yarn or fall back to npm if yarn is not available #nodejs #yarn #npm
const os = require('os');
const exec = require('child_process').exec;
switch (os.platform()) {
case 'win32':
exec('yarn --version', (err, stdout, stderr) => {
if (err) {
exec('npm install');
return;
} else {
exec('yarn');
}
});
break;
case 'darwin':
case 'linux':
exec('if [ -z `which yarn`]; then npm install; else yarn; fi;');
break;
default:
throw new Error('Unsupported platform.');
}
@jsynowiec
Copy link
Author

Or use yarn || npm install. Should work on both, Windows and macOS

Use to run the command following || only if the command preceding || fails. Cmd.exe runs the first command, and then runs the second command only if the first command did not complete successfully (receives an error code greater than zero).
~ Command shell overview

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment