Skip to content

Commit 4c9124c

Browse files
committed
Refactor deploy
1 parent ecb9c02 commit 4c9124c

File tree

5 files changed

+62
-46
lines changed

5 files changed

+62
-46
lines changed

package-lock.json

+6-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@
6565
"dayjs": "^1.8.15",
6666
"debug": "^4.1.1",
6767
"fast-xml-parser": "^3.12.12",
68+
"fp-ts": "^2.5.1",
6869
"got": "^9.6.0",
6970
"grpc": "^1.23.4",
7071
"promise-retry": "^1.1.1",

src/lib/interfaces.ts

+7
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,13 @@ export interface KeyedObject {
1313
}
1414
export type Loglevel = 'INFO' | 'DEBUG' | 'NONE' | 'ERROR'
1515

16+
export type DeployWorkflowFiles = string | string[]
17+
18+
export interface DeployWorkflowBuffer {
19+
definition: Buffer
20+
name: string
21+
}
22+
1623
export interface CompleteFn<WorkerOutputVariables> {
1724
/**
1825
* Complete the job with a success, optionally passing in a state update to merge

src/zb/ZBClient.ts

+46-44
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import chalk from 'chalk'
22
import { EventEmitter } from 'events'
3+
import { either as E, pipeable } from 'fp-ts'
4+
import { array } from 'fp-ts/lib/Array'
35
import * as fs from 'fs'
46
import * as path from 'path'
57
import promiseRetry from 'promise-retry'
@@ -406,51 +408,53 @@ export class ZBClient extends EventEmitter {
406408
* If set false, will not redeploy a workflow that exists.
407409
*/
408410
public async deployWorkflow(
409-
workflow: string | string[] | { definition: Buffer; name: string }
411+
workflow: ZB.DeployWorkflowFiles | ZB.DeployWorkflowBuffer
410412
): 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+
)
441443

442-
if (workFlowRequests.length > 0) {
443-
return this.executeOperation('deployWorkflow', () =>
444+
const deploy = (workflows: ZB.WorkflowRequestObject[]) =>
445+
this.executeOperation('deployWorkflow', () =>
444446
this.gRPCClient.deployWorkflowSync({
445-
workflows: workFlowRequests,
447+
workflows,
446448
})
447449
)
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+
)
454458
}
455459

456460
public failJob(failJobRequest: ZB.FailJobRequest): Promise<void> {
@@ -464,10 +468,8 @@ export class ZBClient extends EventEmitter {
464468
* @param file - Path to bpmn file.
465469
*/
466470
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))
471473
}
472474

473475
/**

tslint.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@
88
"semicolon": false,
99
"quotemark": false,
1010
"trailing-comma": false,
11-
"arrow-parens": false
11+
"arrow-parens": false,
12+
"no-submodule-imports": false
1213
},
1314
"rulesDirectory": [],
1415
"linterOptions": {

0 commit comments

Comments
 (0)