Simplify getFileInfo() (#12802)
This commit is contained in:
1 parent
f105e010e1
commit
133a25091f
3 files changed
+43
-66
No files matched your search
@@ -1,20 +1,23 @@
|
||||
import path from "node:path";
|
||||
import ignore from "ignore";
|
||||
import getFileContentOrNull from "../utils/get-file-content-or-null.js";
|
||||
import readFile from "../utils/read-file.js";
|
||||
|
||||
/**
|
||||
* @param {string?} ignorePath
|
||||
* @param {string?} ignoreFilePath
|
||||
* @param {boolean?} withNodeModules
|
||||
*/
|
||||
async function createIgnorer(ignorePath, withNodeModules) {
|
||||
const ignoreContent = ignorePath
|
||||
? await getFileContentOrNull(path.resolve(ignorePath))
|
||||
: null;
|
||||
async function createIgnorer(ignoreFilePath, withNodeModules) {
|
||||
let content;
|
||||
|
||||
const ignorer = ignore({ allowRelativePaths: true }).add(ignoreContent || "");
|
||||
if (ignoreFilePath) {
|
||||
content = await readFile(path.resolve(ignoreFilePath));
|
||||
}
|
||||
|
||||
const ignorer = ignore({ allowRelativePaths: true }).add(content ?? "");
|
||||
if (!withNodeModules) {
|
||||
ignorer.add("node_modules");
|
||||
}
|
||||
|
||||
return ignorer;
|
||||
}
|
||||
|
||||
|
||||
+28
-54
@@ -1,6 +1,6 @@
|
||||
import path from "node:path";
|
||||
import { inferParser } from "../main/options.js";
|
||||
import * as config from "../config/resolve-config.js";
|
||||
import { resolveConfig } from "../config/resolve-config.js";
|
||||
import createIgnorer from "./create-ignorer.js";
|
||||
|
||||
/**
|
||||
@@ -10,78 +10,52 @@ import createIgnorer from "./create-ignorer.js";
|
||||
|
||||
/**
|
||||
* @param {string} filePath
|
||||
* @param {FileInfoOptions} opts
|
||||
* @param {FileInfoOptions} options
|
||||
* @returns {Promise<FileInfoResult>}
|
||||
*
|
||||
* Please note that prettier.getFileInfo() expects opts.plugins to be an array of paths,
|
||||
* Please note that prettier.getFileInfo() expects options.plugins to be an array of paths,
|
||||
* not an object. A transformation from this array to an object is automatically done
|
||||
* internally by the method wrapper. See withPlugins() in index.js.
|
||||
*/
|
||||
async function getFileInfo(filePath, opts) {
|
||||
async function getFileInfo(filePath, options) {
|
||||
if (typeof filePath !== "string") {
|
||||
throw new TypeError(
|
||||
`expect \`filePath\` to be a string, got \`${typeof filePath}\``
|
||||
);
|
||||
}
|
||||
|
||||
const ignorer = await createIgnorer(opts.ignorePath, opts.withNodeModules);
|
||||
return _getFileInfo({
|
||||
ignorer,
|
||||
filePath,
|
||||
plugins: opts.plugins,
|
||||
resolveConfig: opts.resolveConfig,
|
||||
ignorePath: opts.ignorePath,
|
||||
});
|
||||
}
|
||||
const ignored = await isIgnored(filePath, options);
|
||||
|
||||
function getFileParser(resolvedConfig, filePath, plugins) {
|
||||
if (resolvedConfig && resolvedConfig.parser) {
|
||||
return resolvedConfig.parser;
|
||||
let inferredParser;
|
||||
if (!ignored) {
|
||||
inferredParser = await getParser(filePath, options);
|
||||
}
|
||||
|
||||
const inferredParser = inferParser(filePath, plugins);
|
||||
|
||||
if (inferredParser) {
|
||||
return inferredParser;
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
async function _getFileInfo({
|
||||
ignorer,
|
||||
filePath,
|
||||
plugins,
|
||||
resolveConfig = false,
|
||||
ignorePath,
|
||||
}) {
|
||||
const normalizedFilePath = normalizeFilePath(filePath, ignorePath);
|
||||
|
||||
const fileInfo = {
|
||||
ignored: ignorer.ignores(normalizedFilePath),
|
||||
inferredParser: null,
|
||||
return {
|
||||
ignored,
|
||||
inferredParser: inferredParser ?? null,
|
||||
};
|
||||
|
||||
if (fileInfo.ignored) {
|
||||
return fileInfo;
|
||||
}
|
||||
|
||||
let resolvedConfig;
|
||||
|
||||
if (resolveConfig) {
|
||||
const resolvedConfig = await config.resolveConfig(filePath);
|
||||
fileInfo.inferredParser = getFileParser(resolvedConfig, filePath, plugins);
|
||||
return fileInfo;
|
||||
}
|
||||
|
||||
fileInfo.inferredParser = getFileParser(resolvedConfig, filePath, plugins);
|
||||
return fileInfo;
|
||||
}
|
||||
|
||||
function normalizeFilePath(filePath, ignorePath) {
|
||||
return ignorePath
|
||||
async function getParser(filePath, options) {
|
||||
let config;
|
||||
if (options.resolveConfig) {
|
||||
config = await resolveConfig(filePath);
|
||||
}
|
||||
|
||||
return config?.parser ?? inferParser(filePath, options.plugins);
|
||||
}
|
||||
|
||||
async function isIgnored(filePath, options) {
|
||||
const { ignorePath, withNodeModules } = options;
|
||||
|
||||
const ignorer = await createIgnorer(ignorePath, withNodeModules);
|
||||
|
||||
const normalizedFilePath = ignorePath
|
||||
? path.relative(path.dirname(ignorePath), filePath)
|
||||
: filePath;
|
||||
|
||||
return ignorer.ignores(normalizedFilePath);
|
||||
}
|
||||
|
||||
export default getFileInfo;
|
||||
@@ -2,18 +2,18 @@ import { promises as fsPromises } from "node:fs";
|
||||
|
||||
/**
|
||||
* @param {string} filename
|
||||
* @returns {Promise<null | string>}
|
||||
* @returns {Promise<undefined | string>}
|
||||
*/
|
||||
async function getFileContentOrNull(filename) {
|
||||
async function readFile(filename) {
|
||||
try {
|
||||
return await fsPromises.readFile(filename, "utf8");
|
||||
} catch (error) {
|
||||
if (error && error.code === "ENOENT") {
|
||||
return null;
|
||||
if (error.code === "ENOENT") {
|
||||
return;
|
||||
}
|
||||
|
||||
throw new Error(`Unable to read ${filename}: ${error.message}`);
|
||||
}
|
||||
}
|
||||
|
||||
export default getFileContentOrNull;
|
||||
export default readFile;
|
||||
Reference in new issue
Block a user