diff --git a/src/commands/workflow/compile.ts b/src/commands/workflow/compile.ts index a237ed6..eb834d0 100644 --- a/src/commands/workflow/compile.ts +++ b/src/commands/workflow/compile.ts @@ -2,6 +2,7 @@ import {readFileSync} from 'fs' import Command from '../../root-command' import * as compile from '../../utils/compiler' +import ServiceStart from '../service/start'; export default class WorkflowCompile extends Command { static description = 'Compile a workflow' @@ -17,9 +18,36 @@ export default class WorkflowCompile extends Command { async run(): Promise { const {args} = this.parse(WorkflowCompile) - const definition = await compile.workflow(readFileSync(args.WORKFLOW_FILE)) + const definition = await compile.workflow(readFileSync(args.WORKFLOW_FILE), async (instanceObject: any) => { + if (instanceObject.instanceHash) { + return instanceObject.instanceHash + } + if (instanceObject.service) { + return this.serviceToInstance(instanceObject.service) + } + throw new Error('at least one of the following parameter should be set: "instanceHash" or "service"') + }) this.styledJSON(definition) this.spinner.stop() return definition } + + async serviceToInstance(key: string): Promise { + const {services} = await this.api.service.list({}) + if (!services) throw new Error('no services deployed, please deploy your service first') + const match = services.filter(x => x.hash === key || x.sid === key) + if (!match || match.length === 0) throw new Error(`cannot find any service with the following: ${key}`) + if (match.length > 1) throw new Error(`multiple services match the following sid: ${key}, provide a service's hash instead`) + const service = match[0] + if (!service.hash) throw new Error('invalid service') + const {instances} = await this.api.instance.list({serviceHash: service.hash}) + if (!instances || instances.length === 0) { + const instance = await ServiceStart.run([service.hash, '--silent']) + return instance.hash + } + if (instances.length > 1) throw new Error('multiple instances match the service, use parameter "instanceHash" instead of "service"') + const instance = instances[0] + if (!instance.hash) throw new Error('invalid instance') + return instance.hash + } } diff --git a/src/utils/compiler.ts b/src/utils/compiler.ts index 71ee0e0..cca6d54 100644 --- a/src/utils/compiler.ts +++ b/src/utils/compiler.ts @@ -15,10 +15,6 @@ const parseParams = (params: any): any => mapToArray(params).map(x => ({ object: parseParams(x.object), })) -const prepareInstanceHash = async (object: any): Promise => { - return object.instanceHash -} - export const service = async (content: Buffer): Promise => { const definition = decode(content) return { @@ -36,17 +32,17 @@ export const service = async (content: Buffer): Promise => { } } -export const workflow = async (content: Buffer): Promise => { +export const workflow = async (content: Buffer, instanceResolver: (object: any) => Promise): Promise => { const definition = decode(content) const createNode = async (def: any, index: number): Promise => ({ key: def.key || `node-${index}`, taskKey: def.taskKey, - instanceHash: await prepareInstanceHash(def) + instanceHash: await instanceResolver(def) }) const orderedNodes = await Promise.all(definition.tasks.map(createNode)) as WorkflowType.types.Workflow.INode[] const trigger = { - instanceHash: await prepareInstanceHash(definition.trigger), + instanceHash: await instanceResolver(definition.trigger), taskKey: definition.trigger.taskKey, eventKey: definition.trigger.eventKey, nodeKey: orderedNodes[0].key