Skip to content

Commit

Permalink
Merge pull request #6 from AthennaIO/develop
Browse files Browse the repository at this point in the history
Commands for compiling, testing and starting code
  • Loading branch information
jlenon7 authored Apr 21, 2022
2 parents fdee934 + 0e840c4 commit aa94b65
Show file tree
Hide file tree
Showing 25 changed files with 867 additions and 31 deletions.
99 changes: 83 additions & 16 deletions package-lock.json

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

10 changes: 8 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@athenna/artisan",
"version": "1.0.6",
"version": "1.0.7",
"description": "",
"license": "MIT",
"author": "João Lenon <[email protected]>",
Expand All @@ -19,6 +19,8 @@
"typescript"
],
"devDependencies": {
"@types/cli-table": "0.3.0",
"@types/columnify": "1.5.1",
"@types/jest": "27.0.1",
"@types/node": "14.17.0",
"@typescript-eslint/eslint-plugin": "4.31.0",
Expand Down Expand Up @@ -71,7 +73,9 @@
"ts"
],
"rootDir": ".",
"testRegex": "Test.ts$",
"testMatch": [
"**/tests/**/*Test.ts"
],
"transform": {
"^.+\\.(t|j)s$": "ts-jest"
},
Expand Down Expand Up @@ -160,6 +164,8 @@
"@athenna/logger": "1.1.8",
"@secjs/utils": "1.8.3",
"chalk-rainbow": "1.0.0",
"cli-table": "0.3.11",
"columnify": "1.6.0",
"commander": "9.2.0",
"ejs": "3.1.6",
"figlet": "1.5.2",
Expand Down
60 changes: 57 additions & 3 deletions src/Artisan.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
*/

import figlet from 'figlet'
import columnify from 'columnify'
import chalkRainbow from 'chalk-rainbow'

import { Path } from '@secjs/utils'
Expand Down Expand Up @@ -46,6 +47,48 @@ export class Artisan {
])
}

/**
* List all commands with description.
*
* @param asColumn
* @param alias
*/
listCommands(asColumn: boolean, alias?: string): string
listCommands(
asColumn = false,
alias?: string,
): Record<string, string> | string {
const commands = {}

this.commander.commands.forEach((command: any) => {
if (alias && !command._name.startsWith(`${alias}:`)) {
return
}

let name = command._name

if (command.options.length) name = `${name} [options]`

command._args.forEach(arg => {
if (arg.required) {
name = name.concat(` <${arg._name}>`)

return
}

name = name.concat(` [${arg._name}]`)
})

commands[name] = command._description
})

if (asColumn) {
return columnify(commands).replace('KEY', '').replace('VALUE', '')
}

return commands
}

/**
* Set the version of the CLI.
*
Expand Down Expand Up @@ -117,9 +160,20 @@ export class Artisan {
*
* @return Promise<void>
*/
async main(): Promise<void> {
if (process.argv.length === 2) {
const appNameFiglet = figlet.textSync(Config.get('app.name'))
async main(appName?: string): Promise<void> {
/**
* If argv is less or equal two, means that
* the command that are being run is just
* the CLI entrypoint. Example:
*
* - artisan
* - node artisan
*
* In CLI entrypoint we are going to log the
* chalkRainbow with his application name.
*/
if (process.argv.length <= 2) {
const appNameFiglet = figlet.textSync(appName || Config.get('app.name'))
const appNameFigletColorized = chalkRainbow(appNameFiglet)

process.stdout.write(appNameFigletColorized + '\n' + '\n')
Expand Down
61 changes: 61 additions & 0 deletions src/Commands/Build.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/**
* @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 { Path } from '@secjs/utils'
import { Command } from 'src/Commands/Command'
import { Command as Commander } from 'commander'

export class Build extends Command {
/**
* The name and signature of the console command.
*/
protected signature = 'build'

/**
* The console command description.
*/
protected description = 'Compile project from Typescript to Javascript.'

/**
* Set additional flags in the commander instance.
* This method is executed when registering your command.
*
* @return {void}
*/
public addFlags(commander: Commander): Commander {
return commander
}

/**
* Execute the console command.
*
* @return {Promise<void>}
*/
async handle(_: any, commander: any): Promise<void> {
this.simpleLog('[ BUILDING PROJECT ]', 'rmNewLineStart', 'bold', 'green')

try {
const rimrafPath = Path.noBuild().pwd('node_modules/.bin/rimraf')
let tscPath = Path.noBuild().pwd('node_modules/.bin/tsc')

const tscArgs = commander.args.join(' ')

if (tscArgs) {
tscPath = tscPath.concat(` ${tscArgs}`)
}

await this.execCommand(`${rimrafPath} dist`)
await this.execCommand(tscPath)

this.success('Built successfully.')
} catch (error) {
this.error('Failed to build project.')
}
}
}
Loading

0 comments on commit aa94b65

Please sign in to comment.