Skip to content

Commit

Permalink
Merge pull request #184 from AthennaIO/develop
Browse files Browse the repository at this point in the history
Move build command to core
  • Loading branch information
jlenon7 authored Aug 30, 2023
2 parents 34d75bd + 501b09a commit 4e7b3eb
Show file tree
Hide file tree
Showing 11 changed files with 706 additions and 57 deletions.
594 changes: 541 additions & 53 deletions package-lock.json

Large diffs are not rendered by default.

18 changes: 15 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@athenna/core",
"version": "4.3.0",
"version": "4.4.0",
"description": "The plug and play Node.js framework.",
"license": "MIT",
"author": "João Lenon <[email protected]>",
Expand Down Expand Up @@ -49,7 +49,8 @@
"./commands/MakeTestCommand": "./src/commands/MakeTestCommand.js",
"./commands/ReplCommand": "./src/commands/ReplCommand.js",
"./commands/ServeCommand": "./src/commands/ServeCommand.js",
"./commands/TestCommand": "./src/commands/TestCommand.js"
"./commands/TestCommand": "./src/commands/TestCommand.js",
"./commands/BuildCommand": "./src/commands/BuildCommand.js"
},
"imports": {
"#bin/*": "./bin/*.js",
Expand All @@ -64,7 +65,7 @@
},
"dependencies": {
"pretty-repl": "^3.1.2",
"semver": "^7.5.3"
"semver": "^7.5.4"
},
"devDependencies": {
"@athenna/artisan": "^4.2.0",
Expand All @@ -79,6 +80,7 @@
"@typescript-eslint/parser": "^5.56.0",
"c8": "^7.12.0",
"commitizen": "^4.2.6",
"copyfiles": "^2.4.1",
"cross-env": "^7.0.3",
"cz-conventional-changelog": "^3.3.0",
"eslint": "^8.36.0",
Expand All @@ -92,6 +94,7 @@
"lint-staged": "^12.5.0",
"prettier": "^2.8.7",
"reflect-metadata": "^0.1.13",
"rimraf": "^5.0.1",
"ts-node": "^10.9.1",
"typescript": "^5.0.2"
},
Expand Down Expand Up @@ -225,6 +228,15 @@
"stayAlive": true,
"entrypoint": "#bin/repl",
"path": "#src/commands/ReplCommand"
},
"build": {
"path": "#src/commands/BuildCommand",
"tsconfig": "./tests/stubs/tsconfig.json",
"metaFiles": [
"app/hello.edge",
"LICENSE.md",
".env"
]
}
},
"services": [
Expand Down
85 changes: 85 additions & 0 deletions src/commands/BuildCommand.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
/**
* @athenna/artisan
*
* (c) João Lenon <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

import { BaseCommand } from '@athenna/artisan'
import { isAbsolute, join, parse, sep } from 'node:path'
import { Exec, Path, File, Color } from '@athenna/common'

export class BuildCommand extends BaseCommand {
public static signature(): string {
return 'build'
}

public static description(): string {
return 'Compile your application code from TypeScript to JavaScript.'
}

public async handle(): Promise<void> {
this.logger.simple('({bold,green} [ BUILD APPLICATION ])\n')

let tsConfigPath = Config.get('rc.commands.build.tsconfig')

if (!isAbsolute(tsConfigPath)) {
tsConfigPath = join(Path.pwd(), tsConfigPath)
}

const tsConfig = await new File(tsConfigPath).getContentAsJson()
const metaFiles = Config.get('rc.commands.build.metaFiles', []).join(' ')
const buildDir =
parse(tsConfigPath).dir + sep + tsConfig.compilerOptions.outDir

if (metaFiles.includes(' .env ')) {
metaFiles.replace(' .env ', '')
}

const tasks = this.logger.task()

tasks.add(
`Delete old ${Color.yellow.bold(parse(buildDir).name)} folder`,
async task => {
await Exec.command(`${Path.nodeModulesBin('rimraf')} ${buildDir}`)
.then(() => task.complete())
.catch(error => {
task.fail()
throw error
})
},
)

tasks.add('Compile the application', async task => {
await Exec.command(
`${Path.nodeModulesBin('tsc')} --project ${tsConfigPath}`,
)
.then(() => task.complete())
.catch(error => {
task.fail()
throw error
})
})

if (metaFiles.length) {
tasks.add(`Copy meta files: ${Color.gray(metaFiles)}`, async task => {
await Exec.command(
`${Path.nodeModulesBin('copyfiles')} ${metaFiles} ${buildDir}`,
)
.then(() => task.complete())
.catch(error => {
task.fail()
throw error
})
})
}

await tasks.run()

console.log()

this.logger.success('Application successfully compiled')
}
}
2 changes: 1 addition & 1 deletion src/types/ArtisanOptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ export type ArtisanOptions = {
* The path to the exception handler of console commands. The exception
* handler is responsible to handle all the exception that are throwed
* in Artisan commands. By default, Athenna will use the built in exception
* handler. But you can do your own implementantion extending the
* handler. But you can do your own implementation extending the
* "ConsoleExceptionHandler" class from Artisan and setting the path to it
* here.
*
Expand Down
1 change: 1 addition & 0 deletions tests/helpers/BaseCommandTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ 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.stubs('build'))
await Folder.safeRemove(Path.pwd('tmp'))

await new File(Path.pwd('package.json')).setContent(this.originalPJson)
Expand Down
1 change: 1 addition & 0 deletions tests/stubs/LICENSE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
hello
1 change: 1 addition & 0 deletions tests/stubs/app/hello.edge
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
hello
1 change: 1 addition & 0 deletions tests/stubs/app/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
console.log('hello world!')
5 changes: 5 additions & 0 deletions tests/stubs/artisan.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,11 @@ Config.set('rc.commands.serve', {
path: '#src/commands/ServeCommand',
entrypoint: '#tests/stubs/bootstrap/main',
})
Config.set('rc.commands.build', {
path: '#src/commands/BuildCommand',
tsconfig: './tests/stubs/tsconfig.json',
metaFiles: ['app/hello.edge', 'LICENSE.md', '.env'],
})

/*
|--------------------------------------------------------------------------
Expand Down
27 changes: 27 additions & 0 deletions tests/stubs/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
{
"ts-node": {
"esm": true,
"transpileOnly": true,
"ignoreDiagnostics": [5104]
},
"compilerOptions": {
"strict": false,
"rootDir": ".",
"baseUrl": ".",
"outDir": "build",
"module": "NodeNext",
"target": "ESNext",
"moduleResolution": "NodeNext",
"declaration": true,
"skipLibCheck": true,
"esModuleInterop": true,
"removeComments": false,
"resolveJsonModule": true,
"experimentalDecorators": true,
"useDefineForClassFields": false,
"verbatimModuleSyntax": true,
"forceConsistentCasingInFileNames": true
},
"include": ["app"],
"exclude": ["build", "node_modules"]
}
28 changes: 28 additions & 0 deletions tests/unit/commands/BuildCommandTest.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/**
* @athenna/artisan
*
* (c) João Lenon <[email protected]>
*
* 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 { File, Folder } from '@athenna/common'
import { Test, type Context } from '@athenna/test'
import { BaseCommandTest } from '#tests/helpers/BaseCommandTest'

export default class BuildCommandTest extends BaseCommandTest {
@Test()
public async shouldBeAbleToCompileTheApplication({ assert }: Context) {
const { stdout, stderr } = await Artisan.callInChild('build', this.artisan)

console.log(stderr)
assert.isTrue(stdout.includes('Application successfully compiled'))
assert.isTrue(Folder.existsSync(Path.stubs('build')))
assert.isFalse(File.existsSync(Path.stubs('build/.env')))
assert.isTrue(File.existsSync(Path.stubs('build/LICENSE.md')))
assert.isTrue(File.existsSync(Path.stubs('build/app/index.js')))
assert.isTrue(File.existsSync(Path.stubs('build/app/index.d.ts')))
}
}

0 comments on commit 4e7b3eb

Please sign in to comment.