-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathbuild.js
106 lines (73 loc) · 2.93 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
104
105
106
// read each of the ts files, every export default needs to include a module.exports as well to make it compatible with require() in CommonJS
console.log('Adjusting exports for CommonJS compatibility...');
const fs = require('node:fs');
const { execSync } = require('node:child_process');
let files = {} // <path/to/file>: <content>
function ReadFolder(dir) {
fs.readdirSync(dir).forEach(file => {
const filePath = `${dir}/${file}`;
const stat = fs.statSync(filePath);
if (stat.isDirectory()) {
ReadFolder(filePath);
} else {
files[filePath] = fs.readFileSync(filePath, 'utf8');
}
})
}
ReadFolder(`${__dirname}/src`);
let modified = 0;
for (const file in files) {
if (!file.endsWith('.ts')) continue;
const content = files[file];
if (!content.includes('export default')) continue;
if (content.includes('export default') && content.includes('module.exports')) continue;
const newContent = content + '\nmodule.exports = exports.default;';
fs.writeFileSync(file, newContent);
modified++;
}
console.log('Checking for compilation error...');
execSync(`tsc -p ${__dirname}/tsconfig.json --noEmit`, { stdio: 'inherit' });
if (fs.existsSync(`${__dirname}/build`)) {
console.log('Clearing old build...');
fs.rmSync(`${__dirname}/build`, { recursive: true });
}
console.log('Compiling declarations...');
execSync(`tsc -p ${__dirname}/tsconfig.json --emitDeclarationOnly`, { stdio: 'inherit' });
console.log('Bundling declarations...');
execSync(`npx dts-bundle-generator --project ${__dirname}/tsconfig.json --no-check --out-file ${__dirname}/build/typings.d.ts ${__dirname}/src/index.ts`, { stdio: 'inherit' });
console.log('Clearing excess declarations...');
files = {};
ReadFolder(`${__dirname}/build`);
for (const file in files) {
if (file.endsWith('.d.ts') && !file.endsWith('typings.d.ts')) {
fs.rmSync(file);
}
}
console.log('Compiling to JS...');
execSync(`npx sucrase ${__dirname}/src --out-dir ${__dirname}/build --transforms typescript,imports`, { stdio: 'inherit' });
/*
class Client extends _Events2.default {
#token;
__init() {this.connected_at = null} // set in.wsClientClient
__init2() {this.user = null}
__init3() {this.pressence = 'ONLINE'}
__init4() {this.activity = null}
*/
// remove excessive whitespace
console.log('Cleaning up...');
files = {};
ReadFolder(`${__dirname}/build`);
const topComment = `
///////////////////////////////////////////////////////////////////
// Welcome to the Simplicity source code! //
// This code is written in TypeScript and compiled using Sucrase //
// For any issues, please report them on the GitHub repository //
// https://github.com/MusicMakerOwO/Simplicity/issues //
///////////////////////////////////////////////////////////////////
`.trim();
for (const file in files) {
const content = files[file];
const newContent = content.replace(/(\s*\n){3,}/g, '\n\n');
fs.writeFileSync(file, topComment + '\n\n' + newContent);
}
console.log('Done!');