1 | # yarn-or-npm
|
2 |
|
3 | Execute scripts with Yarn or npm.
|
4 |
|
5 | ```sh
|
6 | yarn add -D yarn-or-npm
|
7 | # or
|
8 | npm i --save-dev yarn-or-npm
|
9 | ```
|
10 |
|
11 | The client is determined by a series of ordered checks:
|
12 |
|
13 | 1. `yarn.lock` file is in the nearest package directory - **yarn**
|
14 | 1. `package-lock.json` file is in the nearest package directory - **npm**
|
15 | 1. `yarn` is installed - **yarn**
|
16 | 1. Fallback - **npm**
|
17 |
|
18 | ## Module
|
19 |
|
20 | ```js
|
21 | import yarnOrNpm, { spawn, hasYarn, hasNpm } from 'yarn-or-npm';
|
22 |
|
23 | // String of `yarn` or `npm` returned
|
24 | console.log(yarnOrNpm());
|
25 |
|
26 | // Boolean values for hasYarn, hasNpm
|
27 | console.log(hasYarn());
|
28 |
|
29 | // Spawn yarn or npm command
|
30 | spawn(['init']);
|
31 |
|
32 | // Spawn sync option
|
33 | spawn.sync(['init'], { stdio: 'inherit' });
|
34 | ```
|
35 |
|
36 | Under the covers, there are cached lookup values being used for efficiency. These can be manually cleared:
|
37 |
|
38 | ```js
|
39 | import yarnOrNpm from 'yarn-or-npm';
|
40 | import { spawnSync } from 'child_process';
|
41 |
|
42 | console.log(yarnOrNpm.hasYarn()); // false
|
43 |
|
44 | spawnSync('npm', ['i', '-g', 'yarn'], { stdio: 'inherit' });
|
45 |
|
46 | console.log(yarnOrNpm.hasYarn()); // false (cached)
|
47 |
|
48 | yarnOrNpm.clearCache();
|
49 | console.log(yarnOrNpm.hasYarn()); // true
|
50 | ```
|
51 |
|
52 | ## CLI
|
53 |
|
54 | ```sh
|
55 | yarn-or-npm <command>
|
56 | # Can also use `yon` shorthand
|
57 | yon <command>
|
58 | ```
|
59 |
|
60 | ## Package
|
61 |
|
62 | Modules 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 | ```
|