Skip to content

Commit 53f5cee

Browse files
committed
[Refactor] use pargs package
1 parent e2c3aab commit 53f5cee

File tree

4 files changed

+43
-142
lines changed

4 files changed

+43
-142
lines changed

.eslintrc

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
"func-style": 0,
1010
"max-nested-callbacks": 0,
1111
"no-console": 0,
12+
"no-extra-parens": 0,
1213
"no-invalid-this": 1,
1314
"sort-keys": 0,
1415
},
@@ -23,11 +24,5 @@
2324
"no-process-exit": 0,
2425
},
2526
},
26-
{
27-
"files": "pargs.mjs",
28-
"rules": {
29-
"max-lines-per-function": 0,
30-
},
31-
},
3227
],
3328
}

api.mjs

Lines changed: 41 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,17 @@
11
#!/usr/bin/env node
22

33
import 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';
512
import {
613
existsSync,
714
readdirSync,
8-
readFileSync,
915
statSync,
1016
} from 'fs';
1117
import { spawn } from 'child_process';
@@ -14,46 +20,45 @@ import inspect from 'object-inspect';
1420
import major from 'semver/functions/major.js';
1521
import { createRequire } from 'module';
1622

17-
import pargs from './pargs.mjs';
23+
import pargs from 'pargs';
1824

1925
const require = createRequire(import.meta.url);
2026

2127
const { version } = require('./package.json');
2228
const majorV = major(version);
2329

24-
const help = readFileSync(join(import.meta.dirname, './help.txt'), 'utf8');
25-
2630
const {
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

5459
let 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]} */
5964
function makeEntries(name) {
@@ -65,7 +70,7 @@ const moduleNames = positionals.map(makeEntries);
6570
/** @type {{ name?: string, main?: string | false, exports?: Record<string, unknown | unknown[]> }} */
6671
let pkg;
6772
if (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(/\/index$/, '')}`;
316321
st.equal(lhs, lhsGuess, `subpackage \`${subPackage}\` LHS + \`${lhs}\` resolves to \`${lhsGuess}\``);

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@
4848
"dependencies": {
4949
"object-inspect": "^1.13.4",
5050
"object.groupby": "^1.0.3",
51+
"pargs": "^1.0.0",
5152
"semver": "^7.7.2",
5253
"tape": "^5.9.0"
5354
},

pargs.mjs

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

0 commit comments

Comments
 (0)