-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcreate.js
36 lines (28 loc) · 966 Bytes
/
create.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
const { readFile, writeFile } = require('fs').promises;
const path = require('path');
const resolve = (...filePath) => path.resolve(__dirname, ...filePath);
const comReg = /\/\*[\s\S]+?\*\//g; // 匹配注释
const funcReg = /function[\s\S]+?\}/g; // 匹配函数块
(async () => {
const target = resolve(__dirname, './code/index.js');
const com = [];
const func = [];
let content = await readFile(target, 'utf-8');
if (/-/g.test(content)) {
throw new TypeError('源代码不能包含 "-" 符号');
}
// 去掉 @export 那一行
content = content.replace(/.*@export\s/g, '');
content.replace(comReg, code => {
com.push(code);
});
content.replace(funcReg, code => {
func.push(code);
});
const result = func.reduce((pre, next, i) => {
pre.push(com[i] + '-' + next);
return pre;
}, []);
const finalResult = `export default ${JSON.stringify(result)};`;
await writeFile(resolve('./data.js'), finalResult);
})();