mstate-cli 0.4.2 → 0.4.4

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,77 +1,77 @@
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 EnvironmentHandler {
9
- constructor() {}
10
-
11
- async cloneEnvironments() {
12
- const workflowName = getValueFromArgs(Variables.WORKFLOW, true);
13
- const secretKey = getSecretKey();
14
-
15
- let query = `?workflow=${workflowName}`;
16
- const url = `${MSTATE_URL}/env/${query}`;
17
-
18
- const responseJSON = await fetch(url, {
19
- headers: {
20
- 'Content-Type': 'application/json',
21
- 'secret-key': secretKey,
22
- },
23
- });
24
-
25
- const response = await responseJSON.json();
26
- if (response?.errors) {
27
- customLog.changeAndThrow(
28
- 'Invalid Parameters for Environment: ',
29
- ...response?.errors,
30
- );
31
- } else {
32
- const config = response?.data;
33
-
34
- const filePath = path.resolve(workflowName, 'environment.json');
35
- fs.writeFileSync(filePath, JSON.stringify(config, null, 2));
36
- }
37
- }
38
-
39
- async saveToDB() {
40
- const workflowName = getValueFromArgs(Variables.WORKFLOW, true);
41
- const secretKey = getSecretKey();
42
-
43
- const filePath = path.resolve(workflowName, 'environment.json');
44
-
45
- const data = fs.readFileSync(filePath, 'utf8');
46
- const environments = JSON.parse(data);
47
-
48
- try {
49
- const url = `${MSTATE_URL}/env`;
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
- workflow: workflowName,
58
- env: environments,
59
- }),
60
- });
61
-
62
- const response = await responseJSON.json();
63
- if (response?.errors) {
64
- customLog.changeAndThrow(
65
- `Invalid Parameters for Environment: `,
66
- ...response?.errors,
67
- );
68
- } else {
69
- customLog.success(`Environments Updated successfully`);
70
- }
71
- } catch (error: any) {
72
- customLog.changeAndThrow(error.message);
73
- }
74
- }
75
- }
76
-
77
- export const environmentHandler = new EnvironmentHandler();
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 EnvironmentHandler {
9
+ constructor() {}
10
+
11
+ async cloneEnvironments() {
12
+ const workflowName = getValueFromArgs(Variables.WORKFLOW, true);
13
+ const secretKey = getSecretKey();
14
+
15
+ let query = `?workflow=${workflowName}&hasNickName=${true}`;
16
+ const url = `${MSTATE_URL}/env/${query}`;
17
+
18
+ const responseJSON = await fetch(url, {
19
+ headers: {
20
+ 'Content-Type': 'application/json',
21
+ 'secret-key': secretKey,
22
+ },
23
+ });
24
+
25
+ const response = await responseJSON.json();
26
+ if (response?.errors) {
27
+ customLog.changeAndThrow(
28
+ 'Invalid Parameters for Environment: ',
29
+ ...response?.errors,
30
+ );
31
+ } else {
32
+ const config = response?.data;
33
+
34
+ const filePath = path.resolve(workflowName, 'environment.json');
35
+ fs.writeFileSync(filePath, JSON.stringify(config, null, 2));
36
+ }
37
+ }
38
+
39
+ async saveToDB() {
40
+ const workflowName = getValueFromArgs(Variables.WORKFLOW, true);
41
+ const secretKey = getSecretKey();
42
+
43
+ const filePath = path.resolve(workflowName, 'environment.json');
44
+
45
+ const data = fs.readFileSync(filePath, 'utf8');
46
+ const environments = JSON.parse(data);
47
+
48
+ try {
49
+ const url = `${MSTATE_URL}/env`;
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
+ workflow: workflowName,
58
+ env: environments,
59
+ }),
60
+ });
61
+
62
+ const response = await responseJSON.json();
63
+ if (response?.errors) {
64
+ customLog.changeAndThrow(
65
+ `Invalid Parameters for Environment: `,
66
+ ...response?.errors,
67
+ );
68
+ } else {
69
+ customLog.success(`Environments Updated successfully`);
70
+ }
71
+ } catch (error: any) {
72
+ customLog.changeAndThrow(error.message);
73
+ }
74
+ }
75
+ }
76
+
77
+ export const environmentHandler = new EnvironmentHandler();
@@ -1,81 +1,122 @@
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.LOGIN, 'Login in your terminal using browser');
10
- customLog.info(CmdAction.ADD, 'Create a new workflow');
11
- customLog.info(
12
- CmdAction.CLONE,
13
- 'Clone workflow with actions and environments',
14
- );
15
- customLog.info(CmdAction.PUSH, 'Update the changes one in cloned workflow');
16
- customLog.info(CmdAction.ADD_USER, 'Add new user to your company');
17
- customLog.info(
18
- CmdAction.LINK,
19
- 'Add workflow and allow user to view workflow in the mobioffice application',
20
- );
21
- customLog.info(
22
- CmdAction.UNLINK,
23
- 'Disallow user to view workflow in the mobioffice application',
24
- );
25
- customLog.info(
26
- [CmdAction.VERSION_FLAG, CmdAction.VERSION].toString(),
27
- 'print mstate-cli version',
28
- );
29
- }
30
-
31
- logActionInfo(action: CmdAction) {
32
- console.log('Used Arguments: ');
33
-
34
- switch (action) {
35
- case CmdAction.LOGIN:
36
- break;
37
-
38
- case CmdAction.ADD:
39
- case CmdAction.PUSH:
40
- case CmdAction.CLONE:
41
- customLog.info(
42
- Variables.WORKFLOW,
43
- 'Specifying workflow name with path',
44
- );
45
- break;
46
-
47
- case CmdAction.LINK:
48
- case CmdAction.UNLINK:
49
- customLog.info(
50
- Variables.WORKFLOW,
51
- 'Specifying workflow name with path',
52
- );
53
- customLog.info(Variables.COMPANY_ID, 'Specifying company nickname');
54
- customLog.info(Variables.USER_ID, 'Specifying user phone number');
55
- break;
56
-
57
- case CmdAction.ADD_USER:
58
- customLog.info(
59
- Variables.USER_ID,
60
- 'Specifying user unique name for the company',
61
- );
62
- customLog.info(Variables.EMAIL, 'Specifying user email');
63
- customLog.info(Variables.PHONE, 'Specifying user phone number');
64
- break;
65
- default:
66
- customLog.error(
67
- `${action ? 'Invalid' : 'Missing'} command: ${action ?? ''}`,
68
- );
69
- console.log(
70
- `\t\t use 'mstate ${CmdAction.HELP_FLAG}, ${CmdAction.HELP}' for getting allowed action`,
71
- );
72
- }
73
-
74
- customLog.info(
75
- Variables.SECRET,
76
- 'Specifying token, If you want to use other token for this command instead of saved one',
77
- );
78
- }
79
- }
80
-
81
- export const helpHandler = new HelpHandler();
1
+ import fs from 'fs';
2
+ import { CmdAction, Variables } from '../common/enum';
3
+ import { customLog } from '../common/utils';
4
+ import { getValueFromArgs } from 'src/common/helpers';
5
+ import path from 'path';
6
+ import { argv } from 'process';
7
+
8
+ class HelpHandler {
9
+ logCommandInfo() {
10
+ console.log('\n\nUsage: mstate <command> [arguments] \n');
11
+
12
+ console.log('Commands:');
13
+ customLog.info(CmdAction.LOGIN, 'Login in your terminal using browser');
14
+ customLog.info(CmdAction.ADD, 'Create a new workflow');
15
+ customLog.info(
16
+ CmdAction.CLONE,
17
+ 'Clone workflow with actions and environments',
18
+ );
19
+ customLog.info(CmdAction.PUSH, 'Update the changes one in cloned workflow');
20
+ customLog.info(CmdAction.ADD_USER, 'Add new user to your company');
21
+ customLog.info(
22
+ CmdAction.LINK,
23
+ 'Add workflow and allow user to view workflow in the mobioffice application',
24
+ );
25
+ customLog.info(
26
+ CmdAction.UNLINK,
27
+ 'Disallow user to view workflow in the mobioffice application',
28
+ );
29
+ customLog.info(
30
+ [CmdAction.VERSION_FLAG, CmdAction.VERSION].toString(),
31
+ 'print mstate-cli version',
32
+ );
33
+ }
34
+
35
+ logActionInfo(action: CmdAction) {
36
+ console.log('Used Arguments: ');
37
+
38
+ switch (action) {
39
+ case CmdAction.LOGIN:
40
+ break;
41
+
42
+ case CmdAction.ADD:
43
+ case CmdAction.PUSH:
44
+ case CmdAction.CLONE:
45
+ customLog.info(
46
+ Variables.WORKFLOW,
47
+ 'Specifying workflow name with path',
48
+ );
49
+ break;
50
+
51
+ case CmdAction.LINK:
52
+ case CmdAction.UNLINK:
53
+ customLog.info(
54
+ Variables.WORKFLOW,
55
+ 'Specifying workflow name with path',
56
+ );
57
+ customLog.info(Variables.COMPANY_ID, 'Specifying company nickname');
58
+ customLog.info(Variables.USER_ID, 'Specifying user phone number');
59
+ break;
60
+
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
+ customLog.info(
79
+ Variables.SECRET,
80
+ 'Specifying token, If you want to use other token for this command instead of saved one',
81
+ );
82
+ }
83
+
84
+ createInfoFiles() {
85
+ const workflowName = getValueFromArgs(Variables.WORKFLOW, true);
86
+ const folderPath = path.resolve(workflowName, '.project');
87
+ fs.mkdirSync(folderPath, { recursive: true });
88
+
89
+ const filePath = path.resolve(folderPath, 'file.json');
90
+ fs.writeFileSync(filePath, JSON.stringify({ path: workflowName }, null, 2));
91
+ }
92
+
93
+ setWorkflowPath(action: CmdAction) {
94
+ const allowedCmds = [
95
+ CmdAction.PUSH,
96
+ CmdAction.ADD,
97
+ CmdAction.UNLINK,
98
+ CmdAction.LINK,
99
+ ];
100
+
101
+ if (allowedCmds.includes(action) === false) {
102
+ return;
103
+ }
104
+
105
+ const workflowName = getValueFromArgs(Variables.WORKFLOW, false);
106
+
107
+ try {
108
+ const filePath = path.resolve('.project', 'file.json');
109
+ const data = fs.readFileSync(filePath, 'utf8');
110
+ const files = JSON.parse(data ?? '');
111
+
112
+ console.log({ path: files.path });
113
+ if (!workflowName) {
114
+ argv.push(Variables.SAVED_VALUE, files.path);
115
+ }
116
+ } catch (e) {
117
+ console.log(e);
118
+ }
119
+ }
120
+ }
121
+
122
+ export const helpHandler = new HelpHandler();
@@ -1,43 +1,43 @@
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
+ 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();