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.
@@ -12,115 +12,107 @@ class ActionHandler {
12
12
  constructor() {}
13
13
 
14
14
  async cloneActions() {
15
- try {
16
- const args = process.argv;
17
- const dirPath = getValueFromArgs(args, Key.WORKFLOW);
18
- const workflowName = dirPath.replace(/\//g, '__');
19
- const secretKey = getValueFromArgs(args, Key.SECRET_KEY);
20
-
21
- if (!secretKey) {
22
- customLog.error(`Parameter secret is required`);
23
- return;
24
- }
25
-
26
- if (!workflowName) {
27
- customLog.error(`Parameter workflow is required`);
28
- return;
29
- }
30
-
31
- try {
32
- const url = `${MSTATE_URL}/action/config/all/?workflow=${workflowName}`;
15
+ const args = process.argv;
16
+ const dirPath = getValueFromArgs(args, Key.WORKFLOW);
17
+ const workflowName = dirPath.replace(/\//g, '__');
18
+ const secretKey = getValueFromArgs(args, Key.SECRET_KEY);
19
+
20
+ if (!secretKey) {
21
+ customLog.changeAndThrow(`Parameter secret is required`);
22
+ return;
23
+ }
33
24
 
34
- const responseJSON = await fetch(url, {
35
- headers: {
36
- 'Content-Type': 'application/json',
37
- 'secret-key': secretKey,
38
- },
39
- });
25
+ if (!workflowName) {
26
+ customLog.changeAndThrow(`Parameter workflow is required`);
27
+ return;
28
+ }
40
29
 
41
- const response = await responseJSON.json();
42
- if (response?.errors) {
43
- customLog.error('Invalid Parameters: ', response?.errors);
44
- } else {
45
- const actionConfigs = response?.data;
30
+ const url = `${MSTATE_URL}/action/config/all/?workflow=${workflowName}`;
46
31
 
47
- const folderPath = path.resolve(dirPath, 'actions');
48
- fs.mkdirSync(folderPath, { recursive: true });
32
+ const responseJSON = await fetch(url, {
33
+ headers: {
34
+ 'Content-Type': 'application/json',
35
+ 'secret-key': secretKey,
36
+ },
37
+ });
49
38
 
50
- actionConfigs.forEach((config: ActionConfig) => {
51
- const file = path.join(folderPath, `${config?.name}.json`);
52
- fs.writeFileSync(file, JSON.stringify(config, null, 2));
39
+ const response = await responseJSON.json();
40
+ if (response?.errors) {
41
+ customLog.changeAndThrow(
42
+ 'Invalid Parameters for Action: ',
43
+ ...response?.errors,
44
+ );
45
+ } else {
46
+ const actionConfigs = response?.data;
53
47
 
54
- customLog.success(
55
- 'file created at',
56
- folderPath,
57
- `${config?.name}.json`,
58
- );
59
- });
48
+ const folderPath = path.resolve(dirPath, 'actions');
49
+ fs.mkdirSync(folderPath, { recursive: true });
50
+
51
+ actionConfigs.forEach((config: ActionConfig) => {
52
+ try {
53
+ const file = path.join(folderPath, `${config?.name}.json`);
54
+ fs.writeFileSync(file, JSON.stringify(config, null, 2));
55
+
56
+ customLog.success(
57
+ 'file created at',
58
+ folderPath,
59
+ `${config?.name}.json`,
60
+ );
61
+ } catch (error: any) {
62
+ customLog.error(error.message);
60
63
  }
61
- } catch (error: any) {
62
- customLog.error('Error in cloning action', error.message);
63
- }
64
- } catch (error: any) {
65
- customLog.error('Error in cloning action', error.message);
64
+ });
66
65
  }
67
66
  }
68
67
 
69
68
  async saveToDB() {
70
- try {
71
- const args = process.argv;
72
- const dirPath = getValueFromArgs(args, Key.WORKFLOW);
73
- const workflowName = dirPath.replace(/\//g, '__');
74
- const secretKey = getValueFromArgs(args, Key.SECRET_KEY);
75
-
76
- if (!secretKey) {
77
- customLog.error(`Parameter secret is required`);
78
- return;
79
- }
80
-
81
- if (!workflowName) {
82
- customLog.error(`Parameter workflow is required`);
83
- return;
84
- }
69
+ const args = process.argv;
70
+ const dirPath = getValueFromArgs(args, Key.WORKFLOW);
71
+ const workflowName = dirPath.replace(/\//g, '__');
72
+ const secretKey = getValueFromArgs(args, Key.SECRET_KEY);
73
+
74
+ if (!secretKey) {
75
+ customLog.changeAndThrow(`Parameter secret is required`);
76
+ return;
77
+ }
85
78
 
86
- const folderPath = path.resolve(dirPath, 'actions');
79
+ if (!workflowName) {
80
+ customLog.changeAndThrow(`Parameter workflow is required`);
81
+ return;
82
+ }
83
+
84
+ const folderPath = path.resolve(dirPath, 'actions');
85
+
86
+ // loop to every folder to publish workflow
87
+ fs.readdir(folderPath, (err, files) => {
88
+ if (!files) return;
87
89
 
88
- // loop to every folder to publish workflow
89
- fs.readdir(folderPath, (err, files) => {
90
- if (!files) return;
91
-
92
- files.forEach(async (fileName) => {
93
- try {
94
- const filePath = path.join(folderPath, fileName);
95
- const data = fs.readFileSync(filePath, 'utf8');
96
-
97
- const url = `${MSTATE_URL}/action/config/?workflow=${workflowName}`;
98
- const responseJSON = await fetch(url, {
99
- method: 'PUT',
100
- headers: {
101
- 'secret-key': secretKey,
102
- 'Content-Type': 'application/json',
103
- },
104
- body: data,
105
- });
106
- const response = await responseJSON.json();
107
-
108
- if (response?.errors) {
109
- customLog.error(
110
- `Invalid Parameters for Action ${fileName}: `,
111
- response?.errors,
112
- );
113
- } else {
114
- customLog.success(`Action ${fileName} Updated successfully`);
115
- }
116
- } catch (error: any) {
117
- customLog.error(error.message);
118
- }
90
+ files.forEach(async (fileName) => {
91
+ const filePath = path.join(folderPath, fileName);
92
+ const data = fs.readFileSync(filePath, 'utf8');
93
+
94
+ const url = `${MSTATE_URL}/action/config/?workflow=${workflowName}`;
95
+ const responseJSON = await fetch(url, {
96
+ method: 'PUT',
97
+ headers: {
98
+ 'secret-key': secretKey,
99
+ 'Content-Type': 'application/json',
100
+ },
101
+ body: data,
119
102
  });
103
+ const response = await responseJSON.json();
104
+
105
+ const actionConfig = JSON.parse(data) as ActionConfig;
106
+ if (response?.errors) {
107
+ customLog.error(
108
+ `Invalid Parameters for Action ${actionConfig.name}: `,
109
+ ...response?.errors,
110
+ );
111
+ } else {
112
+ customLog.success(`Action ${actionConfig.name} Updated successfully`);
113
+ }
120
114
  });
121
- } catch (error: any) {
122
- customLog.error(error.message);
123
- }
115
+ });
124
116
  }
125
117
  }
126
118
 
@@ -0,0 +1,108 @@
1
+ import { MSTATE_URL } from '../common/constant';
2
+ import { customLog, getValueFromArgs } from '../common/utils';
3
+
4
+ enum Key {
5
+ PERMISSIONS = 'permissions=',
6
+ SECRET_KEY = 'secret=',
7
+ USER_ID = 'user=',
8
+ }
9
+
10
+ class CompanyHandler {
11
+ constructor() {}
12
+
13
+ async addToken() {
14
+ const args = process.argv;
15
+
16
+ const pString = getValueFromArgs(args, Key.PERMISSIONS).trim();
17
+ const permissions = pString.split(',');
18
+ const secretKey = getValueFromArgs(args, Key.SECRET_KEY);
19
+ const user = getValueFromArgs(args, Key.USER_ID);
20
+
21
+ if (!pString.length) {
22
+ customLog.changeAndThrow(`Parameter permissions is required`);
23
+ return;
24
+ }
25
+
26
+ if (!secretKey) {
27
+ customLog.changeAndThrow(`Parameter secret is required`);
28
+ return;
29
+ }
30
+
31
+ if (!user) {
32
+ customLog.changeAndThrow(`Parameter user is required`);
33
+ return;
34
+ }
35
+
36
+ const url = `${MSTATE_URL}/company/permission`;
37
+ const responseJSON = await fetch(url, {
38
+ method: 'POST',
39
+ headers: {
40
+ 'secret-key': secretKey,
41
+ 'Content-Type': 'application/json',
42
+ },
43
+ body: JSON.stringify({
44
+ user,
45
+ permissions,
46
+ }),
47
+ });
48
+
49
+ const response = await responseJSON.json();
50
+ if (response?.errors) {
51
+ customLog.changeAndThrow(
52
+ 'Invalid Parameters for Company: ',
53
+ ...response?.errors,
54
+ );
55
+ } else {
56
+ customLog.success('Company updated successfully \n', response?.data);
57
+ }
58
+ }
59
+
60
+ async revokePermission() {
61
+ const args = process.argv;
62
+
63
+ const pString = getValueFromArgs(args, Key.PERMISSIONS);
64
+ const permissions = pString.split(',');
65
+ const secretKey = getValueFromArgs(args, Key.SECRET_KEY);
66
+ const user = getValueFromArgs(args, Key.USER_ID);
67
+
68
+ if (!permissions.length) {
69
+ customLog.changeAndThrow(`Parameter permissions is required`);
70
+ return;
71
+ }
72
+
73
+ if (!secretKey) {
74
+ customLog.changeAndThrow(`Parameter secret is required`);
75
+ return;
76
+ }
77
+
78
+ if (!user) {
79
+ customLog.changeAndThrow(`Parameter user is required`);
80
+ return;
81
+ }
82
+
83
+ const url = `${MSTATE_URL}/company/permission`;
84
+ const responseJSON = await fetch(url, {
85
+ method: 'DELETE',
86
+ headers: {
87
+ 'secret-key': secretKey,
88
+ 'Content-Type': 'application/json',
89
+ },
90
+ body: JSON.stringify({
91
+ user,
92
+ permissions,
93
+ }),
94
+ });
95
+
96
+ const response = await responseJSON.json();
97
+ if (response?.errors) {
98
+ customLog.changeAndThrow(
99
+ 'Invalid Parameters for Company: ',
100
+ ...response?.errors,
101
+ );
102
+ } else {
103
+ customLog.success('Company updated successfully \n', response?.data);
104
+ }
105
+ }
106
+ }
107
+
108
+ export const companyHandler = new CompanyHandler();
@@ -12,97 +12,91 @@ class EnvironmentHandler {
12
12
  constructor() {}
13
13
 
14
14
  async cloneEnvironments() {
15
- try {
16
- const args = process.argv;
17
- const dirPath = getValueFromArgs(args, Key.WORKFLOW);
18
- const workflowName = dirPath.replace(/\//g, '__');
19
- const secretKey = getValueFromArgs(args, Key.SECRET_KEY);
20
-
21
- if (!secretKey) {
22
- customLog.error(`Parameter secret is required`);
23
- return;
24
- }
15
+ const args = process.argv;
16
+ const dirPath = getValueFromArgs(args, Key.WORKFLOW);
17
+ const workflowName = dirPath.replace(/\//g, '__');
18
+ const secretKey = getValueFromArgs(args, Key.SECRET_KEY);
19
+
20
+ if (!secretKey) {
21
+ customLog.changeAndThrow(`Parameter secret is required`);
22
+ return;
23
+ }
25
24
 
26
- if (!workflowName) {
27
- customLog.error(`Parameter workflow is required`);
28
- return;
29
- }
25
+ if (!workflowName) {
26
+ customLog.changeAndThrow(`Parameter workflow is required`);
27
+ return;
28
+ }
30
29
 
31
- try {
32
- const url = `${MSTATE_URL}/env/?workflow=${workflowName}`;
33
-
34
- const responseJSON = await fetch(url, {
35
- headers: {
36
- 'Content-Type': 'application/json',
37
- 'secret-key': secretKey,
38
- },
39
- });
40
-
41
- const response = await responseJSON.json();
42
- if (response?.errors) {
43
- customLog.error('Invalid Parameters: ', response?.errors);
44
- } else {
45
- const config = response?.data;
46
-
47
- const filePath = path.resolve(dirPath, 'environment.json');
48
- fs.writeFileSync(filePath, JSON.stringify(config, null, 2));
49
- }
50
- } catch (error: any) {
51
- customLog.error('Error in cloning action', error.message);
52
- }
53
- } catch (error: any) {
54
- customLog.error('Error in cloning action', error.message);
30
+ const url = `${MSTATE_URL}/env/?workflow=${workflowName}`;
31
+
32
+ const responseJSON = await fetch(url, {
33
+ headers: {
34
+ 'Content-Type': 'application/json',
35
+ 'secret-key': secretKey,
36
+ },
37
+ });
38
+
39
+ const response = await responseJSON.json();
40
+ if (response?.errors) {
41
+ customLog.changeAndThrow(
42
+ 'Invalid Parameters for Environment: ',
43
+ ...response?.errors,
44
+ );
45
+ } else {
46
+ const config = response?.data;
47
+
48
+ const filePath = path.resolve(dirPath, 'environment.json');
49
+ fs.writeFileSync(filePath, JSON.stringify(config, null, 2));
55
50
  }
56
51
  }
57
52
 
58
53
  async saveToDB() {
59
- try {
60
- const args = process.argv;
54
+ const args = process.argv;
61
55
 
62
- const dirPath = getValueFromArgs(args, Key.WORKFLOW);
63
- const workflowName = dirPath.replace(/\//g, '__');
64
- const secretKey = getValueFromArgs(args, Key.SECRET_KEY);
56
+ const dirPath = getValueFromArgs(args, Key.WORKFLOW);
57
+ const workflowName = dirPath.replace(/\//g, '__');
58
+ const secretKey = getValueFromArgs(args, Key.SECRET_KEY);
65
59
 
66
- if (!secretKey) {
67
- customLog.error(`Parameter secret is required`);
68
- return;
69
- }
60
+ if (!secretKey) {
61
+ customLog.changeAndThrow(`Parameter secret is required`);
62
+ return;
63
+ }
70
64
 
71
- if (!workflowName) {
72
- customLog.error(`Parameter workflow is required`);
73
- return;
74
- }
65
+ if (!workflowName) {
66
+ customLog.changeAndThrow(`Parameter workflow is required`);
67
+ return;
68
+ }
75
69
 
76
- const filePath = path.resolve(dirPath, 'environment.json');
70
+ const filePath = path.resolve(dirPath, 'environment.json');
71
+
72
+ const data = fs.readFileSync(filePath, 'utf8');
73
+ const environments = JSON.parse(data);
77
74
 
78
- const data = fs.readFileSync(filePath, 'utf8');
79
- const environments = JSON.parse(data);
80
-
81
- try {
82
- const url = `${MSTATE_URL}/env`;
83
- const responseJSON = await fetch(url, {
84
- method: 'POST',
85
- headers: {
86
- 'secret-key': secretKey,
87
- 'Content-Type': 'application/json',
88
- },
89
- body: JSON.stringify({
90
- workflow: workflowName,
91
- env: environments,
92
- }),
93
- });
94
-
95
- const response = await responseJSON.json();
96
- if (response?.errors) {
97
- customLog.error(`Updating environment:`, response?.errors);
98
- } else {
99
- customLog.success(`Environments Updated successfully`);
100
- }
101
- } catch (error: any) {
102
- customLog.error(error.message);
75
+ try {
76
+ const url = `${MSTATE_URL}/env`;
77
+ const responseJSON = await fetch(url, {
78
+ method: 'POST',
79
+ headers: {
80
+ 'secret-key': secretKey,
81
+ 'Content-Type': 'application/json',
82
+ },
83
+ body: JSON.stringify({
84
+ workflow: workflowName,
85
+ env: environments,
86
+ }),
87
+ });
88
+
89
+ const response = await responseJSON.json();
90
+ if (response?.errors) {
91
+ customLog.changeAndThrow(
92
+ `Invalid Parameters for Environment: `,
93
+ ...response?.errors,
94
+ );
95
+ } else {
96
+ customLog.success(`Environments Updated successfully`);
103
97
  }
104
- } catch (error) {
105
- customLog.error('Something went wrong');
98
+ } catch (error: any) {
99
+ customLog.changeAndThrow(error.message);
106
100
  }
107
101
  }
108
102
  }
@@ -0,0 +1,34 @@
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(CmdAction.PUSH, 'Update the changes one in cloned workflow');
15
+ customLog.info(
16
+ CmdAction.LINK,
17
+ 'add workflow and allow user to view workflow to mobioffice application',
18
+ );
19
+ customLog.info(
20
+ CmdAction.UNLINK,
21
+ 'add workflow and remove user to view workflow to mobioffice application',
22
+ );
23
+ customLog.info(
24
+ [CmdAction.VERSION_FLAG, CmdAction.VERSION].toString(),
25
+ 'add workflow and remove user to view workflow to mobioffice application',
26
+ );
27
+ }
28
+
29
+ logActionInfo(action: CmdAction) {
30
+ console.log({ action });
31
+ }
32
+ }
33
+
34
+ export const helpHandler = new HelpHandler();
@@ -1,9 +1,10 @@
1
- import { MOBIOFFICE_URL } from '../common/constant';
1
+ import { MOBIOFFICE_URL, MSTATE_URL } from '../common/constant';
2
2
  import { CmdAction } from '../common/enum';
3
3
  import { customLog, getValueFromArgs } from '../common/utils';
4
4
 
5
5
  enum Key {
6
6
  WORKFLOW = 'workflow=',
7
+ PERMISSIONS = 'permissions=',
7
8
  SECRET_KEY = 'secret=',
8
9
  COMPANY_ID = 'company=',
9
10
  USER_ID = 'user=',
@@ -13,59 +14,58 @@ class MobiofficeHandler {
13
14
  constructor() {}
14
15
 
15
16
  async linkToMobioffice(action: CmdAction.LINK | CmdAction.UNLINK) {
16
- try {
17
- const args = process.argv;
17
+ const args = process.argv;
18
18
 
19
- const dirPath = getValueFromArgs(args, Key.WORKFLOW);
20
- const workflowName = dirPath.replace(/\//g, '__');
21
- const secretKey = getValueFromArgs(args, Key.SECRET_KEY);
22
- const companyID = getValueFromArgs(args, Key.COMPANY_ID);
23
- const user = getValueFromArgs(args, Key.USER_ID);
19
+ const dirPath = getValueFromArgs(args, Key.WORKFLOW);
20
+ const workflowName = dirPath.replace(/\//g, '__');
21
+ const secretKey = getValueFromArgs(args, Key.SECRET_KEY);
22
+ const companyID = getValueFromArgs(args, Key.COMPANY_ID);
23
+ const user = getValueFromArgs(args, Key.USER_ID);
24
24
 
25
- if (!workflowName) {
26
- customLog.error(`Parameter workflow is required`);
27
- return;
28
- }
25
+ if (!workflowName) {
26
+ customLog.changeAndThrow(`Parameter workflow is required`);
27
+ return;
28
+ }
29
29
 
30
- if (!secretKey) {
31
- customLog.error(`Parameter secret is required`);
32
- return;
33
- }
30
+ if (!secretKey) {
31
+ customLog.changeAndThrow(`Parameter secret is required`);
32
+ return;
33
+ }
34
34
 
35
- if (!companyID) {
36
- customLog.error(`Parameter company is required`);
37
- return;
38
- }
35
+ if (!companyID) {
36
+ customLog.changeAndThrow(`Parameter company is required`);
37
+ return;
38
+ }
39
39
 
40
- if (!user) {
41
- customLog.error(`Parameter user is required`);
42
- return;
43
- }
40
+ if (!user) {
41
+ customLog.changeAndThrow(`Parameter user is required`);
42
+ return;
43
+ }
44
44
 
45
- const url = `${MOBIOFFICE_URL}/Mstate/workFlow/user`;
46
- const responseJSON = await fetch(url, {
47
- method: 'POST',
48
- headers: {
49
- 'secret-key': secretKey,
50
- 'Content-Type': 'application/json',
51
- },
52
- body: JSON.stringify({
53
- action: action.toUpperCase(),
54
- workflow: workflowName,
55
- secret: secretKey,
56
- companyID,
57
- user,
58
- }),
59
- });
45
+ const url = `${MOBIOFFICE_URL}/Mstate/workFlow/user`;
46
+ const responseJSON = await fetch(url, {
47
+ method: 'POST',
48
+ headers: {
49
+ 'secret-key': secretKey,
50
+ 'Content-Type': 'application/json',
51
+ },
52
+ body: JSON.stringify({
53
+ action: action.toUpperCase(),
54
+ workflow: workflowName,
55
+ secret: secretKey,
56
+ companyID,
57
+ user,
58
+ }),
59
+ });
60
60
 
61
- const response = await responseJSON.json();
62
- if (responseJSON.status >= 400) {
63
- customLog.error('Invalid Parameters: ', responseJSON.statusText);
64
- } else {
65
- customLog.success('Workflow Added successfully', response?.message);
66
- }
67
- } catch (error: any) {
68
- customLog.error(error.message);
61
+ const response = await responseJSON.json();
62
+ if (responseJSON.status >= 400) {
63
+ customLog.changeAndThrow(
64
+ 'Invalid Parameters for Linking: ',
65
+ responseJSON.statusText,
66
+ );
67
+ } else {
68
+ customLog.success('Workflow Added successfully', response?.message);
69
69
  }
70
70
  }
71
71
  }