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:
Chris West 2021-04-27 23:41:18 +01:00 committed by GitHub
parent 47537688d2
commit d0fcbfccdd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 13 additions and 26 deletions

View File

@ -1,4 +1,5 @@
import buildDebug from "debug";
import nodeFs from "fs";
import path from "path";
import json5 from "json5";
import gensync from "gensync";
@ -36,11 +37,11 @@ const RELATIVE_CONFIG_FILENAMES = [
const BABELIGNORE_FILENAME = ".babelignore";
export function* findConfigUpwards(rootDir: string): Handler<string | null> {
export function findConfigUpwards(rootDir: string): string | null {
let dirname = rootDir;
while (true) {
for (;;) {
for (const filename of ROOT_CONFIG_FILENAMES) {
if (yield* fs.exists(path.join(dirname, filename))) {
if (nodeFs.existsSync(path.join(dirname, filename))) {
return dirname;
}
}
@ -165,7 +166,7 @@ const readConfigJS = makeStrongCache(function* readConfigJS(
caller: CallerMetadata | void;
}>,
): Handler<ConfigFile | null> {
if (!fs.exists.sync(filepath)) {
if (!nodeFs.existsSync(filepath)) {
cache.never();
return null;
}

View File

@ -11,11 +11,10 @@ import type { CallerMetadata } from "../validation/options";
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
rootDir: string,
): Handler<string | null> {
): string | null {
return null;
}

View File

@ -23,6 +23,8 @@ export function makeStaticFileCache<T>(
}
function fileMtime(filepath: string): number | null {
if (!nodeFs.existsSync(filepath)) return null;
try {
return +nodeFs.statSync(filepath).mtime;
} catch (e) {

View File

@ -23,21 +23,18 @@ import {
import type { ConfigFile, IgnoreFile } from "./files";
import { resolveTargets } from "./resolve-targets";
function* resolveRootMode(
rootDir: string,
rootMode: RootMode,
): Handler<string> {
function resolveRootMode(rootDir: string, rootMode: RootMode): string {
switch (rootMode) {
case "root":
return rootDir;
case "upward-optional": {
const upwardRootDir = yield* findConfigUpwards(rootDir);
const upwardRootDir = findConfigUpwards(rootDir);
return upwardRootDir === null ? rootDir : upwardRootDir;
}
case "upward": {
const upwardRootDir = yield* findConfigUpwards(rootDir);
const upwardRootDir = findConfigUpwards(rootDir);
if (upwardRootDir !== null) return upwardRootDir;
throw Object.assign(
@ -89,7 +86,7 @@ export default function* loadPrivatePartialConfig(
cloneInputAst = true,
} = args;
const absoluteCwd = path.resolve(cwd);
const absoluteRootDir = yield* resolveRootMode(
const absoluteRootDir = resolveRootMode(
path.resolve(absoluteCwd, rootDir),
rootMode,
);

View File

@ -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>({
sync: fs.statSync,
errback: fs.stat,