1
1
import chalk from 'chalk'
2
2
import { EventEmitter } from 'events'
3
+ import { either as E , pipeable } from 'fp-ts'
4
+ import { array } from 'fp-ts/lib/Array'
3
5
import * as fs from 'fs'
4
6
import * as path from 'path'
5
7
import promiseRetry from 'promise-retry'
@@ -406,51 +408,53 @@ export class ZBClient extends EventEmitter {
406
408
* If set false, will not redeploy a workflow that exists.
407
409
*/
408
410
public async deployWorkflow (
409
- workflow : string | string [ ] | { definition : Buffer ; name : string }
411
+ workflow : ZB . DeployWorkflowFiles | ZB . DeployWorkflowBuffer
410
412
) : Promise < ZB . DeployWorkflowResponse > {
411
- const workflows = Array . isArray ( workflow ) ? workflow : [ workflow ]
412
-
413
- const readFile = ( filename : string ) => {
414
- if ( fs . existsSync ( filename ) ) {
415
- return fs . readFileSync ( filename )
416
- }
417
- const name = ` ${ filename } .bpmn`
418
- if ( fs . existsSync ( name ) ) {
419
- return fs . readFileSync ( name )
420
- }
421
- throw new Error ( ` ${ filename } not found.` )
422
- }
423
-
424
- const workFlowRequests : ZB . WorkflowRequestObject [ ] = workflows . map (
425
- wf => {
426
- if ( typeof wf === 'object' ) {
427
- return {
428
- definition : wf . definition ,
429
- name : wf . name ,
430
- type : 1 ,
431
- }
432
- } else {
433
- return {
434
- definition : readFile ( wf ) ,
435
- name : path . basename ( wf ) ,
436
- type : 1 ,
437
- }
438
- }
439
- }
440
- )
413
+ const isBuffer = (
414
+ wf : ZB . DeployWorkflowBuffer | ZB . DeployWorkflowFiles
415
+ ) : wf is ZB . DeployWorkflowBuffer =>
416
+ ! ! ( wf as ZB . DeployWorkflowBuffer ) . definition
417
+
418
+ const coerceFilenamesToArray = ( wf : string | string [ ] ) : string [ ] =>
419
+ Array . isArray ( wf ) ? wf : [ wf ]
420
+
421
+ const bufferOrFiles = (
422
+ wf : ZB . DeployWorkflowFiles | ZB . DeployWorkflowBuffer
423
+ ) : E . Either < ZB . DeployWorkflowBuffer [ ] , string [ ] > =>
424
+ isBuffer ( wf ) ? E . left ( [ wf ] ) : E . right ( coerceFilenamesToArray ( wf ) )
425
+
426
+ const loadBpmnFiles = (
427
+ files : string [ ]
428
+ ) : E . Either < Error , ZB . WorkflowRequestObject [ ] > =>
429
+ array . sequence ( E . either ) (
430
+ files . map ( wf => {
431
+ const definition = fs . existsSync ( wf )
432
+ ? fs . readFileSync ( wf )
433
+ : undefined
434
+ return definition
435
+ ? E . right ( {
436
+ definition,
437
+ name : path . basename ( wf ) ,
438
+ type : 1 ,
439
+ } )
440
+ : E . left ( new Error ( `File ${ wf } not found!` ) )
441
+ } )
442
+ )
441
443
442
- if ( workFlowRequests . length > 0 ) {
443
- return this . executeOperation ( 'deployWorkflow' , ( ) =>
444
+ const deploy = ( workflows : ZB . WorkflowRequestObject [ ] ) =>
445
+ this . executeOperation ( 'deployWorkflow' , ( ) =>
444
446
this . gRPCClient . deployWorkflowSync ( {
445
- workflows : workFlowRequests ,
447
+ workflows,
446
448
} )
447
449
)
448
- } else {
449
- return {
450
- key : '-1' ,
451
- workflows : [ ] ,
452
- }
453
- }
450
+ const error = ( e : Error ) => Promise . reject ( e . message )
451
+
452
+ return pipeable . pipe (
453
+ bufferOrFiles ( workflow ) ,
454
+ E . fold ( deploy , files =>
455
+ pipeable . pipe ( loadBpmnFiles ( files ) , E . fold ( error , deploy ) )
456
+ )
457
+ )
454
458
}
455
459
456
460
public failJob ( failJobRequest : ZB . FailJobRequest ) : Promise < void > {
@@ -464,10 +468,8 @@ export class ZBClient extends EventEmitter {
464
468
* @param file - Path to bpmn file.
465
469
*/
466
470
public getServiceTypesFromBpmn ( files : string | string [ ] ) {
467
- if ( typeof files === 'string' ) {
468
- files = [ files ]
469
- }
470
- return BpmnParser . getTaskTypes ( BpmnParser . parseBpmn ( files ) )
471
+ const fileArray = typeof files === 'string' ? [ files ] : files
472
+ return BpmnParser . getTaskTypes ( BpmnParser . parseBpmn ( fileArray ) )
471
473
}
472
474
473
475
/**
0 commit comments