Files

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

40 lines
830 B
TypeScript
Raw Permalink Normal View History

2025-12-12 14:26:25 +09:00
import fs from 'fs';
import path from 'path';
import findIndexFile from './findIndexFile';
const hooksRegexp = /use([A-Z][a-z]+)+\.(js|tsx|ts)/;
/**
* Returns the hook source in a flat array.
* @param {string} directory
* @param {Array<{ filename: string }>} hooks
*/
export default function findHooks(
directory: string,
hooks: { filename: string; indexFilename: string | null }[] = [],
) {
const items = fs.readdirSync(directory);
items.forEach((item) => {
const itemPath = path.resolve(directory, item);
if (fs.statSync(itemPath).isDirectory()) {
findHooks(itemPath, hooks);
return;
}
if (!hooksRegexp.test(item)) {
return;
}
const indexFile = findIndexFile(directory);
hooks.push({
filename: itemPath,
...indexFile,
});
});
return hooks;
}