Skip to content

Commit

Permalink
refactor: add glob
Browse files Browse the repository at this point in the history
  • Loading branch information
gjbkz committed May 5, 2022
1 parent 5d83094 commit 44e9713
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 6 deletions.
6 changes: 3 additions & 3 deletions src/esmify.test.ts
Original file line number Diff line number Diff line change
@@ -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');
Expand All @@ -14,14 +14,14 @@ type Files = Record<string, string>;
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');
}
Expand Down
6 changes: 3 additions & 3 deletions src/esmify.ts
Original file line number Diff line number Diff line change
@@ -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. */
Expand Down Expand Up @@ -55,7 +55,7 @@ export const esmify = async (
const getRenameMapping = async (patterns: Array<string>, cwd: string) => {
const renames = new Map<string, {path: string, code: string}>();
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')) {
Expand Down Expand Up @@ -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}`);
}
Expand Down
9 changes: 9 additions & 0 deletions src/glob.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import fg from 'fast-glob';
import * as path from 'path';

export const glob = async (patterns: Array<string>, options: fg.Options) => {
return await fg(
patterns.map((pattern) => pattern.split(path.sep).join('/')),
{absolute: true, ...options},
);
};

0 comments on commit 44e9713

Please sign in to comment.