|
| 1 | +import { Workspace } from '@openfn/project'; |
| 2 | +import path from 'path'; |
| 3 | +import type { Logger } from '../util/logger'; |
| 4 | +import type { VersionOptions } from './command'; |
| 5 | + |
| 6 | +const workflowVersionHandler = async ( |
| 7 | + options: VersionOptions, |
| 8 | + logger: Logger |
| 9 | +) => { |
| 10 | + const commandPath = path.resolve(options.projectPath ?? '.'); |
| 11 | + const workspace = new Workspace(commandPath); |
| 12 | + if (!workspace.valid) { |
| 13 | + logger.error('Command was run in an invalid openfn workspace'); |
| 14 | + return; |
| 15 | + } |
| 16 | + |
| 17 | + const output = new Map<string, string>(); |
| 18 | + |
| 19 | + const activeProject = workspace.getActiveProject(); |
| 20 | + if (options.workflow) { |
| 21 | + const workflow = activeProject?.getWorkflow(options.workflow); |
| 22 | + if (!workflow) { |
| 23 | + logger.error(`No workflow found with id/name ${options.workflow}`); |
| 24 | + return; |
| 25 | + } |
| 26 | + output.set(workflow.name || workflow.id, workflow.getVersionHash()); |
| 27 | + } else { |
| 28 | + for (const wf of activeProject?.workflows || []) { |
| 29 | + output.set(wf.name || wf.id, wf.getVersionHash()); |
| 30 | + } |
| 31 | + } |
| 32 | + if (!output.size) { |
| 33 | + logger.error('No workflow available'); |
| 34 | + return; |
| 35 | + } |
| 36 | + |
| 37 | + let final: string; |
| 38 | + if (options.json) { |
| 39 | + final = JSON.stringify(Object.fromEntries(output), undefined, 2); |
| 40 | + } else { |
| 41 | + final = Array.from(output.entries()) |
| 42 | + .map(([key, value]) => key + '\n' + value) |
| 43 | + .join('\n\n'); |
| 44 | + } |
| 45 | + logger.success(`Workflow(s) and their hashes\n\n${final}`); |
| 46 | +}; |
| 47 | + |
| 48 | +export default workflowVersionHandler; |
0 commit comments