diff --git a/.gitignore b/.gitignore index daa6906..1c67c48 100644 --- a/.gitignore +++ b/.gitignore @@ -137,3 +137,6 @@ out # MacOS folder mapper file .DS_Store + +# Temp folder +tmp diff --git a/bin/artisan.ts b/bin/artisan.ts index 0a59182..9020215 100644 --- a/bin/artisan.ts +++ b/bin/artisan.ts @@ -32,6 +32,7 @@ Config.set('rc.commands', [ '#src/Commands/MakeServiceCommand', '#src/Commands/MakeTestCommand', '#src/Commands/ServeCommand', + '#src/Commands/BuildCommand', '#src/Commands/TestCommand', ]) diff --git a/bin/test.ts b/bin/test.ts index c2da654..25fd216 100644 --- a/bin/test.ts +++ b/bin/test.ts @@ -61,7 +61,7 @@ configure({ plugins: [assert()], reporters: [specReporter()], importer: Importer.import, - timeout: 10000, + timeout: 15000, }, }) diff --git a/package-lock.json b/package-lock.json index 1dcc7fa..6edac7c 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,21 +1,21 @@ { "name": "@athenna/core", - "version": "3.1.6", + "version": "3.1.8", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "@athenna/core", - "version": "3.1.6", + "version": "3.1.8", "license": "MIT", "dependencies": { "pretty-repl": "^3.1.1", "semver": "^7.3.8" }, "devDependencies": { - "@athenna/artisan": "^3.3.6", - "@athenna/common": "^3.4.4", - "@athenna/config": "^3.2.3", + "@athenna/artisan": "^3.3.7", + "@athenna/common": "^3.4.5", + "@athenna/config": "^3.2.5", "@athenna/http": "^3.4.3", "@athenna/ioc": "^3.1.8", "@athenna/logger": "^3.1.7", @@ -93,9 +93,9 @@ "dev": true }, "node_modules/@athenna/artisan": { - "version": "3.3.6", - "resolved": "https://registry.npmjs.org/@athenna/artisan/-/artisan-3.3.6.tgz", - "integrity": "sha512-zT9m91vxmb+eEgYtK6eBBgF8VZdDNVsesL8guGstqK+UCZKK7Vfn4RhiajgAO9QLT6vv8LdDqNmigwtqtQjmRQ==", + "version": "3.3.7", + "resolved": "https://registry.npmjs.org/@athenna/artisan/-/artisan-3.3.7.tgz", + "integrity": "sha512-i9xNio4JPIV58iNk5AJnH+aubN1nL0CXOIFC+siaBteIrGLA9ZtbY1QIAUk/XKrjT2f+qDGj2qNFYuyo/SROug==", "dev": true, "dependencies": { "chalk-rainbow": "^1.0.0", @@ -449,9 +449,9 @@ } }, "node_modules/@athenna/common": { - "version": "3.4.4", - "resolved": "https://registry.npmjs.org/@athenna/common/-/common-3.4.4.tgz", - "integrity": "sha512-VjHkScA6cX+cqrmTtOVyE+sw0RZN95zNVSkoUmm9zy0Kz9XKAlPQ3VczSPm34/zFzV0aJP6jnAScNqhmWrrugg==", + "version": "3.4.5", + "resolved": "https://registry.npmjs.org/@athenna/common/-/common-3.4.5.tgz", + "integrity": "sha512-N4xH0+AJgSYo9KKbx3p0OR7BFcRu7F6m0aXSYFh6DyHVkdQ3OKe8Ks6Bf7js0jUuLV66X5+8WY/tDMn9KvLkiw==", "dev": true, "dependencies": { "@fastify/formbody": "^7.4.0", @@ -480,9 +480,9 @@ } }, "node_modules/@athenna/config": { - "version": "3.2.3", - "resolved": "https://registry.npmjs.org/@athenna/config/-/config-3.2.3.tgz", - "integrity": "sha512-LdPqBg+i4wHZ2A/quYmyjbE9excEeHu42TToW0jQzul7GNsu2Nivb3R78rLf9F3F1FpdKa2QpctVUlgb0Vyd3A==", + "version": "3.2.5", + "resolved": "https://registry.npmjs.org/@athenna/config/-/config-3.2.5.tgz", + "integrity": "sha512-07aBbgOmfNbhNVZ6xEm1Wlxwmz11IsaWh1jNxq6tki3R4spPhUBPlPwjHNjtJBfCDqp/xeWMgRyHpFG1zX7C6A==", "dev": true, "dependencies": { "dotenv": "^16.0.3", @@ -10123,9 +10123,9 @@ "dev": true }, "@athenna/artisan": { - "version": "3.3.6", - "resolved": "https://registry.npmjs.org/@athenna/artisan/-/artisan-3.3.6.tgz", - "integrity": "sha512-zT9m91vxmb+eEgYtK6eBBgF8VZdDNVsesL8guGstqK+UCZKK7Vfn4RhiajgAO9QLT6vv8LdDqNmigwtqtQjmRQ==", + "version": "3.3.7", + "resolved": "https://registry.npmjs.org/@athenna/artisan/-/artisan-3.3.7.tgz", + "integrity": "sha512-i9xNio4JPIV58iNk5AJnH+aubN1nL0CXOIFC+siaBteIrGLA9ZtbY1QIAUk/XKrjT2f+qDGj2qNFYuyo/SROug==", "dev": true, "requires": { "chalk-rainbow": "^1.0.0", @@ -10355,9 +10355,9 @@ } }, "@athenna/common": { - "version": "3.4.4", - "resolved": "https://registry.npmjs.org/@athenna/common/-/common-3.4.4.tgz", - "integrity": "sha512-VjHkScA6cX+cqrmTtOVyE+sw0RZN95zNVSkoUmm9zy0Kz9XKAlPQ3VczSPm34/zFzV0aJP6jnAScNqhmWrrugg==", + "version": "3.4.5", + "resolved": "https://registry.npmjs.org/@athenna/common/-/common-3.4.5.tgz", + "integrity": "sha512-N4xH0+AJgSYo9KKbx3p0OR7BFcRu7F6m0aXSYFh6DyHVkdQ3OKe8Ks6Bf7js0jUuLV66X5+8WY/tDMn9KvLkiw==", "dev": true, "requires": { "@fastify/formbody": "^7.4.0", @@ -10386,9 +10386,9 @@ } }, "@athenna/config": { - "version": "3.2.3", - "resolved": "https://registry.npmjs.org/@athenna/config/-/config-3.2.3.tgz", - "integrity": "sha512-LdPqBg+i4wHZ2A/quYmyjbE9excEeHu42TToW0jQzul7GNsu2Nivb3R78rLf9F3F1FpdKa2QpctVUlgb0Vyd3A==", + "version": "3.2.5", + "resolved": "https://registry.npmjs.org/@athenna/config/-/config-3.2.5.tgz", + "integrity": "sha512-07aBbgOmfNbhNVZ6xEm1Wlxwmz11IsaWh1jNxq6tki3R4spPhUBPlPwjHNjtJBfCDqp/xeWMgRyHpFG1zX7C6A==", "dev": true, "requires": { "dotenv": "^16.0.3", diff --git a/package.json b/package.json index 82ef7b4..ce195e1 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@athenna/core", - "version": "3.1.7", + "version": "3.1.8", "description": "The plug and play Node.js framework.", "license": "MIT", "author": "João Lenon ", @@ -44,6 +44,7 @@ "./commands/MakeServiceCommand": "./src/Commands/MakeServiceCommand.js", "./commands/MakeTestCommand": "./src/Commands/MakeTestCommand.js", "./commands/ReplCommand": "./src/Commands/ReplCommand.js", + "./commands/BuildCommand": "./src/Commands/BuildCommand.js", "./commands/ServeCommand": "./src/Commands/ServeCommand.js", "./commands/TestCommand": "./src/Commands/TestCommand.js" }, @@ -60,9 +61,9 @@ "semver": "^7.3.8" }, "devDependencies": { - "@athenna/artisan": "^3.3.6", - "@athenna/common": "^3.4.4", - "@athenna/config": "^3.2.3", + "@athenna/artisan": "^3.3.7", + "@athenna/common": "^3.4.5", + "@athenna/config": "^3.2.5", "@athenna/http": "^3.4.3", "@athenna/ioc": "^3.1.8", "@athenna/logger": "^3.1.7", @@ -235,6 +236,7 @@ "#src/Commands/MakeServiceCommand", "#src/Commands/MakeTestCommand", "#src/Commands/ServeCommand", + "#src/Commands/BuildCommand", "#src/Commands/TestCommand", "#src/Commands/ReplCommand" ], @@ -244,6 +246,7 @@ "make:provider": "#src/Commands/MakeProviderCommand", "make:service": "#src/Commands/MakeServiceCommand", "make:test": "#src/Commands/MakeTestCommand", + "build": "#src/Commands/BuildCommand", "serve": { "entrypoint": "#bin/http", "path": "#src/Commands/ServeCommand" diff --git a/src/Commands/BuildCommand.ts b/src/Commands/BuildCommand.ts new file mode 100644 index 0000000..73ce494 --- /dev/null +++ b/src/Commands/BuildCommand.ts @@ -0,0 +1,79 @@ +/** + * @athenna/core + * + * (c) João Lenon + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +import { Exec, File, Folder } from '@athenna/common' +import { BaseCommand, Option } from '@athenna/artisan' + +export class BuildCommand extends BaseCommand { + @Option({ + signature: '--clean', + description: 'Clean all .js and .d.ts files instead of building your code.', + default: false, + }) + public clean: boolean + + @Option({ + signature: '--ignore-on-clean [folders]', + description: 'Ignore the given folders when cleaning the application.', + default: 'tests|node_modules', + }) + public ignoreOnClean: string + + public static signature(): string { + return 'build' + } + + public static description(): string { + return 'Compile your application code to JavaScript.' + } + + public async handle(): Promise { + if (this.clean) { + const folder = await new Folder(Path.pwd()).load() + const files = folder.getFilesByPattern( + `!(${this.ignoreOnClean})/**/*.@(js|d.ts)`, + ) + + await Exec.concurrently(files, file => file.remove()).then(() => + this.logger.success('Application successfully cleaned.'), + ) + + return + } + + const tsConfig = await this.getTsConfig() + + await Exec.command(`${Path.bin('tsc')} --project ${tsConfig.path}`).then( + () => this.logger.success('Application successfully compiled.'), + ) + } + + private getTsConfig(): Promise { + const path = Config.get( + 'rc.commandsManifest.build.tsconfig', + '../../tmp/tsconfig.build.json', + ) + + const content = { + extends: this.toPosix(Path.pwd('tsconfig.json')), + include: [this.toPosix(Path.pwd('**/*'))], + exclude: [this.toPosix(Path.tests()), this.toPosix(Path.nodeModules())], + } + + return new File(path, JSON.stringify(content, null, 2)).load() + } + + private toPosix(path: string): string { + if (process.platform === 'win32') { + return path.replace(/\\/g, '/').slice(2) + } + + return path + } +} diff --git a/src/Commands/ReplCommand.ts b/src/Commands/ReplCommand.ts index 0ef3060..1c63c71 100644 --- a/src/Commands/ReplCommand.ts +++ b/src/Commands/ReplCommand.ts @@ -13,8 +13,7 @@ import { BaseCommand, CommandSettings, Option } from '@athenna/artisan' export class ReplCommand extends BaseCommand { @Option({ signature: '-e, --env ', - description: - 'Change the evironment where the application will run. Default is ""', + description: 'Change the evironment where the application will run.', default: '', }) public env: string diff --git a/src/Commands/ServeCommand.ts b/src/Commands/ServeCommand.ts index 2831089..e794bca 100644 --- a/src/Commands/ServeCommand.ts +++ b/src/Commands/ServeCommand.ts @@ -13,8 +13,7 @@ import { BaseCommand, CommandSettings, Option } from '@athenna/artisan' export class ServeCommand extends BaseCommand { @Option({ signature: '-e, --env ', - description: - 'Change the evironment where the application will run. Default is ""', + description: 'Change the evironment where the application will run.', default: '', }) public env: string diff --git a/src/Commands/TestCommand.ts b/src/Commands/TestCommand.ts index 3352fc2..403d616 100644 --- a/src/Commands/TestCommand.ts +++ b/src/Commands/TestCommand.ts @@ -13,8 +13,7 @@ import { BaseCommand, Option, CommandSettings } from '@athenna/artisan' export class TestCommand extends BaseCommand { @Option({ signature: '-e, --env ', - description: - 'Change the evironment where your tests wil run. Default is "test"', + description: 'Change the evironment where your tests wil run.', default: 'test', }) public env: string diff --git a/tests/Helpers/BaseCommandTest.ts b/tests/Helpers/BaseCommandTest.ts index 783278e..0ec97c4 100644 --- a/tests/Helpers/BaseCommandTest.ts +++ b/tests/Helpers/BaseCommandTest.ts @@ -9,7 +9,7 @@ import { Config } from '@athenna/config' import { ViewProvider } from '@athenna/view' -import { File, Folder } from '@athenna/common' +import { Exec, File, Folder } from '@athenna/common' import { LoggerProvider } from '@athenna/logger' import { ExitFaker, AfterEach, BeforeEach } from '@athenna/test' import { ConsoleKernel, ArtisanProvider, COMMANDS_SETTINGS, CommanderHandler } from '@athenna/artisan' @@ -57,7 +57,9 @@ export class BaseCommandTest { await File.safeRemove(Path.pwd('docker-compose.yml')) await File.safeRemove(Path.tests('Unit/TestTest.ts')) await Folder.safeRemove(Path.stubs('storage')) + await Folder.safeRemove(Path.pwd('tmp')) + await Exec.command(`cd ${Path.pwd()} && sh scripts/clean`) await new File(Path.pwd('package.json')).setContent(this.originalPJson) } } diff --git a/tests/Unit/Commands/BuildCommandTest.ts b/tests/Unit/Commands/BuildCommandTest.ts new file mode 100644 index 0000000..aab7406 --- /dev/null +++ b/tests/Unit/Commands/BuildCommandTest.ts @@ -0,0 +1,58 @@ +/** + * @athenna/core + * + * (c) João Lenon + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +import { Artisan } from '@athenna/artisan' +import { Exec, File, Folder } from '@athenna/common' +import { Test, TestContext, Timeout } from '@athenna/test' +import { BaseCommandTest } from '#tests/Helpers/BaseCommandTest' + +export default class BuildCommandTest extends BaseCommandTest { + @Test() + @Timeout(60000) + public async shouldBeAbleToBuildTheApplication({ assert }: TestContext) { + const { stdout, stderr } = await Artisan.callInChild('build', this.artisan) + + assert.deepEqual(stderr, '') + assert.isTrue(stdout.includes('Application successfully compiled')) + assert.isTrue(await File.exists(Path.src('Applications/Artisan.js'))) + assert.isTrue(await File.exists(Path.src('Applications/Artisan.d.ts'))) + assert.isFalse(await File.exists(Path.tests('Unit/Applications/ArtisanTest.js'))) + assert.isFalse(await File.exists(Path.tests('Unit/Applications/ArtisanTest.d.ts'))) + } + + @Test() + @Timeout(60000) + public async shouldBeAbleToBuildTheApplicationEvenIfTsConfigAlreadyExistsInTmp({ assert }: TestContext) { + await Artisan.callInChild('build', this.artisan) + + await Folder.safeRemove(Path.pwd('tmp')) + await Exec.command(`cd ${Path.pwd()} && sh scripts/clean`) + + const { stdout, stderr } = await Artisan.callInChild('build', this.artisan) + + assert.deepEqual(stderr, '') + assert.isTrue(stdout.includes('Application successfully compiled')) + assert.isTrue(await File.exists(Path.src('Applications/Artisan.js'))) + assert.isTrue(await File.exists(Path.src('Applications/Artisan.d.ts'))) + assert.isFalse(await File.exists(Path.tests('Unit/Applications/ArtisanTest.js'))) + assert.isFalse(await File.exists(Path.tests('Unit/Applications/ArtisanTest.d.ts'))) + } + + @Test() + @Timeout(60000) + public async shouldBeAbleToCleanAllJsAndDTsFilesFromTheApplication({ assert }: TestContext) { + const { stdout, stderr } = await Artisan.callInChild('build --clean', this.artisan) + + assert.deepEqual(stderr, '') + assert.isTrue(stdout.includes('Application successfully cleaned')) + assert.isFalse(await File.exists(Path.src('Applications/Artisan.js'))) + assert.isFalse(await File.exists(Path.src('Applications/Artisan.d.ts'))) + assert.isTrue(await File.exists(Path.stubs('main.js'))) + } +} diff --git a/tsconfig.json b/tsconfig.json index 2d26006..c237be5 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -23,6 +23,6 @@ "#tests/*": ["./tests/*.ts"] } }, - "include": ["./**/*"], + "include": ["./**/*", "hello/node_modules"], "exclude": ["build", "node_modules"] }