diff --git a/packages/web-react/scripts/helpers.ts b/packages/web-react/scripts/helpers.ts index 72acd5a17b..2bbb9226e0 100644 --- a/packages/web-react/scripts/helpers.ts +++ b/packages/web-react/scripts/helpers.ts @@ -6,47 +6,47 @@ import * as parser from 'recast/parsers/babel'; export const distDir = path.resolve(__dirname, '..', 'dist'); -export function eachFile(dir: string, callback: (absPath: string, relPath: string) => unknown) { +export async function eachFile(dir: string, callback: (absPath: string, relPath: string) => unknown) { const promises: Promise[] = []; - return new Promise((resolve, reject) => { - // @ts-expect-error -- has no properties in common with type 'GlobOptions' - glob(`${dir}/**/*.js`, (error: Error | null, files: string[]) => { - if (error) return reject(error); + try { + const files = await glob(`${dir}/**/*.js`); - files.sort().forEach((file: string) => { - const relPath = path.relative(dir, file); + files.sort().forEach((file: string) => { + const relPath = path.relative(dir, file); - // Outside the distDir, somehow. - if (relPath.startsWith('../')) return; + // Outside the distDir, somehow. + if (relPath.startsWith('../')) return; - // Avoid re-transforming CommonJS bundle files. - if (relPath.endsWith('.cjs.js')) return; - if (relPath.endsWith('.cjs')) return; + // Avoid re-transforming CommonJS bundle files. + if (relPath.endsWith('.cjs.js')) return; + if (relPath.endsWith('.cjs')) return; - // Avoid re-transforming CommonJS bundle files. - if (relPath.endsWith('.min.js')) return; + // Avoid re-transforming CommonJS bundle files. + if (relPath.endsWith('.min.js')) return; - // Avoid re-transforming UMD bundle files. - if (relPath.endsWith('.umd.js')) return; + // Avoid re-transforming UMD bundle files. + if (relPath.endsWith('.umd.js')) return; - // Avoid re-transforming stories files. - if (relPath.endsWith('.stories.js')) return; + // Avoid re-transforming stories files. + if (relPath.endsWith('.stories.js')) return; - // This file is not meant to be imported or processed. - if (relPath.endsWith('invariantErrorCodes.js')) return; + // This file is not meant to be imported or processed. + if (relPath.endsWith('invariantErrorCodes.js')) return; - promises.push( - // eslint-disable-next-line no-shadow - new Promise((resolve) => { - resolve(callback(file, relPath)); - }), - ); - }); - - resolve(); + promises.push( + // eslint-disable-next-line no-shadow + new Promise((resolve) => { + resolve(callback(file, relPath)); + }), + ); }); - }).then(() => Promise.all(promises)); + + Promise.all(promises); + } catch (error) { + // eslint-disable-next-line no-console -- This is a CLI tool. + console.error(error); + } } export function reparse(source: string) {