Skip to content

Commit 870e6aa

Browse files
committed
fix(parse): support native ESM mode;
- do not build `uvu/parse` from source since it needs different contents, like uvu/run - rely on `require` directly for loading file paths (revert 4cedf40)
1 parent 4cedf40 commit 870e6aa

File tree

6 files changed

+61
-17
lines changed

6 files changed

+61
-17
lines changed

.gitignore

-1
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,4 @@ node_modules
88

99
/dist
1010
/assert
11-
/parse
1211
/diff

package.json

-1
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,6 @@
4848
],
4949
"modes": {
5050
"diff": "src/diff.js",
51-
"parse": "src/parse.js",
5251
"assert": "src/assert.js",
5352
"default": "src/index.js"
5453
},

src/parse.d.ts parse/index.d.ts

File renamed without changes.

parse/index.js

+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
// @ts-check
2+
const { readdir, stat } = require('fs');
3+
const { resolve, join } = require('path');
4+
const { promisify } = require('util');
5+
6+
const ls = promisify(readdir);
7+
const toStat = promisify(stat);
8+
const toRegex = x => new RegExp(x, 'i');
9+
10+
async function parse(dir, pattern, opts = {}) {
11+
if (pattern) pattern = toRegex(pattern);
12+
else if (dir) pattern = /(((?:[^\/]*(?:\/|$))*)[\\\/])?\w+\.([mc]js|[jt]sx?)$/;
13+
else pattern = /((\/|^)(tests?|__tests?__)\/.*|\.(tests?|spec)|^\/?tests?)\.([mc]js|[jt]sx?)$/i;
14+
dir = resolve(opts.cwd || '.', dir || '.');
15+
16+
let suites = [];
17+
let requires = [].concat(opts.require || []).filter(Boolean);
18+
let ignores = ['^.git', 'node_modules'].concat(opts.ignore || []).map(toRegex);
19+
20+
requires.forEach(name => {
21+
try { return require(name) }
22+
catch (e) { throw new Error(`Cannot find module "${name}"`) }
23+
});
24+
25+
// NOTE: Node 8.x support
26+
// @modified lukeed/totalist
27+
await (async function collect(d, p) {
28+
await ls(d).then(files => {
29+
return Promise.all(
30+
files.map(async str => {
31+
let name = join(p, str);
32+
for (let i = ignores.length; i--;) {
33+
if (ignores[i].test(name)) return;
34+
}
35+
36+
let file = join(d, str);
37+
let stats = await toStat(file);
38+
if (stats.isDirectory()) return collect(file, name);
39+
else if (pattern.test(name)) suites.push({ name, file });
40+
})
41+
);
42+
});
43+
})(dir, '');
44+
45+
suites.sort((a, b) => a.name.localeCompare(b.name));
46+
47+
return { dir, suites, requires: requires.length > 0 };
48+
}
49+
50+
exports.parse = parse;

src/parse.js parse/index.mjs

+9-13
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,12 @@
1-
import { readdir, stat, existsSync } from 'fs';
1+
import { readdir, stat } from 'fs';
2+
import { createRequire } from 'module';
23
import { resolve, join } from 'path';
34
import { promisify } from 'util';
45

56
const ls = promisify(readdir);
67
const toStat = promisify(stat);
78
const toRegex = x => new RegExp(x, 'i');
89

9-
function exists(dep) {
10-
if (existsSync(dep)) return dep;
11-
try { return require.resolve(dep) }
12-
catch (err) { return false }
13-
}
14-
1510
export async function parse(dir, pattern, opts = {}) {
1611
if (pattern) pattern = toRegex(pattern);
1712
else if (dir) pattern = /(((?:[^\/]*(?:\/|$))*)[\\\/])?\w+\.([mc]js|[jt]sx?)$/;
@@ -22,12 +17,13 @@ export async function parse(dir, pattern, opts = {}) {
2217
let requires = [].concat(opts.require || []).filter(Boolean);
2318
let ignores = ['^.git', 'node_modules'].concat(opts.ignore || []).map(toRegex);
2419

25-
requires.forEach(name => {
26-
let tmp = exists(name);
27-
if (tmp) return require(tmp);
28-
if (tmp = exists(resolve(name))) return require(tmp);
29-
throw new Error(`Cannot find module '${name}'`);
30-
});
20+
if (requires.length) {
21+
let $require = createRequire(import.meta.url);
22+
requires.forEach(name => {
23+
try { return $require(name) }
24+
catch (e) { throw new Error(`Cannot find module "${name}"`) }
25+
});
26+
}
3127

3228
// NOTE: Node 8.x support
3329
// @modified lukeed/totalist

test/parse.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { suite } from 'uvu';
22
import { readdirSync } from 'fs';
33
import { isAbsolute } from 'path';
44
import * as assert from 'uvu/assert';
5-
import * as $ from '../src/parse';
5+
import * as $ from '../parse';
66

77
const FILES = readdirSync(__dirname);
88

@@ -132,7 +132,7 @@ requires('should throw on invalid value(s)', async () => {
132132
assert.unreachable('should have thrown');
133133
} catch (err) {
134134
assert.instance(err, Error);
135-
assert.match(err.message, `Cannot find module 'foobar'`);
135+
assert.match(err.message, `Cannot find module "foobar"`);
136136
}
137137
});
138138

0 commit comments

Comments
 (0)