diff --git a/bin/build.ts b/bin/build.ts index bb22afd..ade3828 100644 --- a/bin/build.ts +++ b/bin/build.ts @@ -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() +} diff --git a/node b/node new file mode 100755 index 0000000..948240a --- /dev/null +++ b/node @@ -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 $@ diff --git a/package-lock.json b/package-lock.json index 4fd00fe..0c07fae 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "@athenna/common", - "version": "3.4.1", + "version": "3.4.2", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "@athenna/common", - "version": "3.4.1", + "version": "3.4.2", "license": "MIT", "dependencies": { "@fastify/formbody": "^7.4.0", diff --git a/package.json b/package.json index 81e4367..e53a221 100644 --- a/package.json +++ b/package.json @@ -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 ", @@ -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": [ diff --git a/src/Exceptions/NodeCommandException.ts b/src/Exceptions/NodeCommandException.ts index 4703dd5..375523b 100644 --- a/src/Exceptions/NodeCommandException.ts +++ b/src/Exceptions/NodeCommandException.ts @@ -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({ diff --git a/src/Helpers/File.ts b/src/Helpers/File.ts index cef6cd1..25a6174 100644 --- a/src/Helpers/File.ts +++ b/src/Helpers/File.ts @@ -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' @@ -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 @@ -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 { + 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. */ diff --git a/tests/Unit/FileTest.ts b/tests/Unit/FileTest.ts index 67b0095..5afcb01 100644 --- a/tests/Unit/FileTest.ts +++ b/tests/Unit/FileTest.ts @@ -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' }) + }) })