@prettier/cli
Advanced tools
Comparing version 0.2.1 to 0.2.2
@@ -16,5 +16,5 @@ #!/usr/bin/env node | ||
.usage(`${color.cyan("prettier")} ${color.yellow("[file/dir/glob...]")} ${color.green("[options]")}`) | ||
.usage(`${color.cyan("prettier")} ${color.yellow('"src/**/*.js"')} ${color.green("--check")} ${color.green("--parallel")}`) | ||
.usage(`${color.cyan("prettier")} ${color.yellow('"src/**/*.js"')} ${color.green("--check")}`) | ||
.usage(`${color.cyan("prettier")} ${color.yellow('"src/**/*.js"')} ${color.green("-l")} ${color.green("--no-cache")}`) | ||
.usage(`${color.cyan("prettier")} ${color.yellow('"src/**/*.js"')} ${color.green("--write")} ${color.green("--parallel")}`) | ||
.usage(`${color.cyan("prettier")} ${color.yellow('"src/**/*.js"')} ${color.green("--write")} ${color.green("--no-parallel")}`) | ||
.usage(`${color.cyan("prettier")} ${color.yellow("./path/to/target/file.js")} ${color.green("--cache-location")} ${color.blue("./path/to/cache/file.json")}`) | ||
@@ -90,7 +90,3 @@ /* OUTPUT OPTIONS */ | ||
.option("--plugin <package...>", "Add a plugin\nMultiple plugins are accepted\nDefaults to []", { section: "Config" }) | ||
// .option( | ||
// "--with-node-modules", | ||
// 'Process files inside the "node_modules" directory', | ||
// { section: "Config" }, | ||
// ) | ||
.option("--with-node-modules", 'Process files inside the "node_modules" directory', { section: "Config" }) | ||
/* EDITOR OPTIONS */ | ||
@@ -97,0 +93,0 @@ .option("--cursor-offset <int>", 'Print (to stderr) where a cursor at the given position would move to after formatting\nDefaults to "-1"', { |
@@ -29,5 +29,5 @@ /// <reference types="node" /> | ||
get(filePath: string): FileData; | ||
set(filePath: string, fileHashPath: string, fileFormatted: boolean, fileContentExpected: string): void; | ||
set(filePath: string, fileRelativePath: string, fileFormatted: boolean, fileContentExpected: string): void; | ||
has(filePath: string, isIgnored: () => PromiseMaybe<boolean>): Promise<boolean>; | ||
} | ||
export default Cache; |
@@ -50,6 +50,5 @@ import fs from "node:fs"; | ||
const fileRelativePath = fastRelativePath(this.rootPath, filePath); | ||
const fileHashPath = sha1base64(fileRelativePath); | ||
const save = this.set.bind(this, filePath, fileHashPath); | ||
const save = this.set.bind(this, filePath, fileRelativePath); | ||
try { | ||
const file = this.store[this.version]?.files?.[fileHashPath]; | ||
const file = this.store[this.version]?.files?.[fileRelativePath]; | ||
if (!file || !isArray(file) || file.length !== 2) | ||
@@ -71,3 +70,3 @@ return { save }; | ||
} | ||
set(filePath, fileHashPath, fileFormatted, fileContentExpected) { | ||
set(filePath, fileRelativePath, fileFormatted, fileContentExpected) { | ||
var _a, _b; | ||
@@ -80,3 +79,3 @@ try { | ||
version.modified = Date.now(); | ||
files[fileHashPath] = [hash, fileFormatted]; | ||
files[fileRelativePath] = [hash, fileFormatted]; | ||
this.dirty = true; | ||
@@ -91,4 +90,3 @@ } | ||
const fileRelativePath = fastRelativePath(this.rootPath, filePath); | ||
const fileHashPath = sha1base64(fileRelativePath); | ||
const file = this.store[this.version]?.files?.[fileHashPath]; | ||
const file = this.store[this.version]?.files?.[fileRelativePath]; | ||
if (isUndefined(file)) { | ||
@@ -99,3 +97,3 @@ const ignored = await isIgnored(); | ||
const files = (version.files || (version.files = {})); | ||
files[fileHashPath] = false; | ||
files[fileRelativePath] = false; | ||
this.dirty = true; | ||
@@ -102,0 +100,0 @@ return false; |
import yaml from "js-yaml"; | ||
import JSON5 from "json5"; | ||
import fs from "node:fs"; | ||
@@ -7,5 +8,4 @@ import path from "node:path"; | ||
import Known from "./known.js"; | ||
import { fastJoinedPath, fastRelativeChildPath } from "./utils.js"; | ||
import { isObject, isTruthy, isUndefined, memoize, noop, normalizePrettierOptions, omit, zipObjectUnless } from "./utils.js"; | ||
//TODO: Maybe completely drop support for JSON5, or implement it properly | ||
import { fastJoinedPath, fastRelativeChildPath, getModule, getModulePath } from "./utils.js"; | ||
import { isObject, isString, isTruthy, isUndefined, memoize, noop, normalizePrettierOptions, omit, zipObjectUnless } from "./utils.js"; | ||
//TODO: Maybe add support for TOML | ||
@@ -15,3 +15,3 @@ const Loaders = { | ||
const module = await import(filePath); | ||
return module.default || module.exports || module.config || module.prettier; //TODO: Streamline this | ||
return module.default || module.exports || module.prettier || module; | ||
}, | ||
@@ -28,8 +28,24 @@ json: async (filePath) => { | ||
}, | ||
package: async (filePath) => { | ||
json5: async (filePath) => { | ||
const fileContent = fs.readFileSync(filePath, "utf8"); | ||
const pkg = JSON.parse(fileContent); | ||
const config = isObject(pkg) && "prettier" in pkg ? pkg.prettier : undefined; | ||
const config = JSON5.parse(fileContent); | ||
return config; | ||
}, | ||
package: async (filePath) => { | ||
const fileBuffer = fs.readFileSync(filePath); | ||
if (!fileBuffer.includes("prettier")) | ||
return; //FIXME: Technically this breaks support for escaped chars, but why would anybody do that though? | ||
const fileContent = fileBuffer.toString("utf8"); | ||
const pkg = JSON.parse(fileContent); | ||
if (isObject(pkg) && "prettier" in pkg) { | ||
const config = pkg.prettier; | ||
if (isObject(config)) { | ||
return config; | ||
} | ||
else if (isString(config)) { | ||
const modulePath = getModulePath(config, filePath); | ||
return Loaders.js(modulePath); | ||
} | ||
} | ||
}, | ||
yaml: async (filePath) => { | ||
@@ -50,3 +66,3 @@ const fileContent = fs.readFileSync(filePath, "utf8"); | ||
".prettierrc.jsonc": Loaders.jsonc, | ||
".prettierrc.json5": Loaders.jsonc, | ||
".prettierrc.json5": Loaders.json5, | ||
".prettierrc.js": Loaders.js, | ||
@@ -64,3 +80,3 @@ ".prettierrc.cjs": Loaders.js, | ||
const loader = File2Loader[fileName] || File2Loader["default"]; | ||
const normalize = (config) => (isObject(config) ? normalizePrettierOptions(config, folderPath) : undefined); | ||
const normalize = (config) => (isObject(config) ? { ...config, ...normalizePrettierOptions(config, folderPath) } : undefined); | ||
return loader(filePath).then(normalize).catch(noop); | ||
@@ -67,0 +83,0 @@ }; |
@@ -21,3 +21,3 @@ import isBinaryPath from "is-binary-path"; | ||
const projectPath = getProjectPath(rootPath); | ||
const [filesPaths, filesNames, filesNamesToPaths, filesFoundPaths, foldersFoundPaths] = await getTargetsPaths(rootPath, options.globs); | ||
const [filesPaths, filesNames, filesNamesToPaths, filesFoundPaths, foldersFoundPaths] = await getTargetsPaths(rootPath, options.globs, options.withNodeModules); // prettier-ignore | ||
const filesPathsTargets = filesPaths.filter(negate(isBinaryPath)).sort(); | ||
@@ -24,0 +24,0 @@ const [foldersPathsTargets, foldersExtraPaths] = getExpandedFoldersPaths(foldersFoundPaths, projectPath); |
@@ -8,2 +8,3 @@ type Bin = ReturnType<typeof import("tiny-bin").default>; | ||
type FormatOptions = { | ||
[pluginOption: string]: unknown; | ||
experimentalTernaries?: boolean; | ||
@@ -44,2 +45,3 @@ arrowParens?: "avoid" | "always"; | ||
editorConfig: boolean; | ||
withNodeModules: boolean; | ||
cache: boolean; | ||
@@ -46,0 +48,0 @@ cacheLocation: string | undefined; |
@@ -16,3 +16,5 @@ import type { FormatOptions, FunctionMaybe, Key, Options, PrettierConfigWithOverrides, PrettierPlugin } from "./types.js"; | ||
declare function getFoldersChildrenPaths(foldersPaths: string[]): Promise<string[]>; | ||
declare function getGlobPaths(rootPath: string, globs: string[]): Promise<import("tiny-readdir-glob/dist/types.js").Result>; | ||
declare function getGlobPaths(rootPath: string, globs: string[], withNodeModules: boolean): Promise<import("tiny-readdir-glob/dist/types.js").Result>; | ||
declare function getModule<T = unknown>(modulePath: string): Promise<T>; | ||
declare function getModulePath(name: string, rootPath: string): string; | ||
declare const getPlugin: ((name: string) => Promise<import("prettier").Plugin<any>>) & { | ||
@@ -27,3 +29,3 @@ cache: Map<string, Promise<import("prettier").Plugin<any>>>; | ||
declare function getProjectPath(rootPath: string): string; | ||
declare function getTargetsPaths(rootPath: string, globs: string[]): Promise<[string[], string[], Record<string, string[]>, string[], string[]]>; | ||
declare function getTargetsPaths(rootPath: string, globs: string[], withNodeModules: boolean): Promise<[string[], string[], Record<string, string[]>, string[], string[]]>; | ||
declare function isArray(value: unknown): value is unknown[]; | ||
@@ -56,2 +58,2 @@ declare function isBoolean(value: unknown): value is boolean; | ||
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, uniq, zipObject, zipObjectUnless, }; | ||
export { castArray, everyOf, fastJoinedPath, fastRelativePath, fastRelativeChildPath, findLastIndex, getCachePath, getFolderChildrenPaths, getFoldersChildrenPaths, getExpandedFoldersPaths, getGlobPaths, getModule, getModulePath, 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, }; |
@@ -95,13 +95,23 @@ import findUp from "find-up-json"; | ||
} | ||
function getGlobPaths(rootPath, globs) { | ||
function getGlobPaths(rootPath, globs, withNodeModules) { | ||
return readdir(globs, { | ||
cwd: rootPath, | ||
followSymlinks: false, | ||
ignore: "**/{.git,.sl,.svn,.hg,node_modules,.DS_Store,Thumbs.db}", | ||
ignore: `**/{.git,.sl,.svn,.hg,.DS_Store,Thumbs.db${withNodeModules ? "" : ",node_modules"}}`, | ||
}); | ||
} | ||
const getPlugin = memoize(async (name) => { | ||
async function getModule(modulePath) { | ||
const moduleExports = await import(modulePath); | ||
const module = moduleExports.default || moduleExports.exports || moduleExports; | ||
return module; | ||
} | ||
function getModulePath(name, rootPath) { | ||
const rootUrl = url.pathToFileURL(rootPath); | ||
const moduleUrl = moduleResolve(name, rootUrl); | ||
const modulePath = url.fileURLToPath(moduleUrl); | ||
return modulePath; | ||
} | ||
const getPlugin = memoize((name) => { | ||
const pluginPath = getPluginPath(name); | ||
const pluginExports = await import(pluginPath); | ||
const plugin = pluginExports.default || pluginExports; | ||
const plugin = getModule(pluginPath); | ||
return plugin; | ||
@@ -111,5 +121,3 @@ }); | ||
const rootPath = path.join(process.cwd(), "index.js"); | ||
const rootUrl = url.pathToFileURL(rootPath); | ||
const pluginUrl = moduleResolve(name, rootUrl); | ||
const pluginPath = url.fileURLToPath(pluginUrl); | ||
const pluginPath = getModulePath(name, rootPath); | ||
return pluginPath; | ||
@@ -164,3 +172,3 @@ } | ||
} | ||
async function getTargetsPaths(rootPath, globs) { | ||
async function getTargetsPaths(rootPath, globs, withNodeModules) { | ||
const targetFiles = []; | ||
@@ -183,3 +191,3 @@ const targetFilesNames = []; | ||
} | ||
const result = await getGlobPaths(rootPath, targetGlobs); | ||
const result = await getGlobPaths(rootPath, targetGlobs, withNodeModules); | ||
const filesPaths = [...targetFiles, ...result.files]; | ||
@@ -190,3 +198,3 @@ const filesNames = [...targetFilesNames, ...result.filesFoundNames]; | ||
const prev = filesNamesToPaths[fileName]; | ||
const next = Array.isArray(prev) ? prev.concat(filesNamesToPaths[fileName]) : filesNamesToPaths[fileName]; | ||
const next = Array.isArray(prev) ? prev.concat(targetFilesNamesToPaths[fileName]) : targetFilesNamesToPaths[fileName]; | ||
filesNamesToPaths[fileName] = uniq(next); | ||
@@ -274,2 +282,3 @@ } | ||
const editorConfig = "editorconfig" in options ? !!options.editorconfig : true; | ||
const withNodeModules = "withNodeModules" in options ? !!options.withNodeModules : false; | ||
const cache = "cache" in options ? !!options.cache : true; | ||
@@ -291,2 +300,3 @@ const cacheLocation = "cacheLocation" in options && isString(options.cacheLocation) ? options.cacheLocation : undefined; | ||
editorConfig, | ||
withNodeModules, | ||
cache, | ||
@@ -392,4 +402,4 @@ cacheLocation, | ||
} | ||
if ("plugin" in options) { | ||
const value = options.plugin; | ||
if ("plugin" in options || "plugins" in options) { | ||
const value = options["plugin"] || options["plugins"]; | ||
if (isArray(value) && value.every(isString)) { | ||
@@ -574,2 +584,2 @@ formatOptions.plugins = value; | ||
} | ||
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, }; | ||
export { castArray, everyOf, fastJoinedPath, fastRelativePath, fastRelativeChildPath, findLastIndex, getCachePath, getFolderChildrenPaths, getFoldersChildrenPaths, getExpandedFoldersPaths, getGlobPaths, getModule, getModulePath, 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.1", | ||
"version": "0.2.2", | ||
"type": "module", | ||
@@ -37,2 +37,3 @@ "bin": { | ||
"json-sorted-stringify": "^1.0.0", | ||
"json5": "^2.2.3", | ||
"kasi": "^1.1.0", | ||
@@ -39,0 +40,0 @@ "pioppo": "^1.1.0", |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
88129
1885
18
+ Addedjson5@^2.2.3
+ Addedjson5@2.2.3(transitive)