Skip to content

Commit

Permalink
Merge pull request #154 from AthennaIO/develop
Browse files Browse the repository at this point in the history
Add build command
  • Loading branch information
jlenon7 authored Mar 23, 2023
2 parents 8071dee + 93cb415 commit 677c5f8
Show file tree
Hide file tree
Showing 12 changed files with 179 additions and 36 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -137,3 +137,6 @@ out

# MacOS folder mapper file
.DS_Store

# Temp folder
tmp
1 change: 1 addition & 0 deletions bin/artisan.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ Config.set('rc.commands', [
'#src/Commands/MakeServiceCommand',
'#src/Commands/MakeTestCommand',
'#src/Commands/ServeCommand',
'#src/Commands/BuildCommand',
'#src/Commands/TestCommand',
])

Expand Down
2 changes: 1 addition & 1 deletion bin/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ configure({
plugins: [assert()],
reporters: [specReporter()],
importer: Importer.import,
timeout: 10000,
timeout: 15000,
},
})

Expand Down
46 changes: 23 additions & 23 deletions package-lock.json

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

11 changes: 7 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
@@ -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 <[email protected]>",
Expand Down Expand Up @@ -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"
},
Expand All @@ -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",
Expand Down Expand Up @@ -235,6 +236,7 @@
"#src/Commands/MakeServiceCommand",
"#src/Commands/MakeTestCommand",
"#src/Commands/ServeCommand",
"#src/Commands/BuildCommand",
"#src/Commands/TestCommand",
"#src/Commands/ReplCommand"
],
Expand All @@ -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"
Expand Down
79 changes: 79 additions & 0 deletions src/Commands/BuildCommand.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
/**
* @athenna/core
*
* (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 { 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<void> {
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<File> {
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
}
}
3 changes: 1 addition & 2 deletions src/Commands/ReplCommand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,7 @@ import { BaseCommand, CommandSettings, Option } from '@athenna/artisan'
export class ReplCommand extends BaseCommand {
@Option({
signature: '-e, --env <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
Expand Down
3 changes: 1 addition & 2 deletions src/Commands/ServeCommand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,7 @@ import { BaseCommand, CommandSettings, Option } from '@athenna/artisan'
export class ServeCommand extends BaseCommand {
@Option({
signature: '-e, --env <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
Expand Down
3 changes: 1 addition & 2 deletions src/Commands/TestCommand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,7 @@ import { BaseCommand, Option, CommandSettings } from '@athenna/artisan'
export class TestCommand extends BaseCommand {
@Option({
signature: '-e, --env <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
Expand Down
4 changes: 3 additions & 1 deletion tests/Helpers/BaseCommandTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -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)
}
}
58 changes: 58 additions & 0 deletions tests/Unit/Commands/BuildCommandTest.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/**
* @athenna/core
*
* (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 { 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')))
}
}
2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,6 @@
"#tests/*": ["./tests/*.ts"]
}
},
"include": ["./**/*"],
"include": ["./**/*", "hello/node_modules"],
"exclude": ["build", "node_modules"]
}

0 comments on commit 677c5f8

Please sign in to comment.