11import type { ExecFileOptionsWithOtherEncoding } from 'node:child_process'
22import * as childProcess from 'node:child_process'
33import * as path from 'node:path'
4- import { promisify } from 'node:util'
4+ import { promisify , stripVTControlCharacters } from 'node:util'
55
66export const execFile = promisify ( childProcess . execFile )
77
@@ -18,14 +18,43 @@ export const defaultExecFileOptions = {
1818 shell : true ,
1919} as const satisfies ExecFileOptionsWithOtherEncoding
2020
21- export const runPrettierCLI = (
21+ // TODO: Fix error messages in tests.
22+ /**
23+ * @todo Fix error messages in tests.
24+ */
25+ export const runPrettierCLI = async (
2226 CLIArguments : readonly string [ ] = [ ] ,
2327 execFileOptions ?: Partial < ExecFileOptionsWithOtherEncoding > ,
24- ) =>
25- execFile ( defaultCLICommand , [ ...defaultCLIArguments , ...CLIArguments ] , {
26- ...defaultExecFileOptions ,
27- ...execFileOptions ,
28- } )
28+ ) => {
29+ try {
30+ const execFileResults = await execFile (
31+ defaultCLICommand ,
32+ [ ...defaultCLIArguments , ...CLIArguments ] ,
33+ {
34+ ...defaultExecFileOptions ,
35+ ...execFileOptions ,
36+ } ,
37+ )
38+
39+ const { stdout, stderr } = execFileResults
40+
41+ if ( stdout ) {
42+ console . log ( stdout )
43+ }
44+
45+ if ( stderr ) {
46+ console . error ( stderr )
47+ }
48+
49+ return execFileResults
50+ } catch ( error ) {
51+ if ( error instanceof Error ) {
52+ error . message = stripVTControlCharacters ( error . message )
53+ }
54+
55+ throw error
56+ }
57+ }
2958
3059export const fixturesDirectoryName = 'temp'
3160
0 commit comments