mstate-cli 0.2.7 → 0.2.9

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,238 +1,238 @@
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
+
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();
package/src/index.ts CHANGED
@@ -1,88 +1,85 @@
1
- #!/usr/bin/env node
2
-
3
- import packageJSON from '../package.json';
4
-
5
- import { actionHandler } from './handlers/action.handler';
6
- import { workflowHandler } from './handlers/workflow.handler';
7
- import { CmdAction } from './common/enum';
8
- import { mobiofficeHandler } from './handlers/mobioffice.handler';
9
- import { customLog } from './common/utils';
10
- import { hasFlag } from './common/helpers';
11
- import { environmentHandler } from './handlers/environment.handler';
12
- import { companyHandler } from './handlers/company.handler';
13
- import { argv } from 'process';
14
- import { helpHandler } from './handlers/help.handler';
15
-
16
- const [action] = argv.slice(2);
17
- (async function () {
18
- switch (action as CmdAction) {
19
- case CmdAction.VERSION:
20
- case CmdAction.VERSION_FLAG:
21
- console.log(`v${packageJSON.version}`);
22
- break;
23
-
24
- case CmdAction.LINK:
25
- case CmdAction.UNLINK:
26
- await mobiofficeHandler.linkToMobioffice(
27
- action as CmdAction.LINK | CmdAction.UNLINK,
28
- );
29
- break;
30
-
31
- case CmdAction.PERMISSION:
32
- if (hasFlag(argv, '-d')) await companyHandler.revokePermission();
33
- else await companyHandler.addToken();
34
- break;
35
-
36
- case CmdAction.SET:
37
- companyHandler.handleUpdateSavedSecretKey();
38
- break;
39
-
40
- case CmdAction.USER:
41
- await companyHandler.addUser();
42
- break;
43
-
44
- case CmdAction.DEV: // comment this case on production build
45
- companyHandler.logToken();
46
- break;
47
-
48
- case CmdAction.LOGIN:
49
- await companyHandler.handleLogin();
50
- break;
51
-
52
- case CmdAction.CLONE:
53
- await workflowHandler.cloneWorkflow();
54
- await actionHandler.cloneActions();
55
- await environmentHandler.cloneEnvironments();
56
- break;
57
- case CmdAction.ADD:
58
- await workflowHandler.addNewWorkflow();
59
- break;
60
- case CmdAction.COPY:
61
- await workflowHandler.copyWorkflow();
62
- case CmdAction.PUSH:
63
- await workflowHandler.updateWorkflowToDB();
64
- await actionHandler.saveToDB();
65
- await environmentHandler.saveToDB();
66
- await workflowHandler.resetCache();
67
- break;
68
- case CmdAction.HELP:
69
- case CmdAction.HELP_FLAG:
70
- helpHandler.logCommandInfo();
71
- break;
72
-
73
- default:
74
- customLog.error(
75
- `${action ? 'Invalid' : 'Missing'} script: ${action ?? ''}`,
76
- );
77
- console.log(
78
- `use 'mstate [command]? ${CmdAction.HELP_FLAG}, ${CmdAction.HELP}' for getting allowed action`,
79
- );
80
- }
81
- })()
82
- .then()
83
- .catch((error: any) => {
84
- if (hasFlag(argv, CmdAction.HELP) || hasFlag(argv, CmdAction.HELP_FLAG)) {
85
- helpHandler.logActionInfo(action as CmdAction);
86
- } else customLog.error(error.message);
87
- });
88
- console.log('');
1
+ #!/usr/bin/env node
2
+
3
+ import packageJSON from '../package.json';
4
+
5
+ import { actionHandler } from './handlers/action.handler';
6
+ import { workflowHandler } from './handlers/workflow.handler';
7
+ import { CmdAction } from './common/enum';
8
+ import { mobiofficeHandler } from './handlers/mobioffice.handler';
9
+ import { customLog } from './common/utils';
10
+ import { hasFlag } from './common/helpers';
11
+ import { environmentHandler } from './handlers/environment.handler';
12
+ import { companyHandler } from './handlers/company.handler';
13
+ import { argv } from 'process';
14
+ import { helpHandler } from './handlers/help.handler';
15
+
16
+ const [action] = argv.slice(2);
17
+ (async function () {
18
+ switch (action as CmdAction) {
19
+ case CmdAction.VERSION:
20
+ case CmdAction.VERSION_FLAG:
21
+ console.log(`v${packageJSON.version}`);
22
+ break;
23
+
24
+ case CmdAction.LINK:
25
+ case CmdAction.UNLINK:
26
+ await mobiofficeHandler.linkToMobioffice(
27
+ action as CmdAction.LINK | CmdAction.UNLINK,
28
+ );
29
+ break;
30
+
31
+ case CmdAction.PERMISSION:
32
+ if (hasFlag(argv, '-d')) await companyHandler.revokePermission();
33
+ else await companyHandler.addToken();
34
+ break;
35
+
36
+ case CmdAction.SET:
37
+ companyHandler.handleUpdateSavedSecretKey();
38
+ break;
39
+
40
+ case CmdAction.USER:
41
+ await companyHandler.addUser();
42
+ break;
43
+
44
+ case CmdAction.DEV: // comment this case on production build
45
+ companyHandler.logToken();
46
+ break;
47
+
48
+ case CmdAction.LOGIN:
49
+ await companyHandler.handleLogin();
50
+ break;
51
+
52
+ case CmdAction.CLONE:
53
+ await workflowHandler.cloneWorkflow();
54
+ await actionHandler.cloneActions();
55
+ await environmentHandler.cloneEnvironments();
56
+ break;
57
+ case CmdAction.ADD:
58
+ await workflowHandler.copyWorkflow();
59
+ case CmdAction.PUSH:
60
+ await workflowHandler.updateWorkflowToDB();
61
+ await actionHandler.saveToDB();
62
+ await environmentHandler.saveToDB();
63
+ await workflowHandler.resetCache();
64
+ break;
65
+ case CmdAction.HELP:
66
+ case CmdAction.HELP_FLAG:
67
+ helpHandler.logCommandInfo();
68
+ break;
69
+
70
+ default:
71
+ customLog.error(
72
+ `${action ? 'Invalid' : 'Missing'} script: ${action ?? ''}`,
73
+ );
74
+ console.log(
75
+ `use 'mstate [command]? ${CmdAction.HELP_FLAG}, ${CmdAction.HELP}' for getting allowed action`,
76
+ );
77
+ }
78
+ })()
79
+ .then()
80
+ .catch((error: any) => {
81
+ if (hasFlag(argv, CmdAction.HELP) || hasFlag(argv, CmdAction.HELP_FLAG)) {
82
+ helpHandler.logActionInfo(action as CmdAction);
83
+ } else customLog.error(error.message);
84
+ });
85
+ console.log('');