reference/scripts/watch.mjs

33 lines
1.1 KiB
JavaScript
Raw Normal View History

2022-09-29 23:09:45 +08:00
import path from 'path';
import chokidar from 'chokidar';
import { getStat } from 'recursive-readdir-files';
2022-11-20 03:26:06 +08:00
import { run, DOCS, createHTML, copyCSSFile, copyJSFile } from './index.mjs';
2022-09-29 23:09:45 +08:00
2022-10-29 00:24:39 +08:00
(async () => {
2022-09-29 23:09:45 +08:00
await run();
2022-10-29 00:24:39 +08:00
const homeMdPath = path.relative(process.cwd(), 'README.md');
2022-11-20 03:26:06 +08:00
const cssDirPath = path.relative(process.cwd(), 'scripts/style');
const jsDirPath = path.relative(process.cwd(), 'scripts/js');
const watcher = chokidar.watch([DOCS, homeMdPath, cssDirPath, jsDirPath], {
2022-09-29 23:09:45 +08:00
ignored: /(^|[\/\\])\../, // ignore dotfiles
2022-10-29 00:24:39 +08:00
persistent: true,
2022-09-29 23:09:45 +08:00
});
2022-10-29 00:24:39 +08:00
watcher
2022-11-20 03:26:06 +08:00
.on('change', async (filepath) => {
if (filepath.endsWith('.md')) {
const stats = await getStat(filepath);
createHTML([stats]);
}
if (filepath.endsWith('.css')) {
copyCSSFile(filepath);
console.log(`♻️ \x1b[32;1m ${path.relative(cssDirPath, filepath)} \x1b[0m`);
}
if (filepath.endsWith('.js')) {
copyJSFile(filepath);
console.log(`♻️ \x1b[32;1m ${path.relative(jsDirPath, filepath)} \x1b[0m`);
}
2022-10-29 00:24:39 +08:00
})
.on('error', (error) => console.log(`Watcher error: ${error}`));
})();