diff --git a/index.js b/index.js index a35c823..a947651 100644 --- a/index.js +++ b/index.js @@ -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...") - fs.rmSync("./dist",{recursive:true,force:true}) - console.log("Compiling the bot...") - //exit on compilation - process.exit(0) +const ts = require("typescript") +const nodepath = require('path') + +//REMOVE EXISTING BUILDS +console.log("OT: Removing Prebuilds...") +fs.rmSync("./dist",{recursive:true,force:true}) + +//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") \ No newline at end of file +//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") +} \ No newline at end of file diff --git a/package.json b/package.json index 5a72c56..681b0d8 100644 --- a/package.json +++ b/package.json @@ -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",