|
|
import fs from "fs"; |
|
|
import chokidar from "chokidar"; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export function watchItems(rootPath: string, cb: () => void): void { |
|
|
chokidar |
|
|
.watch(rootPath, { ignoreInitial: true, awaitWriteFinish: true }) |
|
|
.on("add", cb) |
|
|
.on("unlink", cb); |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export function getItems<Item>(settings: { |
|
|
path: string; |
|
|
resolveItem: (path: string, name: string) => Item; |
|
|
cb?: (name: string) => void; |
|
|
fileFormat?: RegExp; |
|
|
}): Item[] { |
|
|
const { |
|
|
path, |
|
|
resolveItem, |
|
|
cb, |
|
|
fileFormat = new RegExp(/(.+)(?<!\.d)\.[jt]sx?$/), |
|
|
} = settings; |
|
|
const items: Item[] = []; |
|
|
const folders: fs.Dirent[] = []; |
|
|
|
|
|
if (!fs.existsSync(path)) return []; |
|
|
|
|
|
fs.readdirSync(path, { withFileTypes: true }).forEach((item) => { |
|
|
if (item.isDirectory()) { |
|
|
folders.push(item); |
|
|
} |
|
|
|
|
|
if (fileFormat.test(item.name)) { |
|
|
|
|
|
const name = item.name.match(fileFormat)![1]; |
|
|
items.push(resolveItem(path, name)); |
|
|
cb && cb(name); |
|
|
} |
|
|
}); |
|
|
|
|
|
for (const folder of folders) { |
|
|
items.push( |
|
|
...getItems<Item>({ |
|
|
path: `${path}/${folder.name}`, |
|
|
resolveItem, |
|
|
cb, |
|
|
fileFormat, |
|
|
}), |
|
|
); |
|
|
} |
|
|
|
|
|
return items; |
|
|
} |
|
|
|