11#!/usr/bin/env node
22
33import test from 'tape' ;
4- import path , { join } from 'path' ;
4+ import {
5+ basename ,
6+ dirname ,
7+ extname ,
8+ join ,
9+ relative ,
10+ resolve ,
11+ } from 'path' ;
512import {
613 existsSync ,
714 readdirSync ,
8- readFileSync ,
915 statSync ,
1016} from 'fs' ;
1117import { spawn } from 'child_process' ;
@@ -14,46 +20,45 @@ import inspect from 'object-inspect';
1420import major from 'semver/functions/major.js' ;
1521import { createRequire } from 'module' ;
1622
17- import pargs from './ pargs.mjs ' ;
23+ import pargs from 'pargs' ;
1824
1925const require = createRequire ( import . meta. url ) ;
2026
2127const { version } = require ( './package.json' ) ;
2228const majorV = major ( version ) ;
2329
24- const help = readFileSync ( join ( import . meta. dirname , './help.txt' ) , 'utf8' ) ;
25-
2630const {
31+ help,
2732 positionals,
2833 values : {
2934 type,
3035 'skip-shim-returns-polyfill' : skipShimPolyfill ,
3136 'skip-auto-shim' : skipAutoShim ,
3237 'ignore-dirs' : rawIgnoreDirs ,
3338 } ,
34- // eslint-disable-next-line no-extra-parens, max-len
35- } = /** @type {{ positionals: string[], values: { type: 'method' | 'function' | 'property' | 'constructor' | 'multi', 'skip-shim-returns-polyfill': boolean, 'skip-auto-shim': boolean, 'ignore-dirs': string[], multi: boolean } } } */ (
36- pargs ( help , import . meta. filename , {
37- allowPositionals : true ,
38- options : {
39- type : {
40- type : 'string' ,
41- default : 'method' ,
42- } ,
43- 'skip-shim-returns-polyfill' : { type : 'boolean' } ,
44- 'skip-auto-shim' : { type : 'boolean' } ,
45- 'ignore-dirs' : {
46- type : 'string' ,
47- multiple : true ,
48- default : [ ] ,
49- } ,
39+
40+ } = await pargs ( import . meta. filename , {
41+ allowPositionals : true ,
42+ options : {
43+ type : {
44+ type : 'string' ,
45+ default : 'method' ,
46+ } ,
47+ 'skip-shim-returns-polyfill' : { type : 'boolean' } ,
48+ 'skip-auto-shim' : { type : 'boolean' } ,
49+ 'ignore-dirs' : {
50+ type : 'string' ,
51+ multiple : true ,
52+ default : [ ] ,
5053 } ,
51- } )
52- ) ;
54+ } ,
55+ } ) ;
56+
57+ await help ( ) ;
5358
5459let isMulti = type === 'multi' ;
5560
56- const ignoreDirs = [ 'node_modules' , 'coverage' , 'helpers' , 'test' , 'aos' ] . concat ( rawIgnoreDirs . flatMap ( ( x ) => x . split ( ',' ) ) ) ;
61+ const ignoreDirs = [ 'node_modules' , 'coverage' , 'helpers' , 'test' , 'aos' ] . concat ( /** @type { string[] } */ ( rawIgnoreDirs ) . flatMap ( ( x ) => x . split ( ',' ) ) ) ;
5762
5863/** @type {<T extends string>(name: T) => [T, T] } */
5964function makeEntries ( name ) {
@@ -65,7 +70,7 @@ const moduleNames = positionals.map(makeEntries);
6570/** @type {{ name?: string, main?: string | false, exports?: Record<string, unknown | unknown[]> } } */
6671let pkg ;
6772if ( moduleNames . length < 1 ) {
68- const packagePath = path . join ( process . cwd ( ) , 'package.json' ) ;
73+ const packagePath = join ( process . cwd ( ) , 'package.json' ) ;
6974 if ( ! existsSync ( packagePath ) ) {
7075 console . error ( 'Error: No package.json found in the current directory' ) ;
7176 console . error ( 'at least one module name is required when not run in a directory with a package.json' ) ;
@@ -76,9 +81,9 @@ if (moduleNames.length < 1) {
7681 console . error ( 'Error: No "name" found in package.json' ) ;
7782 process . exit ( 2 ) ;
7883 }
79- moduleNames . push ( [ `${ pkg . name } (current directory)` , [ path . join ( process . cwd ( ) , pkg . main || '' ) , process . cwd ( ) ] ] ) ;
84+ moduleNames . push ( [ `${ pkg . name } (current directory)` , [ join ( process . cwd ( ) , pkg . main || '' ) , process . cwd ( ) ] ] ) ;
8085
81- const mainIsJSON = path . extname ( require . resolve ( process . cwd ( ) ) ) === '.json' ;
86+ const mainIsJSON = extname ( require . resolve ( process . cwd ( ) ) ) === '.json' ;
8287 if ( isMulti && ! mainIsJSON ) {
8388 console . error ( 'Error: --type=multi requires package.json main to be a JSON file' ) ;
8489 process . exit ( 3 ) ;
@@ -118,9 +123,9 @@ const testAuto = function testAutoModule(t, prefix, packageDir, asMulti) {
118123 st . comment ( `# SKIP ${ msg } ` ) ;
119124 st . end ( ) ;
120125 } else {
121- require ( path . join ( packageDir , '/auto' ) ) ;
126+ require ( join ( packageDir , '/auto' ) ) ;
122127 st . comment ( `${ msg } (pass \`--skip-auto-shim\` to skip this test)` ) ;
123- const proc = spawn ( path . join ( import . meta. dirname , asMulti ? 'multiAutoTest.js' : 'autoTest.js' ) , [ ] , { cwd : packageDir , stdio : 'inherit' } ) ;
128+ const proc = spawn ( join ( import . meta. dirname , asMulti ? 'multiAutoTest.js' : 'autoTest.js' ) , [ ] , { cwd : packageDir , stdio : 'inherit' } ) ;
124129 st . plan ( 1 ) ;
125130 proc . on ( 'close' , ( code ) => {
126131 st . equal ( code , 0 , 'auto invokes shim' ) ;
@@ -140,7 +145,7 @@ const doValidation = function doActualValidation(t, packageDir, name) {
140145 // eslint-disable-next-line no-extra-parens
141146 const getPolyfill = /** @type {Function } */ ( requireOrEvalError ( `${ packageDir } /polyfill` ) ) ;
142147
143- const prefix = isMulti ? `${ path . basename ( packageDir ) } : ` : '' ;
148+ const prefix = isMulti ? `${ basename ( packageDir ) } : ` : '' ;
144149
145150 t . test ( `${ prefix } export` , ( st ) => {
146151 if ( type === 'property' ) {
@@ -283,7 +288,7 @@ const validateModule = function validateAPIModule(t, nameOrFilePaths) {
283288 t . ok ( implementation instanceof EvalError , 'root lacks an `implementation` module' ) ;
284289
285290 subPackages . forEach ( ( subPackage ) => {
286- const subPackageDir = path . join ( path . dirname ( name ) , subPackage ) ;
291+ const subPackageDir = join ( dirname ( name ) , subPackage ) ;
287292 doValidation ( t , subPackageDir , subPackageDir ) ;
288293 } ) ;
289294
@@ -305,12 +310,12 @@ const validateModule = function validateAPIModule(t, nameOrFilePaths) {
305310 // eslint-disable-next-line no-extra-parens
306311 const rhs = /** @type {string } */ ( exps [ lhs ] ) ;
307312 st . equal ( typeof rhs , 'string' , 'right-hand side of `exports` is a string' ) ;
308- const resolved = path . resolve ( path . join ( packageDir , rhs ) ) ;
309- const lhsGuess = `./${ path . relative (
313+ const resolved = resolve ( join ( packageDir , rhs ) ) ;
314+ const lhsGuess = `./${ relative (
310315 packageDir ,
311- path . join (
312- path . dirname ( resolved ) ,
313- path . basename ( resolved , path . extname ( resolved ) ) ,
316+ join (
317+ dirname ( resolved ) ,
318+ basename ( resolved , extname ( resolved ) ) ,
314319 ) ,
315320 ) . replace ( / \/ i n d e x $ / , '' ) } `;
316321 st . equal ( lhs , lhsGuess , `subpackage \`${ subPackage } \` LHS + \`${ lhs } \` resolves to \`${ lhsGuess } \`` ) ;
0 commit comments