From 44e97139bbdbfe851e5f61e853e8d191c0480809 Mon Sep 17 00:00:00 2001 From: Kei Ito Date: Thu, 5 May 2022 21:42:01 +0900 Subject: [PATCH] refactor: add glob --- src/esmify.test.ts | 6 +++--- src/esmify.ts | 6 +++--- src/glob.ts | 9 +++++++++ 3 files changed, 15 insertions(+), 6 deletions(-) create mode 100644 src/glob.ts diff --git a/src/esmify.test.ts b/src/esmify.test.ts index 84f0a1cc..01af222a 100644 --- a/src/esmify.test.ts +++ b/src/esmify.test.ts @@ -1,10 +1,10 @@ import test from 'ava'; import * as childProcess from 'child_process'; -import fg from 'fast-glob'; import * as fs from 'fs/promises'; import {createRequire} from 'module'; import * as os from 'os'; import * as path from 'path'; +import {glob} from './glob'; const require = createRequire(import.meta.url); const cliFilePath = require.resolve('../bin/esmify.mjs'); @@ -14,14 +14,14 @@ type Files = Record; const createTestDirectory = async () => await fs.mkdtemp(path.join(os.tmpdir(), 'esmify-')); const deployFiles = async (directory: string, files: Files) => { for (const [relativePath, body] of Object.entries(files)) { - const dest = path.join(directory, relativePath); + const dest = path.join(directory, ...relativePath.split('/')); await fs.mkdir(path.dirname(dest), {recursive: true}); await fs.writeFile(dest, body); } }; const readFiles = async (directory: string) => { const files: Files = {}; - for (const file of await fg('**', {cwd: directory, absolute: true})) { + for (const file of await glob(['**'], {cwd: directory})) { const key = path.relative(directory, file).split(path.sep).join('/'); files[key] = await fs.readFile(file, 'utf-8'); } diff --git a/src/esmify.ts b/src/esmify.ts index 2f2f7a19..6f7d1a86 100644 --- a/src/esmify.ts +++ b/src/esmify.ts @@ -1,9 +1,9 @@ import * as acorn from 'acorn'; import * as walk from 'acorn-walk'; import * as console from 'console'; -import fg from 'fast-glob'; import * as fs from 'fs/promises'; import * as path from 'path'; +import {glob} from './glob'; interface Options { /** (default: `process.cwd()`) A path to the directory passed to fast-glob. */ @@ -55,7 +55,7 @@ export const esmify = async ( const getRenameMapping = async (patterns: Array, cwd: string) => { const renames = new Map(); const targetExtensions = ['.js', '.mjs', '.cjs']; - for (const absoluteFilePath of await fg(patterns, {cwd, absolute: true})) { + for (const absoluteFilePath of await glob(patterns, {cwd, absolute: true})) { if (targetExtensions.includes(path.extname(absoluteFilePath))) { let renamedPath = absoluteFilePath; if (absoluteFilePath.endsWith('.js')) { @@ -131,7 +131,7 @@ const isInteger = (input: unknown): input is number => Number.isInteger(input); const resolveLocalSource = async (source: string, importer: string) => { const cwd = path.dirname(importer); - const found = await fg(getRequirePatterns(source), {cwd, absolute: true}); + const found = await glob(getRequirePatterns(source), {cwd}); if (found.length === 0) { throw new Error(`Can't Resolve ${source} from ${importer}`); } diff --git a/src/glob.ts b/src/glob.ts new file mode 100644 index 00000000..9be0768e --- /dev/null +++ b/src/glob.ts @@ -0,0 +1,9 @@ +import fg from 'fast-glob'; +import * as path from 'path'; + +export const glob = async (patterns: Array, options: fg.Options) => { + return await fg( + patterns.map((pattern) => pattern.split(path.sep).join('/')), + {absolute: true, ...options}, + ); +};