-
Notifications
You must be signed in to change notification settings - Fork 2
⚡ Performance: Replace execSync with native Prettier API #2529
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
Changes from all commits
6166d18
2294c3c
859201f
c81dc05
a57af39
c0b722c
9422eef
2c2a6a5
e4b3df8
49aa717
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -34,7 +34,7 @@ const path = require("path"); | |
| const { globSync } = require("glob"); | ||
| const Ajv = require("ajv"); | ||
| const addFormats = require("ajv-formats"); | ||
| const { execSync } = require("child_process"); | ||
| const prettier = require("prettier"); | ||
|
|
||
| // Configuration | ||
| const config = { | ||
|
|
@@ -177,29 +177,39 @@ async function formatFiles(files) { | |
|
|
||
| log.info("Formatting JSON files with Prettier..."); | ||
|
|
||
| const filesArg = files.map((f) => `"${f}"`).join(" "); | ||
| const cmd = config.readOnly | ||
| ? `npx prettier --check --no-config ${filesArg}` | ||
| : `npx prettier --write --no-config ${filesArg}`; | ||
| let formatted = 0; | ||
| let needsFormatting = 0; | ||
| const prettierOptions = { | ||
| parser: "json", | ||
| tabWidth: 2, | ||
| useTabs: false, | ||
| }; | ||
|
|
||
| try { | ||
| const output = execSync(cmd, { encoding: "utf8", stdio: "pipe" }); | ||
| if (config.verbose && output) { | ||
| log.debug(output); | ||
| for (const file of files) { | ||
| const content = fs.readFileSync(file, "utf8"); | ||
| const formattedContent = await prettier.format(content, prettierOptions); | ||
|
|
||
| if (content !== formattedContent) { | ||
| needsFormatting++; | ||
| if (!config.readOnly) { | ||
| fs.writeFileSync(file, formattedContent, "utf8"); | ||
| formatted++; | ||
| log.debug(`Formatted: ${file}`); | ||
| } else { | ||
| log.debug(`Needs formatting: ${file}`); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🎯 Functional Correctness | 🟡 Minor | ⚡ Quick win Show affected filenames in read-only mode. When 🤖 Prompt for AI AgentsSource: MCP tools |
||
| } | ||
| } | ||
| } | ||
|
|
||
| const formatted = config.readOnly ? 0 : files.length; | ||
| log.success(`Formatted ${formatted} file(s)`); | ||
|
|
||
| return { formatted, skipped: 0 }; | ||
| } catch (error) { | ||
| if (config.readOnly && error.status === 1) { | ||
| log.warn("Some files need formatting (read-only mode)"); | ||
| if (error.stdout) { | ||
| console.log(error.stdout.toString()); | ||
| } | ||
| if (config.readOnly && needsFormatting > 0) { | ||
| log.warn(`${needsFormatting} file(s) need formatting (read-only mode)`); | ||
| return { formatted: 0, skipped: files.length }; | ||
| } | ||
|
|
||
| log.success(`Formatted ${formatted} file(s)`); | ||
| return { formatted, skipped: files.length - formatted }; | ||
| } catch (error) { | ||
| log.error(`Prettier failed: ${error.message}`); | ||
| throw error; | ||
| } | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🗄️ Data Integrity & Integration | 🟠 Major | 🏗️ Heavy lift
🔎 Supported by static analysis
🏁 Script executed:
Repository: lightspeedwp/.github
Length of output: 20928
🤖 get_repo_knowledge executed:
get_repo_knowledge lightspeedwp/.github /tmp/coderabbit-repo-knowledge/lightspeedwp-github-64c92c82/learnings /tmp/coderabbit-repo-knowledge/lightspeedwp-github-64c92c82/conventionsLength of output: 15715
🌐 Web query:
Prettier 3 API format filepath resolveConfig ignore files CLI .prettierignore .gitignore documentation💡 Result:
In Prettier 3, file handling via the API and CLI relies on specific mechanisms for configuration resolution and file ignoring. API Usage: The
prettier.resolveConfig(fileUrlOrPath, options)function resolves configuration for a given file [1][2]. It searches for a configuration file starting from the provided file's directory and traversing up the file tree [1][3]. You can also provide an explicit configuration path viaoptions.config[1][2]. To determine if a file is ignored by Prettier, useprettier.getFileInfo(fileUrlOrPath, options)[1][4]. This method returns an object containing anignoredboolean [1][2]. You can influence this check by passingoptions.ignorePathandoptions.withNodeModules[1][2]. CLI Behavior and Ignoring: By default, Prettier ignores files within version control directories (e.g.,.git,.svn,.hg) and node_modules [5][6]. Additionally, it automatically follows ignore rules defined in.gitignore and.prettierignore if they exist in the directory from which the CLI is run [5][6]. To customize ignored files via the CLI, you can use the --ignore-path option [7][8]. By default, Prettier searches for both.gitignore and.prettierignore [7][8]. Specifying --ignore-path overrides this default behavior [8][9]. To use multiple ignore files simultaneously (e.g., keeping the defaults while adding a custom one), you must explicitly list all of them: --ignore-path=.gitignore --ignore-path=.prettierignore --ignore-path=custom-ignore [7][8][9]. For one-off exclusions without modifying ignore files, you can use negative patterns directly in the command line (e.g., prettier. "!/*.{js,jsx,vue}" --write) [5][6].Citations:
🏁 Script executed:
Repository: lightspeedwp/.github
Length of output: 2352
Preserve Prettier CLI file handling.
findJsonFiles()uses only its local ignore list, whileformatFiles()callsprettier.format()with fixed options and nofilepath. This bypasses per-file configuration and.prettierignorerules, so matching files can be formatted or modified differently from the CLI. Resolve each file’s configuration and skip files reported as ignored, or document the intentional divergence.🤖 Prompt for AI Agents
Source: MCP tools