Skip to content

Commit e9e4869

Browse files
committed
feat: init package structure
1 parent 02f96cf commit e9e4869

File tree

14 files changed

+147
-75
lines changed

14 files changed

+147
-75
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,4 @@ This module helps to ensure using of [npm](https://github.com/npm/cli) or [yarn]
99
- <https://github.com/timoxley/npm-which>
1010
- <https://github.com/npm/cli/issues/481>
1111
- <https://stackoverflow.com/questions/46725374/how-to-run-a-script-before-installing-any-npm-module>
12+
- <https://github.com/yarnpkg/yarn/issues/5063>

bin/cli.js

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
#!/usr/bin/env node
2+
3+
const chalk = require('chalk');
4+
const { debug } = require('../lib/utils');
5+
6+
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')}).
8+
9+
${chalk.green('Example')}
10+
$ check-npm-client
11+
$ check-npm-client --npm-only
12+
$ check-npm-client --yarn-only
13+
`;
14+
15+
const args = process.argv.slice(2);
16+
const script = args[0];
17+
18+
debug('cmd:', process.cwd());
19+
debug('script:', script);
20+
21+
// check only for the first params to decide which to use
22+
switch (script) {
23+
// allow npm to run script only
24+
case '--npm-only':
25+
case 'npmOnly':
26+
require('../lib/check-npm')();
27+
break;
28+
// allow yarn to run script only
29+
case '--yarn-only':
30+
case 'yarnOnly':
31+
require('../lib/check-yarn')();
32+
break;
33+
// auto detect if yarn or npm is more suitable according to lock files
34+
default:
35+
if (script !== undefined) {
36+
console.log(
37+
`${chalk.yellow('Unrecognized Command')} "${chalk.red(script)}"`
38+
);
39+
}
40+
console.log(helpText);
41+
}

check.js

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

lib/check-npm.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
// check if the current client is npm
2+
3+
const { debug } = require('./utils');

lib/check-yarn.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
// check if the current client is yarn
2+
3+
const { debug } = require('./utils');
4+
5+
// TODO: to be implemented

index.js renamed to lib/utils.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1+
const debug = require('debug')('check-npm-client');
12
const findProcess = require('find-process');
23

3-
(async (pid) => {
4+
/** find all ancestor process info by pid. If failed to failed to found ppid, then exit. */
5+
async function findAncestorProcessInfo (pid) {
46
let id = pid;
57
let finished = false;
68
let iteration = 1;
@@ -17,4 +19,6 @@ const findProcess = require('find-process');
1719
// TODO: check npm client here
1820
}
1921
}
20-
})(process.pid);
22+
}
23+
24+
exports.debug = debug;

package.json

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,25 @@
11
{
22
"name": "check-npm-client",
33
"version": "1.0.0",
4-
"main": "index.js",
54
"author": "aprilandjan",
65
"license": "MIT",
6+
"files": [
7+
"bin"
8+
],
9+
"engines": {
10+
"node": ">=7.6.0"
11+
},
12+
"bin": {
13+
"check-npm-client": "./bin/cli.js"
14+
},
715
"scripts": {
8-
"preinstall": "node check.js",
916
"check": "node check.js"
1017
},
1118
"dependencies": {
19+
"chalk": "^3.0.0",
20+
"debug": "^4.1.1",
1221
"find-process": "^1.4.2"
22+
},
23+
"devDependencies": {
1324
}
1425
}

test-process.js

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

test/fixture/auto/package.json

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"name": "check-npm-client-auto",
3+
"version": "1.0.0",
4+
"scripts": {
5+
"preinstall": "../../../bin/cli"
6+
}
7+
}

test/fixture/npm-only/package.json

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"name": "check-npm-client-npm-only",
3+
"version": "1.0.0",
4+
"scripts": {
5+
"preinstall": "../../../bin/cli --npm-only"
6+
}
7+
}

test/fixture/yarn-only/package.json

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"name": "check-npm-client-yarn-only",
3+
"version": "1.0.0",
4+
"scripts": {
5+
"preinstall": "../../../bin/cli --yarn-only"
6+
}
7+
}

tested-child.js

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

utils.js

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

yarn.lock

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,25 @@
22
# yarn lockfile v1
33

44

5+
"@types/color-name@^1.1.1":
6+
version "1.1.1"
7+
resolved "https://registry.npm.taobao.org/@types/color-name/download/@types/color-name-1.1.1.tgz#1c1261bbeaa10a8055bbc5d8ab84b7b2afc846a0"
8+
integrity sha1-HBJhu+qhCoBVu8XYq4S3sq/IRqA=
9+
510
ansi-styles@^3.2.1:
611
version "3.2.1"
712
resolved "https://registry.npm.taobao.org/ansi-styles/download/ansi-styles-3.2.1.tgz?cache=0&sync_timestamp=1573557628456&other_urls=https%3A%2F%2Fregistry.npm.taobao.org%2Fansi-styles%2Fdownload%2Fansi-styles-3.2.1.tgz#41fbb20243e50b12be0f04b8dedbf07520ce841d"
813
dependencies:
914
color-convert "^1.9.0"
1015

16+
ansi-styles@^4.1.0:
17+
version "4.2.0"
18+
resolved "https://registry.npm.taobao.org/ansi-styles/download/ansi-styles-4.2.0.tgz?cache=0&sync_timestamp=1573557674483&other_urls=https%3A%2F%2Fregistry.npm.taobao.org%2Fansi-styles%2Fdownload%2Fansi-styles-4.2.0.tgz#5681f0dcf7ae5880a7841d8831c4724ed9cc0172"
19+
integrity sha1-VoHw3PeuWICnhB2IMcRyTtnMAXI=
20+
dependencies:
21+
"@types/color-name" "^1.1.1"
22+
color-convert "^2.0.1"
23+
1124
chalk@^2.0.1:
1225
version "2.4.2"
1326
resolved "https://registry.npm.taobao.org/chalk/download/chalk-2.4.2.tgz?cache=0&sync_timestamp=1573282918610&other_urls=https%3A%2F%2Fregistry.npm.taobao.org%2Fchalk%2Fdownload%2Fchalk-2.4.2.tgz#cd42541677a54333cf541a49108c1432b44c9424"
@@ -16,16 +29,36 @@ chalk@^2.0.1:
1629
escape-string-regexp "^1.0.5"
1730
supports-color "^5.3.0"
1831

32+
chalk@^3.0.0:
33+
version "3.0.0"
34+
resolved "https://registry.npm.taobao.org/chalk/download/chalk-3.0.0.tgz?cache=0&sync_timestamp=1573282918610&other_urls=https%3A%2F%2Fregistry.npm.taobao.org%2Fchalk%2Fdownload%2Fchalk-3.0.0.tgz#3f73c2bf526591f574cc492c51e2456349f844e4"
35+
integrity sha1-P3PCv1JlkfV0zEksUeJFY0n4ROQ=
36+
dependencies:
37+
ansi-styles "^4.1.0"
38+
supports-color "^7.1.0"
39+
1940
color-convert@^1.9.0:
2041
version "1.9.3"
2142
resolved "https://registry.npm.taobao.org/color-convert/download/color-convert-1.9.3.tgz?cache=0&sync_timestamp=1566248870121&other_urls=https%3A%2F%2Fregistry.npm.taobao.org%2Fcolor-convert%2Fdownload%2Fcolor-convert-1.9.3.tgz#bb71850690e1f136567de629d2d5471deda4c1e8"
2243
dependencies:
2344
color-name "1.1.3"
2445

46+
color-convert@^2.0.1:
47+
version "2.0.1"
48+
resolved "https://registry.npm.taobao.org/color-convert/download/color-convert-2.0.1.tgz#72d3a68d598c9bdb3af2ad1e84f21d896abd4de3"
49+
integrity sha1-ctOmjVmMm9s68q0ehPIdiWq9TeM=
50+
dependencies:
51+
color-name "~1.1.4"
52+
2553
color-name@1.1.3:
2654
version "1.1.3"
2755
resolved "https://registry.npm.taobao.org/color-name/download/color-name-1.1.3.tgz#a7d0558bd89c42f795dd42328f740831ca53bc25"
2856

57+
color-name@~1.1.4:
58+
version "1.1.4"
59+
resolved "https://registry.npm.taobao.org/color-name/download/color-name-1.1.4.tgz#c2a09a87acbde69543de6f63fa3995c826c536a2"
60+
integrity sha1-wqCah6y95pVD3m9j+jmVyCbFNqI=
61+
2962
commander@^2.11.0:
3063
version "2.20.3"
3164
resolved "https://registry.npm.taobao.org/commander/download/commander-2.20.3.tgz?cache=0&sync_timestamp=1573464028535&other_urls=https%3A%2F%2Fregistry.npm.taobao.org%2Fcommander%2Fdownload%2Fcommander-2.20.3.tgz#fd485e84c03eb4881c20722ba48035e8531aeb33"
@@ -36,6 +69,13 @@ debug@^2.6.8:
3669
dependencies:
3770
ms "2.0.0"
3871

72+
debug@^4.1.1:
73+
version "4.1.1"
74+
resolved "https://registry.npm.taobao.org/debug/download/debug-4.1.1.tgz#3b72260255109c6b589cee050f1d516139664791"
75+
integrity sha1-O3ImAlUQnGtYnO4FDx1RYTlmR5E=
76+
dependencies:
77+
ms "^2.1.1"
78+
3979
escape-string-regexp@^1.0.5:
4080
version "1.0.5"
4181
resolved "https://registry.npm.taobao.org/escape-string-regexp/download/escape-string-regexp-1.0.5.tgz#1b61c0562190a8dff6ae3bb2cf0200ca130b86d4"
@@ -52,12 +92,29 @@ has-flag@^3.0.0:
5292
version "3.0.0"
5393
resolved "https://registry.npm.taobao.org/has-flag/download/has-flag-3.0.0.tgz#b5d454dc2199ae225699f3467e5a07f3b955bafd"
5494

95+
has-flag@^4.0.0:
96+
version "4.0.0"
97+
resolved "https://registry.npm.taobao.org/has-flag/download/has-flag-4.0.0.tgz#944771fd9c81c81265c4d6941860da06bb59479b"
98+
integrity sha1-lEdx/ZyByBJlxNaUGGDaBrtZR5s=
99+
55100
ms@2.0.0:
56101
version "2.0.0"
57102
resolved "https://registry.npm.taobao.org/ms/download/ms-2.0.0.tgz?cache=0&other_urls=https%3A%2F%2Fregistry.npm.taobao.org%2Fms%2Fdownload%2Fms-2.0.0.tgz#5608aeadfc00be6c2901df5f9861788de0d597c8"
58103

104+
ms@^2.1.1:
105+
version "2.1.2"
106+
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"
107+
integrity sha1-0J0fNXtEP0kzgqjrPM0YOHKuYAk=
108+
59109
supports-color@^5.3.0:
60110
version "5.5.0"
61111
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"
62112
dependencies:
63113
has-flag "^3.0.0"
114+
115+
supports-color@^7.1.0:
116+
version "7.1.0"
117+
resolved "https://registry.npm.taobao.org/supports-color/download/supports-color-7.1.0.tgz?cache=0&other_urls=https%3A%2F%2Fregistry.npm.taobao.org%2Fsupports-color%2Fdownload%2Fsupports-color-7.1.0.tgz#68e32591df73e25ad1c4b49108a2ec507962bfd1"
118+
integrity sha1-aOMlkd9z4lrRxLSRCKLsUHliv9E=
119+
dependencies:
120+
has-flag "^4.0.0"

0 commit comments

Comments
 (0)