Typescript will now be compiled without CLI

This commit is contained in:
DJj123dj
2024-09-01 16:27:25 +02:00
parent 147eae05b3
commit 509e961446
2 changed files with 50 additions and 14 deletions
+46 -11
View File
@@ -1,15 +1,50 @@
//PLEASE COMPILE USING "npm run build" BEFORE STARTING THE BOT!
//OR USE "npm start" TO INSTANTLY COMPILE & START THE BOT!
//@ts-check
const fs = require("fs")
if (process.argv.includes("-%-compile")){
console.log("Removing prebuilds...")
const ts = require("typescript")
const nodepath = require('path')
//REMOVE EXISTING BUILDS
console.log("OT: Removing Prebuilds...")
fs.rmSync("./dist",{recursive:true,force:true})
console.log("Compiling the bot...")
//exit on compilation
process.exit(0)
//COMPILE TYPESCRIPT
console.log("OT: Compiling Typescript...")
const configPath = nodepath.resolve('./tsconfig.json')
const configFile = ts.readConfigFile(configPath,ts.sys.readFile)
//check for tsconfig errors
if (configFile.error){
const message = ts.formatDiagnosticsWithColorAndContext([configFile.error],ts.createCompilerHost({}))
console.error(message)
process.exit(1)
}
//start the bot
console.log("Finished compilation!")
import("./dist/src/index.js")
//parse tsconfig file
const parsedConfig = ts.parseJsonConfigFileContent(configFile.config,ts.sys,nodepath.dirname(configPath))
//create program/compiler
const program = ts.createProgram({
rootNames:parsedConfig.fileNames,
options:parsedConfig.options
})
//emit all compiled files
const emitResult = program.emit()
//print emit errors/warnings (type errors)
const allDiagnostics = ts.getPreEmitDiagnostics(program).concat(emitResult.diagnostics)
const formattedDiagnostics = ts.formatDiagnosticsWithColorAndContext(allDiagnostics, ts.createCompilerHost(parsedConfig.options))
console.log(formattedDiagnostics)
//run with code
if (emitResult.emitSkipped){
console.log("OT: Compilation Failed!")
process.exit(1)
}else{
console.log("OT: Compilation Succeeded!")
if (process.argv.includes("-%-compile")) process.exit(0) //exit when only compile is required!
//START THE BOT
console.log("OT: Starting Bot!")
require("./dist/src/index.js")
}
+3 -2
View File
@@ -12,8 +12,9 @@
],
"main": "index.js",
"scripts": {
"start": "node index.js -%-compile && npx tsc -p ./tsconfig.json && node index.js",
"test": "npm run start -- --dev-config --dev-database"
"build": "node index.js -%-compile",
"start": "node index.js",
"test": "node index.js --dev-config --dev-database"
},
"type": "commonjs",
"license": "GPL-3.0-only",