mstate-cli 0.2.9 → 0.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,48 +1,80 @@
1
- import { CmdAction } from '../common/enum';
2
- import { customLog } from '../common/utils';
3
-
4
- class HelpHandler {
5
- logCommandInfo() {
6
- console.log('\n\nUsage: mstate [cmd] [parameter]="[value]"\n');
7
-
8
- console.log('Commands:');
9
- customLog.info(CmdAction.ADD, 'Add new workflow');
10
- customLog.info(
11
- CmdAction.CLONE,
12
- 'Clone workflow with actions and environments',
13
- );
14
- customLog.info(
15
- CmdAction.COPY,
16
- 'crete a new workflow by copying the current',
17
- );
18
- customLog.info(CmdAction.PUSH, 'Update the changes one in cloned workflow');
19
- customLog.info(
20
- CmdAction.LINK,
21
- 'Add workflow and allow user to view workflow in the mobioffice application',
22
- );
23
- customLog.info(
24
- CmdAction.UNLINK,
25
- 'Disallow user to view workflow in the mobioffice application',
26
- );
27
- customLog.info(
28
- [CmdAction.VERSION_FLAG, CmdAction.VERSION].toString(),
29
- 'print mstate-cli version',
30
- );
31
- }
32
-
33
- logActionInfo(action: CmdAction) {
34
- switch (action) {
35
- case CmdAction.ADD:
36
- break;
37
- default:
38
- customLog.error(
39
- `${action ? 'Invalid' : 'Missing'} script: ${action ?? ''}`,
40
- );
41
- console.log(
42
- `use 'mstate ${CmdAction.HELP_FLAG}, ${CmdAction.HELP}' for getting allowed action`,
43
- );
44
- }
45
- }
46
- }
47
-
48
- export const helpHandler = new HelpHandler();
1
+ import { CmdAction, Variables } from '../common/enum';
2
+ import { customLog } from '../common/utils';
3
+
4
+ class HelpHandler {
5
+ logCommandInfo() {
6
+ console.log('\n\nUsage: mstate <command> [arguments] \n');
7
+
8
+ console.log('Commands:');
9
+ customLog.info(CmdAction.ADD, 'Create a new workflow');
10
+ customLog.info(
11
+ CmdAction.CLONE,
12
+ 'Clone workflow with actions and environments',
13
+ );
14
+ customLog.info(CmdAction.PUSH, 'Update the changes one in cloned workflow');
15
+ customLog.info(CmdAction.ADD_USER, 'Add new user to your company');
16
+ customLog.info(
17
+ CmdAction.LINK,
18
+ 'Add workflow and allow user to view workflow in the mobioffice application',
19
+ );
20
+ customLog.info(
21
+ CmdAction.UNLINK,
22
+ 'Disallow user to view workflow in the mobioffice application',
23
+ );
24
+ customLog.info(
25
+ [CmdAction.VERSION_FLAG, CmdAction.VERSION].toString(),
26
+ 'print mstate-cli version',
27
+ );
28
+ }
29
+
30
+ logActionInfo(action: CmdAction) {
31
+ console.log('Used Arguments: ');
32
+
33
+ switch (action) {
34
+ case CmdAction.ADD:
35
+ customLog.info(
36
+ Variables.WORKFLOW,
37
+ 'Specifying workflow name with path',
38
+ );
39
+ break;
40
+ case CmdAction.CLONE:
41
+ customLog.info(
42
+ Variables.WORKFLOW,
43
+ 'Specifying workflow name with path',
44
+ );
45
+ break;
46
+ case CmdAction.LINK:
47
+ case CmdAction.UNLINK:
48
+ customLog.info(
49
+ Variables.WORKFLOW,
50
+ 'Specifying workflow name with path',
51
+ );
52
+ customLog.info(Variables.COMPANY_ID, 'Specifying company nickname');
53
+ customLog.info(Variables.USER_ID, 'Specifying user phone number');
54
+ break;
55
+ case CmdAction.PUSH:
56
+ customLog.info(
57
+ Variables.WORKFLOW,
58
+ 'Specifying workflow name with path',
59
+ );
60
+ break;
61
+ case CmdAction.ADD_USER:
62
+ customLog.info(
63
+ Variables.USER_ID,
64
+ 'Specifying user unique name for the company',
65
+ );
66
+ customLog.info(Variables.EMAIL, 'Specifying user email');
67
+ customLog.info(Variables.PHONE, 'Specifying user phone number');
68
+ break;
69
+ default:
70
+ customLog.error(
71
+ `${action ? 'Invalid' : 'Missing'} command: ${action ?? ''}`,
72
+ );
73
+ console.log(
74
+ `\t\t use 'mstate ${CmdAction.HELP_FLAG}, ${CmdAction.HELP}' for getting allowed action`,
75
+ );
76
+ }
77
+ }
78
+ }
79
+
80
+ export const helpHandler = new HelpHandler();
@@ -1,78 +1,43 @@
1
- import { MOBIOFFICE_URL } from '../common/constant';
2
- import { CmdAction } from '../common/enum';
3
- import { getValueFromArgs } from '../common/helpers';
4
- import { customLog, getSecretKey } from '../common/utils';
5
-
6
- enum Key {
7
- WORKFLOW = 'workflow=',
8
- PERMISSIONS = 'permissions=',
9
- SECRET_KEY = 'secret=',
10
- COMPANY_NAME = 'company=',
11
- USER_ID = 'user=',
12
- TYPE = 'type=',
13
- }
14
-
15
- class MobiofficeHandler {
16
- private MOBIOFFICE_URL: string = MOBIOFFICE_URL;
17
-
18
- constructor() {}
19
-
20
- async linkToMobioffice(action: CmdAction.LINK | CmdAction.UNLINK) {
21
- const args = process.argv;
22
-
23
- const workflowName = getValueFromArgs(args, Key.WORKFLOW);
24
- const secretKey = getSecretKey();
25
- const companyName = getValueFromArgs(args, Key.COMPANY_NAME);
26
- const user = getValueFromArgs(args, Key.USER_ID);
27
- const type = getValueFromArgs(args, Key.TYPE) || 'private';
28
-
29
- if (!workflowName) {
30
- customLog.changeAndThrow(`Parameter workflow is required`);
31
- return;
32
- }
33
-
34
- if (!secretKey) {
35
- customLog.changeAndThrow(`Parameter secret is required`);
36
- return;
37
- }
38
-
39
- if (!companyName) {
40
- customLog.changeAndThrow(`Parameter company is required`);
41
- return;
42
- }
43
-
44
- if (!user) {
45
- customLog.changeAndThrow(`Parameter user is required`);
46
- return;
47
- }
48
-
49
- const url = `${this.MOBIOFFICE_URL}/Mstate/workFlow/user`;
50
- const responseJSON = await fetch(url, {
51
- method: 'POST',
52
- headers: {
53
- 'secret-key': secretKey,
54
- 'Content-Type': 'application/json',
55
- },
56
- body: JSON.stringify({
57
- action: action.toUpperCase(),
58
- workflow: workflowName,
59
- secret: secretKey,
60
- nickname: companyName,
61
- type,
62
- user,
63
- }),
64
- });
65
-
66
- const response = await responseJSON.json();
67
- if (responseJSON.status >= 400) {
68
- customLog.changeAndThrow(
69
- 'Invalid Parameters for Linking: ',
70
- responseJSON.statusText,
71
- );
72
- } else {
73
- customLog.success('Workflow Added successfully', response?.message);
74
- }
75
- }
76
- }
77
-
78
- export const mobiofficeHandler = new MobiofficeHandler();
1
+ import { MOBIOFFICE_URL } from '../common/constant';
2
+ import { CmdAction, Variables } from '../common/enum';
3
+ import { getValueFromArgs } from '../common/helpers';
4
+ import { customLog, getSecretKey } from '../common/utils';
5
+
6
+ class MobiofficeHandler {
7
+ constructor() {}
8
+
9
+ async linkToMobioffice(action: CmdAction.LINK | CmdAction.UNLINK) {
10
+ const workflowName = getValueFromArgs(Variables.WORKFLOW, true);
11
+ const secretKey = getSecretKey();
12
+ const companyName = getValueFromArgs(Variables.COMPANY_ID, true);
13
+ const user = getValueFromArgs(Variables.USER_ID, true);
14
+
15
+ const url = `${MOBIOFFICE_URL}/Mstate/workFlow/user`;
16
+ const responseJSON = await fetch(url, {
17
+ method: 'POST',
18
+ headers: {
19
+ 'secret-key': secretKey,
20
+ 'Content-Type': 'application/json',
21
+ },
22
+ body: JSON.stringify({
23
+ action: action.toUpperCase(),
24
+ workflow: workflowName,
25
+ secret: secretKey,
26
+ nickname: companyName,
27
+ user,
28
+ }),
29
+ });
30
+
31
+ const response = await responseJSON.json();
32
+ if (responseJSON.status >= 400) {
33
+ customLog.changeAndThrow(
34
+ 'Invalid Parameters for Linking: ',
35
+ responseJSON.statusText,
36
+ );
37
+ } else {
38
+ customLog.success('Workflow Added successfully', response?.message);
39
+ }
40
+ }
41
+ }
42
+
43
+ export const mobiofficeHandler = new MobiofficeHandler();
@@ -1,238 +1,171 @@
1
- import * as fs from 'fs';
2
- import path from 'path';
3
- import { MSTATE_URL } from '../common/constant';
4
- import { customLog, getSecretKey } from '../common/utils';
5
- import { getValueFromArgs } from '../common/helpers';
6
-
7
- enum Key {
8
- PATH = 'path=',
9
- FILE = 'file=',
10
- SECRET_KEY = 'secret=',
11
- WORKFLOW = 'workflow=',
12
- TYPE = 'type=',
13
- }
14
-
15
- class WorkflowHandler {
16
- constructor() {}
17
-
18
- async addNewWorkflow() {
19
- try {
20
- const args = process.argv;
21
-
22
- const workflowPath = getValueFromArgs(args, Key.PATH);
23
- const file = getValueFromArgs(args, Key.FILE);
24
- const type = getValueFromArgs(args, Key.TYPE);
25
- const secretKey = getSecretKey();
26
-
27
- if (!file) {
28
- customLog.changeAndThrow(`Parameter file is required`);
29
- return;
30
- }
31
-
32
- if (!secretKey) {
33
- customLog.changeAndThrow(`Parameter secret is required`);
34
- return;
35
- }
36
-
37
- const filePath = path.resolve(file);
38
- const workflowJSON = fs.readFileSync(filePath, 'utf8');
39
-
40
- let query = '';
41
- if (workflowPath) {
42
- query += `path=${workflowPath}`;
43
- }
44
-
45
- if (query.length) {
46
- query = '?' + query;
47
- }
48
-
49
- if (type) {
50
- query += query ? `&type=${type}` : `?type=${type}`;
51
- }
52
-
53
- const url = `${MSTATE_URL}/workflow/config/new${query}`;
54
- const responseJSON = await fetch(url, {
55
- method: 'POST',
56
- headers: {
57
- 'secret-key': secretKey,
58
- 'Content-Type': 'application/json',
59
- },
60
- body: workflowJSON,
61
- });
62
-
63
- const response = await responseJSON.json();
64
- if (response?.errors) {
65
- customLog.changeAndThrow(
66
- 'Invalid Parameters for Workflow: ',
67
- ...response?.errors,
68
- );
69
- } else {
70
- customLog.success('Workflow Added successfully', response?.data);
71
- }
72
- } catch (error: any) {
73
- customLog.changeAndThrow(error.message);
74
- }
75
- }
76
-
77
- async updateWorkflowToDB() {
78
- const args = process.argv;
79
-
80
- const workflowName = getValueFromArgs(args, Key.WORKFLOW);
81
- const type = getValueFromArgs(args, Key.TYPE);
82
- const secretKey = getSecretKey();
83
-
84
- if (!secretKey) {
85
- customLog.changeAndThrow(`Parameter secret is required`);
86
- return;
87
- }
88
-
89
- if (!workflowName) {
90
- customLog.changeAndThrow(`Parameter workflow is required`);
91
- return;
92
- }
93
-
94
- const folderPath = path.resolve(workflowName);
95
- const filePath = path.join(folderPath, 'workflow.json');
96
-
97
- const workflowJSON = fs.readFileSync(filePath, 'utf8');
98
-
99
- let query = `?workflow=${workflowName}`;
100
-
101
- if (type) {
102
- query += `&type=${type}`;
103
- }
104
-
105
- const url = `${MSTATE_URL}/workflow/config${query}`;
106
- const responseJSON = await fetch(url, {
107
- method: 'PUT',
108
- headers: {
109
- 'secret-key': secretKey,
110
- 'Content-Type': 'application/json',
111
- },
112
- body: workflowJSON,
113
- });
114
-
115
- const response = await responseJSON.json();
116
- if (response?.errors) {
117
- customLog.changeAndThrow(
118
- 'Invalid Parameters for Server: ',
119
- ...response?.errors,
120
- );
121
- } else {
122
- customLog.success('Workflow Uploaded successfully \n', response?.data);
123
- }
124
-
125
- return response;
126
- }
127
-
128
- async cloneWorkflow() {
129
- const args = process.argv;
130
-
131
- const workflowName = getValueFromArgs(args, Key.WORKFLOW);
132
- const type = getValueFromArgs(args, Key.TYPE);
133
-
134
- const secretKey = getSecretKey();
135
-
136
- if (!secretKey) {
137
- customLog.changeAndThrow(`Parameter secret is required`);
138
- return;
139
- }
140
-
141
- if (!workflowName) {
142
- customLog.changeAndThrow(`Parameter workflow is required`);
143
- return;
144
- }
145
- const typeQuery = type ? `&type=${type}` : '';
146
-
147
- const url = `${MSTATE_URL}/workflow/config/?workflow=${workflowName}${typeQuery}`;
148
- const responseJSON = await fetch(url, {
149
- method: 'GET',
150
- headers: {
151
- 'Content-Type': 'application/json',
152
- 'secret-key': secretKey,
153
- },
154
- });
155
-
156
- const response = await responseJSON.json();
157
- if (response?.errors) {
158
- customLog.changeAndThrow(
159
- 'Invalid Parameters for Workflow: ',
160
- ...response?.errors,
161
- );
162
- } else {
163
- const workflowConfig = response?.data;
164
-
165
- const folderPath = path.resolve(workflowName);
166
- fs.mkdirSync(folderPath, { recursive: true });
167
-
168
- const filePath = path.join(folderPath, 'workflow.json');
169
- fs.writeFileSync(filePath, JSON.stringify(workflowConfig, null, 2));
170
-
171
- customLog.success(`Workflow cloned successfully`);
172
- }
173
- }
174
-
175
- async copyWorkflow() {
176
- try {
177
- const args = process.argv;
178
-
179
- const workflowName = getValueFromArgs(args, Key.WORKFLOW);
180
-
181
- if (!workflowName) {
182
- customLog.changeAndThrow(`Parameter workflow is required`);
183
- return;
184
- }
185
-
186
- const workflowPath = ('/' + workflowName).replace(/\/([^\/]*)$/, '');
187
- if (workflowPath) {
188
- process.argv.push(`path=${workflowPath.slice(1, workflowPath.length)}`);
189
- }
190
- process.argv.push(`file=${workflowName + '/workflow.json'}`);
191
-
192
- await this.addNewWorkflow();
193
- } catch (error: any) {
194
- customLog.changeAndThrow(error.message);
195
- }
196
- }
197
-
198
- async resetCache() {
199
- const args = process.argv;
200
- const type = getValueFromArgs(args, Key.TYPE);
201
-
202
- const workflowName = getValueFromArgs(args, Key.WORKFLOW);
203
- const secretKey = getSecretKey();
204
-
205
- if (!secretKey) {
206
- customLog.changeAndThrow(`Parameter secret is required`);
207
- return;
208
- }
209
-
210
- if (!workflowName) {
211
- customLog.changeAndThrow(`Parameter workflow is required`);
212
- return;
213
- }
214
-
215
- const typeQuery = type ? `&type=${type}` : '';
216
-
217
- const url = `${MSTATE_URL}/cache/reset/?workflow=${workflowName}${typeQuery}`;
218
- const responseJSON = await fetch(url, {
219
- method: 'DELETE',
220
- headers: {
221
- 'Content-Type': 'application/json',
222
- 'secret-key': secretKey,
223
- },
224
- });
225
-
226
- const response = await responseJSON.json();
227
- if (response?.errors) {
228
- customLog.changeAndThrow(
229
- 'Invalid Parameters for Workflow: ',
230
- ...response?.errors,
231
- );
232
- } else {
233
- customLog.success(`Workflow updated successfully`);
234
- }
235
- }
236
- }
237
-
238
- export const workflowHandler = new WorkflowHandler();
1
+ import * as fs from 'fs';
2
+ import path from 'path';
3
+ import { MSTATE_URL } from '../common/constant';
4
+ import { customLog, getSecretKey } from '../common/utils';
5
+ import { getValueFromArgs } from '../common/helpers';
6
+ import { Variables } from 'src/common/enum';
7
+
8
+ class WorkflowHandler {
9
+ constructor() {}
10
+
11
+ private async addNewWorkflow(workflowPath: string, file: string) {
12
+ try {
13
+ const secretKey = getSecretKey();
14
+
15
+ if (!file) {
16
+ customLog.changeAndThrow(`Parameter file is required`);
17
+ return;
18
+ }
19
+
20
+ if (!secretKey) {
21
+ customLog.changeAndThrow(`Parameter secret is required`);
22
+ return;
23
+ }
24
+
25
+ const filePath = path.resolve(file);
26
+ const workflowJSON = fs.readFileSync(filePath, 'utf8');
27
+
28
+ let query = '';
29
+ if (workflowPath) {
30
+ query += `path=${workflowPath}`;
31
+ }
32
+
33
+ if (query.length) {
34
+ query = '?' + query;
35
+ }
36
+
37
+ const url = `${MSTATE_URL}/workflow/config/new${query}`;
38
+ const responseJSON = await fetch(url, {
39
+ method: 'POST',
40
+ headers: {
41
+ 'secret-key': secretKey,
42
+ 'Content-Type': 'application/json',
43
+ },
44
+ body: workflowJSON,
45
+ });
46
+
47
+ const response = await responseJSON.json();
48
+ if (response?.errors) {
49
+ customLog.changeAndThrow(
50
+ 'Invalid Parameters for Workflow: ',
51
+ ...response?.errors,
52
+ );
53
+ } else {
54
+ customLog.success('Workflow Added successfully', response?.data);
55
+ }
56
+ } catch (error: any) {
57
+ customLog.changeAndThrow(error.message);
58
+ }
59
+ }
60
+
61
+ async updateWorkflowToDB() {
62
+ const workflowName = getValueFromArgs(Variables.WORKFLOW, true);
63
+ const secretKey = getSecretKey();
64
+
65
+ const folderPath = path.resolve(workflowName);
66
+ const filePath = path.join(folderPath, 'workflow.json');
67
+
68
+ const workflowJSON = fs.readFileSync(filePath, 'utf8');
69
+
70
+ let query = `?workflow=${workflowName}`;
71
+
72
+ const url = `${MSTATE_URL}/workflow/config${query}`;
73
+ const responseJSON = await fetch(url, {
74
+ method: 'PUT',
75
+ headers: {
76
+ 'secret-key': secretKey,
77
+ 'Content-Type': 'application/json',
78
+ },
79
+ body: workflowJSON,
80
+ });
81
+
82
+ const response = await responseJSON.json();
83
+ if (response?.errors) {
84
+ customLog.changeAndThrow(
85
+ 'Invalid Parameters for Server: ',
86
+ ...response?.errors,
87
+ );
88
+ } else {
89
+ customLog.success('Workflow Uploaded successfully \n', response?.data);
90
+ }
91
+
92
+ return response;
93
+ }
94
+
95
+ async cloneWorkflow() {
96
+ const workflowName = getValueFromArgs(Variables.WORKFLOW, true);
97
+ const secretKey = getSecretKey();
98
+
99
+ const url = `${MSTATE_URL}/workflow/config/?workflow=${workflowName}`;
100
+ const responseJSON = await fetch(url, {
101
+ method: 'GET',
102
+ headers: {
103
+ 'Content-Type': 'application/json',
104
+ 'secret-key': secretKey,
105
+ },
106
+ });
107
+
108
+ const response = await responseJSON.json();
109
+ if (response?.errors) {
110
+ customLog.changeAndThrow(
111
+ 'Invalid Parameters for Workflow: ',
112
+ ...response?.errors,
113
+ );
114
+ } else {
115
+ const workflowConfig = response?.data;
116
+
117
+ const folderPath = path.resolve(workflowName);
118
+ fs.mkdirSync(folderPath, { recursive: true });
119
+
120
+ const filePath = path.join(folderPath, 'workflow.json');
121
+ fs.writeFileSync(filePath, JSON.stringify(workflowConfig, null, 2));
122
+
123
+ customLog.success(`Workflow cloned successfully`);
124
+ }
125
+ }
126
+
127
+ async copyWorkflow() {
128
+ try {
129
+ const workflowName = getValueFromArgs(Variables.WORKFLOW, true);
130
+
131
+ let path = '';
132
+ let file = '';
133
+
134
+ const workflowPath = ('/' + workflowName).replace(/\/([^\/]*)$/, '');
135
+ if (workflowPath) {
136
+ path = workflowPath.slice(1, workflowPath.length);
137
+ }
138
+ file = workflowName + '/workflow.json';
139
+
140
+ await this.addNewWorkflow(path, file);
141
+ } catch (error: any) {
142
+ customLog.changeAndThrow(error.message);
143
+ }
144
+ }
145
+
146
+ async resetCache() {
147
+ const workflowName = getValueFromArgs(Variables.WORKFLOW, true);
148
+ const secretKey = getSecretKey();
149
+
150
+ const url = `${MSTATE_URL}/cache/reset/?workflow=${workflowName}`;
151
+ const responseJSON = await fetch(url, {
152
+ method: 'DELETE',
153
+ headers: {
154
+ 'Content-Type': 'application/json',
155
+ 'secret-key': secretKey,
156
+ },
157
+ });
158
+
159
+ const response = await responseJSON.json();
160
+ if (response?.errors) {
161
+ customLog.changeAndThrow(
162
+ 'Invalid Parameters for Workflow: ',
163
+ ...response?.errors,
164
+ );
165
+ } else {
166
+ customLog.success(`Workflow updated successfully`);
167
+ }
168
+ }
169
+ }
170
+
171
+ export const workflowHandler = new WorkflowHandler();