-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
155 lines (141 loc) · 3.49 KB
/
app.js
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
const { info } = require('console');
const fs = require('fs');
const path = require('path');
const excludeDifferentExtensions = (data) => {
for (const key in data) {
if (key === '_files') {
const filenames = new Set();
data[key] = data[key].filter((file) => {
const filename = file.name;
if (filenames.has(filename)) {
return false; // 如果已经存在相同文件名,则排除该文件
} else {
filenames.add(filename);
return true;
}
});
} else if (typeof data[key] === 'object') {
data[key] = excludeDifferentExtensions(data[key]); // 递归处理嵌套结构
}
}
return data; // 返回处理后的数据
};
const noCreateFileName = [
'16w',
'20w',
'32w',
'40w',
'48w',
'60w',
'64w',
'72w',
'80w',
'96w',
'256w',
'16',
'20',
'32',
'40',
'48',
'60',
'64',
'72',
'80',
'96',
'256',
];
const containsKeyword = (fileName) => {
for (let i = 0; i < noCreateFileName.length; i++) {
if (fileName.includes(noCreateFileName[i])) {
return true;
}
}
return false;
};
const typeMap = {
AppIcon: '应用图标',
FileTypeIcon: '文件类型图标',
folders: '文件夹图标',
Android: '安卓图标',
Windows: 'Windows图标',
};
/**
* getFileType
* @type {string} filePath - filePath
*/
const getFileType = (
filePath = 'folders\\cyan blue\\cyan blue_Desktop.png'
) => {
let result;
let types = [];
if (filePath.split('\\')[0] === 'folders') {
types = ['文件夹图标'];
} else {
filePath =
filePath.split('\\')[0] +
'\\' +
filePath.split('\\')[1] +
filePath.split('\\')[2];
for (let typeKey in typeMap) {
if (filePath.includes(typeKey)) {
types.push(typeMap[typeKey]);
}
}
}
result = types.join('/');
return types;
};
// 函数用于遍历目录下的所有文件
const traverseDirectory = (dir, rootDir) => {
const companies = {};
// 获取目录下的所有文件和文件夹
const items = fs.readdirSync(dir);
for (const item of items) {
if (item === '.git' || item === 'web' || item.endsWith('.md')) {
// 如果是 .git 或者 web 目录,则跳过
continue;
}
const itemPath = path.join(dir, item);
const relativePath = path.relative(__dirname, itemPath);
const relativePathFroImgs = path.relative(rootDir, itemPath);
const stats = fs.statSync(itemPath);
if (stats.isDirectory()) {
// 如果是文件夹,则递归遍历
const software = traverseDirectory(itemPath, rootDir);
companies[item] = software;
} else {
if (dir !== rootDir) {
// 如果是根目录下的单个文件,则跳过
if (!companies['_files']) {
companies['_files'] = [];
}
const infos = relativePathFroImgs.split('/');
const company = infos[0];
const app = infos[1];
// const app;
// Au16w Au20w Au32w
if (!containsKeyword(item.split('.')[0]))
companies['_files'].push({
path: relativePath,
name: item.split('.')[0],
company: company === 'folders' ? '' : company,
app: company === 'folders' ? '' : app,
type: getFileType(relativePathFroImgs),
});
}
}
}
return companies;
};
// 遍历当前目录
const rootDir = path.join(__dirname, 'imgs');
let result = traverseDirectory(rootDir, rootDir);
result = excludeDifferentExtensions(result);
// 将结果写入config.json文件
const outputDir = __dirname;
if (!fs.existsSync(outputDir)) {
fs.mkdirSync(outputDir);
}
const configPath = path.join(outputDir, 'config.json');
fs.writeFileSync(configPath, JSON.stringify(result, null, 2));
console.log('config.json 文件已生成');