|
| 1 | +import { Command } from '@cliffy/command' |
| 2 | +import { createEngine } from '../lib/engine.ts' |
| 3 | +import type { GlobalOptions } from '../lib/types.ts' |
| 4 | +import { Config } from '../lib/config.ts' |
| 5 | + |
| 6 | +export type ListTargetsOptions = typeof listTargets extends |
| 7 | + Command<void, void, infer Options, infer Argument, GlobalOptions> ? Options |
| 8 | + : never |
| 9 | + |
| 10 | +const logTargets = (targets: string[]) => { |
| 11 | + return targets.map((target) => { |
| 12 | + console.log(target) |
| 13 | + }) |
| 14 | +} |
| 15 | + |
| 16 | +export const listTargets = new Command<GlobalOptions>() |
| 17 | + .description('list-targets') |
| 18 | + .option('-e, --engine-only', 'list only engine targets') |
| 19 | + .option('-p, --project-only', 'list only project targets', { conflicts: ['engine-only'] }) |
| 20 | + .action(async (options) => { |
| 21 | + const { engineOnly, projectOnly } = options as ListTargetsOptions |
| 22 | + const config = Config.getInstance() |
| 23 | + const { engine: { path: enginePath }, project: { path: projectPath } } = config.mergeConfigCLIConfig({ |
| 24 | + cliOptions: options, |
| 25 | + }) |
| 26 | + |
| 27 | + const engine = createEngine(enginePath) |
| 28 | + const engineTargets = await engine.parseEngineTargets() |
| 29 | + let projectTargets: string[] = [] |
| 30 | + |
| 31 | + if (projectPath) { |
| 32 | + projectTargets = (await engine.parseProjectTargets(projectPath)).filter((target) => |
| 33 | + !engineTargets.includes(target) |
| 34 | + ) |
| 35 | + } |
| 36 | + |
| 37 | + if (engineOnly) { |
| 38 | + return logTargets(engineTargets) |
| 39 | + } |
| 40 | + |
| 41 | + if (projectOnly) { |
| 42 | + return logTargets(projectTargets) |
| 43 | + } |
| 44 | + |
| 45 | + const targets = Array.from(new Set([...engineTargets, ...projectTargets])) |
| 46 | + return logTargets(targets) |
| 47 | + }) |
0 commit comments