From 4fbe5e6e4f74ae091a20d042704a4ca48b66eff0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20Lenon?= Date: Tue, 21 Mar 2023 11:17:05 -0300 Subject: [PATCH 1/5] chore(scripts): move node cli to a shell script file --- node | 6 ++++++ package-lock.json | 4 ++-- package.json | 9 ++++----- 3 files changed, 12 insertions(+), 7 deletions(-) create mode 100755 node 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": [ From 9dd964434b65223c3eb99674e30dfed4271dc352 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20Lenon?= Date: Tue, 21 Mar 2023 11:18:39 -0300 Subject: [PATCH 2/5] chore(build): implement beforeAll and afterAll functions --- bin/build.ts | 62 +++++++++++++++++++++++++++++++++++----------------- 1 file changed, 42 insertions(+), 20 deletions(-) diff --git a/bin/build.ts b/bin/build.ts index bb22afd..75c7213 100644 --- a/bin/build.ts +++ b/bin/build.ts @@ -7,7 +7,7 @@ * file that was distributed with this source code. */ -import { Path, File, Exec } from '#src' +import { Path, File, Exec, Folder, Is } from '#src' /* |-------------------------------------------------------------------------- @@ -19,34 +19,56 @@ import { Path, File, Exec } from '#src' const path = Path.nodeModules('@athenna/tsconfig.build.json') -/* -|-------------------------------------------------------------------------- -| TypeScript Config -|-------------------------------------------------------------------------- -| -| Create the tsconfig file for building the project. -*/ +async function beforeAll() { + const tsconfig = await new File('../tsconfig.json').getContentAsBuilder() -const tsconfig = await new File('../tsconfig.json').getContentAsJson() + 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']) -delete tsconfig['ts-node'] + const oldBuild = new Folder(Path.pwd('/build')) + await new File(path, JSON.stringify(tsconfig.get())).load() -tsconfig.compilerOptions.rootDir = '../../src' -tsconfig.compilerOptions.outDir = '../../build' + if (oldBuild.folderExists) { + await oldBuild.remove() + } +} -tsconfig.include = ['../../src'] -tsconfig.exclude = ['../../bin', '../../node_modules', '../../tests'] +async function afterAll() { + const tsConfigBuild = await new File(path).load() + + if (tsConfigBuild.fileExists) { + await tsConfigBuild.remove() + } +} /* |-------------------------------------------------------------------------- | Compilation |-------------------------------------------------------------------------- | -| Saving the file in some path, deleting old "build" folder, executing -| compilation and deleting the tsconfig file generated. +| Executing compilation and deleting the tsconfig.build file generated. */ -const file = new File(path, '') -await file.setContent(JSON.stringify(tsconfig)) -await Exec.command(`rimraf ../build && tsc --project ${path}`) -await file.remove() +try { + await beforeAll() + + const { stdout } = await Exec.command( + `node_modules/.bin/tsc --project ${path}`, + ) + + if (stdout) { + console.log(stdout) + } +} catch (error) { + if (!Is.Exception(error)) { + // eslint-disable-next-line no-ex-assign + error = error.toAthennaException() + } + + console.error(await error.prettify()) +} finally { + await afterAll() +} From 55ec3e678db23d28cd94d163b483a7eab52a3e31 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20Lenon?= Date: Tue, 21 Mar 2023 11:19:46 -0300 Subject: [PATCH 3/5] fix(message): remove some new lines --- src/Exceptions/NodeCommandException.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) 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({ From ebbfa4f4b4741704dd305dcff835369be5a6f0c0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20Lenon?= Date: Tue, 21 Mar 2023 11:20:06 -0300 Subject: [PATCH 4/5] feat(file): implement function to convert content to ObjectBuilder --- src/Helpers/File.ts | 24 +++++++++++++++++++++++- tests/Unit/FileTest.ts | 12 ++++++++++++ 2 files changed, 35 insertions(+), 1 deletion(-) 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' }) + }) }) From 349f84d8b7c7150322d611d2c31a9ad721b3a91f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20Lenon?= Date: Tue, 21 Mar 2023 11:30:33 -0300 Subject: [PATCH 5/5] style(build): add some comments with explanation --- bin/build.ts | 56 +++++++++++++++++++++++++++++----------------------- 1 file changed, 31 insertions(+), 25 deletions(-) diff --git a/bin/build.ts b/bin/build.ts index 75c7213..ade3828 100644 --- a/bin/build.ts +++ b/bin/build.ts @@ -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() @@ -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 {