main
ts 30 lines 1.09 KB
Raw
1 import { Callback, Dict } from "./cross"
2 import { basename, extname, join } from 'path'
3 import { watchDir } from './util-files'
4 import { debounceAsync } from './debounceAsync'
5 import { readdir } from 'fs/promises'
6 import { configReady } from './config'
7
8 export const ICONS_FOLDER = 'icons'
9
10 export type CustomizedIcons = undefined | Dict<string>
11 export let customizedIcons: CustomizedIcons
12 configReady.then(() => { // wait for cwd to be defined
13 watchIconsFolder('.', v => customizedIcons = v)
14 })
15 export function watchIconsFolder(parentFolder: string, cb: Callback<CustomizedIcons>) {
16 const iconsFolder = join(parentFolder, ICONS_FOLDER)
17 const watcher = watchDir(iconsFolder, debounceAsync(async () => {
18 let res: any = {} // reset
19 try {
20 for (const f of await readdir(iconsFolder, { withFileTypes: true })) {
21 if (!f.isFile()) continue
22 const k = basename(f.name, extname(f.name))
23 res[k] = f.name
24 }
25 cb(res)
26 }
27 catch { cb(undefined) } // no such dir
28 }), true)
29 return () => watcher.stop()
30 }