-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild.js
51 lines (41 loc) · 1.48 KB
/
build.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
const { run } = require('runjs');
const chalk = require('chalk');
const fs = require('fs');
const path = require('path');
const filePath = path.join(__dirname, 'es6/JyfColorCrafter.js');
console.log(chalk`{blue.underline begin ejs}`);
run(`npx babel JyfColorCrafter.js --out-dir ejs`)
console.log(chalk`{blue.underline finish ejs}`);
console.log(chalk`{blue.underline begin es6}`);
run(`npx babel JyfColorCrafter.js --out-dir es6`)
fs.readFile(filePath, 'utf8', (err, data) => {
if (err) {
console.error('read file error:', err);
return;
}
function escapeRegExp(string) {
return string.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'); // $& 表示整个被匹配的字符串
}
const A = `(void 0, function () {
'use strict';`;
const B = ` return JyfColorCrafter;
});`;
const escapedA = escapeRegExp(A);
const escapedB = escapeRegExp(B);
const regex = new RegExp(`${escapedA}(.*?)${escapedB}`, 's'); // 's' 标志表示让 . 匹配换行符
const match = data.match(regex);
if (!match) {
console.log(chalk`{red.underline write file error: can't find match}`);
return;
}
const modifiedData = match[1] + `export default JyfColorCrafter;`;
// 异步写入修改后的内容到文件
fs.writeFile(filePath, modifiedData, 'utf8', (err) => {
if (err) {
console.log(chalk`{red.underline write file error: ${err}}`);
return;
}
console.log(chalk`{blue.underline finish es6}`);
console.log(chalk`{blue.underline finish 100%}`);
});
});