@@ -2,6 +2,7 @@ import Tinypool from "tinypool";
22import { resolvePlugins } from "../libs/apis" ;
33import type {
44 FormatEmbeddedCodeParam ,
5+ FormatEmbeddedDocParam ,
56 FormatFileParam ,
67 SortTailwindClassesArgs ,
78} from "../libs/apis" ;
@@ -35,41 +36,54 @@ export async function formatEmbeddedCode(
3536 options : FormatEmbeddedCodeParam [ "options" ] ,
3637 code : string ,
3738) : Promise < string > {
38- return pool ! . run ( { options, code } satisfies FormatEmbeddedCodeParam , {
39- name : "formatEmbeddedCode" ,
40- } ) ;
39+ return pool !
40+ . run ( { options, code } satisfies FormatEmbeddedCodeParam , { name : "formatEmbeddedCode" } )
41+ . catch ( rethrowAsError ) ;
42+ }
43+
44+ export async function formatEmbeddedDoc (
45+ options : FormatEmbeddedDocParam [ "options" ] ,
46+ texts : string [ ] ,
47+ ) : Promise < string [ ] > {
48+ return pool !
49+ . run ( { options, texts } satisfies FormatEmbeddedDocParam , {
50+ name : "formatEmbeddedDoc" ,
51+ } )
52+ . catch ( rethrowAsError ) ;
4153}
4254
4355export async function formatFile (
4456 options : FormatFileParam [ "options" ] ,
4557 code : string ,
4658) : Promise < string > {
47- return (
48- pool !
49- . run ( { options, code } satisfies FormatFileParam , { name : "formatFile" } )
50- // `tinypool` with `runtime: "child_process"` serializes Error as plain objects via IPC.
51- // (e.g. `{ name, message, stack, ... }`)
52- // And napi-rs converts unknown JS values to Rust Error by calling `String()` on them,
53- // which yields `"[object Object]"` for plain objects...
54- // So, this function reconstructs a proper `Error` instance so napi-rs can extract the message.
55- . catch ( ( err ) => {
56- if ( err instanceof Error ) throw err ;
57- if ( err !== null && typeof err === "object" ) {
58- const obj = err as { name : string ; message : string } ;
59- const newErr = new Error ( obj . message ) ;
60- newErr . name = obj . name ;
61- throw newErr ;
62- }
63- throw new Error ( String ( err ) ) ;
64- } )
65- ) ;
59+ return pool !
60+ . run ( { options, code } satisfies FormatFileParam , { name : "formatFile" } )
61+ . catch ( rethrowAsError ) ;
6662}
6763
6864export async function sortTailwindClasses (
6965 options : SortTailwindClassesArgs [ "options" ] ,
7066 classes : string [ ] ,
7167) : Promise < string [ ] > {
72- return pool ! . run ( { classes, options } satisfies SortTailwindClassesArgs , {
73- name : "sortTailwindClasses" ,
74- } ) ;
68+ return pool !
69+ . run ( { classes, options } satisfies SortTailwindClassesArgs , { name : "sortTailwindClasses" } )
70+ . catch ( rethrowAsError ) ;
71+ }
72+
73+ // ---
74+
75+ // `tinypool` with `runtime: "child_process"` serializes Error as plain objects via IPC.
76+ // (e.g. `{ name, message, stack, ... }`)
77+ // And napi-rs converts unknown JS values to Rust Error by calling `String()` on them,
78+ // which yields `"[object Object]"` for plain objects...
79+ // So, this function reconstructs a proper `Error` instance so napi-rs can extract the message.
80+ function rethrowAsError ( err : unknown ) : never {
81+ if ( err instanceof Error ) throw err ;
82+ if ( err !== null && typeof err === "object" ) {
83+ const obj = err as { name : string ; message : string } ;
84+ const newErr = new Error ( obj . message ) ;
85+ newErr . name = obj . name ;
86+ throw newErr ;
87+ }
88+ throw new Error ( String ( err ) ) ;
7589}
0 commit comments