Skip to content

Commit 008e3a7

Browse files
committedNov 1, 2023
Reuse loadFile and ignore fake requests (resolves #325)
1 parent e37f15b commit 008e3a7

File tree

4 files changed

+22
-8
lines changed

4 files changed

+22
-8
lines changed
 

‎src/plugins/husky/index.ts

+4-3
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
1-
import { readFileSync } from 'fs';
21
import { _getDependenciesFromScripts } from '../../binaries/index.js';
32
import { getGitHookPaths } from '../../util/git.js';
43
import { FAKE_PATH } from '../../util/loader.js';
54
import { timerify } from '../../util/Performance.js';
6-
import { hasDependency } from '../../util/plugin.js';
5+
import { hasDependency, loadFile } from '../../util/plugin.js';
76
import type { IsPluginEnabledCallback, GenericPluginCallback } from '../../types/plugins.js';
87

98
// https://typicode.github.io/husky
@@ -25,7 +24,9 @@ const findHuskyDependencies: GenericPluginCallback = async (configFilePath, opti
2524

2625
if (isProduction || configFilePath === FAKE_PATH) return [];
2726

28-
const script = readFileSync(configFilePath);
27+
const script = await loadFile(configFilePath);
28+
29+
if (!script) return [];
2930

3031
return _getDependenciesFromScripts(String(script), {
3132
cwd,

‎src/plugins/lefthook/index.ts

+5-3
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
1-
import { readFileSync } from 'fs';
21
import { _getDependenciesFromScripts } from '../../binaries/index.js';
32
import { getGitHookPaths } from '../../util/git.js';
43
import { getValuesByKeyDeep } from '../../util/object.js';
54
import { timerify } from '../../util/Performance.js';
6-
import { hasDependency, load } from '../../util/plugin.js';
5+
import { hasDependency, load, loadFile } from '../../util/plugin.js';
76
import { fromBinary } from '../../util/protocols.js';
87
import type { IsPluginEnabledCallback, GenericPluginCallback } from '../../types/plugins.js';
98

@@ -35,7 +34,10 @@ const findLefthookDependencies: GenericPluginCallback = async (configFilePath, o
3534
return [...lefthook, ..._getDependenciesFromScripts(scripts, { cwd, manifest, knownGlobalsOnly: true })];
3635
}
3736

38-
const script = readFileSync(configFilePath, 'utf8');
37+
const script = await loadFile(configFilePath);
38+
39+
if (!script) return [];
40+
3941
const scriptDependencies = _getDependenciesFromScripts([script], { cwd, manifest, knownGlobalsOnly: false });
4042
const matches = scriptDependencies.find(dep => dependencies.includes(fromBinary(dep)));
4143
return matches ? [matches] : [];

‎src/util/loader.ts

+12-1
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,21 @@ const load = async (filePath: string) => {
3131
return imported.default ?? imported;
3232
}
3333

34-
return jiti(filePath);
34+
return await jiti(filePath);
35+
} catch (error) {
36+
throw new LoaderError(`Error loading ${filePath}`, { cause: error });
37+
}
38+
};
39+
40+
const loadFileAsync = async (filePath: string) => {
41+
// TODO: Turn into a config issue warning
42+
if (filePath === FAKE_PATH) return;
43+
try {
44+
return await loadFile(filePath);
3545
} catch (error) {
3646
throw new LoaderError(`Error loading ${filePath}`, { cause: error });
3747
}
3848
};
3949

4050
export const _load = timerify(load);
51+
export const _loadFile = timerify(loadFileAsync);

‎src/util/plugin.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
export { _load as load } from './loader.js';
1+
export { _load as load, _loadFile as loadFile } from './loader.js';
22
import { arrayify } from './array.js';
33
import type { RawPluginConfiguration } from '../types/config.js';
44

0 commit comments

Comments
 (0)