UNPKG

1.53 kBMarkdownView Raw
1# yarn-or-npm
2
3Execute scripts with Yarn or npm.
4
5```sh
6yarn add -D yarn-or-npm
7# or
8npm i --save-dev yarn-or-npm
9```
10
11The client is determined by a series of ordered checks:
12
131. `yarn.lock` file is in the nearest package directory - **yarn**
141. `package-lock.json` file is in the nearest package directory - **npm**
151. `yarn` is installed - **yarn**
161. Fallback - **npm**
17
18## Module
19
20```js
21import yarnOrNpm, { spawn, hasYarn, hasNpm } from 'yarn-or-npm';
22
23// String of `yarn` or `npm` returned
24console.log(yarnOrNpm());
25
26// Boolean values for hasYarn, hasNpm
27console.log(hasYarn());
28
29// Spawn yarn or npm command
30spawn(['init']);
31
32// Spawn sync option
33spawn.sync(['init'], { stdio: 'inherit' });
34```
35
36Under the covers, there are cached lookup values being used for efficiency. These can be manually cleared:
37
38```js
39import yarnOrNpm from 'yarn-or-npm';
40import { spawnSync } from 'child_process';
41
42console.log(yarnOrNpm.hasYarn()); // false
43
44spawnSync('npm', ['i', '-g', 'yarn'], { stdio: 'inherit' });
45
46console.log(yarnOrNpm.hasYarn()); // false (cached)
47
48yarnOrNpm.clearCache();
49console.log(yarnOrNpm.hasYarn()); // true
50```
51
52## CLI
53
54```sh
55yarn-or-npm <command>
56# Can also use `yon` shorthand
57yon <command>
58```
59
60## Package
61
62Modules with bin files can be called directly in `package.json` scripts:
63
64```json
65{
66 "devDependencies": {
67 ...
68 "yarn-or-npm": "^1.0.0"
69 },
70 "scripts": {
71 "compile": "babel src --out-dir dist",
72 "lint": "eslint .",
73 "prepublish": "yarn-or-npm run lint && yarn-or-npm run compile"
74 }
75}
76```