@@ -5,6 +5,7 @@ import path from 'node:path';
5
5
import process from 'node:process' ;
6
6
import childProcess from 'node:child_process' ;
7
7
import fs from 'node:fs' ;
8
+ import { createRequire } from 'node:module' ;
8
9
import { Command } from 'commander' ;
9
10
import * as utils from '../utils.js' ;
10
11
@@ -126,38 +127,57 @@ async function main(argv = process.argv) {
126
127
// Always include README if it exists
127
128
const markdownFiles : string [ ] = [ ] ;
128
129
if ( fs . existsSync ( 'README.md' ) ) markdownFiles . push ( 'README.md' ) ;
129
-
130
- // Add files from pages/, blog/, docs/ **if they exist AND contain md/mdx**
131
130
for ( const dir of [ 'pages' , 'blog' , 'docs' ] ) {
132
- if ( fs . existsSync ( dir ) ) {
133
- markdownFiles . push ( ...utils . collectMarkdown ( dir ) ) ;
134
- }
131
+ if ( fs . existsSync ( dir ) ) markdownFiles . push ( ...utils . collectMarkdown ( dir ) ) ;
135
132
}
136
-
137
133
if ( markdownFiles . length === 0 ) {
138
134
console . warn ( 'Skipping Prettier: no Markdown/MDX files found.' ) ;
139
135
return ;
140
136
}
141
137
142
138
const prettierArgs = [ fix ? '--write' : '--check' , ...markdownFiles ] ;
143
-
144
139
console . error ( 'Running prettier:' ) ;
145
- console . error ( ' ' + [ 'prettier' , ...prettierArgs ] . join ( ' ' ) ) ;
146
140
141
+ const require = createRequire ( import . meta. url ) ;
142
+ let prettierBin : string | null = null ;
147
143
try {
148
- childProcess . execFileSync ( 'prettier' , prettierArgs , {
149
- stdio : 'inherit' ,
150
- windowsHide : true ,
151
- encoding : 'utf-8' ,
152
- shell : platform === 'win32' ,
153
- cwd : process . cwd ( ) ,
154
- } ) ;
144
+ // Resolves to @matrixai /lint/node_modules/prettier/bin/prettier.cjs
145
+ prettierBin = require . resolve ( 'prettier/bin/prettier.cjs' ) ;
146
+ } catch {
147
+ // Bundled copy not found
148
+ }
149
+
150
+ try {
151
+ if ( prettierBin ) {
152
+ console . error (
153
+ ` ${ process . execPath } ${ prettierBin } ${ prettierArgs . join ( ' ' ) } ` ,
154
+ ) ;
155
+ childProcess . execFileSync (
156
+ process . execPath ,
157
+ [ prettierBin , ...prettierArgs ] ,
158
+ {
159
+ stdio : 'inherit' ,
160
+ windowsHide : true ,
161
+ encoding : 'utf-8' ,
162
+ cwd : process . cwd ( ) ,
163
+ } ,
164
+ ) ;
165
+ } else {
166
+ console . error ( ' prettier ' + prettierArgs . join ( ' ' ) ) ;
167
+ childProcess . execFileSync ( 'prettier' , prettierArgs , {
168
+ stdio : 'inherit' ,
169
+ windowsHide : true ,
170
+ encoding : 'utf-8' ,
171
+ shell : platform === 'win32' ,
172
+ cwd : process . cwd ( ) ,
173
+ } ) ;
174
+ }
155
175
} catch ( err ) {
156
176
if ( ! fix ) {
157
177
console . error ( 'Prettier check failed.' ) ;
158
178
hadFailure = true ;
159
179
} else {
160
- throw err ; // Unexpected if --write fails
180
+ throw err ; // Should not happen when --write
161
181
}
162
182
}
163
183
0 commit comments