mstate-cli 0.1.3 → 0.1.6

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.
@@ -23,12 +23,12 @@ class WorkflowHandler {
23
23
  const secretKey = getValueFromArgs(args, Key.SECRET_KEY);
24
24
 
25
25
  if (!file) {
26
- customLog.error(`Parameter file is required`);
26
+ customLog.changeAndThrow(`Parameter file is required`);
27
27
  return;
28
28
  }
29
29
 
30
30
  if (!secretKey) {
31
- customLog.error(`Parameter secret is required`);
31
+ customLog.changeAndThrow(`Parameter secret is required`);
32
32
  return;
33
33
  }
34
34
 
@@ -56,106 +56,144 @@ class WorkflowHandler {
56
56
 
57
57
  const response = await responseJSON.json();
58
58
  if (response?.errors) {
59
- customLog.error('Invalid Parameters: ', response?.errors);
59
+ customLog.changeAndThrow(
60
+ 'Invalid Parameters for Workflow: ',
61
+ ...response?.errors,
62
+ );
60
63
  } else {
61
64
  customLog.success('Workflow Added successfully', response?.data);
62
65
  }
63
66
  } catch (error: any) {
64
- customLog.error(error.message);
67
+ customLog.changeAndThrow(error.message);
65
68
  }
66
69
  }
67
70
 
