mstate-cli 0.1.4 → 0.1.7

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,107 @@ 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;
71
-
72
- const dirPath = getValueFromArgs(args, Key.WORKFLOW);
73
- const workflowName = dirPath.replace(/\//g, '__');
74
- const secretKey = getValueFromArgs(args, Key.SECRET_KEY);
72
+ const args = process.argv;
75
73
 
76
- if (!secretKey) {
77
- customLog.error(`Parameter secret is required`);
78
- return;
79
- }
74
+ const dirPath = getValueFromArgs(args, Key.WORKFLOW);
75
+ const workflowName = dirPath.replace(/\//g, '__');
76
+ const secretKey = getValueFromArgs(args, Key.SECRET_KEY);
80
77
 
81
- if (!workflowName) {
82
- customLog.error(`Parameter workflow is required`);
83
- return;
84
- }
78
+ if (!secretKey) {
79
+ customLog.changeAndThrow(`Parameter secret is required`);
80
+ return;
81
+ }
85
82
 
86
- const folderPath = path.resolve(dirPath);
87
- const filePath = path.join(folderPath, 'workflow.json');
83
+ if (!workflowName) {
84
+ customLog.changeAndThrow(`Parameter workflow is required`);
85
+ return;
86
+ }
88
87
 
89
- const workflowJSON = fs.readFileSync(filePath, 'utf8');
88
+ const folderPath = path.resolve(dirPath);
89
+ const filePath = path.join(folderPath, 'workflow.json');
90
90
 
91
- let query = `?workflow=${workflowName}`;
91
+ const workflowJSON = fs.readFileSync(filePath, 'utf8');
92
92
 
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
- });
93
+ let query = `?workflow=${workflowName}`;
102
94
 
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
- }
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
+ });
109
104
 
110
- return response;
111
- } catch (error: any) {
112
- customLog.error(error.message);
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
113
  }
114
+
115
+ return response;
114
116
  }
115
117
 
116
118
  async cloneWorkflow() {
117
- try {
118
- const args = process.argv;
119
+ const args = process.argv;
119
120
 
120
- const dirPath = getValueFromArgs(args, Key.WORKFLOW);
121
- const workflowName = dirPath.replace(/\//g, '__');
122
- const secretKey = getValueFromArgs(args, Key.SECRET_KEY);
121
+ const dirPath = getValueFromArgs(args, Key.WORKFLOW);
122
+ const workflowName = dirPath.replace(/\//g, '__');
123
+ const secretKey = getValueFromArgs(args, Key.SECRET_KEY);
123
124
 
124
- if (!secretKey) {
125
- customLog.error(`Parameter secret is required`);
126
- return;
127
- }
125
+ if (!secretKey) {
126
+ customLog.changeAndThrow(`Parameter secret is required`);
127
+ return;
128
+ }
128
129
 
129
- if (!workflowName) {
130
- customLog.error(`Parameter workflow is required`);
131
- return;
132
- }
130
+ if (!workflowName) {
131
+ customLog.changeAndThrow(`Parameter workflow is required`);
132
+ return;
133
+ }
133
134
 
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
- });
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
+ });
142
143
 
143
- const response = await responseJSON.json();
144
- if (response?.errors) {
145
- customLog.error('Invalid Parameters: ', response?.errors);
146
- } else {
147
- const workflowConfig = response?.data;
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;
148
152
 
149
- const folderPath = path.resolve(dirPath);
150
- fs.mkdirSync(folderPath, { recursive: true });
153
+ const folderPath = path.resolve(dirPath);
154
+ fs.mkdirSync(folderPath, { recursive: true });
151
155
 
152
- const filePath = path.join(folderPath, 'workflow.json');
153
- fs.writeFileSync(filePath, JSON.stringify(workflowConfig, null, 2));
156
+ const filePath = path.join(folderPath, 'workflow.json');
157
+ fs.writeFileSync(filePath, JSON.stringify(workflowConfig, null, 2));
154
158
 
155
- customLog.success(`Workflow cloned successfully`);
156
- }
157
- } catch (error: any) {
158
- customLog.error('Error in cloning workflow', error.message);
159
+ customLog.success(`Workflow cloned successfully`);
159
160
  }
160
161
  }
161
162
 
@@ -167,12 +168,12 @@ class WorkflowHandler {
167
168
  const secretKey = getValueFromArgs(args, Key.SECRET_KEY);
168
169
 
169
170
  if (!secretKey) {
170
- customLog.error(`Parameter secret is required`);
171
+ customLog.changeAndThrow(`Parameter secret is required`);
171
172
  return;
172
173
  }
173
174
 
174
175
  if (!workflowName) {
175
- customLog.error(`Parameter workflow is required`);
176
+ customLog.changeAndThrow(`Parameter workflow is required`);
176
177
  return;
177
178
  }
178
179
 
@@ -187,7 +188,10 @@ class WorkflowHandler {
187
188
 
188
189
  const response = await responseJSON.json();
189
190
  if (response?.errors) {
190
- customLog.error('Invalid Parameters: ', response?.errors);
191
+ customLog.changeAndThrow(
192
+ 'Invalid Parameters for Workflow: ',
193
+ ...response?.errors,
194
+ );
191
195
  } else {
192
196
  customLog.success(`Workflow updated successfully`);
193
197
  }
package/src/index.ts CHANGED
@@ -6,70 +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
- workflowHandler.resetCache();
40
- });
41
- break;
42
- case CmdAction.HELP:
43
- case CmdAction.HELP_FLAG:
44
- 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;
45
34
 
46
- console.log('Commands:');
47
- customLog.info(CmdAction.ADD, 'Add new workflow');
48
- customLog.info(
49
- CmdAction.CLONE,
50
- 'Clone workflow with actions and environments',
51
- );
52
- customLog.info(CmdAction.PUSH, 'Update the changes one in cloned workflow');
53
- customLog.info(
54
- CmdAction.LINK,
55
- 'add workflow and allow user to view workflow to mobioffice application',
56
- );
57
- customLog.info(
58
- CmdAction.UNLINK,
59
- 'add workflow and remove user to view workflow to mobioffice application',
60
- );
61
- customLog.info(
62
- [CmdAction.VERSION_FLAG, CmdAction.VERSION].toString(),
63
- 'add workflow and remove user to view workflow to mobioffice application',
64
- );
65
- break;
66
- default:
67
- customLog.error(
68
- `${action ? 'Invalid' : 'Missing'} script: ${action ?? ''}`,
69
- );
70
- console.log(
71
- `use 'mstate ${CmdAction.HELP_FLAG}, ${CmdAction.HELP}' for getting allowed action`,
72
- );
73
- }
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;
74
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
+ });
75
67
  console.log('');