perf(core): check files before interacting with them (#13090)
* perf(core): check files before interacting with them * refactor: inline fs.exists.sync -> nodeFs.existsSync
This commit is contained in:
parent
47537688d2
commit
d0fcbfccdd
@ -1,4 +1,5 @@
|
|||||||
import buildDebug from "debug";
|
import buildDebug from "debug";
|
||||||
|
import nodeFs from "fs";
|
||||||
import path from "path";
|
import path from "path";
|
||||||
import json5 from "json5";
|
import json5 from "json5";
|
||||||
import gensync from "gensync";
|
import gensync from "gensync";
|
||||||
@ -36,11 +37,11 @@ const RELATIVE_CONFIG_FILENAMES = [
|
|||||||
|
|
||||||
const BABELIGNORE_FILENAME = ".babelignore";
|
const BABELIGNORE_FILENAME = ".babelignore";
|
||||||
|
|
||||||
export function* findConfigUpwards(rootDir: string): Handler<string | null> {
|
export function findConfigUpwards(rootDir: string): string | null {
|
||||||
let dirname = rootDir;
|
let dirname = rootDir;
|
||||||
while (true) {
|
for (;;) {
|
||||||
for (const filename of ROOT_CONFIG_FILENAMES) {
|
for (const filename of ROOT_CONFIG_FILENAMES) {
|
||||||
if (yield* fs.exists(path.join(dirname, filename))) {
|
if (nodeFs.existsSync(path.join(dirname, filename))) {
|
||||||
return dirname;
|
return dirname;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -165,7 +166,7 @@ const readConfigJS = makeStrongCache(function* readConfigJS(
|
|||||||
caller: CallerMetadata | void;
|
caller: CallerMetadata | void;
|
||||||
}>,
|
}>,
|
||||||
): Handler<ConfigFile | null> {
|
): Handler<ConfigFile | null> {
|
||||||
if (!fs.exists.sync(filepath)) {
|
if (!nodeFs.existsSync(filepath)) {
|
||||||
cache.never();
|
cache.never();
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|||||||
@ -11,11 +11,10 @@ import type { CallerMetadata } from "../validation/options";
|
|||||||
|
|
||||||
export type { ConfigFile, IgnoreFile, RelativeConfig, FilePackageData };
|
export type { ConfigFile, IgnoreFile, RelativeConfig, FilePackageData };
|
||||||
|
|
||||||
// eslint-disable-next-line require-yield
|
export function findConfigUpwards(
|
||||||
export function* findConfigUpwards(
|
|
||||||
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
||||||
rootDir: string,
|
rootDir: string,
|
||||||
): Handler<string | null> {
|
): string | null {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -23,6 +23,8 @@ export function makeStaticFileCache<T>(
|
|||||||
}
|
}
|
||||||
|
|
||||||
function fileMtime(filepath: string): number | null {
|
function fileMtime(filepath: string): number | null {
|
||||||
|
if (!nodeFs.existsSync(filepath)) return null;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
return +nodeFs.statSync(filepath).mtime;
|
return +nodeFs.statSync(filepath).mtime;
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
|
|||||||
@ -23,21 +23,18 @@ import {
|
|||||||
import type { ConfigFile, IgnoreFile } from "./files";
|
import type { ConfigFile, IgnoreFile } from "./files";
|
||||||
import { resolveTargets } from "./resolve-targets";
|
import { resolveTargets } from "./resolve-targets";
|
||||||
|
|
||||||
function* resolveRootMode(
|
function resolveRootMode(rootDir: string, rootMode: RootMode): string {
|
||||||
rootDir: string,
|
|
||||||
rootMode: RootMode,
|
|
||||||
): Handler<string> {
|
|
||||||
switch (rootMode) {
|
switch (rootMode) {
|
||||||
case "root":
|
case "root":
|
||||||
return rootDir;
|
return rootDir;
|
||||||
|
|
||||||
case "upward-optional": {
|
case "upward-optional": {
|
||||||
const upwardRootDir = yield* findConfigUpwards(rootDir);
|
const upwardRootDir = findConfigUpwards(rootDir);
|
||||||
return upwardRootDir === null ? rootDir : upwardRootDir;
|
return upwardRootDir === null ? rootDir : upwardRootDir;
|
||||||
}
|
}
|
||||||
|
|
||||||
case "upward": {
|
case "upward": {
|
||||||
const upwardRootDir = yield* findConfigUpwards(rootDir);
|
const upwardRootDir = findConfigUpwards(rootDir);
|
||||||
if (upwardRootDir !== null) return upwardRootDir;
|
if (upwardRootDir !== null) return upwardRootDir;
|
||||||
|
|
||||||
throw Object.assign(
|
throw Object.assign(
|
||||||
@ -89,7 +86,7 @@ export default function* loadPrivatePartialConfig(
|
|||||||
cloneInputAst = true,
|
cloneInputAst = true,
|
||||||
} = args;
|
} = args;
|
||||||
const absoluteCwd = path.resolve(cwd);
|
const absoluteCwd = path.resolve(cwd);
|
||||||
const absoluteRootDir = yield* resolveRootMode(
|
const absoluteRootDir = resolveRootMode(
|
||||||
path.resolve(absoluteCwd, rootDir),
|
path.resolve(absoluteCwd, rootDir),
|
||||||
rootMode,
|
rootMode,
|
||||||
);
|
);
|
||||||
|
|||||||
@ -8,18 +8,6 @@ export const readFile = gensync<(filepath: string, encoding: "utf8") => string>(
|
|||||||
},
|
},
|
||||||
);
|
);
|
||||||
|
|
||||||
export const exists = gensync<(filepath: string) => boolean>({
|
|
||||||
sync(path) {
|
|
||||||
try {
|
|
||||||
fs.accessSync(path);
|
|
||||||
return true;
|
|
||||||
} catch {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
},
|
|
||||||
errback: (path, cb) => fs.access(path, undefined, err => cb(null, !err)),
|
|
||||||
});
|
|
||||||
|
|
||||||
export const stat = gensync<typeof fs.statSync>({
|
export const stat = gensync<typeof fs.statSync>({
|
||||||
sync: fs.statSync,
|
sync: fs.statSync,
|
||||||
errback: fs.stat,
|
errback: fs.stat,
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user