Skip to content

Commit 57a0ec9

Browse files
committed
refactor: use environments to check the current client
1 parent e9e4869 commit 57a0ec9

File tree

9 files changed

+75
-32
lines changed

9 files changed

+75
-32
lines changed

bin/cli.js

100644100755
Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
#!/usr/bin/env node
22

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

66
const helpText = `
7-
Add the script as the pre-script in your package.json to ensure that your script is executed by specific npm client (${chalk.green('npm')} or ${chalk.red('yarn')}).
7+
${chalk.green('Usage')}
8+
add check script as the pre-hook script in your package.json
9+
to ensure that your script is executed by specific npm client (${chalk.green('npm')} or ${chalk.green('yarn')}).
810
911
${chalk.green('Example')}
1012
$ check-npm-client
@@ -17,18 +19,19 @@ const script = args[0];
1719

1820
debug('cmd:', process.cwd());
1921
debug('script:', script);
22+
debug('npm user agent', process.env.npm_config_user_agent);
2023

2124
// check only for the first params to decide which to use
2225
switch (script) {
2326
// allow npm to run script only
2427
case '--npm-only':
2528
case 'npmOnly':
26-
require('../lib/check-npm')();
29+
ensureNpm();
2730
break;
2831
// allow yarn to run script only
2932
case '--yarn-only':
3033
case 'yarnOnly':
31-
require('../lib/check-yarn')();
34+
ensureYarn();
3235
break;
3336
// auto detect if yarn or npm is more suitable according to lock files
3437
default:
@@ -37,5 +40,6 @@ switch (script) {
3740
`${chalk.yellow('Unrecognized Command')} "${chalk.red(script)}"`
3841
);
3942
}
43+
console.log(process.env.npm_config_user_agent);
4044
console.log(helpText);
4145
}

lib/utils.js

Lines changed: 40 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
const debug = require('debug')('check-npm-client');
2+
const chalk = require('chalk');
23
const findProcess = require('find-process');
4+
const parseNpmUserAgent = require('npm-config-user-agent-parser');
35

46
/** find all ancestor process info by pid. If failed to failed to found ppid, then exit. */
57
async function findAncestorProcessInfo (pid) {
@@ -10,15 +12,49 @@ async function findAncestorProcessInfo (pid) {
1012
const [info] = await findProcess('pid', id, true);
1113
// cannot found this process info, or process is not invoked by node
1214
if (!info) {
13-
console.log(`cannot found process info by id ${id}.`)
15+
debug(`cannot found process info by id ${id}.`)
1416
finished = true;
1517
} else {
16-
console.log(`====> round ${iteration++}`);
17-
console.log(`pid = ${info.pid}, cmd = ${info.cmd}`, info);
18+
debug(`round ${iteration++}`);
19+
debug(`pid = ${info.pid}, cmd = ${info.cmd}`, info);
1820
id = info.ppid;
1921
// TODO: check npm client here
2022
}
2123
}
2224
}
2325

24-
exports.debug = debug;
26+
/** get current npm client */
27+
function checkNpmClient (client) {
28+
const current = parseNpmUserAgent(process.env.npm_config_user_agent);
29+
// debug('parsed npm user agent:', current);
30+
if (client === 'yarn' && !current.yarn
31+
|| client === 'npm' && !current.npm
32+
) {
33+
return false;
34+
}
35+
return true;
36+
}
37+
38+
function ensureYarn () {
39+
if (!checkNpmClient('yarn')) {
40+
console.error(`[check-npm-client] please use ${chalk.green('yarn')} instead of ${chalk.red('npm')}`);
41+
process.exit(-1);
42+
}
43+
debug('the script is executed by yarn');
44+
}
45+
46+
function ensureNpm () {
47+
if (!checkNpmClient('npm')) {
48+
console.error(`[check-npm-client] please use ${chalk.green('npm')} instead of ${chalk.red('yarn')}`);
49+
process.exit(-1);
50+
}
51+
debug('the script is executed by npm');
52+
}
53+
54+
module.exports = {
55+
debug,
56+
findAncestorProcessInfo,
57+
checkNpmClient,
58+
ensureYarn,
59+
ensureNpm,
60+
};

package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@
1818
"dependencies": {
1919
"chalk": "^3.0.0",
2020
"debug": "^4.1.1",
21-
"find-process": "^1.4.2"
21+
"find-process": "^1.4.2",
22+
"npm-config-user-agent-parser": "^1.0.0"
2223
},
23-
"devDependencies": {
24-
}
24+
"devDependencies": {}
2525
}

test/fixture/auto/package.json

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

test/fixture/npm-only/package.json

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

test/fixture/project/index.js

Whitespace-only changes.

test/fixture/project/package.json

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"name": "check-npm-client-auto",
3+
"version": "1.0.0",
4+
"scripts": {
5+
"precheckYarn": "../../../bin/cli --yarn-only",
6+
"checkYarn": "node index.js",
7+
"precheckNpm": "../../../bin/cli --npm-only",
8+
"checkNpm": "node index.js",
9+
"precheck": "../../../bin/cli",
10+
"check": "node index.js"
11+
}
12+
}

test/fixture/yarn-only/package.json

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

yarn.lock

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,18 @@ ms@^2.1.1:
106106
resolved "https://registry.npm.taobao.org/ms/download/ms-2.1.2.tgz?cache=0&other_urls=https%3A%2F%2Fregistry.npm.taobao.org%2Fms%2Fdownload%2Fms-2.1.2.tgz#d09d1f357b443f493382a8eb3ccd183872ae6009"
107107
integrity sha1-0J0fNXtEP0kzgqjrPM0YOHKuYAk=
108108

109+
npm-config-user-agent-parser@^1.0.0:
110+
version "1.0.0"
111+
resolved "https://registry.npm.taobao.org/npm-config-user-agent-parser/download/npm-config-user-agent-parser-1.0.0.tgz#09d6a2fb3ab5c59af4135c6ee2da9c13ba7007d7"
112+
integrity sha1-Cdai+zq1xZr0E1xu4tqcE7pwB9c=
113+
dependencies:
114+
semver "^5.5.0"
115+
116+
semver@^5.5.0:
117+
version "5.7.1"
118+
resolved "https://registry.npm.taobao.org/semver/download/semver-5.7.1.tgz?cache=0&sync_timestamp=1565627380363&other_urls=https%3A%2F%2Fregistry.npm.taobao.org%2Fsemver%2Fdownload%2Fsemver-5.7.1.tgz#a954f931aeba508d307bbf069eff0c01c96116f7"
119+
integrity sha1-qVT5Ma66UI0we78Gnv8MAclhFvc=
120+
109121
supports-color@^5.3.0:
110122
version "5.5.0"
111123
resolved "https://registry.npm.taobao.org/supports-color/download/supports-color-5.5.0.tgz?cache=0&other_urls=https%3A%2F%2Fregistry.npm.taobao.org%2Fsupports-color%2Fdownload%2Fsupports-color-5.5.0.tgz#e2e69a44ac8772f78a1ec0b35b689df6530efc8f"

0 commit comments

Comments
 (0)