Skip to content

Commit

Permalink
Merge pull request #36 from AthennaIO/develop
Browse files Browse the repository at this point in the history
Move node cli to a shell script file and better error handling of build script file
  • Loading branch information
jlenon7 authored Mar 21, 2023
2 parents 09f2e32 + 349f84d commit 22d77fe
Show file tree
Hide file tree
Showing 7 changed files with 96 additions and 29 deletions.
64 changes: 46 additions & 18 deletions bin/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,46 +7,74 @@
* file that was distributed with this source code.
*/

import { Path, File, Exec } from '#src'
/* 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}`

/*
|--------------------------------------------------------------------------
| TypeScript Config
| Before all hook.
|--------------------------------------------------------------------------
|
| Create the tsconfig file for building the project.
| 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.
*/

const tsconfig = await new File('../tsconfig.json').getContentAsJson()
async function beforeAll() {
const tsconfig = await new File('../tsconfig.json').getContentAsBuilder()

delete tsconfig['ts-node']
tsconfig.delete('ts-node')
tsconfig.set('include', ['../../src'])
tsconfig.set('compilerOptions.rootDir', '../../src')
tsconfig.set('compilerOptions.outDir', '../../build')
tsconfig.set('exclude', ['../../bin', '../../node_modules', '../../tests'])

tsconfig.compilerOptions.rootDir = '../../src'
tsconfig.compilerOptions.outDir = '../../build'
const oldBuild = new Folder(Path.pwd('/build'))
await new File(path, JSON.stringify(tsconfig.get())).load()

tsconfig.include = ['../../src']
tsconfig.exclude = ['../../bin', '../../node_modules', '../../tests']
if (oldBuild.folderExists) await oldBuild.remove()
}

/*
|--------------------------------------------------------------------------
| Compilation
| After all hook.
|--------------------------------------------------------------------------
|
| Saving the file in some path, deleting old "build" folder, executing
| compilation and deleting the tsconfig 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.
*/

const file = new File(path, '')
await file.setContent(JSON.stringify(tsconfig))
await Exec.command(`rimraf ../build && tsc --project ${path}`)
await file.remove()
async function afterAll() {
const tsConfigBuild = await new File(path).load()

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

try {
await beforeAll()

const { stdout } = await Exec.command(compileCommand)

if (stdout) console.log(stdout)
} catch (error) {
if (!Is.Exception(error)) error = error.toAthennaException()

console.error(await error.prettify())
} finally {
await afterAll()
}
6 changes: 6 additions & 0 deletions node
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#!/bin/bash

# Node.js executable with all arguments required to run the application.
node="node --loader ts-node/esm --experimental-import-meta-resolve --no-warnings"

$node $@
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 4 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@athenna/common",
"version": "3.4.1",
"version": "3.4.2",
"description": "The Athenna common helpers to use in any Node.js ESM project.",
"license": "MIT",
"author": "João Lenon <[email protected]>",
Expand All @@ -17,11 +17,10 @@
"standalone"
],
"scripts": {
"node": "cross-env NODE_OPTIONS=\"--experimental-import-meta-resolve\" ts-node",
"build": "npm run node --silent -- bin/build.ts",
"build": "sh node bin/build.ts",
"lint:fix": "eslint \"{src,tests}/**/*.ts\" --fix",
"test": "npm run --silent lint:fix && npm run node --silent -- bin/test.ts",
"test:debug": "cross-env DEBUG=api:* npm run node --silent -- bin/test.ts --inspect",
"test": "npm run --silent lint:fix && sh node bin/test.ts",
"test:debug": "cross-env DEBUG=api:* sh node --inspect bin/test.ts",
"test:coverage": "c8 npm run --silent test"
},
"files": [
Expand Down
6 changes: 3 additions & 3 deletions src/Exceptions/NodeCommandException.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,15 @@ export class NodeCommandException extends Exception {
let help = ''

if (error.stdout) {
help = help.concat(`Command stdout:\n\n${error.stdout}\n\n`)
help = help.concat(`Command stdout:\n\n${error.stdout}`)
}

if (error.stderr) {
help = help.concat(`Command stderr:\n\n${error.stderr}\n\n`)
help = help.concat(`Command stderr:\n\n${error.stderr}`)
}

if (!error.stdout && !error.stdout) {
help = `Command error:\n\n${JSON.stringify(error)}\n\n`
help = `Command error:\n\n${JSON.stringify(error)}`
}

super({
Expand Down
24 changes: 23 additions & 1 deletion src/Helpers/File.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ import { lookup } from 'mime-types'
import { Is } from '#src/Helpers/Is'
import { pathToFileURL } from 'node:url'
import { Path } from '#src/Helpers/Path'
import { Json } from '#src/Helpers/Json'
import { randomBytes } from 'node:crypto'
import { Debug } from '#src/Helpers/Debug'
import { StreamOptions } from 'node:stream'
Expand All @@ -37,6 +36,7 @@ import { Module } from '#src/Helpers/Module'
import { Options } from '#src/Helpers/Options'
import { isAbsolute, parse, sep } from 'node:path'
import { NotFoundFileException } from '#src/Exceptions/NotFoundFileException'
import { Json, ObjectBuilder, ObjectBuilderOptions } from '#src/Helpers/Json'

export interface FileJSON {
dir: string
Expand Down Expand Up @@ -781,6 +781,28 @@ export class File {
return Json.parse(content)
}

/**
* Get only the content of the file as an instance of ObjectBuilder.
*/
public async getContentAsBuilder(options?: {
saveContent?: boolean
builder?: ObjectBuilderOptions
}): Promise<ObjectBuilder> {
return this.getContentAsJson(options).then(content =>
new ObjectBuilder().set(content),
)
}

/**
* Get only the content of the file as an instance of ObjectBuilder.
*/
public getContentAsBuilderSync(options?: {
saveContent?: boolean
builder?: ObjectBuilderOptions
}): ObjectBuilder {
return new ObjectBuilder().set(this.getContentAsJsonSync(options))
}

/**
* Create a readable stream of the file.
*/
Expand Down
12 changes: 12 additions & 0 deletions tests/Unit/FileTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -302,4 +302,16 @@ test.group('FileTest', group => {

assert.isNull(notFound)
})

test('should be able to get the file content as object builder instance', async ({ assert }) => {
const bigFileContent = await bigFile.setContentSync('{"hello":"world"}').getContentAsBuilder()

assert.deepEqual(bigFileContent.get(), { hello: 'world' })
})

test('should be able to get the file content as string json', async ({ assert }) => {
const bigFileContent = bigFile.setContentSync('{"hello":"world"}').getContentAsBuilderSync()

assert.deepEqual(bigFileContent.get(), { hello: 'world' })
})
})

0 comments on commit 22d77fe

Please sign in to comment.