Skip to content

Commit 6fe79da

Browse files
committed
docs: update docs
1 parent 57a0ec9 commit 6fe79da

File tree

6 files changed

+82
-15
lines changed

6 files changed

+82
-15
lines changed

README.md

Lines changed: 73 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,80 @@
11
# check-npm-client
22

3-
This module helps to ensure using of [npm](https://github.com/npm/cli) or [yarn](https://github.com/yarnpkg/yarn) as npm client before try to install any dependencies to your project. It is currently working in progress.
3+
`npm` and `yarn` are both npm clients used to manage node.js project dependencies. They are somehow different in some behavior, and thus can affect project running.
44

5-
## references
5+
This package provide ready-to-use functionality to check current npm client. When used as [pre](https://docs.npmjs.com/misc/scripts#hook-scripts) commands in npm scripts, it will check if the script is executed by specific npm client (`yarn` or `npm`). If it is restricted, then the script execution will be aborted early.
6+
7+
## Usage Scenario
8+
9+
`yarn` uses [yarn.lock](https://yarnpkg.com/lang/en/docs/yarn-lock/) to ensure versions of dependencies, but `npm` use [package-lock.json](https://docs.npmjs.com/files/package-lock.json) to do that instead. If you use `yarn` to install dependencies in a project which is actually managed by `npm` and `package-lock.json`, the actually installed dependencies in `node_modules` might be different due to [semver](https://docs.npmjs.com/misc/semver) behavior, and this could lead to some un-expected exception while code running.
10+
11+
This is common when working with others because people tends to use `yarn` or `npm` as their wishes.
12+
13+
## Installation
14+
15+
```bash
16+
$ npm install check-npm-client
17+
```
18+
19+
or alternatively, with `yarn`:
20+
21+
```bash
22+
$ yarn add check-npm-client
23+
```
24+
25+
## Example
26+
27+
This package provide a command line tool to invoke checking functionality:
28+
29+
```bash
30+
# the user must use `npm` to execute the script
31+
$ check-npm-client --npm-only
32+
33+
# the user must use `yarn` to execute the script
34+
$ check-npm-client --yarn-only
35+
36+
# automatically check according to current working directory lock files if exists
37+
# not available yet
38+
$ check-npm-client
39+
```
40+
41+
Besides, you can use it programmatically:
42+
43+
```javascript
44+
const { checkNpmClient } = require('check-npm-client');
45+
console.log(checkNpmClient('yarn')); // true/false
46+
```
47+
48+
### Ensure before installation
49+
50+
Add the script in your `package.json` to ensure that user must install dependencies with `yarn`:
51+
52+
```json
53+
{
54+
"scripts": {
55+
"preinstall": "check-npm-client --yarn-only"
56+
}
57+
}
58+
```
59+
60+
**Note**: `yarn` and `npm` treat `preinstall` script a little differently. `npm` execute it only before `npm install` (not before `npm install <module>`), but `yarn` always run it before any installation. See [issue](https://github.com/npm/cli/issues/481) here. So if specified as `--yarn-only` and user use `npm` to install another dependency, the `preinstall` will not be invoked (so the ensurance will fail).
61+
62+
### Ensure before any other script
63+
64+
Add the script in your `package.json` to ensure that user must use `npm` to run the script `my-task`:
65+
66+
```json
67+
{
68+
"scripts": {
69+
"premy-task": "check-npm-client --npm-only",
70+
"my-task": "node my-task.js"
71+
}
72+
}
73+
```
74+
75+
## References
676

7-
- <https://github.com/nodejs/node/issues/14957>
8-
- <https://github.com/yibn2008/find-process>
9-
- <https://github.com/timoxley/npm-which>
1077
- <https://github.com/npm/cli/issues/481>
78+
- <https://github.com/yibn2008/find-process>
1179
- <https://stackoverflow.com/questions/46725374/how-to-run-a-script-before-installing-any-npm-module>
1280
- <https://github.com/yarnpkg/yarn/issues/5063>

bin/cli.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#!/usr/bin/env node
22

33
const chalk = require('chalk');
4-
const { debug, ensureNpm, ensureYarn } = require('../lib/utils');
4+
const { debug, ensureNpm, ensureYarn } = require('../lib');
55

66
const helpText = `
77
${chalk.green('Usage')}

lib/check-npm.js

Lines changed: 0 additions & 3 deletions
This file was deleted.

lib/check-yarn.js

Lines changed: 0 additions & 5 deletions
This file was deleted.
File renamed without changes.

package.json

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,27 @@
11
{
22
"name": "check-npm-client",
3+
"description": "A utility to check which npm client(yarn/npm) is used",
34
"version": "1.0.0",
45
"author": "aprilandjan",
56
"license": "MIT",
67
"files": [
7-
"bin"
8+
"bin",
9+
"lib"
810
],
911
"engines": {
1012
"node": ">=7.6.0"
1113
},
1214
"bin": {
1315
"check-npm-client": "./bin/cli.js"
1416
},
17+
"main": "./lib/index.js",
1518
"scripts": {
1619
"check": "node check.js"
1720
},
21+
"repository": {
22+
"type": "git",
23+
"url": "https://github.com/aprilandjan/ascii-tree-generator"
24+
},
1825
"dependencies": {
1926
"chalk": "^3.0.0",
2027
"debug": "^4.1.1",

0 commit comments

Comments
 (0)