Skip to content

Commit

Permalink
Avoiding resolving formatting options for individual files, if those …
Browse files Browse the repository at this point in the history
…files will be cached
  • Loading branch information
fabiospampinato committed Nov 30, 2023
1 parent 89f0d87 commit c062cf9
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 17 deletions.
18 changes: 10 additions & 8 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import Logger from "./logger.js";
import { makePrettier } from "./prettier.js";
import { getExpandedFoldersPaths, getFoldersChildrenPaths, getProjectPath, getTargetsPaths } from "./utils.js";
import { fastRelativePath, isString, isUndefined, pluralize } from "./utils.js";
import type { Options } from "./types.js";
import type { FormatOptions, Options } from "./types.js";

async function run(options: Options): Promise<void> {
const logger = new Logger(options.logLevel);
Expand Down Expand Up @@ -51,16 +51,18 @@ async function run(options: Options): Promise<void> {
filesPathsTargets.map(async (filePath) => {
const ignored = await getIgnoreResolved(filePath);
if (ignored) return;
//TODO: Avoid always generating options for cached files, as they won't be needed
const editorConfig = options.editorConfig ? getEditorConfigFormatOptions(await getEditorConfigResolved(filePath)) : {}; // prettier-ignore
const prettierConfig = options.config ? await getPrettierConfigResolved(filePath) : {};
const formatOptions = { ...editorConfig, ...prettierConfig, ...options.formatOptions };
const getFormatOptions = async (): Promise<FormatOptions> => {
const editorConfig = options.editorConfig ? getEditorConfigFormatOptions(await getEditorConfigResolved(filePath)) : {}; // prettier-ignore
const prettierConfig = options.config ? await getPrettierConfigResolved(filePath) : {};
const formatOptions = { ...editorConfig, ...prettierConfig, ...options.formatOptions };
return formatOptions;
};
if (options.check || options.list) {
return prettier.checkWithPath(filePath, formatOptions);
return prettier.checkWithPath(filePath, getFormatOptions);
} else if (options.write) {
return prettier.writeWithPath(filePath, formatOptions);
return prettier.writeWithPath(filePath, getFormatOptions);
} else {
return prettier.formatWithPath(filePath, formatOptions);
return prettier.formatWithPath(filePath, getFormatOptions);
}
}),
);
Expand Down
22 changes: 21 additions & 1 deletion src/prettier_parallel.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import os from "node:os";
import WorkTank from "worktank";
import { resolve } from "./utils.js";
import type { Options, Prettier } from "./types.js";

function makeParallel(options: Options): Prettier {
Expand All @@ -9,7 +10,26 @@ function makeParallel(options: Options): Prettier {
methods: new URL("./prettier_serial.js", import.meta.url),
});

return pool.proxy();
return {
async check(filePath, fileContent, formatOptions) {
return pool.exec("check", [filePath, fileContent, await resolve(formatOptions)]);
},
async checkWithPath(filePath, formatOptions) {
return pool.exec("checkWithPath", [filePath, await resolve(formatOptions)]);
},
async format(filePath, fileContent, formatOptions) {
return pool.exec("format", [filePath, fileContent, await resolve(formatOptions)]);
},
async formatWithPath(filePath, formatOptions) {
return pool.exec("formatWithPath", [filePath, await resolve(formatOptions)]);
},
async write(filePath, fileContent, formatOptions) {
return pool.exec("write", [filePath, fileContent, await resolve(formatOptions)]);
},
async writeWithPath(filePath, formatOptions) {
return pool.exec("writeWithPath", [filePath, await resolve(formatOptions)]);
},
};
}

export { makeParallel };
17 changes: 9 additions & 8 deletions src/prettier_serial.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,23 +13,24 @@ import prettierMeriyah from "prettier-internal/plugins/meriyah";
import prettierPostcss from "prettier-internal/plugins/postcss";
import prettierTypescript from "prettier-internal/plugins/typescript";
import prettierYaml from "prettier-internal/plugins/yaml";
import type { FormatOptions } from "./types.js";
import { resolve } from "./utils.js";
import type { LazyFormatOptions } from "./types.js";

//TODO: Avoid loading plugins until they are actually needed

async function check(filePath: string, fileContent: string, formatOptions: FormatOptions): Promise<boolean> {
async function check(filePath: string, fileContent: string, formatOptions: LazyFormatOptions): Promise<boolean> {
const fileContentFormatted = await format(filePath, fileContent, formatOptions);
return fileContent === fileContentFormatted;
}

async function checkWithPath(filePath: string, formatOptions: FormatOptions): Promise<boolean> {
async function checkWithPath(filePath: string, formatOptions: LazyFormatOptions): Promise<boolean> {
const fileContent = await readFile(filePath, "utf8");
return check(filePath, fileContent, formatOptions);
}

async function format(filePath: string, fileContent: string, formatOptions: FormatOptions): Promise<string> {
async function format(filePath: string, fileContent: string, formatOptions: LazyFormatOptions): Promise<string> {
return prettier.format(fileContent, {
...formatOptions,
...(await resolve(formatOptions)),
filepath: filePath,
plugins: [
prettierAcorn,
Expand All @@ -49,19 +50,19 @@ async function format(filePath: string, fileContent: string, formatOptions: Form
});
}

async function formatWithPath(filePath: string, formatOptions: FormatOptions): Promise<string> {
async function formatWithPath(filePath: string, formatOptions: LazyFormatOptions): Promise<string> {
const fileContent = await readFile(filePath, "utf8");
return format(filePath, fileContent, formatOptions);
}

async function write(filePath: string, fileContent: string, formatOptions: FormatOptions): Promise<boolean> {
async function write(filePath: string, fileContent: string, formatOptions: LazyFormatOptions): Promise<boolean> {
const fileContentFormatted = await format(filePath, fileContent, formatOptions);
if (fileContent === fileContentFormatted) return true;
await writeFile(filePath, fileContentFormatted, "utf8");
return false;
}

async function writeWithPath(filePath: string, formatOptions: FormatOptions): Promise<boolean> {
async function writeWithPath(filePath: string, formatOptions: LazyFormatOptions): Promise<boolean> {
const fileContent = await readFile(filePath, "utf8");
return write(filePath, fileContent, formatOptions);
}
Expand Down
3 changes: 3 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ type FunctionMaybe<T> = T | (() => T);

type Ignore = (filePath: string) => boolean;

type LazyFormatOptions = FunctionMaybe<PromiseMaybe<FormatOptions>>;

type LogLevel = "error" | "warn" | "log" | "debug" | "silent";

type Options = {
Expand Down Expand Up @@ -68,6 +70,7 @@ export type {
FormatOptions,
FunctionMaybe,
Ignore,
LazyFormatOptions,
LogLevel,
Options,
Prettier,
Expand Down

0 comments on commit c062cf9

Please sign in to comment.