yarn-or-npm
Execute scripts with Yarn or npm.
yarn add -D yarn-or-npm
npm i --save-dev yarn-or-npm
The client is determined by a series of ordered checks:
yarn.lock
file is in the nearest package directory - yarnpackage-lock.json
file is in the nearest package directory - npmyarn
is installed - yarn- Fallback - npm
Module
import yarnOrNpm, { spawn, hasYarn, hasNpm } from 'yarn-or-npm';
console.log(yarnOrNpm());
console.log(hasYarn());
spawn(['init']);
spawn.sync(['init'], { stdio: 'inherit' });
Under the covers, there are cached lookup values being used for efficiency. These can be manually cleared:
import yarnOrNpm from 'yarn-or-npm';
import { spawnSync } from 'child_process';
console.log(yarnOrNpm.hasYarn());
spawnSync('npm', ['i', '-g', 'yarn'], { stdio: 'inherit' });
console.log(yarnOrNpm.hasYarn());
yarnOrNpm.clearCache();
console.log(yarnOrNpm.hasYarn());
CLI
yarn-or-npm <command>
yon <command>
Package
Modules with bin files can be called directly in package.json
scripts:
{
"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"
}
}