UNPKG

f2e-server3

Version:

f2e-server 3.0

188 lines (187 loc) 7.76 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); __setModuleDefault(result, mod); return result; }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.beginWatch = exports.inputProvider = void 0; const node_path_1 = __importDefault(require("node:path")); const promises_1 = __importDefault(require("node:fs/promises")); const _ = __importStar(require("../utils/misc")); const logger_1 = __importDefault(require("../utils/logger")); const utils_1 = require("../utils"); const node_process_1 = require("node:process"); const inputProvider = (options, store) => { const { buildFilter, onSet, root } = options; return async function build(pathname) { // 路径被过滤,直接返回 if (pathname && (store.ignores.has(pathname) || !buildFilter || !buildFilter(pathname, 0))) { return; } const absolutePath = node_path_1.default.join(root, pathname); const stat = await promises_1.default.stat(absolutePath); if (stat.isDirectory()) { store.save({ originPath: pathname, outputPath: pathname, data: store._get(pathname) || {}, }); try { const files = await promises_1.default.readdir(absolutePath); await Promise.all(files.map(file => build(pathname ? (pathname + '/' + file) : file))); } catch (e) { logger_1.default.error(e); } } if (stat.isFile()) { if (buildFilter && !buildFilter(pathname, stat.size)) { return; } try { const data = await promises_1.default.readFile(absolutePath, _.isText(pathname) ? 'utf-8' : undefined); const result = await onSet(pathname, data, store); store.save(result); } catch (e) { logger_1.default.error(e); } } }; }; exports.inputProvider = inputProvider; const beginWatch = (options, store, build) => async () => { const { buildWatcher, watch, watch_timeout = 100, watchFilter, root } = options; if (watch && watchFilter) { let chokidar = undefined; if (utils_1.ENGINE_TYPE === 'deno') { logger_1.default.debug(`Deno 环境下 chokidar 不可用, 使用 Deno.watchFs 监听文件变化`); } else { try { chokidar = await (0, utils_1.dynamicImport)('chokidar'); } catch (e) { if (process.platform === 'win32') { logger_1.default.debug('chokidar 未安装, 使用 fs.watch 监听文件变化'); } else { logger_1.default.error('chokidar 未安装, 请安装: `npm i chokidar -D`'); (0, node_process_1.exit)(1); } } } const watcher_map = new Map(); const loop_watcher = function loop() { watcher_map.forEach((item, pathname) => { if (item.ready) { watcher_map.delete(pathname); item.excute(); } else { item.ready = true; } }); setTimeout(loop, watch_timeout); }; loop_watcher(); if (chokidar) { chokidar.watch(root, { ignoreInitial: true, ignored: [(filename) => { const p = _.pathname_fixer(node_path_1.default.relative(root, filename)); return !!p && !watchFilter(p); }] }).on('all', async (eventType, filename) => { const p = _.pathname_fixer(node_path_1.default.relative(root, filename)); if (p && watchFilter(p)) { watcher_map.set(p, { ready: false, excute: async () => { await build(p); buildWatcher && buildWatcher(p, eventType, build, store); } }); } }); } else { const doWatcher = async (filename, eventType) => { if (filename) { const pathname = _.pathname_fixer(filename); if (watchFilter(pathname)) { watcher_map.set(pathname, { ready: false, excute: async () => { await build(pathname); buildWatcher && buildWatcher(pathname, eventType, build, store); } }); } } }; switch (utils_1.ENGINE_TYPE) { case 'deno': (async () => { try { const watcher = Deno.watchFs(root, { recursive: true }); for await (const info of watcher) { if (info.kind === 'modify') { info.paths.forEach(p => doWatcher(node_path_1.default.relative(root, p), info.kind)); } } } catch (err) { logger_1.default.error(err); } })(); break; default: (async () => { try { const watcher = promises_1.default.watch(root, { recursive: true, persistent: true, }); for await (const info of watcher) { switch (info.eventType) { case 'rename': break; case 'change': info.filename && doWatcher(info.filename, info.eventType); break; } } } catch (err) { logger_1.default.error(err); } })(); } } } else { logger_1.default.error('watch 需要同时配置 watchFilter'); } }; exports.beginWatch = beginWatch;