1
1
#!/usr/bin/env node
2
2
const path = require ( 'path' ) ;
3
- const fs = require ( 'fs' ) ;
3
+ const fs = require ( 'fs-extra ' ) ;
4
4
const zlib = require ( 'zlib' ) ;
5
5
6
6
const execa = require ( 'execa' ) ;
7
- const meow = require ( 'meow ' ) ;
7
+ const yargs = require ( 'yargs ' ) ;
8
8
const readPkg = require ( 'read-pkg' ) ;
9
9
const requireFromString = require ( 'require-from-string' ) ;
10
10
const tar = require ( 'tar-fs' ) ;
11
- const { fix } = require ( '@commitlint/test ' ) ;
11
+ const tmp = require ( 'tmp ' ) ;
12
12
13
- const builtin = require . resolve ( 'is-builtin-module' ) ;
13
+ tmp . setGracefulCleanup ( ) ;
14
14
15
15
const PRELUDE = `
16
- var _require = require;
16
+ var Module = require('module');
17
+ var originalLoader = Module._load
17
18
18
- require = function(id) {
19
+ Module._load = function(path, parent) {
20
+ if (path.startsWith('.') || Module.builtinModules.includes(path)) {
21
+ return originalLoader.apply(this, arguments);
22
+ }
19
23
var dummy = new Proxy({}, {
20
24
get() {
21
25
return dummy;
22
26
}
23
27
});
24
-
25
- var _isBuiltIn = _require('${ builtin } ');
26
- if (id[0] === '.' || _isBuiltIn(id)) {
27
- return _require(id);
28
- } else {
29
- return dummy;
30
- }
28
+ return dummy;
31
29
};
32
30
` ;
33
31
34
- function main ( cli ) {
32
+ function main ( flags ) {
35
33
if ( ! Proxy ) {
36
34
console
37
35
. warn ( 'Skipping pkg-check, detected missing Proxy support' )
38
36
. process . exit ( 0 ) ;
39
37
}
40
38
41
- const cwd = cli . flags . cwd || process . cwd ( ) ;
39
+ const cwd = flags . cwd || process . cwd ( ) ;
42
40
const skipImport =
43
- typeof cli . flags . skipImport === 'boolean' ? cli . flags . skipImport : false ;
41
+ typeof flags . skipImport === 'boolean' ? flags . skipImport : false ;
44
42
45
43
return readPkg ( { cwd} ) . then ( ( pkg ) => {
46
44
return getTarballFiles ( cwd , { write : ! skipImport } ) . then ( ( tarball ) => {
47
45
return getPackageFiles ( cwd ) . then ( ( pkgFiles ) => {
48
46
let problems = [ ] ;
49
47
50
- if ( ! cli . flags . skipBin ) {
48
+ if ( ! flags . skipBin ) {
51
49
problems = problems . concat (
52
50
pkgFiles . bin
53
51
. filter ( ( binFile ) => tarball . files . indexOf ( binFile ) === - 1 )
@@ -59,18 +57,15 @@ function main(cli) {
59
57
) ;
60
58
}
61
59
62
- if (
63
- ! cli . flags . skipMain &&
64
- tarball . files . indexOf ( pkgFiles . main ) === - 1
65
- ) {
60
+ if ( ! flags . skipMain && tarball . files . indexOf ( pkgFiles . main ) === - 1 ) {
66
61
problems . push ( {
67
62
type : 'main' ,
68
63
file : pkgFiles . main ,
69
64
message : `Required main file ${ pkgFiles . main } not found for ${ pkg . name } ` ,
70
65
} ) ;
71
66
}
72
67
73
- if ( ! cli . flags . skipImport && ! cli . flags . skipMain ) {
68
+ if ( ! flags . skipImport && ! flags . skipMain ) {
74
69
const importable = fileImportable (
75
70
path . join ( tarball . dirname , pkgFiles . main )
76
71
) ;
@@ -94,20 +89,36 @@ function main(cli) {
94
89
} ) ;
95
90
}
96
91
97
- main (
98
- meow ( `
99
- pkg-check
100
-
101
- Check if a package creates valid tarballs
102
-
103
- Options
104
- --skip-main Skip main checks
105
- --skip-bin Skip bin checks
106
- --skip-import Skip import smoke test
107
-
108
- Examples
109
- $ pkg-check
110
- ` )
92
+ main ( yargs
93
+ . options ( {
94
+ cwd : {
95
+ description : 'directory to execute in' ,
96
+ type : 'string' ,
97
+ } ,
98
+ skipMain : {
99
+ default : false ,
100
+ type : 'boolean' ,
101
+ description : 'Skip main checks' ,
102
+ } ,
103
+ skipBin : {
104
+ default : false ,
105
+ type : 'boolean' ,
106
+ description : 'Skip bin checks' ,
107
+ } ,
108
+ skipImport : {
109
+ default : false ,
110
+ type : 'boolean' ,
111
+ description : 'Skip import smoke test' ,
112
+ } ,
113
+ } )
114
+ . scriptName ( 'pkg-check' )
115
+ . usage ( 'pkg-check\n' )
116
+ . usage ( 'Check if a package creates valid tarballs' )
117
+ . example ( '$0' , '' )
118
+ . help ( )
119
+ . version ( )
120
+ . strict ( )
121
+ . argv
111
122
)
112
123
. then ( ( report ) => {
113
124
if ( report . problems . length > 0 ) {
@@ -125,16 +136,20 @@ main(
125
136
. catch ( ( err ) => {
126
137
setTimeout ( ( ) => {
127
138
throw err ;
128
- } ) ;
139
+ } , 0 ) ;
140
+ } ) ;
141
+
142
+ async function getTarballFiles ( source , options ) {
143
+ const tmpDir = tmp . dirSync ( {
144
+ keep : false ,
145
+ unsafeCleanup : true ,
129
146
} ) ;
147
+ const cwd = tmpDir . name ;
148
+ await fs . copy ( source , cwd ) ;
149
+ const tarball = path . join ( cwd , 'test-archive.tgz' ) ;
150
+ await execa ( 'yarn' , [ 'pack' , '--filename' , tarball ] , { cwd : source } ) ;
130
151
131
- function getTarballFiles ( source , options ) {
132
- return fix
133
- . bootstrap ( source )
134
- . then ( ( cwd ) =>
135
- execa ( 'npm' , [ 'pack' ] , { cwd} ) . then ( ( cp ) => path . join ( cwd , cp . stdout ) )
136
- )
137
- . then ( ( tarball ) => getArchiveFiles ( tarball , options ) ) ;
152
+ return getArchiveFiles ( tarball , options ) ;
138
153
}
139
154
140
155
function getArchiveFiles ( filePath , options ) {
@@ -173,7 +188,7 @@ function getPackageFiles(source) {
173
188
174
189
function normalizeMainPath ( mainPath ) {
175
190
const norm = path . normalize ( mainPath ) ;
176
- if ( norm [ norm . length - 1 ] === '/' ) {
191
+ if ( norm [ norm . length - 1 ] === path . sep ) {
177
192
return `${ norm } index.js` ;
178
193
}
179
194
return norm ;
0 commit comments