forked from bpmnServer/bpmn-client
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcli.js
245 lines (245 loc) · 9.44 KB
/
cli.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
//import { BPMNClient } from "bpmn-client";
const _1 = require("./");
const readline = require("readline");
const dotenv = require('dotenv');
const res = dotenv.config();
console.log(res);
const server = new _1.BPMNClient(process.env.HOST, process.env.PORT, process.env.API_KEY);
const cl = readline.createInterface(process.stdin, process.stdout);
const question = function (q) {
return new Promise((res, rej) => {
cl.question(q, answer => {
res(answer);
});
});
};
completeUserTask();
function menu() {
console.log('Commands:');
console.log(' q to quit');
console.log(' s start process ');
console.log(' lo list outstanding items');
console.log(' li list items');
console.log(' l list instances for a process');
console.log(' di display Instance information');
console.log(' i Invoke Task');
console.log(' sgl Signal Task');
console.log(' msg Message Task');
console.log(' d delete instnaces');
console.log(' ? repeat this list');
}
function completeUserTask() {
return __awaiter(this, void 0, void 0, function* () {
menu();
let option = '';
var command;
while (option !== 'q') {
command = yield question('Enter Command, q to quit\n\r>');
let opts = command.split(' ');
option = opts[0];
switch (option) {
case '?':
menu();
break;
case 'lo':
console.log("Listing Outstanding Items");
yield findItems({ "items.status": "wait" });
break;
case 'l':
console.log("Listing Instances for a Process");
yield listInstances();
break;
case 'li':
console.log("list items");
yield listItems();
break;
case 'di':
console.log("Displaying Instance Details");
yield displayInstance();
break;
case 'i':
console.log("invoking");
yield invoke();
break;
case 's':
console.log("Starting Process");
yield start();
break;
case 'sgl':
console.log("Signalling Process");
yield signal();
break;
case 'msg':
console.log("Message Process");
yield message();
break;
case 'd':
console.log("deleting");
yield delInstances();
break;
}
}
console.log("bye");
cl.close();
});
}
function start() {
return __awaiter(this, void 0, void 0, function* () {
const name = yield question('Please provide your process name: ');
let taskData = yield question('Please provide your Task Data (json obj) if any: ');
console.log(taskData);
try {
if (taskData === "") {
taskData = {};
}
else {
taskData = JSON.parse(taskData.toString());
}
}
catch (exc) {
console.log(exc);
return;
}
let response = yield server.engine.start(name, taskData);
console.log("Process " + name + " started:", 'InstanceId', response.id);
return yield displayInstance(response.id);
});
}
function findItems(query) {
return __awaiter(this, void 0, void 0, function* () {
var items = yield server.datastore.findItems(query);
console.log(items);
for (var i = 0; i < items.length; i++) {
let item = items[i];
console.log(`${item.name} - ${item.elementId} instanceId: ${item['instanceId']}`);
}
});
}
function listItems() {
return __awaiter(this, void 0, void 0, function* () {
const answer = yield question('Please items criteria name value pair; example: items.status wait ');
let str = '' + answer;
const list = str.split(' ');
let criteria = {};
console.log(list);
for (var i = 0; i < list.length; i += 2) {
console.log(list[i], list[i + 1]);
criteria[list[i]] = list[i + 1];
}
console.log(criteria);
var items = yield server.datastore.findItems(criteria);
console.log(items.length);
for (var j = 0; j < items.length; j++) {
let item = items[j];
console.log(`element: ${item.elementId} status: ${item.status} processName: ${item['processName']} InstanceId: ${item['instanceId']} id: ${item.id}`);
}
});
}
function listInstances() {
return __awaiter(this, void 0, void 0, function* () {
const name = yield question('Please provide your process name: ');
let insts = yield server.datastore.findInstances({ name: name });
for (var i = 0; i < insts.length; i++) {
let inst = insts[i];
console.log(`name: ${inst.name} status: ${inst.status} instanceId: ${inst.id}
startedAt: ${inst.startedAt} endedAt ${inst.endedAt}`, 'data:', inst.data);
}
});
}
function displayInstance(instanceId = null) {
return __awaiter(this, void 0, void 0, function* () {
if (instanceId == null)
instanceId = yield question('Please provide your Instance ID: ');
let insts = yield server.datastore.findInstances({ id: instanceId });
for (var i = 0; i < insts.length; i++) {
let inst = insts[i];
var items = inst.items;
console.log(`name: ${inst.name} status: ${inst.status} instanceId: ${inst.id}
startedAt: ${inst.startedAt} endedAt ${inst.endedAt}`, 'data:', inst.data);
for (var j = 0; j < items.length; j++) {
let item = items[j];
console.log(`element: ${item.elementId} status: ${item.status} id: ${item.id}`);
}
}
});
}
function invoke() {
return __awaiter(this, void 0, void 0, function* () {
const instanceId = yield question('Please provide your Instance ID: ');
const taskId = yield question('Please provide your Task ID: ');
let taskData = yield question('Please provide your Task Data (json obj) if any: ');
if (taskData === "") {
taskData = {};
}
else {
taskData = JSON.parse(taskData.toString());
}
try {
let response = yield server.engine.invoke({ id: instanceId, "items.elementId": taskId }, taskData);
console.log("Completed UserTask:", taskId);
return yield displayInstance(response.id);
}
catch (exc) {
console.log("Invoking task failed for:", taskId, instanceId);
yield findItems({ id: instanceId, "items.elementId": taskId });
}
});
}
function signal() {
return __awaiter(this, void 0, void 0, function* () {
const signalId = yield question('Please provide signal ID: ');
let signalData = yield question('Please provide your Data (json obj) if any: ');
//if (typeof signalData === 'string' && signalData.trim() === '') {
if (signalData === "") {
signalData = {};
}
else {
try {
signalData = JSON.parse(signalData.toString());
}
catch (exc) {
console.log(exc);
return;
}
}
let response = yield server.engine.throwSignal(signalId, signalData);
console.log("Signal Response:", response);
});
}
function message() {
return __awaiter(this, void 0, void 0, function* () {
const messageId = yield question('Please provide message ID: ');
let messageData = yield question('Please provide your Data (json obj) if any: ');
if (typeof messageData === 'string' && messageData.trim() === '') {
messageData = {};
}
else {
messageData = JSON.parse(messageData.toString());
}
let response = yield server.engine.throwMessage(messageId, messageData);
if (response['id'])
return yield displayInstance(response['id']);
else {
console.log(' no results.');
return null;
}
});
}
function delInstances() {
return __awaiter(this, void 0, void 0, function* () {
const name = yield question('Please provide process name to delete instances for process: ');
let response = yield server.datastore.deleteInstances({ name: name });
console.log("Instances Deleted:", response['result']['deletedCount']);
});
}