From 909fcbe834e088944c8f584746c4f00c9df8145c Mon Sep 17 00:00:00 2001 From: Segun Adebayo Date: Sat, 30 Sep 2023 22:34:26 +0100 Subject: [PATCH] refactor: use `promise.allSettled` (#1458) refactor: use promise all settled --- .changeset/pretty-lamps-tease.md | 6 ++++++ packages/generator/src/artifacts/css/flat-css.ts | 3 ++- packages/generator/src/artifacts/css/global-css.ts | 4 +++- packages/generator/src/artifacts/css/keyframe-css.ts | 4 +++- packages/generator/src/artifacts/css/parser-css.ts | 2 +- packages/generator/src/artifacts/css/reset-css.ts | 3 ++- packages/generator/src/artifacts/css/static-css.ts | 4 +++- packages/generator/src/artifacts/css/token-css.ts | 2 +- packages/node/src/builder.ts | 2 +- packages/node/src/debug-files.ts | 4 ++-- packages/node/src/extract.ts | 9 ++++++--- packages/node/src/output-engine.ts | 2 +- 12 files changed, 31 insertions(+), 14 deletions(-) create mode 100644 .changeset/pretty-lamps-tease.md diff --git a/.changeset/pretty-lamps-tease.md b/.changeset/pretty-lamps-tease.md new file mode 100644 index 000000000..fa2c87efa --- /dev/null +++ b/.changeset/pretty-lamps-tease.md @@ -0,0 +1,6 @@ +--- +'@pandacss/generator': patch +'@pandacss/node': patch +--- + +- Fix issue with `Promise.all` where it aborts premature ine weird events. Switched to `Promise.allSettled` diff --git a/packages/generator/src/artifacts/css/flat-css.ts b/packages/generator/src/artifacts/css/flat-css.ts index e4508897f..03a887ee8 100644 --- a/packages/generator/src/artifacts/css/flat-css.ts +++ b/packages/generator/src/artifacts/css/flat-css.ts @@ -37,7 +37,8 @@ export const generateFlattenedCss = (ctx: Context) => (options: { files: string[ sheet.append(...files) const output = sheet.toCss({ optimize: true, minify }) - ctx.hooks.callHook('generator:css', 'styles.css', output) + + void ctx.hooks.callHook('generator:css', 'styles.css', output) return output } diff --git a/packages/generator/src/artifacts/css/global-css.ts b/packages/generator/src/artifacts/css/global-css.ts index ff639e947..e53d63438 100644 --- a/packages/generator/src/artifacts/css/global-css.ts +++ b/packages/generator/src/artifacts/css/global-css.ts @@ -44,6 +44,8 @@ export const generateGlobalCss = (ctx: Context) => { sheet.processGlobalCss(globalCss) const output = sheet.toCss({ optimize }) - ctx.hooks.callHook('generator:css', 'global.css', output) + + void ctx.hooks.callHook('generator:css', 'global.css', output) + return output } diff --git a/packages/generator/src/artifacts/css/keyframe-css.ts b/packages/generator/src/artifacts/css/keyframe-css.ts index 2e212b0b9..b3099f106 100644 --- a/packages/generator/src/artifacts/css/keyframe-css.ts +++ b/packages/generator/src/artifacts/css/keyframe-css.ts @@ -23,6 +23,8 @@ export function generateKeyframeCss(ctx: Context) { }) const output = rule.toString() - ctx.hooks.callHook('generator:css', 'keyframes.css', output) + + void ctx.hooks.callHook('generator:css', 'keyframes.css', output) + return output } diff --git a/packages/generator/src/artifacts/css/parser-css.ts b/packages/generator/src/artifacts/css/parser-css.ts index 486bd5809..c301778c5 100644 --- a/packages/generator/src/artifacts/css/parser-css.ts +++ b/packages/generator/src/artifacts/css/parser-css.ts @@ -88,7 +88,7 @@ export const generateParserCss = (ctx: Context) => (result: ParserResultType) => tryCatch( ({ sheet, result, config: { minify, optimize } }) => { const css = !result.isEmpty() ? sheet.toCss({ minify, optimize }) : undefined - ctx.hooks.callHook('parser:css', result.filePath ?? '', css) + void ctx.hooks.callHook('parser:css', result.filePath ?? '', css) return css }, (err) => { diff --git a/packages/generator/src/artifacts/css/reset-css.ts b/packages/generator/src/artifacts/css/reset-css.ts index 511e5d856..6bbe1fd53 100644 --- a/packages/generator/src/artifacts/css/reset-css.ts +++ b/packages/generator/src/artifacts/css/reset-css.ts @@ -212,6 +212,7 @@ export function generateResetCss(ctx: Context, scope = '') { } }` - ctx.hooks.callHook('generator:css', 'reset.css', output) + void ctx.hooks.callHook('generator:css', 'reset.css', output) + return output } diff --git a/packages/generator/src/artifacts/css/static-css.ts b/packages/generator/src/artifacts/css/static-css.ts index ce96d3a8a..d5c05f3e7 100644 --- a/packages/generator/src/artifacts/css/static-css.ts +++ b/packages/generator/src/artifacts/css/static-css.ts @@ -38,6 +38,8 @@ export const generateStaticCss = (ctx: Context) => { }) const output = sheet.toCss({ optimize }) - ctx.hooks.callHook('generator:css', 'static.css', output) + + void ctx.hooks.callHook('generator:css', 'static.css', output) + return output } diff --git a/packages/generator/src/artifacts/css/token-css.ts b/packages/generator/src/artifacts/css/token-css.ts index c0df1ae84..408946eae 100644 --- a/packages/generator/src/artifacts/css/token-css.ts +++ b/packages/generator/src/artifacts/css/token-css.ts @@ -51,7 +51,7 @@ export function generateTokenCss(ctx: Context) { } ` - ctx.hooks.callHook('generator:css', 'tokens.css', output) + void ctx.hooks.callHook('generator:css', 'tokens.css', output) return output } diff --git a/packages/node/src/builder.ts b/packages/node/src/builder.ts index 2764d38f9..deef6e4d9 100644 --- a/packages/node/src/builder.ts +++ b/packages/node/src/builder.ts @@ -189,7 +189,7 @@ export class Builder { const done = logger.time.info('Extracted in') - await Promise.all(ctx.getFiles().map((file) => this.extractFile(ctx, file))) + await Promise.allSettled(ctx.getFiles().map((file) => this.extractFile(ctx, file))) done() } diff --git a/packages/node/src/debug-files.ts b/packages/node/src/debug-files.ts index 315ffd89e..ecc5b5a44 100644 --- a/packages/node/src/debug-files.ts +++ b/packages/node/src/debug-files.ts @@ -20,7 +20,7 @@ export async function debugFiles(ctx: PandaContext, options: { outdir: string; d } const filesWithCss = [] - await Promise.all( + await Promise.allSettled( files.map(async (file) => { const measure = logger.time.debug(`Parsed ${file}`) const result = ctx.project.parseSourceFile(file) @@ -47,7 +47,7 @@ export async function debugFiles(ctx: PandaContext, options: { outdir: string; d logger.info('cli', `Writing ${colors.bold(`${outdir}/${astJsonPath}`)}`) logger.info('cli', `Writing ${colors.bold(`${outdir}/${cssPath}`)}`) - return Promise.all([ + return Promise.allSettled([ fs.writeFile(`${outdir}/${astJsonPath}`, JSON.stringify(result.toJSON(), null, 2)), fs.writeFile(`${outdir}/${cssPath}`, css), ]) diff --git a/packages/node/src/extract.ts b/packages/node/src/extract.ts index 4dcc28299..9159fede1 100644 --- a/packages/node/src/extract.ts +++ b/packages/node/src/extract.ts @@ -48,7 +48,7 @@ export function extractFile(ctx: PandaContext, file: string) { } function extractFiles(ctx: PandaContext) { - return Promise.all(ctx.getFiles().map((file) => writeFileChunk(ctx, file))) + return Promise.allSettled(ctx.getFiles().map((file) => writeFileChunk(ctx, file))) } const randomWords = ['Sweet', 'Divine', 'Pandalicious', 'Super'] @@ -56,8 +56,11 @@ const pickRandom = (arr: string[]) => arr[Math.floor(Math.random() * arr.length) export async function emitArtifacts(ctx: PandaContext) { if (ctx.config.clean) ctx.output.empty() - await Promise.all(ctx.getArtifacts().map(ctx.output.write)) - ctx.hooks.callHook('generator:done') + + await Promise.allSettled(ctx.getArtifacts().map(ctx.output.write)) + + void ctx.hooks.callHook('generator:done') + return { box: createBox({ content: ctx.messages.codegenComplete(), diff --git a/packages/node/src/output-engine.ts b/packages/node/src/output-engine.ts index f70b98fcd..9279caf7f 100644 --- a/packages/node/src/output-engine.ts +++ b/packages/node/src/output-engine.ts @@ -15,7 +15,7 @@ export const getOutputEngine = ({ const { dir = paths.root, files } = output fs.ensureDirSync(path.join(...dir)) - return Promise.all( + return Promise.allSettled( files.map(async ({ file, code }) => { const absPath = path.join(...dir, file) if (code) {