Skip to content

Makes js-lint invoke bundled prettier cli rather than target repo's prettier #21

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 0 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,5 @@
"tsx": "^3.12.7",
"typedoc": "^0.24.8",
"typescript": "^5.1.6"
},
"peerDependencies": {
"eslint": ">=9.0.0"
}
}
52 changes: 36 additions & 16 deletions src/bin/lint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import path from 'node:path';
import process from 'node:process';
import childProcess from 'node:child_process';
import fs from 'node:fs';
import { createRequire } from 'node:module';
import { Command } from 'commander';
import * as utils from '../utils.js';

Expand Down Expand Up @@ -126,38 +127,57 @@ async function main(argv = process.argv) {
// Always include README if it exists
const markdownFiles: string[] = [];
if (fs.existsSync('README.md')) markdownFiles.push('README.md');

// Add files from pages/, blog/, docs/ **if they exist AND contain md/mdx**
for (const dir of ['pages', 'blog', 'docs']) {
if (fs.existsSync(dir)) {
markdownFiles.push(...utils.collectMarkdown(dir));
}
if (fs.existsSync(dir)) markdownFiles.push(...utils.collectMarkdown(dir));
}

if (markdownFiles.length === 0) {
console.warn('Skipping Prettier: no Markdown/MDX files found.');
return;
}

const prettierArgs = [fix ? '--write' : '--check', ...markdownFiles];

console.error('Running prettier:');
console.error(' ' + ['prettier', ...prettierArgs].join(' '));

const require = createRequire(import.meta.url);
let prettierBin: string | null = null;
try {
childProcess.execFileSync('prettier', prettierArgs, {
stdio: 'inherit',
windowsHide: true,
encoding: 'utf-8',
shell: platform === 'win32',
cwd: process.cwd(),
});
// Resolves to @matrixai/lint/node_modules/prettier/bin/prettier.cjs
prettierBin = require.resolve('prettier/bin/prettier.cjs');
Comment on lines +141 to +145
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't use createRequire this is hack to allow for synchronous dynamic importing of code. If you need to resolve a path to an absolute use import.meta.url to get the current directory and use path.resolve to resolve that to an absolute path.

} catch {
// Bundled copy not found
}

try {
if (prettierBin) {
console.error(
` ${process.execPath} ${prettierBin} ${prettierArgs.join(' ')}`,
);
childProcess.execFileSync(
process.execPath,
[prettierBin, ...prettierArgs],
{
stdio: 'inherit',
windowsHide: true,
encoding: 'utf-8',
cwd: process.cwd(),
},
);
} else {
console.error(' prettier ' + prettierArgs.join(' '));
childProcess.execFileSync('prettier', prettierArgs, {
stdio: 'inherit',
windowsHide: true,
encoding: 'utf-8',
shell: platform === 'win32',
cwd: process.cwd(),
});
}
} catch (err) {
if (!fix) {
console.error('Prettier check failed.');
hadFailure = true;
} else {
throw err; // Unexpected if --write fails
throw err; // Should not happen when --write
}
}

Expand Down