临时文件兜底
This commit is contained in:
1 parent
08456f26cf
commit
be058c6c80
3 files changed
+52
-3
No files matched your search
@@ -16,6 +16,24 @@ const QUIET_MS = 1000;
|
||||
// fs.watch 在网络盘/外置盘上可能漏事件,用低频兜底扫描补上。
|
||||
const FALLBACK_MS = 30000;
|
||||
|
||||
/** 删除指定目录中超过保留时间的本插件临时文件。 */
|
||||
function cleanupStaleFiles(directory, prefix, maxAgeMs, now = Date.now()) {
|
||||
let names;
|
||||
try { names = fs.readdirSync(directory); } catch (error) { return 0; }
|
||||
let removed = 0;
|
||||
for (const name of names) {
|
||||
if (!name.startsWith(prefix)) continue;
|
||||
const file = path.join(directory, name);
|
||||
try {
|
||||
const stat = fs.lstatSync(file);
|
||||
if (!stat.isFile() || now - stat.mtimeMs < maxAgeMs) continue;
|
||||
fs.unlinkSync(file);
|
||||
removed += 1;
|
||||
} catch (error) { /* 单个文件失败不影响其余清理和监听 */ }
|
||||
}
|
||||
return removed;
|
||||
}
|
||||
|
||||
function createLibraryWatcher(options) {
|
||||
const onBatch = options.onBatch || (() => {});
|
||||
const onLog = options.onLog || (() => {});
|
||||
@@ -94,7 +112,7 @@ function createLibraryWatcher(options) {
|
||||
|
||||
// 同时支持两种加载方式:Node 里用 require(跑测试),插件页面里用 <script> 标签。
|
||||
// service 插件页面的 require 解析基准不是插件目录,所以页面里必须走 script 标签。
|
||||
const libraryWatcherApi = { createLibraryWatcher, QUIET_MS, FALLBACK_MS };
|
||||
const libraryWatcherApi = { createLibraryWatcher, cleanupStaleFiles, QUIET_MS, FALLBACK_MS };
|
||||
// 优先挂 window:Eagle 插件页面里也存在 module 对象(它有 Node 模块系统),
|
||||
// 先判断 module 会让页面拿到模块却取不到 window 上的名字。Node 里没有 window,自然走 require 分支。
|
||||
if (typeof window !== 'undefined') window.SpineThumbLibraryWatcher = libraryWatcherApi;
|
||||
|
||||
Reference in new issue
Block a user