-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcustom-plugin.js
More file actions
59 lines (51 loc) · 1.76 KB
/
custom-plugin.js
File metadata and controls
59 lines (51 loc) · 1.76 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
const fs = require('fs');
const path = require('path');
// 统计指定目录下的 js 文件数量
function countJsFiles(dir) {
// 计数器
let count = 0;
// 读取目录中的所有文件和子目录
const items = fs.readdirSync(dir);
for (const item of items) {
// 获取当前项的完整路径
const fullPath = path.join(dir, item);
// 检查文件或目录
const stats = fs.statSync(fullPath);
if (stats.isDirectory()) {
// 如果是目录,递归调用
count += countJsFiles(fullPath);
} else if (stats.isFile() && fullPath.endsWith('.js')) {
// 如果是 .js 文件,累加计数
count++;
}
}
return count;
}
class CustomPlugin {
constructor(config) {
this.name = 'CustomPlugin';
this.config = config;
this.devMode = false;
}
apply(scanner) {
// 自定义插件逻辑, 统计指定类型的文件数量,注册到 code 钩子中
scanner.hooks.code.tapPromise(this.name, async (context) => {
// this.devLog('config custom', this.config);
try {
context.logger.log('info', 'start custom plugin...');
const countFileType = this.config.countFileType;
const scanDir = path.join(context.baseDir, context.codeDir);
const count = countJsFiles(scanDir);
context.scanResults.customInfo = {
total: count,
};
context.logger.log('info', `total ${count} ${countFileType} files`);
context.logger.log('info', `custom plugin completed`);
} catch(error) {
context.scanResults.customInfo = null;
context.logger.log('error', `error in plugin ${this.name}: ${error.stack}`);
}
});
}
}
module.exports = CustomPlugin;