-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtsc.ts
162 lines (135 loc) · 4.06 KB
/
tsc.ts
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
156
157
158
159
160
161
162
import { ChildProcess, exec } from 'child_process';
import fs from 'fs-extra';
import path from 'path';
import { removeSync, copyFileSync } from 'fs-extra';
import packageJSON from './package.json';
const argv = process.argv.slice(2);
let buildIgnore: string[] = [];
const isTest = argv.length > 0 && argv[0] === 'test';
const isGUI = !(argv.length > 1 && argv[1] === 'false');
if (!isTest)
buildIgnore = [
'*/\\.env',
'*/node_modules/*'
];
if (!isGUI)
buildIgnore = buildIgnore.concat([
'./gui*',
'./build*'
]);
const ignore = [
...buildIgnore,
'*/\\.git*',
'./lib*',
'*/@types*',
'./out*',
'./bin/mkvtoolnix*',
'./config/token.yml$',
'./config/updates.json$',
'./config/cr_token.yml$',
'./config/funi_token.yml$',
'*/\\.eslint*',
'*/*\\.tsx?$',
'./fonts*',
'./gui/react*',
].map(a => a.replace(/\*/g, '[^]*').replace(/\.\//g, escapeRegExp(__dirname) + '/').replace(/\//g, path.sep === '\\' ? '\\\\' : '/')).map(a => new RegExp(a, 'i'));
export { ignore };
(async () => {
const waitForProcess = async (proc: ChildProcess) => {
return new Promise((resolve, reject) => {
proc.stdout?.on('data', console.log);
proc.stderr?.on('data', console.error);
proc.on('close', resolve);
proc.on('error', reject);
});
};
process.stdout.write('Removing lib dir... ');
removeSync('lib');
process.stdout.write('✓\nRunning tsc... ');
const tsc = exec('npx tsc');
await waitForProcess(tsc);
if (!isGUI) {
fs.emptyDirSync(path.join('lib', 'gui'));
fs.rmdirSync(path.join('lib', 'gui'));
}
if (!isTest && isGUI) {
process.stdout.write('✓\nBuilding react... ');
const installReactDependencies = exec('npm install', {
cwd: path.join(__dirname, 'gui', 'react'),
});
await waitForProcess(installReactDependencies);
const react = exec('npm run build', {
cwd: path.join(__dirname, 'gui', 'react'),
});
await waitForProcess(react);
}
process.stdout.write('✓\nCopying files... ');
if (!isTest && isGUI) {
copyDir(path.join(__dirname, 'gui', 'react', 'build'), path.join(__dirname, 'lib', 'gui', 'electron', 'build'));
}
const files = readDir(__dirname);
files.forEach(item => {
const itemPath = path.join(__dirname, 'lib', item.path.replace(__dirname, ''));
if (item.stats.isDirectory()) {
if (!fs.existsSync(itemPath))
fs.mkdirSync(itemPath);
} else {
copyFileSync(item.path, itemPath);
}
});
process.stdout.write('✓\nInstalling dependencies... ');
if (!isTest && !isGUI) {
alterJSON();
}
if (!isTest) {
const dependencies = exec(`npm install ${isGUI ? '' : '--production'}`, {
cwd: path.join(__dirname, 'lib')
});
await waitForProcess(dependencies);
}
process.stdout.write('✓\n');
})();
function alterJSON() {
packageJSON.main = 'index.js';
fs.writeFileSync(path.join('lib', 'package.json'), JSON.stringify(packageJSON, null, 4));
}
function readDir (dir: string): {
path: string,
stats: fs.Stats
}[] {
const items: {
path: string,
stats: fs.Stats
}[] = [];
const content = fs.readdirSync(dir);
itemLoop: for (const item of content) {
const itemPath = path.join(dir, item);
for (const ignoreItem of ignore) {
if (ignoreItem.test(itemPath))
continue itemLoop;
}
const stats = fs.statSync(itemPath);
items.push({
path: itemPath,
stats
});
if (stats.isDirectory()) {
items.push(...readDir(itemPath));
}
}
return items;
}
async function copyDir(src: string, dest: string) {
await fs.promises.mkdir(dest, { recursive: true });
const entries = await fs.promises.readdir(src, { withFileTypes: true });
for (const entry of entries) {
const srcPath = path.join(src, entry.name);
const destPath = path.join(dest, entry.name);
entry.isDirectory() ?
await copyDir(srcPath, destPath) :
await fs.promises.copyFile(srcPath, destPath);
}
}
function escapeRegExp(string: string): string {
return string.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'); // $& means the whole matched string
}