11#!/usr/bin/env node
22const path = require ( 'path' ) ;
3- const fs = require ( 'fs' ) ;
3+ const fs = require ( 'fs-extra ' ) ;
44const zlib = require ( 'zlib' ) ;
55
66const execa = require ( 'execa' ) ;
7- const meow = require ( 'meow ' ) ;
7+ const yargs = require ( 'yargs ' ) ;
88const readPkg = require ( 'read-pkg' ) ;
99const requireFromString = require ( 'require-from-string' ) ;
1010const tar = require ( 'tar-fs' ) ;
11- const { fix } = require ( '@commitlint/test ' ) ;
11+ const tmp = require ( 'tmp ' ) ;
1212
13- const builtin = require . resolve ( 'is-builtin-module' ) ;
13+ tmp . setGracefulCleanup ( ) ;
1414
1515const PRELUDE = `
16- var _require = require;
16+ var Module = require('module');
17+ var originalLoader = Module._load
1718
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+ }
1923 var dummy = new Proxy({}, {
2024 get() {
2125 return dummy;
2226 }
2327 });
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;
3129};
3230` ;
3331
34- function main ( cli ) {
32+ function main ( flags ) {
3533 if ( ! Proxy ) {
3634 console
3735 . warn ( 'Skipping pkg-check, detected missing Proxy support' )
3836 . process . exit ( 0 ) ;
3937 }
4038
41- const cwd = cli . flags . cwd || process . cwd ( ) ;
39+ const cwd = flags . cwd || process . cwd ( ) ;
4240 const skipImport =
43- typeof cli . flags . skipImport === 'boolean' ? cli . flags . skipImport : false ;
41+ typeof flags . skipImport === 'boolean' ? flags . skipImport : false ;
4442
4543 return readPkg ( { cwd} ) . then ( ( pkg ) => {
4644 return getTarballFiles ( cwd , { write : ! skipImport } ) . then ( ( tarball ) => {
4745 return getPackageFiles ( cwd ) . then ( ( pkgFiles ) => {
4846 let problems = [ ] ;
4947
50- if ( ! cli . flags . skipBin ) {
48+ if ( ! flags . skipBin ) {
5149 problems = problems . concat (
5250 pkgFiles . bin
5351 . filter ( ( binFile ) => tarball . files . indexOf ( binFile ) === - 1 )
@@ -59,18 +57,15 @@ function main(cli) {
5957 ) ;
6058 }
6159
62- if (
63- ! cli . flags . skipMain &&
64- tarball . files . indexOf ( pkgFiles . main ) === - 1
65- ) {
60+ if ( ! flags . skipMain && tarball . files . indexOf ( pkgFiles . main ) === - 1 ) {
6661 problems . push ( {
6762 type : 'main' ,
6863 file : pkgFiles . main ,
6964 message : `Required main file ${ pkgFiles . main } not found for ${ pkg . name } ` ,
7065 } ) ;
7166 }
7267
73- if ( ! cli . flags . skipImport && ! cli . flags . skipMain ) {
68+ if ( ! flags . skipImport && ! flags . skipMain ) {
7469 const importable = fileImportable (
7570 path . join ( tarball . dirname , pkgFiles . main )
7671 ) ;
@@ -94,20 +89,36 @@ function main(cli) {
9489 } ) ;
9590}
9691
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
111122)
112123 . then ( ( report ) => {
113124 if ( report . problems . length > 0 ) {
@@ -125,16 +136,20 @@ main(
125136 . catch ( ( err ) => {
126137 setTimeout ( ( ) => {
127138 throw err ;
128- } ) ;
139+ } , 0 ) ;
140+ } ) ;
141+
142+ async function getTarballFiles ( source , options ) {
143+ const tmpDir = tmp . dirSync ( {
144+ keep : false ,
145+ unsafeCleanup : true ,
129146 } ) ;
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 } ) ;
130151
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 ) ;
138153}
139154
140155function getArchiveFiles ( filePath , options ) {
@@ -173,7 +188,7 @@ function getPackageFiles(source) {
173188
174189function normalizeMainPath ( mainPath ) {
175190 const norm = path . normalize ( mainPath ) ;
176- if ( norm [ norm . length - 1 ] === '/' ) {
191+ if ( norm [ norm . length - 1 ] === path . sep ) {
177192 return `${ norm } index.js` ;
178193 }
179194 return norm ;
0 commit comments