Skip to content

Commit

Permalink
feat: add help and version flags
Browse files Browse the repository at this point in the history
  • Loading branch information
thetutlage committed Nov 23, 2019
1 parent c655ec6 commit 46fb6f9
Show file tree
Hide file tree
Showing 4 changed files with 95 additions and 4 deletions.
43 changes: 43 additions & 0 deletions src/Ignitor/Ace/AppCommands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ export class AppCommands {
constructor (
private _buildRoot: string,
private _ace: typeof ace,
private _additionalManifestCommands: any,
) {
}

Expand Down Expand Up @@ -77,6 +78,46 @@ export class AppCommands {
})
}

/**
* Adding flags
*/
private _addKernelFlags (kernel: ace.Kernel) {
/**
* Showing help including core commands
*/
kernel.flag('help', async (value, _parsed, command) => {
if (!value) {
return
}

/**
* Updating manifest commands object during help
*/
Object.keys(this._additionalManifestCommands).forEach((commandName) => {
kernel.manifestCommands![commandName] = this._additionalManifestCommands[commandName]
})

kernel.printHelp(command)
process.exit(0)
}, { alias: 'h' })

/**
* Showing app and AdonisJs version
*/
kernel.flag('version', async (value) => {
if (!value) {
return
}

const appVersion = this._bootstrapper.application.version
const adonisVersion = this._bootstrapper.application.adonisVersion

console.log(`App version`, appVersion ? appVersion.version : 'NA')
console.log(`Framework version`, adonisVersion ? adonisVersion.version : 'NA')
process.exit(0)
}, { alias: 'v' })
}

/**
* Boot the application.
*/
Expand Down Expand Up @@ -106,8 +147,10 @@ export class AppCommands {
const manifest = new this._ace.Manifest(this._buildRoot)
const kernel = new this._ace.Kernel(this._bootstrapper.application)
this._addKernelHooks(kernel)
this._addKernelFlags(kernel)

kernel.useManifest(manifest)
await kernel.preloadManifest()
await kernel.handle(argv)

this._signalsListener.listen(async () => {
Expand Down
30 changes: 27 additions & 3 deletions src/Ignitor/Ace/CoreCommands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@
* file that was distributed with this source code.
*/

import { join } from 'path'
import ace from '@adonisjs/ace'
import { join, dirname } from 'path'
import { Ioc } from '@adonisjs/fold'
import { esmRequire } from '@poppinss/utils'
import { esmRequire, resolveFrom } from '@poppinss/utils'
import { Application } from '@adonisjs/application/build/standalone'

import { isMissingModuleError } from '../../utils'
Expand All @@ -28,6 +28,17 @@ export class CoreCommands {
'serve',
]

/**
* Returns assembler manifest file for showing help
*/
public static getManifestJSON () {
try {
return require('@adonisjs/assembler/build/ace-manifest.json')
} catch (error) {
return {}
}
}

private _application: Application

constructor (private _appRoot: string, private _ace: typeof ace) {
Expand Down Expand Up @@ -76,8 +87,21 @@ export class CoreCommands {
this._setupApplication()
await this._importAssembler(argv[0])

const manifest = new this._ace.Manifest(this._appRoot)
const manifest = new this._ace.Manifest(dirname(resolveFrom(this._appRoot, '@adonisjs/assembler')))
const kernel = new this._ace.Kernel(this._application)

/**
* Showing commands help
*/
kernel.flag('help', async (value, _parsed, command) => {
if (!value) {
return
}

kernel.printHelp(command)
process.exit(0)
}, { alias: 'h' })

kernel.useManifest(manifest)
await kernel.handle(argv)
}
Expand Down
15 changes: 15 additions & 0 deletions src/Ignitor/Ace/GenerateManifest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,21 @@ export class GenerateManifest {
) {
}

/**
* Returns manifest object for showing help
*/
public static getManifestJSON () {
return {
'generate:manifest': {
commandName: 'generate:manifest',
description: 'Generate manifest file to execute ace commands',
args: [],
flags: [],
settings: {},
},
}
}

/**
* Raises human friendly error when the `build` directory is
* missing during `generate:manifest` command.
Expand Down
11 changes: 10 additions & 1 deletion src/Ignitor/Ace/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -127,10 +127,19 @@ export class Ace {
return
}

/**
* Passing manifest json of core commands and generate manifest, so that
* we can append in the help output
*/
const additionalManifestJSON = Object.assign(
CoreCommands.getManifestJSON(),
GenerateManifest.getManifestJSON(),
)

/**
* Proxy over to application commands
*/
await new AppCommands(buildDir, ace!).handle(argv)
await new AppCommands(buildDir, ace!, additionalManifestJSON).handle(argv)
} catch (error) {
ace.handleError(error, (_error, logger) => {
if (error instanceof AceRuntimeException) {
Expand Down

0 comments on commit 46fb6f9

Please sign in to comment.