68
71
  async updateWorkflowToDB() {
69
- try {
70
- const args = process.argv;
72
+ const args = process.argv;
71
73
 
72
- const dirPath = getValueFromArgs(args, Key.WORKFLOW);
73
- const workflowName = dirPath.replace(/\//g, '__');
74
- const secretKey = getValueFromArgs(args, Key.SECRET_KEY);
74
+ const dirPath = getValueFromArgs(args, Key.WORKFLOW);
75
+ const workflowName = dirPath.replace(/\//g, '__');
76
+ const secretKey = getValueFromArgs(args, Key.SECRET_KEY);
75
77
 
76
- if (!secretKey) {
77
- customLog.error(`Parameter secret is required`);
78
- return;
79
- }
78
+ if (!secretKey) {
79
+ customLog.changeAndThrow(`Parameter secret is required`);
80
+ return;
81
+ }
80
82
 
81
- if (!workflowName) {
82
- customLog.error(`Parameter workflow is required`);
83
- return;
84
- }
83
+ if (!workflowName) {
84
+ customLog.changeAndThrow(`Parameter workflow is required`);
85
+ return;
86
+ }
85
87
 
86
- const folderPath = path.resolve(dirPath);
87
- const filePath = path.join(folderPath, 'workflow.json');
88
+ const folderPath = path.resolve(dirPath);
89
+ const filePath = path.join(folderPath, 'workflow.json');
90
+
91
+ const workflowJSON = fs.readFileSync(filePath, 'utf8');
92
+
93
+ let query = `?workflow=${workflowName}`;
94
+
95
+ const url = `${MSTATE_URL}/workflow/config${query}`;
96
+ const responseJSON = await fetch(url, {
97
+ method: 'PUT',
98
+ headers: {
99
+ 'secret-key': secretKey,
100
+ 'Content-Type': 'application/json',
101
+ },
102
+ body: workflowJSON,
103
+ });
104
+
105
+ const response = await responseJSON.json();
106
+ if (response?.errors) {
107
+ customLog.changeAndThrow(
108
+ 'Invalid Parameters for Server: ',
109
+ ...response?.errors,
110
+ );
111
+ } else {
112
+ customLog.success('Workflow Uploaded successfully \n', response?.data);
113
+ }
88
114
 
89
- const workflowJSON = fs.readFileSync(filePath, 'utf8');
115
+ return response;
116
+ }
90
117
 
91
- let query = `?workflow=${workflowName}`;
118
+ async cloneWorkflow() {
119
+ const args = process.argv;
92
120
 
93
- const url = `${MSTATE_URL}/workflow/config${query}`;
94
- const responseJSON = await fetch(url, {
95
- method: 'PUT',
96
- headers: {
97
- 'secret-key': secretKey,
98
- 'Content-Type': 'application/json',
99
- },
100
- body: workflowJSON,
101
- });
121
+ const dirPath = getValueFromArgs(args, Key.WORKFLOW);
122
+ const workflowName = dirPath.replace(/\//g, '__');
123
+ const secretKey = getValueFromArgs(args, Key.SECRET_KEY);
102
124
 
103
- const response = await responseJSON.json();
104
- if (response?.errors) {
105
- customLog.error('Invalid Parameters: ', response?.errors);
106
- } else {
107
- customLog.success('Workflow Uploaded successfully \n', response?.data);
108
- }
125
+ if (!secretKey) {
126
+ customLog.changeAndThrow(`Parameter secret is required`);
127
+ return;
128
+ }
109
129
 
110
- return response;
111
- } catch (error: any) {
112
- customLog.error(error.message);
130
+ if (!workflowName) {
131
+ customLog.changeAndThrow(`Parameter workflow is required`);
132
+ return;
113
133
  }
114
- }
115
134
 
116
- async cloneWorkflow() {
117
- try {
118
- const args = process.argv;
135
+ const url = `${MSTATE_URL}/workflow/config/${workflowName}`;
136
+ const responseJSON = await fetch(url, {
137
+ method: 'GET',
138
+ headers: {
139
+ 'Content-Type': 'application/json',
140
+ 'secret-key': secretKey,
141
+ },
142
+ });
143
+
144
+ const response = await responseJSON.json();
145
+ if (response?.errors) {
146
+ customLog.changeAndThrow(
147
+ 'Invalid Parameters for Workflow: ',
148
+ ...response?.errors,
149
+ );
150
+ } else {
151
+ const workflowConfig = response?.data;
119
152
 
120
- const dirPath = getValueFromArgs(args, Key.WORKFLOW);
121
- const workflowName = dirPath.replace(/\//g, '__');
122
- const secretKey = getValueFromArgs(args, Key.SECRET_KEY);
153
+ const folderPath = path.resolve(dirPath);
154
+ fs.mkdirSync(folderPath, { recursive: true });
123
155
 
124
- if (!secretKey) {
125
- customLog.error(`Parameter secret is required`);
126
- return;
127
- }
156
+ const filePath = path.join(folderPath, 'workflow.json');
157
+ fs.writeFileSync(filePath, JSON.stringify(workflowConfig, null, 2));
128
158
 
129
- if (!workflowName) {
130
- customLog.error(`Parameter workflow is required`);
131
- return;
132
- }
159
+ customLog.success(`Workflow cloned successfully`);
160
+ }
161
+ }
133
162
 
134
- const url = `${MSTATE_URL}/workflow/config/${workflowName}`;
135
- const responseJSON = await fetch(url, {
136
- method: 'GET',
137
- headers: {
138
- 'Content-Type': 'application/json',
139
- 'secret-key': secretKey,
140
- },
141
- });
163
+ async resetCache() {
164
+ const args = process.argv;
142
165
 
143
- const response = await responseJSON.json();
144
- if (response?.errors) {
145
- customLog.error('Invalid Parameters: ', response?.errors);
146
- } else {
147
- const workflowConfig = response?.data;
166
+ const dirPath = getValueFromArgs(args, Key.WORKFLOW);
167
+ const workflowName = dirPath.replace(/\//g, '__');
168
+ const secretKey = getValueFromArgs(args, Key.SECRET_KEY);
148
169
 
149
- const folderPath = path.resolve(dirPath);
150
- fs.mkdirSync(folderPath, { recursive: true });
170
+ if (!secretKey) {
171
+ customLog.changeAndThrow(`Parameter secret is required`);
172
+ return;
173
+ }
151
174
 
152
- const filePath = path.join(folderPath, 'workflow.json');
153
- fs.writeFileSync(filePath, JSON.stringify(workflowConfig, null, 2));
175
+ if (!workflowName) {
176
+ customLog.changeAndThrow(`Parameter workflow is required`);
177
+ return;
178
+ }
154
179
 
155
- customLog.success(`Workflow cloned successfully`);
156
- }
157
- } catch (error: any) {
158
- customLog.error('Error in cloning workflow', error.message);
180
+ const url = `${MSTATE_URL}/cache/reset/${workflowName}`;
181
+ const responseJSON = await fetch(url, {
182
+ method: 'DELETE',
183
+ headers: {
184
+ 'Content-Type': 'application/json',
185
+ 'secret-key': secretKey,
186
+ },
187
+ });
188
+
189
+ const response = await responseJSON.json();
190
+ if (response?.errors) {
191
+ customLog.changeAndThrow(
192
+ 'Invalid Parameters for Workflow: ',
193
+ ...response?.errors,
194
+ );
195
+ } else {
196
+ customLog.success(`Workflow updated successfully`);
159
197
  }
160
198
  }
161
199
  }
package/src/index.ts CHANGED
@@ -6,69 +6,62 @@ import { actionHandler } from './handlers/action.handler';
6
6
  import { workflowHandler } from './handlers/workflow.handler';
7
7
  import { CmdAction } from './common/enum';
8
8
  import { mobiofficeHandler } from './handlers/mobioffice.handler';
9
- import { customLog } from './common/utils';
9
+ import { customLog, hasFlag } from './common/utils';
10
10
  import { environmentHandler } from './handlers/environment.handler';
11
+ import { companyHandler } from './handlers/company.handler';
12
+ import { argv } from 'process';
13
+ import { helpHandler } from './handlers/help.handler';
11
14
 
12
- const [action] = process.argv.slice(2);
15
+ const [action] = argv.slice(2);
16
+ (async function () {
17
+ switch (action as CmdAction) {
18
+ case CmdAction.VERSION:
19
+ case CmdAction.VERSION_FLAG:
20
+ console.log(`v${packageJSON.version}`);
21
+ break;
13
22
 
14
- switch (action as CmdAction) {
15
- case CmdAction.VERSION:
16
- case CmdAction.VERSION_FLAG:
17
- console.log(`v${packageJSON.version}`);
18
- break;
23
+ case CmdAction.LINK:
24
+ case CmdAction.UNLINK:
25
+ await mobiofficeHandler.linkToMobioffice(
26
+ action as CmdAction.LINK | CmdAction.UNLINK,
27
+ );
28
+ break;
19
29
 
20
- case CmdAction.LINK:
21
- case CmdAction.UNLINK:
22
- mobiofficeHandler.linkToMobioffice(
23
- action as CmdAction.LINK | CmdAction.UNLINK,
24
- );
25
- break;
26
- case CmdAction.CLONE:
27
- workflowHandler.cloneWorkflow().then(() => {
28
- actionHandler.cloneActions();
29
- environmentHandler.cloneEnvironments();
30
- });
31
- break;
32
- case CmdAction.ADD:
33
- workflowHandler.saveToDB();
34
- break;
35
- case CmdAction.PUSH:
36
- workflowHandler.updateWorkflowToDB().then(() => {
37
- actionHandler.saveToDB();
38
- environmentHandler.saveToDB();
39
- });
40
- break;
41
- case CmdAction.HELP:
42
- case CmdAction.HELP_FLAG:
43
- console.log('\n\nUsage: mstate [cmd] [parameter]="[value]"\n');
30
+ case CmdAction.USER:
31
+ if (hasFlag(argv, '-d')) companyHandler.revokePermission();
32
+ else companyHandler.addToken();
33
+ break;
44
34
 
45
- console.log('Commands:');
46
- customLog.info(CmdAction.ADD, 'Add new workflow');
47
- customLog.info(
48
- CmdAction.CLONE,
49
- 'Clone workflow with actions and environments',
50
- );
51
- customLog.info(CmdAction.PUSH, 'Update the changes one in cloned workflow');
52
- customLog.info(
53
- CmdAction.LINK,
54
- 'add workflow and allow user to view workflow to mobioffice application',
55
- );
56
- customLog.info(
57
- CmdAction.UNLINK,
58
- 'add workflow and remove user to view workflow to mobioffice application',
59
- );
60
- customLog.info(
61
- [CmdAction.VERSION_FLAG, CmdAction.VERSION].toString(),
62
- 'add workflow and remove user to view workflow to mobioffice application',
63
- );
64
- break;
65
- default:
66
- customLog.error(
67
- `${action ? 'Invalid' : 'Missing'} script: ${action ?? ''}`,
68
- );
69
- console.log(
70
- `use 'mstate ${CmdAction.HELP_FLAG}, ${CmdAction.HELP}' for getting allowed action`,
71
- );
72
- }
35
+ case CmdAction.CLONE:
36
+ await workflowHandler.cloneWorkflow();
37
+ await actionHandler.cloneActions();
38
+ await environmentHandler.cloneEnvironments();
39
+ break;
40
+ case CmdAction.ADD:
41
+ await workflowHandler.saveToDB();
42
+ break;
43
+ case CmdAction.PUSH:
44
+ await workflowHandler.updateWorkflowToDB();
45
+ await actionHandler.saveToDB();
46
+ await environmentHandler.saveToDB();
47
+ await workflowHandler.resetCache();
48
+ break;
49
+ case CmdAction.HELP:
50
+ case CmdAction.HELP_FLAG:
51
+ helpHandler.logCommandInfo();
52
+ break;
73
53
 
54
+ default:
55
+ customLog.changeAndThrow(
56
+ `${action ? 'Invalid' : 'Missing'} script: ${action ?? ''}`,
57
+ );
58
+ console.log(
59
+ `use 'mstate ${CmdAction.HELP_FLAG}, ${CmdAction.HELP}' for getting allowed action`,
60
+ );
61
+ }
62
+ })().catch((error: any) => {
63
+ if (hasFlag(argv, CmdAction.HELP) || hasFlag(argv, CmdAction.HELP_FLAG)) {
64
+ helpHandler.logActionInfo(action as CmdAction);
65
+ } else customLog.error(error.message);
66
+ });
74
67
  console.log('');