-
Notifications
You must be signed in to change notification settings - Fork 32
/
uTools.js
146 lines (138 loc) · 4.24 KB
/
uTools.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
const path = require('path');
const fs = require('fs-extra');
const { build } = require('esbuild');
/**
* @description
* @param {string} dirPath
* @return {string[]}
*/
function getAllFiles(dirPath) {
const files = fs.readdirSync(dirPath);
let result = [];
// eslint-disable-next-line no-restricted-syntax
for (const file of files) {
const filePath = `${dirPath}/${file}`;
// eslint-disable-next-line no-await-in-loop
const stats = fs.statSync(filePath);
if (stats.isDirectory()) {
result = result.concat(getAllFiles(filePath));
} else {
result.push(filePath);
}
}
return result;
}
getAllFiles(path.join(__dirname, 'dist', 'uTools'))
.filter((s) => s.includes('index.js'))
.forEach(async (file) => {
const mainFilePath = file
.replace(/\\/g, '/')
.replace('index.js', 'src/main');
const mainBundleFilePath = file
.replace(/\\/g, '/')
.replace('index.js', 'src/mainBundle');
const content = fs.readFileSync(file).toString();
const indexContent = content
.replace(
'Object.defineProperty(exports, "__esModule", { value: true });',
`const mainFilePath = "${mainFilePath}";`,
)
.replace('// @ts-ignore', '')
.replace(
'const main_1 = require("./src/main");',
`const main_1 = require(mainFilePath);`,
)
.replace(
'(0, main_1.bootstrap)()',
`(0, main_1.bootstrap)(mainFilePath)`,
);
const indexBundleContent = content
.replace(
'Object.defineProperty(exports, "__esModule", { value: true });',
`const mainFilePath = "${mainBundleFilePath}";`,
)
.replace('// @ts-ignore', '')
.replace(
'const main_1 = require("./src/main");',
`const main_1 = require(mainFilePath);`,
)
.replace(
'(0, main_1.bootstrap)()',
`(0, main_1.bootstrap)(mainFilePath)`,
);
fs.writeFileSync(file, indexContent);
const indexBundleFile = file
.replace(/\\/g, '/')
.replace('index.js', 'indexBundle.js');
fs.writeFileSync(indexBundleFile, indexBundleContent);
await build({
entryPoints: [`${mainFilePath}.js`],
bundle: true,
minify: true,
// only needed if you have dependencies
external: ['electron', 'typescript-json-schema'],
platform: 'node',
format: 'cjs',
outfile: mainFilePath.replace('/main', '/mainBundle.js'),
});
const conetnt = fs.readFileSync(`${mainFilePath}.js`).toString();
fs.writeFileSync(
`${mainFilePath}.js`,
`const moduleAlias = require('module-alias');
moduleAlias.addAlias('@share', '${path
.join(__dirname, 'dist', 'share')
.replace(/\\/g, '/')}');
${conetnt}`,
);
build({
entryPoints: [`${mainFilePath}.js`],
bundle: false,
minify: false,
// only needed if you have dependencies
// external: ['electron'],
platform: 'node',
format: 'cjs',
outfile: `${mainFilePath}.js`,
allowOverwrite: true,
});
});
// bundle other files
['share/uTools/webviewBaseController.js'].forEach(async (file) => {
const entryFile = path.join(__dirname, 'dist', file);
build({
entryPoints: [entryFile],
bundle: true,
minify: true,
// only needed if you have dependencies
external: ['electron'],
platform: 'node',
format: 'cjs',
outfile: entryFile.replace('.js', 'Bundle.js'),
allowOverwrite: true,
});
});
// 复制物料文件
const copyDir = () => {
const dirPath = path.join(__dirname, 'uTools');
const distDirPath = path.join(__dirname, 'dist', 'uTools');
const dirNameArry = fs.readdirSync(dirPath);
// eslint-disable-next-line no-restricted-syntax
for (const dirName of dirNameArry) {
const childDirPath = path.join(dirPath, dirName);
const stats = fs.statSync(childDirPath);
if (stats.isDirectory()) {
const copyDirArray = fs.readdirSync(childDirPath);
// eslint-disable-next-line no-restricted-syntax
for (const copyDirName of copyDirArray) {
if (copyDirName !== 'script') {
const copyDirPath = path.join(childDirPath, copyDirName);
fs.copySync(
copyDirPath,
path.join(distDirPath, dirName, copyDirName),
);
}
}
}
}
};
copyDir();