This repository has been archived by the owner on Aug 19, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbuild.js
103 lines (88 loc) · 2.96 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
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
const path = require('path');
const fs = require('fs-extra');
const babel = require("@babel/core");
/**
* https://babeljs.io/docs/en/babel-preset-typescript#via-node-api
*/
function transformTS(filePath) {
const data = babel.transformSync(fs.readFileSync(filePath).toString(), {
presets: ["@babel/preset-typescript"],
filename: filePath
});
return data.code;
}
const EXT = { ts: 'js', tsx: 'js' };
function changeExtension(file) {
const ext = path.extname(file);
const basename = path.basename(file, ext);
return path.join(path.dirname(file), `${basename}.${EXT[ext.slice(1)]}`);
}
function transformTSTraversal(folderPath) {
const files = {};
const traverse = (fileName) => {
const fileExt = path.extname(fileName).slice(1);
if (fs.lstatSync(fileName).isDirectory()) {
fs.readdirSync(fileName)
.forEach(file => {
traverse(path.join(fileName, file));
});
} else if (Object.keys(EXT).some(ext => ext === fileExt) && !fileName.endsWith('.d.ts')) {
files[changeExtension(path.relative(folderPath, fileName))] = transformTS(fileName);
}
}
traverse(folderPath);
return files;
}
// build
const srcDir = path.resolve(__dirname, 'common');
const jsDir = path.resolve(__dirname, 'js');
const tsDir = path.resolve(__dirname, 'ts');
const BLACKLIST = [];
function finalizeBuild(templateDir, template) {
// update template.json
const templateJSONPath = path.resolve(templateDir, 'template.json');
const templateData = JSON.parse(fs.readFileSync(templateJSONPath).toString());
templateData.package.sandboxConfig = {
fabric: null,
template
}
fs.writeFileSync(templateJSONPath, JSON.stringify(templateData, null, '\t'));
// update app .env
const envPath = path.resolve(templateDir, 'template', '.env');
let env = fs.readFileSync(envPath).toString();
env += `\nREACT_APP_TEMPLATE=${template}\n`;
fs.writeFileSync(envPath, env);
// copy sandbox.js
const sandboxPath = path.resolve(__dirname, 'sandbox.js');
fs.copySync(sandboxPath, path.resolve(templateDir, 'template', 'sandbox.js'));
}
function buildJSTemplate() {
// copy non ts files
fs.copySync(srcDir, jsDir, {
filter: (src, dest) => {
if (BLACKLIST.some(p => p.endsWith(path.basename(src)))) return false;
const fileExt = path.extname(src).slice(1);
return Object.keys(EXT).every(ext => ext !== fileExt) || src.endsWith('.d.ts');
}
});
// transform ts files to js and write to folder
const files = transformTSTraversal(srcDir);
for (const fileName in files) {
const dest = path.resolve(jsDir, fileName);
fs.ensureFileSync(dest, files[fileName]);
fs.writeFileSync(dest, files[fileName]);
}
finalizeBuild(jsDir, 'js');
}
function buildTSTemplate() {
// copy files
fs.copySync(srcDir, tsDir, {
filter: (src, dest) => {
if (BLACKLIST.some(p => p.endsWith(path.basename(src)))) return false;
return true;
}
});
finalizeBuild(tsDir, 'ts');
}
buildJSTemplate();
buildTSTemplate();