Skip to content

Commit 2471371

Browse files
refactor: replace npm glob with node fs glob. (#53)
1 parent 30572ae commit 2471371

4 files changed

Lines changed: 139 additions & 14 deletions

File tree

package-lock.json

Lines changed: 9 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@knighted/module",
3-
"version": "1.5.1",
3+
"version": "1.5.2",
44
"description": "Bidirectional transform for ES modules and CommonJS.",
55
"type": "module",
66
"main": "dist/module.js",
@@ -75,7 +75,6 @@
7575
"typescript": "^5.9.3"
7676
},
7777
"dependencies": {
78-
"glob": "^13.0.6",
7978
"magic-string": "^0.30.21",
8079
"oxc-parser": "^0.116.0",
8180
"periscopic": "^4.0.2"

src/cli.ts

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,8 @@ import {
55
stderr as defaultStderr,
66
} from 'node:process'
77
import { parseArgs } from 'node:util'
8-
import { readFile, mkdir, writeFile } from 'node:fs/promises'
8+
import { readFile, mkdir, writeFile, glob } from 'node:fs/promises'
99
import { dirname, resolve, relative, join, basename } from 'node:path'
10-
import { glob } from 'glob'
1110

1211
import type { TemplateLiteral } from '@oxc-project/types'
1312

@@ -539,14 +538,14 @@ const normalizeSourceMapArgv = (argv: string[]) => {
539538
const expandFiles = async (patterns: string[], cwd: string, ignore?: string[]) => {
540539
const files = new Set<string>()
541540
for (const pattern of patterns) {
542-
const matches = await glob(pattern, {
541+
for await (const match of glob(pattern, {
543542
cwd,
544-
absolute: true,
545-
nodir: true,
546-
windowsPathsNoEscape: true,
547-
ignore,
548-
})
549-
for (const m of matches) files.add(resolve(m))
543+
exclude: ignore,
544+
withFileTypes: true,
545+
})) {
546+
if (match.isDirectory()) continue
547+
files.add(resolve(match.parentPath, match.name))
548+
}
550549
}
551550
return [...files]
552551
}

test/cli.ts

Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,59 @@ test('--ignore excludes glob matches', async () => {
123123
}
124124
})
125125

126+
test('glob expansion excludes directories', async () => {
127+
const temp = await mkdtemp(join(tmpdir(), 'module-cli-glob-nodir-'))
128+
const nested = join(temp, 'nested')
129+
const input = join(temp, 'input.cjs')
130+
131+
await mkdir(nested, { recursive: true })
132+
await copyFile(fixture, input)
133+
134+
try {
135+
const result = runCli(['--list', '--target', 'module', '--cwd', temp, '*'])
136+
137+
assert.equal(result.status, 0)
138+
assert.ok(result.stdout.includes('input.cjs'))
139+
assert.ok(!result.stdout.includes('nested'))
140+
} finally {
141+
await rm(temp, { recursive: true, force: true })
142+
}
143+
})
144+
145+
test('windows: backslash glob and ignore patterns are honored', async () => {
146+
if (process.platform !== 'win32') return
147+
148+
const temp = await mkdtemp(join(tmpdir(), 'module-cli-win-glob-'))
149+
const srcDir = join(temp, 'src')
150+
const keep = join(srcDir, 'keep.cjs')
151+
const ignoredDir = join(temp, 'node_modules', 'pkg')
152+
const ignored = join(ignoredDir, 'index.cjs')
153+
154+
await mkdir(srcDir, { recursive: true })
155+
await mkdir(ignoredDir, { recursive: true })
156+
await copyFile(fixture, keep)
157+
await copyFile(fixture, ignored)
158+
159+
try {
160+
const result = runCli([
161+
'--list',
162+
'--target',
163+
'module',
164+
'--cwd',
165+
temp,
166+
'src\\**\\*.cjs',
167+
'--ignore',
168+
'node_modules\\**',
169+
])
170+
171+
assert.equal(result.status, 0)
172+
assert.ok(result.stdout.includes('keep.cjs'))
173+
assert.ok(!result.stdout.includes('node_modules'))
174+
} finally {
175+
await rm(temp, { recursive: true, force: true })
176+
}
177+
})
178+
126179
test('-H error exits on dual package hazard', async () => {
127180
const temp = await mkdtemp(join(tmpdir(), 'module-cli-dual-hazard-'))
128181
const file = join(temp, 'entry.mjs')
@@ -670,6 +723,74 @@ test('globals-only pre-tsc flow matches README example', async () => {
670723
}
671724
})
672725

726+
test('globals-only pre-tsc flow with README glob pattern', async () => {
727+
const temp = await mkdtemp(join(tmpdir(), 'module-cli-pre-tsc-glob-'))
728+
const srcDir = join(temp, 'src')
729+
const nestedDir = join(srcDir, 'nested')
730+
const file = join(nestedDir, 'index.ts')
731+
732+
await mkdir(nestedDir, { recursive: true })
733+
await writeFile(
734+
join(temp, 'tsconfig.json'),
735+
JSON.stringify(
736+
{
737+
compilerOptions: {
738+
target: 'ES2020',
739+
module: 'commonjs',
740+
outDir: 'dist',
741+
strict: false,
742+
esModuleInterop: true,
743+
types: ['node'],
744+
typeRoots: [join(projectRoot, 'node_modules', '@types')],
745+
},
746+
include: ['src'],
747+
},
748+
null,
749+
2,
750+
),
751+
'utf8',
752+
)
753+
await writeFile(
754+
file,
755+
[
756+
"import fs from 'node:fs'",
757+
'export const meta = import.meta.url',
758+
'export const size = fs.statSync(__filename).size',
759+
'',
760+
].join('\n'),
761+
'utf8',
762+
)
763+
764+
try {
765+
const before = spawnSync(process.execPath, [tscBin, '-p', temp], { encoding: 'utf8' })
766+
assert.notEqual(before.status, 0)
767+
768+
const result = runCli(
769+
[
770+
'--target',
771+
'commonjs',
772+
'--transform-syntax',
773+
'globals-only',
774+
'--ignore',
775+
'node_modules/**',
776+
'--in-place',
777+
'src/**/*.{ts,js,mts,cts}',
778+
],
779+
undefined,
780+
{ cwd: temp },
781+
)
782+
783+
assert.equal(result.status, 0)
784+
const transformed = await readFile(file, 'utf8')
785+
assert.ok(!transformed.includes('import.meta'))
786+
787+
const after = spawnSync(process.execPath, [tscBin, '-p', temp], { encoding: 'utf8' })
788+
assert.equal(after.status, 0, after.stderr || after.stdout)
789+
} finally {
790+
await rm(temp, { recursive: true, force: true })
791+
}
792+
})
793+
673794
test('--dry-run does not create outputs when out-dir provided', async () => {
674795
const temp = await mkdtemp(join(tmpdir(), 'module-cli-'))
675796
const outDir = join(temp, 'out')

0 commit comments

Comments
 (0)