-
Notifications
You must be signed in to change notification settings - Fork 2
/
build.js
322 lines (255 loc) · 14.7 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
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
const { exec, execSync } = require('child_process')
const platform = process.platform
const {
readFileSync, readdirSync,
writeFile, copyFile,
existsSync
} = require('fs')
const package = require('./package.json')
const date = Date.now()
const colors = {
red: '\x1b[31m',
green: '\x1b[32m',
yellow: '\x1b[33m',
blue: '\x1b[34m',
magenta: '\x1b[35m',
cyan: '\x1b[36m',
white: '\x1b[37m',
reset: '\x1b[0m'
}
const settingsArray = [
'xp',
'maxXP',
'multiplier',
'status',
'ignoredUsers',
'lockedChannels',
'ignoreBots',
'filter'
]
const settingsArrays = [
'ignoredUsers',
'lockedChannels',
]
function removeBuild() {
if (existsSync('./dist')) {
if (platform == 'win32') execSync('del /s /q dist')
else execSync('rm -rf dist')
}
}
/**
* Returns a string that contains the code
* and a message in the beginning.
*
* Also you can send the typedefs file in the end
* of the string.
*
* @param {String} code The code to put.
* @param {Boolean} sendTypedefs If true, it will put the module typedefs in the end of the string.
* @param {Boolean} sendDiscordImports If true, it will put the Discod imports after the message.
* @param {Boolean} sendEvents If true, it will put the module will put it's typedefs in the end of the string.
* @returns {String} The code to use in any files.
*/
function getCodeWithInfo(code, sendTypedefs = false, sendDiscordImports = false, sendEvents = false) {
const typedefsText = readFileSync('./typedefs.js').toString().split('// ---------------------------------------').filter(x => x.length).map(x => x.slice(2))
const typedefs = typedefsText.slice(0, 3).join('\n')
const events = typedefsText[3]
return `// This file was generated automatically!
// I'm not responsible for the quality of this code!
// The module is made in TypeScript.
// See the source code here:
// https://github.com/shadowplay1/discord-leveling-super
// Thanks!${sendDiscordImports ?
`\n\nconst {
Message, GuildMember, User,
MessageEmbed, MessageAttachment, Guild,
MessageOptions, Channel, TextChannel
} = require('discord.js')` : ''}
${code}${sendTypedefs ? `\n// ---------------------------------------\n${typedefs
.replace('\n', '')
.replace(
'// Typedefs area starts here...',
'// Typedefs area starts here...\n// ---------------------------------------'
)
.replace('\n\n// Events area starts here...', '')
.slice(0, -5)
}` : ''}${sendEvents ? `\n\n// ---------------------------------------\n// Events area starts here...\n// ---------------------------------------\n${events}` : ''}`
}
console.log(`${colors.blue}Building ${package.name || __dirname.split('\\').slice(-1)}@${package.version || '1.0.0'}...`);
new Promise((resolve, reject) => {
exec('npm run begin', err => {
if (err) {
const buildDate = Date.now()
const timeTaken = ((buildDate - date) / 1000).toFixed(3)
console.log()
console.log(`${colors.red}Build failed:${colors.cyan}`)
console.log(err)
console.log()
console.log(`${colors.yellow}Failed to install TypeScript.`)
console.log(`Time taken: ${colors.green}${timeTaken}s${colors.yellow}.`)
return
}
exec('npm run buildfiles', (err, stdout) => {
if (err) {
const errorLines = stdout.split('\n').slice(4)
const error = errorLines[0]
if (!error) {
const buildDate = Date.now()
const timeTaken = ((buildDate - date) / 1000).toFixed(3)
removeBuild()
console.log()
console.log(`${colors.red}Build failed:${colors.cyan}`)
console.log(err)
console.log()
console.log(`${colors.yellow}An unexpected error has occured.`)
console.log(`Time taken: ${colors.green}${timeTaken}s${colors.yellow}.`)
console.log(`All build files were cleared.${colors.reset}`)
}
const data = error.split('(')
const file = data[0]
const lineData = data[1].split(')')[0].split(',')
const line = lineData[0]
const symbol = lineData[1]
const buildDate = Date.now()
const timeTaken = ((buildDate - date) / 1000).toFixed(3)
removeBuild()
console.log()
console.log(`${colors.red}Build failed:${colors.cyan}`)
console.log(error)
console.log()
console.log(`${colors.yellow}Fix the error at ${colors.green}${file}:${line}:${symbol}${colors.yellow} and try again.`)
console.log(`Time taken: ${colors.green}${timeTaken}s${colors.yellow}.`)
console.log(`All build files were cleared.${colors.reset}`)
return
}
copyFile('./LICENSE', './dist/LICENSE', err => {
if (err) return reject(err)
const modulePackage = package
modulePackage.scripts = {
test: 'echo "ok" && exit 0',
postinstall: 'node install.js',
buildfiles: 'tsc',
begin: 'npm i typescript'
}
writeFile('./dist/package.json', JSON.stringify(modulePackage, null, '\t'), err => {
if (err) return reject(err)
copyFile('./install.js', './dist/install.js', err => {
if (err) return reject(err)
copyFile('./index.js', './dist/index.js', err => {
if (err) return reject(err)
copyFile('./README.md', './dist/README.md', err => {
if (err) return reject(err)
exec(
'mkdir dist\\typings\\interfaces' +
'&& mkdir dist\\typings\\classes' +
'&& mkdir dist\\typings\\managers',
err => {
if (err && !existsSync('./dist/typings') && !existsSync('./dist/typings/interfaces')) return reject(err)
const interfacesDir = readdirSync('./typings/interfaces')
const classesDir = readdirSync('./typings/classes')
const managersDir = readdirSync('./typings/managers')
for (let i of interfacesDir) {
copyFile(`./typings/interfaces/${i}`, `./dist/typings/interfaces/${i}`, err => {
if (err) return reject(err)
})
}
for (let i of classesDir) {
const fileContent = readFileSync(`./typings/classes/${i}`).toString()
const textContent = getCodeWithInfo(fileContent)
writeFile(`./dist/typings/classes/${i}`, textContent, err => {
if (err) return reject(err)
})
}
for (let i of managersDir) {
const fileContent = readFileSync(`./typings/managers/${i}`).toString()
const textContent = getCodeWithInfo(fileContent)
writeFile(`./dist/typings/managers/${i}`, textContent, err => {
if (err) return reject(err)
})
}
const fileText = readFileSync('./typings/Leveling.d.ts').toString()
const indexText = getCodeWithInfo(fileText)
writeFile(`./dist/typings/Leveling.d.ts`, indexText, err => {
if (err) return reject(err)
const indexFile = readFileSync('./dist/src/index.js').toString()
const indexText = getCodeWithInfo(indexFile, true, true, true)
writeFile('./dist/src/index.js', indexText, err => {
if (err) return reject(err)
const classesDir = readdirSync('./dist/src/classes')
const managersDir = readdirSync('./dist/src/managers')
const structuresDir = readdirSync('./dist/src/structures')
for (let i of classesDir) {
const fileContent = readFileSync(`./dist/src/classes/${i}`).toString()
.replaceAll('keyof LevelingEvents', 'String')
.replaceAll('(...args: LevelingEvents[K][]) => void', 'Function')
.replaceAll('LevelingEvents[K][]', 'any')
const textContent = getCodeWithInfo(fileContent)
writeFile(`./dist/src/classes/${i}`, textContent, err => {
if (err) return reject(err)
})
}
for (let i of managersDir) {
const fileContent = readFileSync(`./dist/src/managers/${i}`).toString()
.replaceAll('keyof SettingsTypes', settingsArray.map(x => `'${x}'`).join(' | '))
.replaceAll('SettingsTypes[K]', 'any')
.replaceAll('keyof SettingsArrays', settingsArrays.map(x => `'${x}'`).join(' | '))
.replaceAll('SettingsArrays[K]', 'any')
const textContent = getCodeWithInfo(fileContent, true, true)
writeFile(`./dist/src/managers/${i}`, textContent, err => {
if (err) return reject(err)
})
}
for (let i of structuresDir) {
const fileContent = readFileSync(`./dist/src/structures/${i}`).toString()
const textContent = getCodeWithInfo(fileContent)
writeFile(`./dist/src/structures/${i}`, textContent, err => {
if (err) return reject(err)
})
}
exec(platform == 'win32' ? 'rd node_modules\\typescript /s /q' : 'rm -rf node_modules\\typescript', err => {
if (err) {
const buildDate = Date.now()
const timeTaken = ((buildDate - date) / 1000).toFixed(3)
console.log()
console.log(`${colors.red}Build failed:${colors.cyan}`)
console.log(err)
console.log()
console.log(`${colors.yellow}Failed to uninstall TypeScript.`)
console.log(`Time taken: ${colors.green}${timeTaken}s${colors.yellow}.`)
return
}
let pkgContent = readFileSync('./package.json').toString()
.replace(',\r\n' + '\t\t"typescript": "^4.4.3"', '')
writeFile('./package.json', pkgContent, err => {
if (err) return reject(err)
resolve(true)
})
})
})
})
})
})
})
})
})
})
})
})
}).then(() => {
const buildDate = Date.now()
const timeTaken = ((buildDate - date) / 1000).toFixed(3)
console.log()
console.log(`${colors.yellow}dist/index.js ${timeTaken}s`)
console.log(`${colors.green}Module built successfully!${colors.reset}`)
}).catch(err => {
const buildDate = Date.now()
const timeTaken = ((buildDate - date) / 1000).toFixed(3)
removeBuild()
console.log()
console.log(`${colors.red}Build failed:${colors.cyan}`)
console.log(err)
console.log()
console.log(`${colors.yellow}An unexpected error has occured.`)
console.log(`Time taken: ${colors.green}${timeTaken}s${colors.yellow}.`)
console.log(`All build files were cleared.${colors.reset}`)
})