mstate-cli 0.0.2 → 0.0.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.
- package/Readme.md +11 -5
- package/dist/common/constant.js +4 -3
- package/dist/common/enum.js +4 -1
- package/dist/common/utils.js +25 -0
- package/dist/handlers/action.handler.js +148 -0
- package/dist/handlers/environment.handler.js +139 -0
- package/dist/handlers/mobioffice.handler.js +77 -0
- package/dist/{actions/workflow.action.js → handlers/workflow.handler.js} +60 -52
- package/dist/index.js +30 -7
- package/package.json +1 -1
- package/src/common/constant.ts +3 -2
- package/src/common/enum.ts +4 -1
- package/src/common/utils.ts +27 -0
- package/src/handlers/action.handler.ts +126 -0
- package/src/handlers/environment.handler.ts +108 -0
- package/src/handlers/mobioffice.handler.ts +72 -0
- package/src/handlers/workflow.handler.ts +160 -0
- package/src/index.ts +41 -7
- package/src/interface.d.ts +1 -1
- package/dist/actions/publishWorkflow.js +0 -78
- package/src/actions/workflow.action.ts +0 -163
package/dist/index.js
CHANGED
@@ -1,21 +1,44 @@
|
|
1
1
|
#!/usr/bin/env node
|
2
2
|
"use strict";
|
3
3
|
Object.defineProperty(exports, "__esModule", { value: true });
|
4
|
-
const
|
4
|
+
const action_handler_1 = require("./handlers/action.handler");
|
5
|
+
const workflow_handler_1 = require("./handlers/workflow.handler");
|
5
6
|
const enum_1 = require("./common/enum");
|
6
|
-
const
|
7
|
+
const mobioffice_handler_1 = require("./handlers/mobioffice.handler");
|
8
|
+
const utils_1 = require("./common/utils");
|
9
|
+
const environment_handler_1 = require("./handlers/environment.handler");
|
10
|
+
const [action] = process.argv.slice(2);
|
7
11
|
switch (action) {
|
8
12
|
case enum_1.CmdAction.LINK:
|
13
|
+
case enum_1.CmdAction.UNLINK:
|
14
|
+
mobioffice_handler_1.mobiofficeHandler.linkToMobioffice(action);
|
9
15
|
break;
|
10
16
|
case enum_1.CmdAction.CLONE:
|
11
|
-
|
17
|
+
workflow_handler_1.workflowHandler.cloneWorkflow().then(() => {
|
18
|
+
action_handler_1.actionHandler.cloneActions();
|
19
|
+
environment_handler_1.environmentHandler.cloneEnvironments();
|
20
|
+
});
|
12
21
|
break;
|
13
22
|
case enum_1.CmdAction.ADD:
|
14
|
-
|
23
|
+
workflow_handler_1.workflowHandler.saveToDB();
|
15
24
|
break;
|
16
|
-
case enum_1.CmdAction.
|
17
|
-
|
25
|
+
case enum_1.CmdAction.PUSH:
|
26
|
+
workflow_handler_1.workflowHandler.updateWorkflowToDB().then(() => {
|
27
|
+
action_handler_1.actionHandler.saveToDB();
|
28
|
+
environment_handler_1.environmentHandler.saveToDB();
|
29
|
+
});
|
30
|
+
break;
|
31
|
+
case enum_1.CmdAction.HELP:
|
32
|
+
case enum_1.CmdAction.HELP_FLAG:
|
33
|
+
console.log('\n\nUsage: mstate [cmd] [parameter]="[value]"\n');
|
34
|
+
console.log('Commands:');
|
35
|
+
utils_1.customLog.info(enum_1.CmdAction.ADD, 'Add new workflow');
|
36
|
+
utils_1.customLog.info(enum_1.CmdAction.CLONE, 'Clone workflow with actions and environments');
|
37
|
+
utils_1.customLog.info(enum_1.CmdAction.PUSH, 'Update the changes one in cloned workflow');
|
38
|
+
utils_1.customLog.info(enum_1.CmdAction.LINK, 'add workflow and allow user to view workflow to mobioffice application');
|
39
|
+
utils_1.customLog.info(enum_1.CmdAction.UNLINK, 'add workflow and remove user to view workflow to mobioffice application');
|
18
40
|
break;
|
19
41
|
default:
|
20
|
-
|
42
|
+
utils_1.customLog.error(`Invalid script: "${action}"`);
|
43
|
+
console.log('use `mstate help, -h` for getting allowed action');
|
21
44
|
}
|
package/package.json
CHANGED
package/src/common/constant.ts
CHANGED
@@ -1,2 +1,3 @@
|
|
1
|
-
export const MSTATE_URL = 'https://dev.mstate.mobioffice.io/api';
|
2
|
-
|
1
|
+
// export const MSTATE_URL = 'https://dev.mstate.mobioffice.io/api';
|
2
|
+
export const MSTATE_URL = 'https://api.mstate.mobioffice.io/api';
|
3
|
+
export const MOBIOFFICE_URL = 'https://server.mobioffice.io/api';
|
package/src/common/enum.ts
CHANGED
@@ -0,0 +1,27 @@
|
|
1
|
+
export function getValueFromArgs(args: string[], key: string) {
|
2
|
+
return args.find((str) => str.includes(key))?.replace(key, '') ?? '';
|
3
|
+
}
|
4
|
+
|
5
|
+
export const customLog = {
|
6
|
+
error(...message: string[]) {
|
7
|
+
const RED_COLOR = '\x1b[31m'; // ANSI code for red
|
8
|
+
const RESET_COLOR = '\x1b[0m'; // ANSI code to reset to default color
|
9
|
+
|
10
|
+
console.error(`${RED_COLOR}[MSTATE][ERROR] ${RESET_COLOR}`, ...message);
|
11
|
+
},
|
12
|
+
|
13
|
+
success(...message: string[]) {
|
14
|
+
const GREEN_COLOR = '\x1b[32m'; // ANSI code for green
|
15
|
+
const RESET_COLOR = '\x1b[0m'; // ANSI code to reset to default color
|
16
|
+
|
17
|
+
console.log(`${GREEN_COLOR}[MSTATE] ${RESET_COLOR}`, ...message);
|
18
|
+
},
|
19
|
+
info(key: string, desc: string) {
|
20
|
+
const optionPadding = 30; // Define spacing width for the option column
|
21
|
+
const descriptionPadding = 60; // Define spacing width for the description column
|
22
|
+
console.log(
|
23
|
+
' ',
|
24
|
+
key.padEnd(optionPadding) + desc.padEnd(descriptionPadding),
|
25
|
+
);
|
26
|
+
},
|
27
|
+
};
|
@@ -0,0 +1,126 @@
|
|
1
|
+
import * as fs from 'fs';
|
2
|
+
import path from 'path';
|
3
|
+
import { MSTATE_URL } from '../common/constant';
|
4
|
+
import { customLog, getValueFromArgs } from '../common/utils';
|
5
|
+
|
6
|
+
enum Key {
|
7
|
+
WORKFLOW = 'workflow=',
|
8
|
+
SECRET_KEY = 'secret=',
|
9
|
+
}
|
10
|
+
|
11
|
+
class ActionHandler {
|
12
|
+
constructor() {}
|
13
|
+
|
14
|
+
async cloneActions() {
|
15
|
+
try {
|
16
|
+
const args = process.argv;
|
17
|
+
const workflowName = getValueFromArgs(args, Key.WORKFLOW);
|
18
|
+
const secretKey = getValueFromArgs(args, Key.SECRET_KEY);
|
19
|
+
|
20
|
+
if (!secretKey) {
|
21
|
+
customLog.error(`Parameter secret is required`);
|
22
|
+
return;
|
23
|
+
}
|
24
|
+
|
25
|
+
if (!workflowName) {
|
26
|
+
customLog.error(`Parameter workflow is required`);
|
27
|
+
return;
|
28
|
+
}
|
29
|
+
|
30
|
+
try {
|
31
|
+
const url = `${MSTATE_URL}/action/config/all/?workflow=${workflowName}`;
|
32
|
+
|
33
|
+
const responseJSON = await fetch(url, {
|
34
|
+
headers: {
|
35
|
+
'Content-Type': 'application/json',
|
36
|
+
'secret-key': secretKey,
|
37
|
+
},
|
38
|
+
});
|
39
|
+
|
40
|
+
const response = await responseJSON.json();
|
41
|
+
if (response?.errors) {
|
42
|
+
customLog.error('Invalid Parameters: ', response?.errors);
|
43
|
+
} else {
|
44
|
+
const actionConfigs = response?.data;
|
45
|
+
|
46
|
+
const folderPath = path.resolve(workflowName, 'actions');
|
47
|
+
fs.mkdirSync(folderPath, { recursive: true });
|
48
|
+
|
49
|
+
actionConfigs.forEach((config: ActionConfig) => {
|
50
|
+
const file = path.join(folderPath, `${config?.name}.json`);
|
51
|
+
fs.writeFileSync(file, JSON.stringify(config, null, 2));
|
52
|
+
|
53
|
+
customLog.success(
|
54
|
+
'file created at',
|
55
|
+
folderPath,
|
56
|
+
`${config?.name}.json`,
|
57
|
+
);
|
58
|
+
});
|
59
|
+
}
|
60
|
+
} catch (error: any) {
|
61
|
+
customLog.error('Error in cloning action', error.message);
|
62
|
+
}
|
63
|
+
} catch (error: any) {
|
64
|
+
customLog.error('Error in cloning action', error.message);
|
65
|
+
}
|
66
|
+
}
|
67
|
+
|
68
|
+
async saveToDB() {
|
69
|
+
try {
|
70
|
+
const args = process.argv;
|
71
|
+
|
72
|
+
const workflowName = getValueFromArgs(args, Key.WORKFLOW);
|
73
|
+
const secretKey = getValueFromArgs(args, Key.SECRET_KEY);
|
74
|
+
|
75
|
+
if (!secretKey) {
|
76
|
+
customLog.error(`Parameter secret is required`);
|
77
|
+
return;
|
78
|
+
}
|
79
|
+
|
80
|
+
if (!workflowName) {
|
81
|
+
customLog.error(`Parameter workflow is required`);
|
82
|
+
return;
|
83
|
+
}
|
84
|
+
|
85
|
+
const folderPath = path.resolve(workflowName, 'actions');
|
86
|
+
|
87
|
+
// loop to every folder to publish workflow
|
88
|
+
fs.readdir(folderPath, (err, files) => {
|
89
|
+
if (!files) return;
|
90
|
+
|
91
|
+
files.forEach(async (fileName) => {
|
92
|
+
try {
|
93
|
+
const filePath = path.join(folderPath, fileName);
|
94
|
+
const data = fs.readFileSync(filePath, 'utf8');
|
95
|
+
|
96
|
+
const url = `${MSTATE_URL}/action/config/?workflow=${workflowName}`;
|
97
|
+
const responseJSON = await fetch(url, {
|
98
|
+
method: 'PUT',
|
99
|
+
headers: {
|
100
|
+
'secret-key': secretKey,
|
101
|
+
'Content-Type': 'application/json',
|
102
|
+
},
|
103
|
+
body: data,
|
104
|
+
});
|
105
|
+
const response = await responseJSON.json();
|
106
|
+
|
107
|
+
if (response?.errors) {
|
108
|
+
customLog.error(
|
109
|
+
`Invalid Parameters for Action ${fileName}: `,
|
110
|
+
response?.errors,
|
111
|
+
);
|
112
|
+
} else {
|
113
|
+
customLog.success(`Action ${fileName} Updated successfully`);
|
114
|
+
}
|
115
|
+
} catch (error: any) {
|
116
|
+
customLog.error(error.message);
|
117
|
+
}
|
118
|
+
});
|
119
|
+
});
|
120
|
+
} catch (error: any) {
|
121
|
+
customLog.error(error.message);
|
122
|
+
}
|
123
|
+
}
|
124
|
+
}
|
125
|
+
|
126
|
+
export const actionHandler = new ActionHandler();
|
@@ -0,0 +1,108 @@
|
|
1
|
+
import * as fs from 'fs';
|
2
|
+
import path from 'path';
|
3
|
+
import { MSTATE_URL } from '../common/constant';
|
4
|
+
import { customLog, getValueFromArgs } from '../common/utils';
|
5
|
+
|
6
|
+
enum Key {
|
7
|
+
WORKFLOW = 'workflow=',
|
8
|
+
SECRET_KEY = 'secret=',
|
9
|
+
}
|
10
|
+
|
11
|
+
class EnvironmentHandler {
|
12
|
+
constructor() {}
|
13
|
+
|
14
|
+
async cloneEnvironments() {
|
15
|
+
try {
|
16
|
+
const args = process.argv;
|
17
|
+
const workflowName = getValueFromArgs(args, Key.WORKFLOW);
|
18
|
+
const secretKey = getValueFromArgs(args, Key.SECRET_KEY);
|
19
|
+
|
20
|
+
if (!secretKey) {
|
21
|
+
customLog.error(`Parameter secret is required`);
|
22
|
+
return;
|
23
|
+
}
|
24
|
+
|
25
|
+
if (!workflowName) {
|
26
|
+
customLog.error(`Parameter workflow is required`);
|
27
|
+
return;
|
28
|
+
}
|
29
|
+
|
30
|
+
try {
|
31
|
+
const url = `${MSTATE_URL}/env/?workflow=${workflowName}`;
|
32
|
+
|
33
|
+
const responseJSON = await fetch(url, {
|
34
|
+
headers: {
|
35
|
+
'Content-Type': 'application/json',
|
36
|
+
'secret-key': secretKey,
|
37
|
+
},
|
38
|
+
});
|
39
|
+
|
40
|
+
const response = await responseJSON.json();
|
41
|
+
if (response?.errors) {
|
42
|
+
customLog.error('Invalid Parameters: ', response?.errors);
|
43
|
+
} else {
|
44
|
+
const config = response?.data;
|
45
|
+
|
46
|
+
const filePath = path.resolve(workflowName, 'environment.json');
|
47
|
+
fs.writeFileSync(filePath, JSON.stringify(config, null, 2));
|
48
|
+
}
|
49
|
+
} catch (error: any) {
|
50
|
+
customLog.error('Error in cloning action', error.message);
|
51
|
+
}
|
52
|
+
} catch (error: any) {
|
53
|
+
customLog.error('Error in cloning action', error.message);
|
54
|
+
}
|
55
|
+
}
|
56
|
+
|
57
|
+
async saveToDB() {
|
58
|
+
try {
|
59
|
+
const args = process.argv;
|
60
|
+
|
61
|
+
const workflowName = getValueFromArgs(args, Key.WORKFLOW);
|
62
|
+
const secretKey = getValueFromArgs(args, Key.SECRET_KEY);
|
63
|
+
|
64
|
+
if (!secretKey) {
|
65
|
+
customLog.error(`Parameter secret is required`);
|
66
|
+
return;
|
67
|
+
}
|
68
|
+
|
69
|
+
if (!workflowName) {
|
70
|
+
customLog.error(`Parameter workflow is required`);
|
71
|
+
return;
|
72
|
+
}
|
73
|
+
|
74
|
+
const filePath = path.resolve(workflowName, 'environment.json');
|
75
|
+
|
76
|
+
const data = fs.readFileSync(filePath, 'utf8');
|
77
|
+
const environments = JSON.parse(data);
|
78
|
+
|
79
|
+
try {
|
80
|
+
const url = `${MSTATE_URL}/env`;
|
81
|
+
const responseJSON = await fetch(url, {
|
82
|
+
method: 'POST',
|
83
|
+
headers: {
|
84
|
+
'secret-key': secretKey,
|
85
|
+
'Content-Type': 'application/json',
|
86
|
+
},
|
87
|
+
body: JSON.stringify({
|
88
|
+
workflow: workflowName,
|
89
|
+
env: environments,
|
90
|
+
}),
|
91
|
+
});
|
92
|
+
|
93
|
+
const response = await responseJSON.json();
|
94
|
+
if (response?.errors) {
|
95
|
+
customLog.error(`Updating environment:`, response?.errors);
|
96
|
+
} else {
|
97
|
+
customLog.success(`Environments Updated successfully`);
|
98
|
+
}
|
99
|
+
} catch (error: any) {
|
100
|
+
customLog.error(error.message);
|
101
|
+
}
|
102
|
+
} catch (error) {
|
103
|
+
customLog.error('Something went wrong');
|
104
|
+
}
|
105
|
+
}
|
106
|
+
}
|
107
|
+
|
108
|
+
export const environmentHandler = new EnvironmentHandler();
|
@@ -0,0 +1,72 @@
|
|
1
|
+
import { MOBIOFFICE_URL } from '../common/constant';
|
2
|
+
import { CmdAction } from '../common/enum';
|
3
|
+
import { customLog, getValueFromArgs } from '../common/utils';
|
4
|
+
|
5
|
+
enum Key {
|
6
|
+
WORKFLOW = 'workflow=',
|
7
|
+
SECRET_KEY = 'secret=',
|
8
|
+
COMPANY_ID = 'company=',
|
9
|
+
USER_ID = 'user=',
|
10
|
+
}
|
11
|
+
|
12
|
+
class MobiofficeHandler {
|
13
|
+
constructor() {}
|
14
|
+
|
15
|
+
async linkToMobioffice(action: CmdAction.LINK | CmdAction.UNLINK) {
|
16
|
+
try {
|
17
|
+
const args = process.argv;
|
18
|
+
|
19
|
+
const workflowPath = getValueFromArgs(args, Key.WORKFLOW);
|
20
|
+
const secretKey = getValueFromArgs(args, Key.SECRET_KEY);
|
21
|
+
const companyID = getValueFromArgs(args, Key.COMPANY_ID);
|
22
|
+
const user = getValueFromArgs(args, Key.USER_ID);
|
23
|
+
|
24
|
+
if (!workflowPath) {
|
25
|
+
customLog.error(`Parameter workflow is required`);
|
26
|
+
return;
|
27
|
+
}
|
28
|
+
|
29
|
+
if (!secretKey) {
|
30
|
+
customLog.error(`Parameter secret is required`);
|
31
|
+
return;
|
32
|
+
}
|
33
|
+
|
34
|
+
if (!companyID) {
|
35
|
+
customLog.error(`Parameter company is required`);
|
36
|
+
return;
|
37
|
+
}
|
38
|
+
|
39
|
+
if (!user) {
|
40
|
+
customLog.error(`Parameter user is required`);
|
41
|
+
return;
|
42
|
+
}
|
43
|
+
|
44
|
+
const url = `${MOBIOFFICE_URL}/Mstate/workFlow/user`;
|
45
|
+
const responseJSON = await fetch(url, {
|
46
|
+
method: 'POST',
|
47
|
+
headers: {
|
48
|
+
'secret-key': secretKey,
|
49
|
+
'Content-Type': 'application/json',
|
50
|
+
},
|
51
|
+
body: JSON.stringify({
|
52
|
+
action: action.toUpperCase(),
|
53
|
+
workflow: workflowPath,
|
54
|
+
secret: secretKey,
|
55
|
+
companyID,
|
56
|
+
user,
|
57
|
+
}),
|
58
|
+
});
|
59
|
+
|
60
|
+
const response = await responseJSON.json();
|
61
|
+
if (responseJSON.status >= 400) {
|
62
|
+
customLog.error('Invalid Parameters: ', responseJSON.statusText);
|
63
|
+
} else {
|
64
|
+
customLog.success('Workflow Added successfully', response?.message);
|
65
|
+
}
|
66
|
+
} catch (error: any) {
|
67
|
+
customLog.error(error.message);
|
68
|
+
}
|
69
|
+
}
|
70
|
+
}
|
71
|
+
|
72
|
+
export const mobiofficeHandler = new MobiofficeHandler();
|
@@ -0,0 +1,160 @@
|
|
1
|
+
import * as fs from 'fs';
|
2
|
+
import path from 'path';
|
3
|
+
import { MSTATE_URL } from '../common/constant';
|
4
|
+
import { customLog, getValueFromArgs } from '../common/utils';
|
5
|
+
|
6
|
+
enum Key {
|
7
|
+
PATH = 'path=',
|
8
|
+
FILE = 'file=',
|
9
|
+
SECRET_KEY = 'secret=',
|
10
|
+
WORKFLOW = 'workflow=',
|
11
|
+
}
|
12
|
+
|
13
|
+
class WorkflowHandler {
|
14
|
+
constructor() {}
|
15
|
+
|
16
|
+
async saveToDB() {
|
17
|
+
try {
|
18
|
+
const args = process.argv;
|
19
|
+
|
20
|
+
const workflowPath = getValueFromArgs(args, Key.PATH);
|
21
|
+
const file = getValueFromArgs(args, Key.FILE);
|
22
|
+
const secretKey = getValueFromArgs(args, Key.SECRET_KEY);
|
23
|
+
|
24
|
+
const filePath = path.resolve(file);
|
25
|
+
const workflowJSON = fs.readFileSync(filePath, 'utf8');
|
26
|
+
|
27
|
+
if (!file) {
|
28
|
+
customLog.error(`Parameter file is required`);
|
29
|
+
return;
|
30
|
+
}
|
31
|
+
|
32
|
+
if (!secretKey) {
|
33
|
+
customLog.error(`Parameter secret is required`);
|
34
|
+
return;
|
35
|
+
}
|
36
|
+
|
37
|
+
let query = '';
|
38
|
+
if (workflowPath) {
|
39
|
+
query += `path=${workflowPath}`;
|
40
|
+
}
|
41
|
+
|
42
|
+
if (query.length) {
|
43
|
+
query = '?' + query;
|
44
|
+
}
|
45
|
+
|
46
|
+
const url = `${MSTATE_URL}/workflow/config/new${query}`;
|
47
|
+
const responseJSON = await fetch(url, {
|
48
|
+
method: 'POST',
|
49
|
+
headers: {
|
50
|
+
'secret-key': secretKey,
|
51
|
+
'Content-Type': 'application/json',
|
52
|
+
},
|
53
|
+
body: workflowJSON,
|
54
|
+
});
|
55
|
+
|
56
|
+
const response = await responseJSON.json();
|
57
|
+
if (response?.errors) {
|
58
|
+
customLog.error('Invalid Parameters: ', response?.errors);
|
59
|
+
} else {
|
60
|
+
customLog.success('Workflow Added successfully', response?.data);
|
61
|
+
}
|
62
|
+
} catch (error: any) {
|
63
|
+
customLog.error(error.message);
|
64
|
+
}
|
65
|
+
}
|
66
|
+
|
67
|
+
async updateWorkflowToDB() {
|
68
|
+
try {
|
69
|
+
const args = process.argv;
|
70
|
+
|
71
|
+
const workflow = getValueFromArgs(args, Key.WORKFLOW);
|
72
|
+
const secretKey = getValueFromArgs(args, Key.SECRET_KEY);
|
73
|
+
|
74
|
+
if (!secretKey) {
|
75
|
+
customLog.error(`Parameter secret is required`);
|
76
|
+
return;
|
77
|
+
}
|
78
|
+
|
79
|
+
if (!workflow) {
|
80
|
+
customLog.error(`Parameter workflow is required`);
|
81
|
+
return;
|
82
|
+
}
|
83
|
+
|
84
|
+
const folderPath = path.resolve(workflow);
|
85
|
+
const filePath = path.join(folderPath, 'workflow.json');
|
86
|
+
|
87
|
+
const workflowJSON = fs.readFileSync(filePath, 'utf8');
|
88
|
+
|
89
|
+
let query = `?workflow=${workflow}`;
|
90
|
+
|
91
|
+
const url = `${MSTATE_URL}/workflow/config${query}`;
|
92
|
+
const responseJSON = await fetch(url, {
|
93
|
+
method: 'PUT',
|
94
|
+
headers: {
|
95
|
+
'secret-key': secretKey,
|
96
|
+
'Content-Type': 'application/json',
|
97
|
+
},
|
98
|
+
body: workflowJSON,
|
99
|
+
});
|
100
|
+
|
101
|
+
const response = await responseJSON.json();
|
102
|
+
if (response?.errors) {
|
103
|
+
customLog.error('Invalid Parameters: ', response?.errors);
|
104
|
+
} else {
|
105
|
+
customLog.success('Workflow Uploaded successfully \n', response?.data);
|
106
|
+
}
|
107
|
+
|
108
|
+
return response;
|
109
|
+
} catch (error: any) {
|
110
|
+
customLog.error(error.message);
|
111
|
+
}
|
112
|
+
}
|
113
|
+
|
114
|
+
async cloneWorkflow() {
|
115
|
+
try {
|
116
|
+
const args = process.argv;
|
117
|
+
|
118
|
+
const workflowName = getValueFromArgs(args, Key.WORKFLOW);
|
119
|
+
const secretKey = getValueFromArgs(args, Key.SECRET_KEY);
|
120
|
+
|
121
|
+
if (!secretKey) {
|
122
|
+
customLog.error(`Parameter secret is required`);
|
123
|
+
return;
|
124
|
+
}
|
125
|
+
|
126
|
+
if (!workflowName) {
|
127
|
+
customLog.error(`Parameter workflow is required`);
|
128
|
+
return;
|
129
|
+
}
|
130
|
+
|
131
|
+
const url = `${MSTATE_URL}/workflow/config/${workflowName}`;
|
132
|
+
const responseJSON = await fetch(url, {
|
133
|
+
method: 'GET',
|
134
|
+
headers: {
|
135
|
+
'Content-Type': 'application/json',
|
136
|
+
'secret-key': secretKey,
|
137
|
+
},
|
138
|
+
});
|
139
|
+
|
140
|
+
const response = await responseJSON.json();
|
141
|
+
if (response?.errors) {
|
142
|
+
customLog.error('Invalid Parameters: ', response?.errors);
|
143
|
+
} else {
|
144
|
+
const workflowConfig = response?.data;
|
145
|
+
|
146
|
+
const folderPath = path.resolve(workflowName);
|
147
|
+
fs.mkdirSync(folderPath, { recursive: true });
|
148
|
+
|
149
|
+
const filePath = path.join(folderPath, 'workflow.json');
|
150
|
+
fs.writeFileSync(filePath, JSON.stringify(workflowConfig, null, 2));
|
151
|
+
|
152
|
+
customLog.success(`Workflow "${workflowName}" is cloned successfully`);
|
153
|
+
}
|
154
|
+
} catch (error: any) {
|
155
|
+
customLog.error('Error in cloning workflow', error.message);
|
156
|
+
}
|
157
|
+
}
|
158
|
+
}
|
159
|
+
|
160
|
+
export const workflowHandler = new WorkflowHandler();
|
package/src/index.ts
CHANGED
@@ -1,23 +1,57 @@
|
|
1
1
|
#!/usr/bin/env node
|
2
2
|
|
3
|
-
import {
|
3
|
+
import { actionHandler } from './handlers/action.handler';
|
4
|
+
import { workflowHandler } from './handlers/workflow.handler';
|
4
5
|
import { CmdAction } from './common/enum';
|
6
|
+
import { mobiofficeHandler } from './handlers/mobioffice.handler';
|
7
|
+
import { customLog } from './common/utils';
|
8
|
+
import { environmentHandler } from './handlers/environment.handler';
|
5
9
|
|
6
|
-
const [action
|
10
|
+
const [action] = process.argv.slice(2);
|
7
11
|
|
8
12
|
switch (action as CmdAction) {
|
9
13
|
case CmdAction.LINK:
|
14
|
+
case CmdAction.UNLINK:
|
15
|
+
mobiofficeHandler.linkToMobioffice(
|
16
|
+
action as CmdAction.LINK | CmdAction.UNLINK,
|
17
|
+
);
|
10
18
|
break;
|
11
19
|
case CmdAction.CLONE:
|
12
|
-
|
20
|
+
workflowHandler.cloneWorkflow().then(() => {
|
21
|
+
actionHandler.cloneActions();
|
22
|
+
environmentHandler.cloneEnvironments();
|
23
|
+
});
|
13
24
|
break;
|
14
25
|
case CmdAction.ADD:
|
15
|
-
|
26
|
+
workflowHandler.saveToDB();
|
16
27
|
break;
|
17
|
-
case CmdAction.
|
18
|
-
|
28
|
+
case CmdAction.PUSH:
|
29
|
+
workflowHandler.updateWorkflowToDB().then(() => {
|
30
|
+
actionHandler.saveToDB();
|
31
|
+
environmentHandler.saveToDB();
|
32
|
+
});
|
19
33
|
break;
|
34
|
+
case CmdAction.HELP:
|
35
|
+
case CmdAction.HELP_FLAG:
|
36
|
+
console.log('\n\nUsage: mstate [cmd] [parameter]="[value]"\n');
|
20
37
|
|
38
|
+
console.log('Commands:');
|
39
|
+
customLog.info(CmdAction.ADD, 'Add new workflow');
|
40
|
+
customLog.info(
|
41
|
+
CmdAction.CLONE,
|
42
|
+
'Clone workflow with actions and environments',
|
43
|
+
);
|
44
|
+
customLog.info(CmdAction.PUSH, 'Update the changes one in cloned workflow');
|
45
|
+
customLog.info(
|
46
|
+
CmdAction.LINK,
|
47
|
+
'add workflow and allow user to view workflow to mobioffice application',
|
48
|
+
);
|
49
|
+
customLog.info(
|
50
|
+
CmdAction.UNLINK,
|
51
|
+
'add workflow and remove user to view workflow to mobioffice application',
|
52
|
+
);
|
53
|
+
break;
|
21
54
|
default:
|
22
|
-
|
55
|
+
customLog.error(`Invalid script: "${action}"`);
|
56
|
+
console.log('use `mstate help, -h` for getting allowed action');
|
23
57
|
}
|