Skip to content
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
6 changes: 4 additions & 2 deletions src/cache.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import fs from "node:fs";
import os from "node:os";
import path from "node:path";
import { fastRelativePath, getCachePath, isArray, isBoolean, isObject, isString, isUndefined, sha1hex, sha1base64 } from "./utils.js";
import type Logger from "./logger.js";
Expand Down Expand Up @@ -31,11 +32,12 @@ class Cache {
private logger: Logger;
private dirty: boolean;

constructor(version: string, rootPath: string, options: Options, logger: Logger) {
constructor(version: string, rootPath: string, cacheRootPath: string | undefined, options: Options, logger: Logger) {
this.version = sha1hex(version);
this.logger = logger;
this.rootPath = rootPath;
this.storePath = options.cacheLocation || path.join(getCachePath(rootPath), `${sha1hex(rootPath)}.json`);
const cachePath = cacheRootPath ? getCachePath(cacheRootPath) : path.join(os.tmpdir(), "prettier", ".prettier-caches");
this.storePath = options.cacheLocation || path.join(cachePath, `${sha1hex(rootPath)}.json`);
this.store = this.read();
this.dirty = false;
}
Expand Down
2 changes: 1 addition & 1 deletion src/config_prettier.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ const Loaders = {
},
toml: async (filePath: string): Promise<unknown> => {
const fileContent = fs.readFileSync(filePath, "utf8");
const {parse} = await import("smol-toml");
const { parse } = await import("smol-toml");
return parse(fileContent);
},
yaml: async (filePath: string): Promise<unknown> => {
Expand Down
13 changes: 11 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,16 @@ import { PRETTIER_VERSION, CLI_VERSION } from "./constants.js";
import Known from "./known.js";
import Logger from "./logger.js";
import { makePrettier } from "./prettier.js";
import { castArray, getExpandedFoldersPaths, getFoldersChildrenPaths, getPluginsVersions, getProjectPath, getStdin, getTargetsPaths } from "./utils.js";
import {
castArray,
getCacheRootPath,
getExpandedFoldersPaths,
getFoldersChildrenPaths,
getPluginsVersions,
getProjectPath,
getStdin,
getTargetsPaths,
} from "./utils.js";
import {
fastRelativePath,
isNull,
Expand Down Expand Up @@ -116,7 +125,7 @@ async function runGlobs(options: Options, pluginsDefaultOptions: PluginsOptions,
const cacheVersion = stringify({ prettierVersion, cliVersion, pluginsNames, pluginsVersions, editorConfigs, ignoreContents, prettierConfigs, ignoreManualFilesPaths, ignoreManualFilesContents, prettierManualFilesPaths, prettierManualFilesContents, cliContextConfig, cliFormatConfig, pluginsDefaultOptions, pluginsCustomOptions }); // prettier-ignore

const shouldCache = options.cache && !options.dump && !pluginsVersionsMissing.length && isUndefined(cliContextConfig.cursorOffset);
const cache = shouldCache ? new Cache(cacheVersion, projectPath, options, stdout) : undefined;
const cache = shouldCache ? new Cache(cacheVersion, projectPath, getCacheRootPath(rootPath), options, stdout) : undefined;
const prettier = await makePrettier(options, cache);

//TODO: Maybe do work in chunks here, as keeping too many formatted files in memory can be a problem
Expand Down
31 changes: 17 additions & 14 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,10 @@ function getCachePath(rootPath: string): string {
return cachePath;
}

function getCacheRootPath(rootPath: string): string | undefined {
return findDirectoryUpwards(rootPath, (folderPath) => fs.existsSync(path.join(folderPath, "node_modules")));
}

function getDirectoryPaths(rootPath: string, withNodeModules: boolean) {
const ignoreGlob = `**/{.git,.sl,.svn,.hg,.DS_Store,Thumbs.db${withNodeModules ? "" : ",node_modules"}}`;
const ignoreRe = zeptomatch.compile(ignoreGlob);
Expand Down Expand Up @@ -181,6 +185,17 @@ function getPluginsVersions(names: string[]): (string | null)[] {
return pluginsVersions;
}

function findDirectoryUpwards(rootPath: string, isMatch: (folderPath: string) => boolean): string | undefined {
let currentPath = rootPath;

while (true) {
if (isMatch(currentPath)) return currentPath;
const currentPathNext = path.dirname(currentPath);
if (currentPath === currentPathNext) return;
currentPath = currentPathNext;
}
}

function getProjectPath(rootPath: string): string {
function isProjectPath(folderPath: string): boolean {
const gitPath = path.join(folderPath, ".git");
Expand All @@ -194,20 +209,7 @@ function getProjectPath(rootPath: string): string {
return false;
}

let currentPath = rootPath;

while (true) {
if (isProjectPath(currentPath)) {
return currentPath;
} else {
const currentPathNext = path.dirname(currentPath);
if (currentPath === currentPathNext) {
return rootPath;
} else {
currentPath = currentPathNext;
}
}
}
return findDirectoryUpwards(rootPath, isProjectPath) ?? rootPath;
}

function getStats(targetPath: string): fs.Stats | undefined {
Expand Down Expand Up @@ -753,6 +755,7 @@ export {
fastRelativeChildPath,
findLastIndex,
getCachePath,
getCacheRootPath,
getFolderChildrenPaths,
getFoldersChildrenPaths,
getExpandedFoldersPaths,
Expand Down
Loading