@prettier/cli
Advanced tools
Comparing version 0.2.0 to 0.2.1
/// <reference types="node" /> | ||
import type Logger from "./logger.js"; | ||
import type { Options } from "./types.js"; | ||
import type { Options, PromiseMaybe } from "./types.js"; | ||
type Store = Partial<{ | ||
@@ -9,3 +9,3 @@ [version: string]: StoreVersion; | ||
modified: number; | ||
files: Partial<Record<string, StoreFile>>; | ||
files: Partial<Record<string, StoreFile | false>>; | ||
}>; | ||
@@ -31,3 +31,4 @@ type StoreFile = [hash: string, formatted: boolean]; | ||
set(filePath: string, fileHashPath: string, fileFormatted: boolean, fileContentExpected: string): void; | ||
has(filePath: string, isIgnored: () => PromiseMaybe<boolean>): Promise<boolean>; | ||
} | ||
export default Cache; |
import fs from "node:fs"; | ||
import path from "node:path"; | ||
import { fastRelativePath, getCachePath, isArray, isBoolean, isObject, isString, sha1hex, sha1base64 } from "./utils.js"; | ||
import { fastRelativePath, getCachePath, isArray, isBoolean, isObject, isString, isUndefined, sha1hex, sha1base64 } from "./utils.js"; | ||
//TODO: Maybe remember thrown errors also, if they are under a certain size | ||
@@ -85,3 +85,25 @@ class Cache { | ||
} | ||
async has(filePath, isIgnored) { | ||
var _a, _b; | ||
const fileRelativePath = fastRelativePath(this.rootPath, filePath); | ||
const fileHashPath = sha1base64(fileRelativePath); | ||
const file = this.store[this.version]?.files?.[fileHashPath]; | ||
if (isUndefined(file)) { | ||
const ignored = await isIgnored(); | ||
if (ignored) { | ||
const version = ((_a = this.store)[_b = this.version] || (_a[_b] = {})); | ||
const files = (version.files || (version.files = {})); | ||
files[fileHashPath] = false; | ||
this.dirty = true; | ||
return false; | ||
} | ||
else { | ||
return true; | ||
} | ||
} | ||
else { | ||
return !!file; | ||
} | ||
} | ||
} | ||
export default Cache; |
@@ -14,3 +14,3 @@ import isBinaryPath from "is-binary-path"; | ||
import { getExpandedFoldersPaths, getFoldersChildrenPaths, getPluginsVersions, getProjectPath, getTargetsPaths } from "./utils.js"; | ||
import { fastRelativePath, isString, isUndefined, negate, pluralize } from "./utils.js"; | ||
import { fastRelativePath, isString, isUndefined, negate, pluralize, uniq } from "./utils.js"; | ||
async function run(options, pluginsOptions) { | ||
@@ -22,6 +22,5 @@ const logger = new Logger(options.logLevel); | ||
const projectPath = getProjectPath(rootPath); | ||
const [filesPaths, filesNames, filesFoundPaths, foldersFoundPaths] = await getTargetsPaths(rootPath, options.globs); | ||
const [filesPaths, filesNames, filesNamesToPaths, filesFoundPaths, foldersFoundPaths] = await getTargetsPaths(rootPath, options.globs); | ||
const filesPathsTargets = filesPaths.filter(negate(isBinaryPath)).sort(); | ||
const [foldersPathsTargetsUnsorted, foldersExtraPaths] = getExpandedFoldersPaths(foldersFoundPaths, projectPath); | ||
const foldersPathsTargets = [...foldersPathsTargetsUnsorted, rootPath, ...foldersExtraPaths]; | ||
const [foldersPathsTargets, foldersExtraPaths] = getExpandedFoldersPaths(foldersFoundPaths, projectPath); | ||
const filesExtraPaths = await getFoldersChildrenPaths([rootPath, ...foldersExtraPaths]); | ||
@@ -40,8 +39,12 @@ const filesExtraNames = filesExtraPaths.map((filePath) => path.basename(filePath)); | ||
const prettierConfigNames = ["package.json", ".prettierrc", ".prettierrc.yml", ".prettierrc.yaml", ".prettierrc.json", ".prettierrc.jsonc", ".prettierrc.json5", ".prettierrc.js", "prettier.config.js", ".prettierrc.cjs", "prettier.config.cjs", ".prettierrc.mjs", "prettier.config.mjs"].filter(Known.hasFileName); // prettier-ignore | ||
const editorConfigs = options.editorConfig ? await getEditorConfigsMap(foldersPathsTargets, editorConfigNames) : {}; | ||
const ignoreContents = await getIgnoresContentMap(foldersPathsTargets, ignoreNames); | ||
const prettierConfigs = options.config ? await getPrettierConfigsMap(foldersPathsTargets, prettierConfigNames) : {}; | ||
const fileNames2parentPaths = (names) => names.flatMap((name) => filesNamesToPaths[name]?.map(path.dirname) || []); | ||
const editorConfigPaths = uniq([...fileNames2parentPaths(editorConfigNames), rootPath, ...foldersExtraPaths]); | ||
const ignorePaths = uniq([...fileNames2parentPaths(ignoreNames), rootPath, ...foldersExtraPaths]); | ||
const prettierConfigPaths = uniq([...fileNames2parentPaths(prettierConfigNames), rootPath, ...foldersExtraPaths]); | ||
const editorConfigs = options.editorConfig ? await getEditorConfigsMap(editorConfigPaths, editorConfigNames) : {}; | ||
const ignoreContents = await getIgnoresContentMap(ignorePaths, ignoreNames); | ||
const prettierConfigs = options.config ? await getPrettierConfigsMap(prettierConfigPaths, prettierConfigNames) : {}; | ||
const cliContextConfig = options.contextOptions; | ||
const cliFormatConfig = options.formatOptions; | ||
const cacheVersion = stringify({ prettierVersion, cliVersion, pluginsNames, pluginsVersions, editorConfigs, prettierConfigs, cliContextConfig, cliFormatConfig, pluginsOptions }); // prettier-ignore | ||
const cacheVersion = stringify({ prettierVersion, cliVersion, pluginsNames, pluginsVersions, editorConfigs, ignoreContents, prettierConfigs, cliContextConfig, cliFormatConfig, pluginsOptions }); // prettier-ignore | ||
const shouldCache = isUndefined(cliContextConfig.cursorOffset); | ||
@@ -52,3 +55,5 @@ const cache = shouldCache ? new Cache(cacheVersion, projectPath, options, logger) : undefined; | ||
const filesResults = await Promise.allSettled(filesPathsTargets.map(async (filePath) => { | ||
const ignored = await getIgnoreResolved(filePath, ignoreNames); | ||
const isIgnored = () => getIgnoreResolved(filePath, ignoreNames); | ||
const isCacheable = () => cache?.has(filePath, isIgnored); | ||
const ignored = cache ? !(await isCacheable()) : await isIgnored(); | ||
if (ignored) | ||
@@ -83,2 +88,3 @@ return; | ||
let totalErrored = 0; | ||
let pathsErrored = []; | ||
for (let i = 0, l = filesResults.length; i < l; i++) { | ||
@@ -113,4 +119,5 @@ const fileResult = filesResults[i]; | ||
const error = fileResult.reason; | ||
totalErrored += 1; | ||
pathsErrored.push(filesPathsTargets[i]); | ||
if (error.name !== "UndefinedParserError" || !options.ignoreUnknown) { | ||
totalErrored += 1; | ||
const filePath = filesPathsTargets[i]; | ||
@@ -134,2 +141,3 @@ const fileRelativePath = fastRelativePath(rootPath, filePath); | ||
logger.prefixed.debug(`Files errored: ${totalErrored}`); | ||
logger.prefixed.debug(() => pathsErrored.map((filePath) => fastRelativePath(rootPath, filePath)).join("\n")); | ||
if (!totalMatched) { | ||
@@ -136,0 +144,0 @@ if (options.errorOnUnmatchedPattern) { |
@@ -12,3 +12,6 @@ import Pioppo from "pioppo"; | ||
return; | ||
this.pioppo.info(resolve(message)); | ||
message = resolve(message); | ||
if (!message) | ||
return; | ||
this.pioppo.info(message); | ||
}; | ||
@@ -37,3 +40,6 @@ this.silent = (message) => { | ||
return; | ||
const lines = resolve(message).split(/\r?\n|\r/g); | ||
message = resolve(message); | ||
if (!message) | ||
return; | ||
const lines = message.split(/\r?\n|\r/g); | ||
const linesPrefixed = lines.map((line) => `${prefix} ${line}`); | ||
@@ -40,0 +46,0 @@ this.pioppo.info(linesPrefixed.join("\n")); |
@@ -26,3 +26,3 @@ import type { FormatOptions, FunctionMaybe, Key, Options, PrettierConfigWithOverrides, PrettierPlugin } from "./types.js"; | ||
declare function getProjectPath(rootPath: string): string; | ||
declare function getTargetsPaths(rootPath: string, globs: string[]): Promise<string[][]>; | ||
declare function getTargetsPaths(rootPath: string, globs: string[]): Promise<[string[], string[], Record<string, string[]>, string[], string[]]>; | ||
declare function isArray(value: unknown): value is unknown[]; | ||
@@ -52,4 +52,5 @@ declare function isBoolean(value: unknown): value is boolean; | ||
declare function someOf<T>(fns: ((arg: T) => unknown)[]): (arg: T) => boolean; | ||
declare function uniq<T>(values: T[]): T[]; | ||
declare function zipObject<T extends Key, U>(keys: T[], values: U[]): Partial<Record<T, U>>; | ||
declare function zipObjectUnless<T extends Key, U>(keys: T[], values: U[], unless: (value: U) => boolean): Partial<Record<T, U>>; | ||
export { castArray, everyOf, fastJoinedPath, fastRelativePath, fastRelativeChildPath, findLastIndex, getCachePath, getFolderChildrenPaths, getFoldersChildrenPaths, getExpandedFoldersPaths, getGlobPaths, getPlugin, getPluginPath, getPluginVersion, getPlugins, getPluginsPaths, getPluginsVersions, getProjectPath, getTargetsPaths, isArray, isBoolean, isFalsy, isFunction, isInteger, isNumber, isObject, isPromise, isString, isTruthy, isUndefined, memoize, negate, noop, normalizeOptions, normalizeFormatOptions, normalizePluginOptions, normalizePrettierOptions, omit, once, pluralize, resolve, sha1hex, sha1base64, someOf, zipObject, zipObjectUnless, }; | ||
export { castArray, everyOf, fastJoinedPath, fastRelativePath, fastRelativeChildPath, findLastIndex, getCachePath, getFolderChildrenPaths, getFoldersChildrenPaths, getExpandedFoldersPaths, getGlobPaths, getPlugin, getPluginPath, getPluginVersion, getPlugins, getPluginsPaths, getPluginsVersions, getProjectPath, getTargetsPaths, isArray, isBoolean, isFalsy, isFunction, isInteger, isNumber, isObject, isPromise, isString, isTruthy, isUndefined, memoize, negate, noop, normalizeOptions, normalizeFormatOptions, normalizePluginOptions, normalizePrettierOptions, omit, once, pluralize, resolve, sha1hex, sha1base64, someOf, uniq, zipObject, zipObjectUnless, }; |
@@ -165,2 +165,3 @@ import findUp from "find-up-json"; | ||
const targetFilesNames = []; | ||
const targetFilesNamesToPaths = {}; | ||
const targetGlobs = []; | ||
@@ -170,4 +171,7 @@ for (const glob of globs) { | ||
if (isFile(filePath)) { | ||
const fileName = path.basename(filePath); | ||
targetFiles.push(filePath); | ||
targetFilesNames.push(path.basename(filePath)); | ||
targetFilesNames.push(fileName); | ||
targetFilesNamesToPaths.propertyIsEnumerable(fileName) || (targetFilesNamesToPaths[fileName] = []); | ||
targetFilesNamesToPaths[fileName].push(filePath); | ||
} | ||
@@ -181,5 +185,11 @@ else { | ||
const filesNames = [...targetFilesNames, ...result.filesFoundNames]; | ||
const filesNamesToPaths = result.filesFoundNamesToPaths; | ||
for (const fileName in targetFilesNamesToPaths) { | ||
const prev = filesNamesToPaths[fileName]; | ||
const next = Array.isArray(prev) ? prev.concat(filesNamesToPaths[fileName]) : filesNamesToPaths[fileName]; | ||
filesNamesToPaths[fileName] = uniq(next); | ||
} | ||
const filesFoundPaths = result.filesFound; | ||
const foldersFoundPaths = [rootPath, ...result.directoriesFound]; | ||
return [filesPaths, filesNames, filesFoundPaths, foldersFoundPaths]; | ||
return [filesPaths, filesNames, filesNamesToPaths, filesFoundPaths, foldersFoundPaths]; | ||
} | ||
@@ -537,2 +547,7 @@ function isArray(value) { | ||
} | ||
function uniq(values) { | ||
if (values.length < 2) | ||
return values; | ||
return Array.from(new Set(values)); | ||
} | ||
function zipObject(keys, values) { | ||
@@ -555,2 +570,2 @@ const map = {}; | ||
} | ||
export { castArray, everyOf, fastJoinedPath, fastRelativePath, fastRelativeChildPath, findLastIndex, getCachePath, getFolderChildrenPaths, getFoldersChildrenPaths, getExpandedFoldersPaths, getGlobPaths, getPlugin, getPluginPath, getPluginVersion, getPlugins, getPluginsPaths, getPluginsVersions, getProjectPath, getTargetsPaths, isArray, isBoolean, isFalsy, isFunction, isInteger, isNumber, isObject, isPromise, isString, isTruthy, isUndefined, memoize, negate, noop, normalizeOptions, normalizeFormatOptions, normalizePluginOptions, normalizePrettierOptions, omit, once, pluralize, resolve, sha1hex, sha1base64, someOf, zipObject, zipObjectUnless, }; | ||
export { castArray, everyOf, fastJoinedPath, fastRelativePath, fastRelativeChildPath, findLastIndex, getCachePath, getFolderChildrenPaths, getFoldersChildrenPaths, getExpandedFoldersPaths, getGlobPaths, getPlugin, getPluginPath, getPluginVersion, getPlugins, getPluginsPaths, getPluginsVersions, getProjectPath, getTargetsPaths, isArray, isBoolean, isFalsy, isFunction, isInteger, isNumber, isObject, isPromise, isString, isTruthy, isUndefined, memoize, negate, noop, normalizeOptions, normalizeFormatOptions, normalizePluginOptions, normalizePrettierOptions, omit, once, pluralize, resolve, sha1hex, sha1base64, someOf, uniq, zipObject, zipObjectUnless, }; |
@@ -6,3 +6,3 @@ { | ||
"license": "MIT", | ||
"version": "0.2.0", | ||
"version": "0.2.1", | ||
"type": "module", | ||
@@ -42,3 +42,3 @@ "bin": { | ||
"tiny-jsonc": "^1.0.1", | ||
"tiny-readdir-glob": "^1.1.0", | ||
"tiny-readdir-glob": "^1.2.1", | ||
"tiny-spinner": "^2.0.3", | ||
@@ -45,0 +45,0 @@ "worktank": "^2.6.0", |
86812
1861
Updatedtiny-readdir-glob@^1.2.1