Skip to content

Commit

Permalink
style(build): add some comments with explanation
Browse files Browse the repository at this point in the history
  • Loading branch information
jlenon7 committed Mar 21, 2023
1 parent ebbfa4f commit 349f84d
Showing 1 changed file with 31 additions and 25 deletions.
56 changes: 31 additions & 25 deletions bin/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,32 @@
* file that was distributed with this source code.
*/

/* eslint-disable no-ex-assign */

import { Path, File, Exec, Folder, Is } from '#src'

/*
|--------------------------------------------------------------------------
| TypeScript build file path
| The tsconfig.build.json path and the compile command.
|--------------------------------------------------------------------------
|
| Where the TypeScript build file will be saved.
| The path where tsconfig.build.json will be created and the compilation
| command that will be used to compile the files.
*/

const path = Path.nodeModules('@athenna/tsconfig.build.json')
const compileCommand = `node_modules/.bin/tsc --project ${path}`

/*
|--------------------------------------------------------------------------
| Before all hook.
|--------------------------------------------------------------------------
|
| This function will be executed before the compilation starts. Briefly,
| this function will create a tsconfig.build.json file with the correct
| configurations to compile the files such as rootDir, outDir, etc and
| also delete the old build folder if it exists.
*/

async function beforeAll() {
const tsconfig = await new File('../tsconfig.json').getContentAsBuilder()
Expand All @@ -31,42 +46,33 @@ async function beforeAll() {
const oldBuild = new Folder(Path.pwd('/build'))
await new File(path, JSON.stringify(tsconfig.get())).load()

if (oldBuild.folderExists) {
await oldBuild.remove()
}
}

async function afterAll() {
const tsConfigBuild = await new File(path).load()

if (tsConfigBuild.fileExists) {
await tsConfigBuild.remove()
}
if (oldBuild.folderExists) await oldBuild.remove()
}

/*
|--------------------------------------------------------------------------
| Compilation
| After all hook.
|--------------------------------------------------------------------------
|
| Executing compilation and deleting the tsconfig.build file generated.
| This function will be executed after some error occurs or after the compi
| lation finishes. This function just delete the tsconfig.build.json file if
| it exists.
*/

async function afterAll() {
const tsConfigBuild = await new File(path).load()

if (tsConfigBuild.fileExists) await tsConfigBuild.remove()
}

try {
await beforeAll()

const { stdout } = await Exec.command(
`node_modules/.bin/tsc --project ${path}`,
)
const { stdout } = await Exec.command(compileCommand)

if (stdout) {
console.log(stdout)
}
if (stdout) console.log(stdout)
} catch (error) {
if (!Is.Exception(error)) {
// eslint-disable-next-line no-ex-assign
error = error.toAthennaException()
}
if (!Is.Exception(error)) error = error.toAthennaException()

console.error(await error.prettify())
} finally {
Expand Down

0 comments on commit 349f84d

Please sign in to comment.