(app: ApplicationService, commandName?: string)
| 32 | * ``` |
| 33 | */ |
| 34 | export function createAceKernel(app: ApplicationService, commandName?: string) { |
| 35 | const kernel = new Kernel(app) |
| 36 | kernel.info.set('binary', 'node ace') |
| 37 | |
| 38 | /** |
| 39 | * Lazy import commands mentioned in the "commands" array |
| 40 | * of rcFile |
| 41 | */ |
| 42 | app.rcFile.commands.forEach((commandModule) => { |
| 43 | kernel.addLoader(() => |
| 44 | typeof commandModule === 'function' ? commandModule() : app.import(commandModule) |
| 45 | ) |
| 46 | }) |
| 47 | |
| 48 | /** |
| 49 | * When we know the command we are running ahead of time, then we |
| 50 | * defer loading the application commands if the command has |
| 51 | * already been registered by other loaders. |
| 52 | */ |
| 53 | const fsLoader = new FsLoader<typeof BaseCommand>(app.commandsPath()) |
| 54 | kernel.addLoader({ |
| 55 | async getMetaData() { |
| 56 | if (!commandName || !kernel.getCommand(commandName)) { |
| 57 | return fsLoader.getMetaData() |
| 58 | } |
| 59 | return [] |
| 60 | }, |
| 61 | getCommand(command) { |
| 62 | return fsLoader.getCommand(command) |
| 63 | }, |
| 64 | }) |
| 65 | |
| 66 | /** |
| 67 | * Custom global flags |
| 68 | */ |
| 69 | kernel.defineFlag('ansi', { |
| 70 | type: 'boolean', |
| 71 | showNegatedVariantInHelp: true, |
| 72 | description: 'Force enable or disable colorful output', |
| 73 | }) |
| 74 | |
| 75 | kernel.defineFlag('help', { |
| 76 | type: 'boolean', |
| 77 | description: HelpCommand.description, |
| 78 | }) |
| 79 | |
| 80 | /** |
| 81 | * Flag listener to turn colors on/off |
| 82 | */ |
| 83 | kernel.on('ansi', (_, $kernel, parsed) => { |
| 84 | if (parsed.flags.ansi === false) { |
| 85 | $kernel.ui.switchMode('silent') |
| 86 | } |
| 87 | |
| 88 | if (parsed.flags.ansi === true) { |
| 89 | $kernel.ui.switchMode('normal') |
| 90 | } |
| 91 | }) |
no test coverage detected