import { CliCommandDefinition, CliCommandHandler, CliProgramDefinition, ParsedCliCommand } from '@cirrusct/cli'; import { MrCliCommandGlobalOptions, mrCliGlobalOptions, MrCommand, MrService } from '@cirrusct/mr-core'; import { mrCliDebugCliOptions } from './debug/MrCliDebugCliOptions'; const createCliCommandHandler = (mrCommand: MrCommand, mrService: MrService): CliCommandHandler => { return async ( args: {}, options: TOptions, parsedCmd?: ParsedCliCommand ) => { const session = await mrService.createSession({ includeDependencyTypes: options.deps, scope: options.scope, }); await mrCommand.run(session, args, options, parsedCmd); }; }; const createCliCommandDefinitions = (mrService: MrService): CliCommandDefinition[] => { return Object.keys(mrService.commandService.commands).map(k => { const mrCommand = mrService.commandService.commands[k]; const cliDef = mrService.commandService.commands[k].definition.cli; return { ...cliDef, handler: createCliCommandHandler(mrCommand, mrService), }; }); }; /** * Create Cli Program Definition from MrCommand definitions * @param cliEnv CliEnvironment Instance */ export const createCliProgramDefinition = (mrService: MrService): CliProgramDefinition => { const program: CliProgramDefinition = { commands: createCliCommandDefinitions(mrService), description: 'Mr Cli', name: 'mr', options: mrCliGlobalOptions.concat(mrCliDebugCliOptions), }; return program; };