mm_scanner 1.0.2 → 1.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/index.js +2 -0
- package/lib/cache_store.js +151 -0
- package/lib/cache_update.js +109 -0
- package/lib/diff_util.js +33 -0
- package/lib/fs_util.js +50 -0
- package/lib/instance_registry.js +92 -0
- package/lib/path_util.js +58 -0
- package/lib/scanner.js +302 -900
- package/lib/scanner_config.js +36 -0
- package/lib/symbols.js +8 -0
- package/lib/watch_manager.js +170 -0
- package/lib/watcher.js +119 -0
- package/package.json +5 -3
package/index.js
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
const Scanner = require('./lib/scanner');
|
|
2
|
+
const { Watcher } = require('mm_watcher');
|
|
2
3
|
const Admin = require('mm_corejs').Admin;
|
|
3
4
|
|
|
4
5
|
const admin = new Admin();
|
|
@@ -6,6 +7,7 @@ admin.setClass(Scanner);
|
|
|
6
7
|
|
|
7
8
|
module.exports = {
|
|
8
9
|
Scanner,
|
|
10
|
+
Watcher,
|
|
9
11
|
admin,
|
|
10
12
|
scannerAdmin(scope, config) {
|
|
11
13
|
if (!scope) {
|
|
@@ -0,0 +1,151 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const { getMtime } = require('./fs_util');
|
|
4
|
+
|
|
5
|
+
/* 缓存条目上限,超出后整体清空 */
|
|
6
|
+
const CACHE_MAX = 200;
|
|
7
|
+
/* 短时间窗口内直接复用,避免每次重复校验目录与文件的时间戳 */
|
|
8
|
+
const CACHE_TTL = 2000;
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* 扫描结果缓存
|
|
12
|
+
* @description 跨实例共享同一目录的遍历结果,未监听时按时间戳校验有效性
|
|
13
|
+
*/
|
|
14
|
+
class CacheStore {
|
|
15
|
+
/** 缓存键 -> 缓存项 */
|
|
16
|
+
#stores = new Map();
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* 读取缓存项,不做有效性校验
|
|
20
|
+
* @param {string} key 缓存键
|
|
21
|
+
* @returns {object|null} 缓存项,不存在返回 null
|
|
22
|
+
*/
|
|
23
|
+
readCache(key) {
|
|
24
|
+
return this.#stores.get(key) || null;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* 读取仍然有效的缓存
|
|
29
|
+
* @param {string} key 缓存键
|
|
30
|
+
* @returns {object|null} 缓存项,不存在或已失效返回 null
|
|
31
|
+
*/
|
|
32
|
+
getCache(key) {
|
|
33
|
+
const cache = this.#stores.get(key);
|
|
34
|
+
if (!cache) {
|
|
35
|
+
return null;
|
|
36
|
+
}
|
|
37
|
+
if (cache.watched) {
|
|
38
|
+
return cache;
|
|
39
|
+
}
|
|
40
|
+
const now = Date.now();
|
|
41
|
+
if (now - cache.time <= CACHE_TTL) {
|
|
42
|
+
return cache;
|
|
43
|
+
}
|
|
44
|
+
if (this._isCacheValid(cache)) {
|
|
45
|
+
cache.time = now;
|
|
46
|
+
return cache;
|
|
47
|
+
}
|
|
48
|
+
this.#stores.delete(key);
|
|
49
|
+
return null;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* 判断写入新缓存键是否会触发上限淘汰
|
|
54
|
+
* @param {string} key 缓存键
|
|
55
|
+
* @returns {boolean} 已达上限且键不存在返回 true
|
|
56
|
+
*/
|
|
57
|
+
isOverflow(key) {
|
|
58
|
+
return this.#stores.size >= CACHE_MAX && !this.#stores.has(key);
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* 写入扫描缓存
|
|
63
|
+
* @description 已有监听器时保留监听标记,避免监听期间退化回时间戳校验
|
|
64
|
+
* @param {string} key 缓存键
|
|
65
|
+
* @param {{ dir: string, extensions: Array<string>, ignore: Array<string>, items: Array<object>, dirs: Array<string> }} options 缓存内容
|
|
66
|
+
* @returns {void}
|
|
67
|
+
*/
|
|
68
|
+
setCache(key, options) {
|
|
69
|
+
const old = this.#stores.get(key);
|
|
70
|
+
const watched = !!(old && old.watched);
|
|
71
|
+
this.#stores.set(key, this._createCache(options, watched));
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
/**
|
|
75
|
+
* 清空全部缓存项
|
|
76
|
+
* @returns {void}
|
|
77
|
+
*/
|
|
78
|
+
clear() {
|
|
79
|
+
this.#stores.clear();
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
/**
|
|
83
|
+
* 记录缓存对应的目录与文件时间戳
|
|
84
|
+
* @param {object} cache 缓存项
|
|
85
|
+
* @returns {void}
|
|
86
|
+
*/
|
|
87
|
+
captureMtimes(cache) {
|
|
88
|
+
cache.dir_mtimes = [];
|
|
89
|
+
for (let i = 0; i < cache.dirs.length; i++) {
|
|
90
|
+
cache.dir_mtimes.push(getMtime(cache.dirs[i]));
|
|
91
|
+
}
|
|
92
|
+
cache.file_mtimes = [];
|
|
93
|
+
for (let i = 0; i < cache.items.length; i++) {
|
|
94
|
+
cache.file_mtimes.push(getMtime(cache.items[i].absPath));
|
|
95
|
+
}
|
|
96
|
+
cache.time = Date.now();
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
/**
|
|
100
|
+
* 校验缓存是否有效
|
|
101
|
+
* @description 目录时间戳变化表示增删条目,文件时间戳变化表示内容修改
|
|
102
|
+
* @param {object} cache 缓存项
|
|
103
|
+
* @returns {boolean} 全部未变化返回 true
|
|
104
|
+
* @private
|
|
105
|
+
*/
|
|
106
|
+
_isCacheValid(cache) {
|
|
107
|
+
for (let i = 0; i < cache.dirs.length; i++) {
|
|
108
|
+
if (getMtime(cache.dirs[i]) !== cache.dir_mtimes[i]) {
|
|
109
|
+
return false;
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
for (let i = 0; i < cache.items.length; i++) {
|
|
113
|
+
if (getMtime(cache.items[i].absPath) !== cache.file_mtimes[i]) {
|
|
114
|
+
return false;
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
return true;
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
/**
|
|
121
|
+
* 创建缓存项
|
|
122
|
+
* @param {{ dir: string, extensions: Array<string>, ignore: Array<string>, items: Array<object>, dirs: Array<string> }} options 缓存内容
|
|
123
|
+
* @param {boolean} watched 是否已开启监听
|
|
124
|
+
* @returns {object} 缓存项
|
|
125
|
+
* @private
|
|
126
|
+
*/
|
|
127
|
+
_createCache(options, watched) {
|
|
128
|
+
const items = [];
|
|
129
|
+
for (let i = 0; i < options.items.length; i++) {
|
|
130
|
+
items.push({ ...options.items[i] });
|
|
131
|
+
}
|
|
132
|
+
const cache = {
|
|
133
|
+
dir: options.dir,
|
|
134
|
+
extensions: [...options.extensions],
|
|
135
|
+
ignore: [...options.ignore],
|
|
136
|
+
items,
|
|
137
|
+
dirs: [...options.dirs],
|
|
138
|
+
watched: !!watched,
|
|
139
|
+
dir_mtimes: [],
|
|
140
|
+
file_mtimes: [],
|
|
141
|
+
time: Date.now(),
|
|
142
|
+
};
|
|
143
|
+
if (!cache.watched) {
|
|
144
|
+
this.captureMtimes(cache);
|
|
145
|
+
}
|
|
146
|
+
return cache;
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
/* 导出单例 */
|
|
151
|
+
module.exports = new CacheStore();
|
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const path = require('path');
|
|
4
|
+
const { createItem } = require('./path_util');
|
|
5
|
+
const { walkDir } = require('./fs_util');
|
|
6
|
+
const { diffItems } = require('./diff_util');
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* 缓存条目增量维护
|
|
10
|
+
* @description 依据监听器分类后的变更事件精确更新缓存条目,无法精确定位时全量重扫兜底
|
|
11
|
+
*/
|
|
12
|
+
class CacheUpdater {
|
|
13
|
+
/**
|
|
14
|
+
* 按监听变更事件更新单个路径
|
|
15
|
+
* @param {object} cache 缓存项
|
|
16
|
+
* @param {{ type: string, path: string }} event 变更事件,type 为 add/set/del
|
|
17
|
+
* @param {Array<object>} changes 变更收集器
|
|
18
|
+
* @returns {boolean} del 事件未命中缓存条目时返回 false,需要全量刷新
|
|
19
|
+
*/
|
|
20
|
+
applyChange(cache, event, changes) {
|
|
21
|
+
const item = createItem(cache.dir, path.join(cache.dir, event.path));
|
|
22
|
+
|
|
23
|
+
if (event.type === 'del') {
|
|
24
|
+
return this._delItem(cache, item.key, changes);
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
if (!cache.extensions.includes(item.ext)) {
|
|
28
|
+
this._delSameName(cache, item, changes);
|
|
29
|
+
return true;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
const added = this._addItem(cache, item);
|
|
33
|
+
changes.push({ type: added ? 'add' : 'change', key: item.key, item: { ...item } });
|
|
34
|
+
return true;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* 全量重扫缓存对应的目录
|
|
39
|
+
* @param {object} cache 缓存项
|
|
40
|
+
* @param {Array<object>} changes 变更收集器
|
|
41
|
+
* @returns {void}
|
|
42
|
+
*/
|
|
43
|
+
refreshCache(cache, changes) {
|
|
44
|
+
const before = cache.items;
|
|
45
|
+
const files = new Map();
|
|
46
|
+
const dirs = [];
|
|
47
|
+
walkDir(cache.dir, cache.dir, cache, files, dirs);
|
|
48
|
+
const after = [...files.values()];
|
|
49
|
+
cache.items = after;
|
|
50
|
+
cache.dirs = dirs;
|
|
51
|
+
cache.time = Date.now();
|
|
52
|
+
|
|
53
|
+
const diff = diffItems(before, after);
|
|
54
|
+
for (let i = 0; i < diff.length; i++) changes.push(diff[i]);
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/**
|
|
58
|
+
* 向缓存中新增或更新一个文件条目
|
|
59
|
+
* @param {object} cache 缓存项
|
|
60
|
+
* @param {{ key: string, absPath: string, relPath: string, dir: string, name: string, ext: string }} item 文件条目
|
|
61
|
+
* @returns {boolean} 新增返回 true,覆盖已有条目返回 false
|
|
62
|
+
* @private
|
|
63
|
+
*/
|
|
64
|
+
_addItem(cache, item) {
|
|
65
|
+
const index = cache.items.findIndex((one) => one.key === item.key);
|
|
66
|
+
if (index >= 0) {
|
|
67
|
+
cache.items[index] = item;
|
|
68
|
+
return false;
|
|
69
|
+
}
|
|
70
|
+
cache.items.push(item);
|
|
71
|
+
return true;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
/**
|
|
75
|
+
* 从缓存中移除指定条目并记录变更
|
|
76
|
+
* @param {object} cache 缓存项
|
|
77
|
+
* @param {string} key 条目键
|
|
78
|
+
* @param {Array<object>} changes 变更收集器
|
|
79
|
+
* @returns {boolean} 是否移除成功
|
|
80
|
+
* @private
|
|
81
|
+
*/
|
|
82
|
+
_delItem(cache, key, changes) {
|
|
83
|
+
const index = cache.items.findIndex((one) => one.key === key);
|
|
84
|
+
if (index < 0) return false;
|
|
85
|
+
const [item] = cache.items.splice(index, 1);
|
|
86
|
+
changes.push({ type: 'unlink', key, item: { ...item } });
|
|
87
|
+
return true;
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
/**
|
|
91
|
+
* 移除同目录同文件名但扩展名不同的条目
|
|
92
|
+
* @description 文件改名后扩展名不再受支持时,需要清掉旧条目
|
|
93
|
+
* @param {object} cache 缓存项
|
|
94
|
+
* @param {{ key: string, dir: string, name: string }} item 当前文件条目
|
|
95
|
+
* @param {Array<object>} changes 变更收集器
|
|
96
|
+
* @returns {void}
|
|
97
|
+
* @private
|
|
98
|
+
*/
|
|
99
|
+
_delSameName(cache, item, changes) {
|
|
100
|
+
for (let i = cache.items.length - 1; i >= 0; i--) {
|
|
101
|
+
const one = cache.items[i];
|
|
102
|
+
if (one.key === item.key || one.dir !== item.dir || one.name !== item.name) continue;
|
|
103
|
+
this._delItem(cache, one.key, changes);
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
/* 导出单例 */
|
|
109
|
+
module.exports = new CacheUpdater();
|
package/lib/diff_util.js
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* 对比两份条目集合,生成增量变更
|
|
5
|
+
* @param {Array<object>} before 变更前的条目
|
|
6
|
+
* @param {Array<object>} after 变更后的条目
|
|
7
|
+
* @returns {Array<{ type: string, key: string, item: object }>} 变更列表
|
|
8
|
+
*/
|
|
9
|
+
function diffItems(before, after) {
|
|
10
|
+
const before_map = new Map();
|
|
11
|
+
for (let i = 0; i < before.length; i++) before_map.set(before[i].key, before[i]);
|
|
12
|
+
|
|
13
|
+
const after_map = new Map();
|
|
14
|
+
for (let i = 0; i < after.length; i++) after_map.set(after[i].key, after[i]);
|
|
15
|
+
|
|
16
|
+
const changes = [];
|
|
17
|
+
for (const [key, item] of after_map) {
|
|
18
|
+
const old = before_map.get(key);
|
|
19
|
+
if (!old) {
|
|
20
|
+
changes.push({ type: 'add', key, item: { ...item } });
|
|
21
|
+
continue;
|
|
22
|
+
}
|
|
23
|
+
if (old.absPath !== item.absPath || old.relPath !== item.relPath) {
|
|
24
|
+
changes.push({ type: 'change', key, item: { ...item } });
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
for (const [key, item] of before_map) {
|
|
28
|
+
if (!after_map.has(key)) changes.push({ type: 'unlink', key, item: { ...item } });
|
|
29
|
+
}
|
|
30
|
+
return changes;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
module.exports = { diffItems };
|
package/lib/fs_util.js
ADDED
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const fs = require('fs');
|
|
4
|
+
const path = require('path');
|
|
5
|
+
const { createItem } = require('./path_util');
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* 获取路径的修改时间
|
|
9
|
+
* @param {string} target 文件或目录路径
|
|
10
|
+
* @returns {number} 毫秒时间戳,读取失败返回 -1
|
|
11
|
+
*/
|
|
12
|
+
function getMtime(target) {
|
|
13
|
+
try {
|
|
14
|
+
return fs.statSync(target).mtimeMs;
|
|
15
|
+
} catch {
|
|
16
|
+
return -1;
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* 递归遍历目录并写入条目
|
|
22
|
+
* @param {string} dir 当前目录绝对路径
|
|
23
|
+
* @param {string} base_dir 扫描根目录绝对路径
|
|
24
|
+
* @param {{ extensions: string[], ignore: string[] }} config 扫描配置
|
|
25
|
+
* @param {Map<string, object>} files 条目容器
|
|
26
|
+
* @param {Array<string>} dirs 遍历过的目录,用于记录与缓存校验
|
|
27
|
+
* @returns {void}
|
|
28
|
+
*/
|
|
29
|
+
function walkDir(dir, base_dir, config, files, dirs) {
|
|
30
|
+
dirs.push(dir);
|
|
31
|
+
const entries = fs.readdirSync(dir, { withFileTypes: true });
|
|
32
|
+
|
|
33
|
+
for (const entry of entries) {
|
|
34
|
+
const full_path = path.join(dir, entry.name);
|
|
35
|
+
|
|
36
|
+
if (entry.isDirectory()) {
|
|
37
|
+
if (config.ignore.includes(entry.name)) continue;
|
|
38
|
+
walkDir(full_path, base_dir, config, files, dirs);
|
|
39
|
+
continue;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
if (!entry.isFile()) continue;
|
|
43
|
+
|
|
44
|
+
const item = createItem(base_dir, full_path);
|
|
45
|
+
if (!config.extensions.includes(item.ext)) continue;
|
|
46
|
+
files.set(item.key, item);
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
module.exports = { getMtime, walkDir };
|
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const cache_store = require('./cache_store');
|
|
4
|
+
const { SYNC, NOTIFY } = require('./symbols');
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* 监听器登记表
|
|
8
|
+
* @description 同一缓存键下的多个 Watcher 共享一个目录监听器
|
|
9
|
+
*/
|
|
10
|
+
class InstanceRegistry {
|
|
11
|
+
/** 缓存键 -> Set<Watcher> */
|
|
12
|
+
#instances = new Map();
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* 登记监听中的实例
|
|
16
|
+
* @param {string} key 缓存键
|
|
17
|
+
* @param {object} instance 监听器实例
|
|
18
|
+
* @returns {void}
|
|
19
|
+
*/
|
|
20
|
+
add(key, instance) {
|
|
21
|
+
let set = this.#instances.get(key);
|
|
22
|
+
if (!set) {
|
|
23
|
+
set = new Set();
|
|
24
|
+
this.#instances.set(key, set);
|
|
25
|
+
}
|
|
26
|
+
set.add(instance);
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* 注销监听中的实例
|
|
31
|
+
* @param {string} key 缓存键
|
|
32
|
+
* @param {object} instance 监听器实例
|
|
33
|
+
* @returns {boolean} 最后一个实例被注销返回 true
|
|
34
|
+
*/
|
|
35
|
+
del(key, instance) {
|
|
36
|
+
const set = this.#instances.get(key);
|
|
37
|
+
if (!set) return false;
|
|
38
|
+
set.delete(instance);
|
|
39
|
+
if (set.size === 0) {
|
|
40
|
+
this.#instances.delete(key);
|
|
41
|
+
return true;
|
|
42
|
+
}
|
|
43
|
+
return false;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* 判断实例是否处于监听状态
|
|
48
|
+
* @param {string} key 缓存键
|
|
49
|
+
* @param {object} instance 监听器实例
|
|
50
|
+
* @returns {boolean} 已登记返回 true
|
|
51
|
+
*/
|
|
52
|
+
has(key, instance) {
|
|
53
|
+
const set = this.#instances.get(key);
|
|
54
|
+
return !!(set && set.has(instance));
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/**
|
|
58
|
+
* 移除指定缓存键下的全部实例登记
|
|
59
|
+
* @param {string} key 缓存键
|
|
60
|
+
* @returns {void}
|
|
61
|
+
*/
|
|
62
|
+
remove(key) {
|
|
63
|
+
this.#instances.delete(key);
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
/**
|
|
67
|
+
* 清空全部实例登记
|
|
68
|
+
* @returns {void}
|
|
69
|
+
*/
|
|
70
|
+
clear() {
|
|
71
|
+
this.#instances.clear();
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
/**
|
|
75
|
+
* 把缓存的最新条目与变更同步到监听中的实例
|
|
76
|
+
* @param {string} key 缓存键
|
|
77
|
+
* @param {Array<object>} changes 变更列表
|
|
78
|
+
* @returns {void}
|
|
79
|
+
*/
|
|
80
|
+
sync(key, changes) {
|
|
81
|
+
const cache = cache_store.readCache(key);
|
|
82
|
+
const set = this.#instances.get(key);
|
|
83
|
+
if (!cache || !set) return;
|
|
84
|
+
for (const instance of set) {
|
|
85
|
+
instance[SYNC](cache.items, cache.dirs);
|
|
86
|
+
instance[NOTIFY](changes);
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
/* 导出单例 */
|
|
92
|
+
module.exports = new InstanceRegistry();
|
package/lib/path_util.js
ADDED
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const path = require('path');
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* key 规范化:统一分隔符、去 "./" 前缀与首尾斜杠
|
|
7
|
+
* @param {string} p 原始路径
|
|
8
|
+
* @returns {string} 规范化后的路径键
|
|
9
|
+
*/
|
|
10
|
+
function normalizeKey(p) {
|
|
11
|
+
return String(p)
|
|
12
|
+
.replace(/\\/g, '/')
|
|
13
|
+
.replace(/^\.\//, '')
|
|
14
|
+
.replace(/^\/+|\/+$/g, '');
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* 扩展名规范化:补点、转小写
|
|
19
|
+
* @param {string} ext 原始扩展名
|
|
20
|
+
* @returns {string} 规范化后的扩展名
|
|
21
|
+
*/
|
|
22
|
+
function normalizeExt(ext) {
|
|
23
|
+
const e = String(ext).trim().toLowerCase();
|
|
24
|
+
return e.startsWith('.') ? e : '.' + e;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* 生成扫描缓存键
|
|
29
|
+
* @param {string} dir 目录绝对路径
|
|
30
|
+
* @param {Array<string>} extensions 扩展名数组
|
|
31
|
+
* @param {Array<string>} ignore 忽略目录数组
|
|
32
|
+
* @returns {string} 缓存键
|
|
33
|
+
*/
|
|
34
|
+
function buildCacheKey(dir, extensions, ignore) {
|
|
35
|
+
return [dir, extensions.join(','), ignore.join(',')].join('\u0001');
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* 构造单个文件的条目元信息
|
|
40
|
+
* @param {string} base_dir 扫描根目录绝对路径
|
|
41
|
+
* @param {string} abs_path 文件绝对路径
|
|
42
|
+
* @returns {{ key: string, absPath: string, relPath: string, dir: string, name: string, ext: string }} 条目
|
|
43
|
+
*/
|
|
44
|
+
function createItem(base_dir, abs_path) {
|
|
45
|
+
const rel_path = path.relative(base_dir, abs_path).replace(/\\/g, '/');
|
|
46
|
+
const ext = path.extname(rel_path).toLowerCase();
|
|
47
|
+
const dir_part = path.posix.dirname(rel_path);
|
|
48
|
+
return {
|
|
49
|
+
key: normalizeKey(rel_path),
|
|
50
|
+
absPath: abs_path,
|
|
51
|
+
relPath: rel_path,
|
|
52
|
+
dir: dir_part === '.' ? '' : dir_part,
|
|
53
|
+
name: path.basename(rel_path, ext),
|
|
54
|
+
ext,
|
|
55
|
+
};
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
module.exports = { normalizeKey, normalizeExt, buildCacheKey, createItem };
|