Skip to content

Commit 8d69d34

Browse files
committed
fix: improve prettier execution handling in lint script by making it invoke local prettier instead of repo prettier
1 parent 2b0aa4f commit 8d69d34

File tree

1 file changed

+36
-16
lines changed

1 file changed

+36
-16
lines changed

src/bin/lint.ts

Lines changed: 36 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import path from 'node:path';
55
import process from 'node:process';
66
import childProcess from 'node:child_process';
77
import fs from 'node:fs';
8+
import { createRequire } from 'node:module';
89
import { Command } from 'commander';
910
import * as utils from '../utils.js';
1011

@@ -126,38 +127,57 @@ async function main(argv = process.argv) {
126127
// Always include README if it exists
127128
const markdownFiles: string[] = [];
128129
if (fs.existsSync('README.md')) markdownFiles.push('README.md');
129-
130-
// Add files from pages/, blog/, docs/ **if they exist AND contain md/mdx**
131130
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));
135132
}
136-
137133
if (markdownFiles.length === 0) {
138134
console.warn('Skipping Prettier: no Markdown/MDX files found.');
139135
return;
140136
}
141137

142138
const prettierArgs = [fix ? '--write' : '--check', ...markdownFiles];
143-
144139
console.error('Running prettier:');
145-
console.error(' ' + ['prettier', ...prettierArgs].join(' '));
146140

141+
const require = createRequire(import.meta.url);
142+
let prettierBin: string | null = null;
147143
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+
}
155175
} catch (err) {
156176
if (!fix) {
157177
console.error('Prettier check failed.');
158178
hadFailure = true;
159179
} else {
160-
throw err; // Unexpected if --write fails
180+
throw err; // Should not happen when --write
161181
}
162182
}
163183

0 commit comments

Comments
 (0)