Use Yarn or npm to execute a command
npm install yarn-or-npmExecute scripts with Yarn or npm.
``sh`
yarn add -D yarn-or-npmor
npm i --save-dev yarn-or-npm
The client is determined by a series of ordered checks:
1. yarn.lock file is in the nearest package directory - yarnpackage-lock.json
1. file is in the nearest package directory - npmyarn
1. is installed - yarn
1. Fallback - npm
`js
import yarnOrNpm, { spawn, hasYarn, hasNpm } from 'yarn-or-npm';
// String of yarn or npm returned
console.log(yarnOrNpm());
// Boolean values for hasYarn, hasNpm
console.log(hasYarn());
// Spawn yarn or npm command
spawn(['init']);
// Spawn sync option
spawn.sync(['init'], { stdio: 'inherit' });
`
Under the covers, there are cached lookup values being used for efficiency. These can be manually cleared:
`js
import yarnOrNpm from 'yarn-or-npm';
import { spawnSync } from 'child_process';
console.log(yarnOrNpm.hasYarn()); // false
spawnSync('npm', ['i', '-g', 'yarn'], { stdio: 'inherit' });
console.log(yarnOrNpm.hasYarn()); // false (cached)
yarnOrNpm.clearCache();
console.log(yarnOrNpm.hasYarn()); // true
`
`shyon
yarn-or-npm Can also use
shorthand`
yon
Modules with bin files can be called directly in package.json scripts:
`json``
{
"devDependencies": {
...
"yarn-or-npm": "^1.0.0"
},
"scripts": {
"compile": "babel src --out-dir dist",
"lint": "eslint .",
"prepublish": "yarn-or-npm run lint && yarn-or-npm run compile"
}
}