Overview
Prettier exports Node.js API functions for formatting text and resolving user configs, but doesn't export an equivalent to its CLI. This means that if we want to format many files on disk at once with Prettier -i.e. run the equivalent of the Prettier CLI- we have to either:
- Piece the CLI together in our own logic: i.e. duplicate much of the CLI's logic
- Spin up and await a child process: which incurs process overhead
(question: are there other available strategies?)
Request: could Prettier export the logic used in its CLI run function so that we can call to Prettier end-to-end in-memory?
import { runPrettierCLI } from "prettier/cli";
const exitCode = await runPrettierCLI([".", "--write"]);
I'd be very happy to send a PR if this is accepted. 🙂
Context
This is coming out of JoshuaKGoldberg/formatly#114. Formatly is a project that gives one entry point to run whatever formatter can be detected under the user's project, such as Prettier. Its Prettier runner creates a child process right now, but in changesets/changesets#1639 the performance overhead was noted as often hundreds of ms.
Providing a standalone CLI-like function would mean integrations such as Changesets' and Knip's (webpro-nl/knip#1029) would be much faster for end-users.
Environments:
- Prettier Version: 3.5.3, n/a
- Running Prettier via: Node.js API
- Runtime: Node.js
- Operating System: n/a
- Prettier plugins (if any): n/a
Overview
Prettier exports Node.js API functions for formatting text and resolving user configs, but doesn't export an equivalent to its CLI. This means that if we want to format many files on disk at once with Prettier -i.e. run the equivalent of the Prettier CLI- we have to either:
(question: are there other available strategies?)
Request: could Prettier export the logic used in its CLI
runfunction so that we can call to Prettier end-to-end in-memory?I'd be very happy to send a PR if this is accepted. 🙂
Context
This is coming out of JoshuaKGoldberg/formatly#114. Formatly is a project that gives one entry point to run whatever formatter can be detected under the user's project, such as Prettier. Its Prettier runner creates a child process right now, but in changesets/changesets#1639 the performance overhead was noted as often hundreds of ms.
Providing a standalone CLI-like function would mean integrations such as Changesets' and Knip's (webpro-nl/knip#1029) would be much faster for end-users.
Environments: