mstate-cli 0.0.2 → 0.1.0

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 CHANGED
@@ -2,20 +2,26 @@
2
2
 
3
3
  ## Commands
4
4
 
5
- - clone
5
+ ### add
6
+
7
+ ```cmd
8
+ mstate add secret="secret-key" file="json-file-path" path="path-to-workflow"
9
+ ```
10
+
11
+ ### clone
6
12
 
7
13
  ```cmd
8
14
  mstate clone secret="secret-key" workflow="workflow-path__workflow-name"
9
15
  ```
10
16
 
11
- - add
17
+ ### push
12
18
 
13
19
  ```cmd
14
- mstate add path/to/json secret="secret-key" path="flow-path"
20
+ mstate push secret="secret-key" workflow="workflow-path__workflow-name"
15
21
  ```
16
22
 
17
- - update
23
+ ### link and unlink
18
24
 
19
25
  ```cmd
20
- mstate update path/to/json secret="secret-key" path="flow-path"
26
+ mstate [link | unlink] secret="secret-key" workflow="workflow-path__workflow-name" user="user-id" company="company-id"
21
27
  ```
@@ -1,5 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.MSTATE_URL = void 0;
3
+ exports.MOBIOFFICE_URL = exports.MSTATE_URL = void 0;
4
4
  exports.MSTATE_URL = 'https://dev.mstate.mobioffice.io/api';
5
- // export const MSTATE_URL = "https://api.mstate.mobioffice.io/api";
5
+ // export const MSTATE_URL = 'https://api.mstate.mobioffice.io/api';
6
+ exports.MOBIOFFICE_URL = 'https://server.mobioffice.io/api';
@@ -4,7 +4,10 @@ exports.CmdAction = void 0;
4
4
  var CmdAction;
5
5
  (function (CmdAction) {
6
6
  CmdAction["LINK"] = "link";
7
+ CmdAction["UNLINK"] = "unlink";
7
8
  CmdAction["ADD"] = "add";
8
9
  CmdAction["CLONE"] = "clone";
9
- CmdAction["UPDATE"] = "update";
10
+ CmdAction["PUSH"] = "push";
11
+ CmdAction["HELP"] = "help";
12
+ CmdAction["HELP_FLAG"] = "-h";
10
13
  })(CmdAction || (exports.CmdAction = CmdAction = {}));
@@ -0,0 +1,25 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.customLog = void 0;
4
+ exports.getValueFromArgs = getValueFromArgs;
5
+ function getValueFromArgs(args, key) {
6
+ var _a, _b;
7
+ return (_b = (_a = args.find((str) => str.includes(key))) === null || _a === void 0 ? void 0 : _a.replace(key, '')) !== null && _b !== void 0 ? _b : '';
8
+ }
9
+ exports.customLog = {
10
+ error(...message) {
11
+ const RED_COLOR = '\x1b[31m'; // ANSI code for red
12
+ const RESET_COLOR = '\x1b[0m'; // ANSI code to reset to default color
13
+ console.error(`${RED_COLOR}[MSTATE][ERROR] ${RESET_COLOR}`, ...message);
14
+ },
15
+ success(...message) {
16
+ const GREEN_COLOR = '\x1b[32m'; // ANSI code for green
17
+ const RESET_COLOR = '\x1b[0m'; // ANSI code to reset to default color
18
+ console.log(`${GREEN_COLOR}[MSTATE] ${RESET_COLOR}`, ...message);
19
+ },
20
+ info(key, desc) {
21
+ const optionPadding = 30; // Define spacing width for the option column
22
+ const descriptionPadding = 60; // Define spacing width for the description column
23
+ console.log(' ', key.padEnd(optionPadding) + desc.padEnd(descriptionPadding));
24
+ },
25
+ };
@@ -0,0 +1,150 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || function (mod) {
19
+ if (mod && mod.__esModule) return mod;
20
+ var result = {};
21
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
22
+ __setModuleDefault(result, mod);
23
+ return result;
24
+ };
25
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
26
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
27
+ return new (P || (P = Promise))(function (resolve, reject) {
28
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
29
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
30
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
31
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
32
+ });
33
+ };
34
+ var __importDefault = (this && this.__importDefault) || function (mod) {
35
+ return (mod && mod.__esModule) ? mod : { "default": mod };
36
+ };
37
+ Object.defineProperty(exports, "__esModule", { value: true });
38
+ exports.actionHandler = void 0;
39
+ const fs = __importStar(require("fs"));
40
+ const path_1 = __importDefault(require("path"));
41
+ const constant_1 = require("../common/constant");
42
+ const utils_1 = require("../common/utils");
43
+ var Key;
44
+ (function (Key) {
45
+ Key["WORKFLOW"] = "workflow=";
46
+ Key["SECRET_KEY"] = "secret=";
47
+ })(Key || (Key = {}));
48
+ class ActionHandler {
49
+ constructor() { }
50
+ cloneActions() {
51
+ return __awaiter(this, void 0, void 0, function* () {
52
+ try {
53
+ const args = process.argv;
54
+ const dirPath = (0, utils_1.getValueFromArgs)(args, Key.WORKFLOW);
55
+ const workflowName = dirPath.replace(/\//g, '__');
56
+ const secretKey = (0, utils_1.getValueFromArgs)(args, Key.SECRET_KEY);
57
+ if (!secretKey) {
58
+ utils_1.customLog.error(`Parameter secret is required`);
59
+ return;
60
+ }
61
+ if (!workflowName) {
62
+ utils_1.customLog.error(`Parameter workflow is required`);
63
+ return;
64
+ }
65
+ try {
66
+ const url = `${constant_1.MSTATE_URL}/action/config/all/?workflow=${workflowName}`;
67
+ const responseJSON = yield fetch(url, {
68
+ headers: {
69
+ 'Content-Type': 'application/json',
70
+ 'secret-key': secretKey,
71
+ },
72
+ });
73
+ const response = yield responseJSON.json();
74
+ if (response === null || response === void 0 ? void 0 : response.errors) {
75
+ utils_1.customLog.error('Invalid Parameters: ', response === null || response === void 0 ? void 0 : response.errors);
76
+ }
77
+ else {
78
+ const actionConfigs = response === null || response === void 0 ? void 0 : response.data;
79
+ const folderPath = path_1.default.resolve(dirPath, 'actions');
80
+ fs.mkdirSync(folderPath, { recursive: true });
81
+ actionConfigs.forEach((config) => {
82
+ const file = path_1.default.join(folderPath, `${config === null || config === void 0 ? void 0 : config.name}.json`);
83
+ fs.writeFileSync(file, JSON.stringify(config, null, 2));
84
+ utils_1.customLog.success('file created at', folderPath, `${config === null || config === void 0 ? void 0 : config.name}.json`);
85
+ });
86
+ }
87
+ }
88
+ catch (error) {
89
+ utils_1.customLog.error('Error in cloning action', error.message);
90
+ }
91
+ }
92
+ catch (error) {
93
+ utils_1.customLog.error('Error in cloning action', error.message);
94
+ }
95
+ });
96
+ }
97
+ saveToDB() {
98
+ return __awaiter(this, void 0, void 0, function* () {
99
+ try {
100
+ const args = process.argv;
101
+ const dirPath = (0, utils_1.getValueFromArgs)(args, Key.WORKFLOW);
102
+ const workflowName = dirPath.replace(/\//g, '__');
103
+ const secretKey = (0, utils_1.getValueFromArgs)(args, Key.SECRET_KEY);
104
+ if (!secretKey) {
105
+ utils_1.customLog.error(`Parameter secret is required`);
106
+ return;
107
+ }
108
+ if (!workflowName) {
109
+ utils_1.customLog.error(`Parameter workflow is required`);
110
+ return;
111
+ }
112
+ const folderPath = path_1.default.resolve(dirPath, 'actions');
113
+ // loop to every folder to publish workflow
114
+ fs.readdir(folderPath, (err, files) => {
115
+ if (!files)
116
+ return;
117
+ files.forEach((fileName) => __awaiter(this, void 0, void 0, function* () {
118
+ try {
119
+ const filePath = path_1.default.join(folderPath, fileName);
120
+ const data = fs.readFileSync(filePath, 'utf8');
121
+ const url = `${constant_1.MSTATE_URL}/action/config/?workflow=${workflowName}`;
122
+ const responseJSON = yield fetch(url, {
123
+ method: 'PUT',
124
+ headers: {
125
+ 'secret-key': secretKey,
126
+ 'Content-Type': 'application/json',
127
+ },
128
+ body: data,
129
+ });
130
+ const response = yield responseJSON.json();
131
+ if (response === null || response === void 0 ? void 0 : response.errors) {
132
+ utils_1.customLog.error(`Invalid Parameters for Action ${fileName}: `, response === null || response === void 0 ? void 0 : response.errors);
133
+ }
134
+ else {
135
+ utils_1.customLog.success(`Action ${fileName} Updated successfully`);
136
+ }
137
+ }
138
+ catch (error) {
139
+ utils_1.customLog.error(error.message);
140
+ }
141
+ }));
142
+ });
143
+ }
144
+ catch (error) {
145
+ utils_1.customLog.error(error.message);
146
+ }
147
+ });
148
+ }
149
+ }
150
+ exports.actionHandler = new ActionHandler();
@@ -0,0 +1,141 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || function (mod) {
19
+ if (mod && mod.__esModule) return mod;
20
+ var result = {};
21
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
22
+ __setModuleDefault(result, mod);
23
+ return result;
24
+ };
25
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
26
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
27
+ return new (P || (P = Promise))(function (resolve, reject) {
28
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
29
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
30
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
31
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
32
+ });
33
+ };
34
+ var __importDefault = (this && this.__importDefault) || function (mod) {
35
+ return (mod && mod.__esModule) ? mod : { "default": mod };
36
+ };
37
+ Object.defineProperty(exports, "__esModule", { value: true });
38
+ exports.environmentHandler = void 0;
39
+ const fs = __importStar(require("fs"));
40
+ const path_1 = __importDefault(require("path"));
41
+ const constant_1 = require("../common/constant");
42
+ const utils_1 = require("../common/utils");
43
+ var Key;
44
+ (function (Key) {
45
+ Key["WORKFLOW"] = "workflow=";
46
+ Key["SECRET_KEY"] = "secret=";
47
+ })(Key || (Key = {}));
48
+ class EnvironmentHandler {
49
+ constructor() { }
50
+ cloneEnvironments() {
51
+ return __awaiter(this, void 0, void 0, function* () {
52
+ try {
53
+ const args = process.argv;
54
+ const dirPath = (0, utils_1.getValueFromArgs)(args, Key.WORKFLOW);
55
+ const workflowName = dirPath.replace(/\//g, '__');
56
+ const secretKey = (0, utils_1.getValueFromArgs)(args, Key.SECRET_KEY);
57
+ if (!secretKey) {
58
+ utils_1.customLog.error(`Parameter secret is required`);
59
+ return;
60
+ }
61
+ if (!workflowName) {
62
+ utils_1.customLog.error(`Parameter workflow is required`);
63
+ return;
64
+ }
65
+ try {
66
+ const url = `${constant_1.MSTATE_URL}/env/?workflow=${workflowName}`;
67
+ const responseJSON = yield fetch(url, {
68
+ headers: {
69
+ 'Content-Type': 'application/json',
70
+ 'secret-key': secretKey,
71
+ },
72
+ });
73
+ const response = yield responseJSON.json();
74
+ if (response === null || response === void 0 ? void 0 : response.errors) {
75
+ utils_1.customLog.error('Invalid Parameters: ', response === null || response === void 0 ? void 0 : response.errors);
76
+ }
77
+ else {
78
+ const config = response === null || response === void 0 ? void 0 : response.data;
79
+ const filePath = path_1.default.resolve(dirPath, 'environment.json');
80
+ fs.writeFileSync(filePath, JSON.stringify(config, null, 2));
81
+ }
82
+ }
83
+ catch (error) {
84
+ utils_1.customLog.error('Error in cloning action', error.message);
85
+ }
86
+ }
87
+ catch (error) {
88
+ utils_1.customLog.error('Error in cloning action', error.message);
89
+ }
90
+ });
91
+ }
92
+ saveToDB() {
93
+ return __awaiter(this, void 0, void 0, function* () {
94
+ try {
95
+ const args = process.argv;
96
+ const dirPath = (0, utils_1.getValueFromArgs)(args, Key.WORKFLOW);
97
+ const workflowName = dirPath.replace(/\//g, '__');
98
+ const secretKey = (0, utils_1.getValueFromArgs)(args, Key.SECRET_KEY);
99
+ if (!secretKey) {
100
+ utils_1.customLog.error(`Parameter secret is required`);
101
+ return;
102
+ }
103
+ if (!workflowName) {
104
+ utils_1.customLog.error(`Parameter workflow is required`);
105
+ return;
106
+ }
107
+ const filePath = path_1.default.resolve(dirPath, 'environment.json');
108
+ const data = fs.readFileSync(filePath, 'utf8');
109
+ const environments = JSON.parse(data);
110
+ try {
111
+ const url = `${constant_1.MSTATE_URL}/env`;
112
+ const responseJSON = yield fetch(url, {
113
+ method: 'POST',
114
+ headers: {
115
+ 'secret-key': secretKey,
116
+ 'Content-Type': 'application/json',
117
+ },
118
+ body: JSON.stringify({
119
+ workflow: workflowName,
120
+ env: environments,
121
+ }),
122
+ });
123
+ const response = yield responseJSON.json();
124
+ if (response === null || response === void 0 ? void 0 : response.errors) {
125
+ utils_1.customLog.error(`Updating environment:`, response === null || response === void 0 ? void 0 : response.errors);
126
+ }
127
+ else {
128
+ utils_1.customLog.success(`Environments Updated successfully`);
129
+ }
130
+ }
131
+ catch (error) {
132
+ utils_1.customLog.error(error.message);
133
+ }
134
+ }
135
+ catch (error) {
136
+ utils_1.customLog.error('Something went wrong');
137
+ }
138
+ });
139
+ }
140
+ }
141
+ exports.environmentHandler = new EnvironmentHandler();
@@ -0,0 +1,78 @@
1
+ "use strict";
2
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
+ return new (P || (P = Promise))(function (resolve, reject) {
5
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
9
+ });
10
+ };
11
+ Object.defineProperty(exports, "__esModule", { value: true });
12
+ exports.mobiofficeHandler = void 0;
13
+ const constant_1 = require("../common/constant");
14
+ const utils_1 = require("../common/utils");
15
+ var Key;
16
+ (function (Key) {
17
+ Key["WORKFLOW"] = "workflow=";
18
+ Key["SECRET_KEY"] = "secret=";
19
+ Key["COMPANY_ID"] = "company=";
20
+ Key["USER_ID"] = "user=";
21
+ })(Key || (Key = {}));
22
+ class MobiofficeHandler {
23
+ constructor() { }
24
+ linkToMobioffice(action) {
25
+ return __awaiter(this, void 0, void 0, function* () {
26
+ try {
27
+ const args = process.argv;
28
+ const dirPath = (0, utils_1.getValueFromArgs)(args, Key.WORKFLOW);
29
+ const workflowName = dirPath.replace(/\//g, '__');
30
+ const secretKey = (0, utils_1.getValueFromArgs)(args, Key.SECRET_KEY);
31
+ const companyID = (0, utils_1.getValueFromArgs)(args, Key.COMPANY_ID);
32
+ const user = (0, utils_1.getValueFromArgs)(args, Key.USER_ID);
33
+ if (!workflowName) {
34
+ utils_1.customLog.error(`Parameter workflow is required`);
35
+ return;
36
+ }
37
+ if (!secretKey) {
38
+ utils_1.customLog.error(`Parameter secret is required`);
39
+ return;
40
+ }
41
+ if (!companyID) {
42
+ utils_1.customLog.error(`Parameter company is required`);
43
+ return;
44
+ }
45
+ if (!user) {
46
+ utils_1.customLog.error(`Parameter user is required`);
47
+ return;
48
+ }
49
+ const url = `${constant_1.MOBIOFFICE_URL}/Mstate/workFlow/user`;
50
+ const responseJSON = yield fetch(url, {
51
+ method: 'POST',
52
+ headers: {
53
+ 'secret-key': secretKey,
54
+ 'Content-Type': 'application/json',
55
+ },
56
+ body: JSON.stringify({
57
+ action: action.toUpperCase(),
58
+ workflow: workflowName,
59
+ secret: secretKey,
60
+ companyID,
61
+ user,
62
+ }),
63
+ });
64
+ const response = yield responseJSON.json();
65
+ if (responseJSON.status >= 400) {
66
+ utils_1.customLog.error('Invalid Parameters: ', responseJSON.statusText);
67
+ }
68
+ else {
69
+ utils_1.customLog.success('Workflow Added successfully', response === null || response === void 0 ? void 0 : response.message);
70
+ }
71
+ }
72
+ catch (error) {
73
+ utils_1.customLog.error(error.message);
74
+ }
75
+ });
76
+ }
77
+ }
78
+ exports.mobiofficeHandler = new MobiofficeHandler();
@@ -35,138 +35,149 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
35
35
  return (mod && mod.__esModule) ? mod : { "default": mod };
36
36
  };
37
37
  Object.defineProperty(exports, "__esModule", { value: true });
38
- exports.workflowAction = void 0;
38
+ exports.workflowHandler = void 0;
39
39
  const fs = __importStar(require("fs"));
40
40
  const path_1 = __importDefault(require("path"));
41
41
  const constant_1 = require("../common/constant");
42
+ const utils_1 = require("../common/utils");
42
43
  var Key;
43
44
  (function (Key) {
44
- Key["PATH"] = "path";
45
- Key["SECRET_KEY"] = "secret";
46
- Key["WORKFLOW"] = "workflow";
45
+ Key["PATH"] = "path=";
46
+ Key["FILE"] = "file=";
47
+ Key["SECRET_KEY"] = "secret=";
48
+ Key["WORKFLOW"] = "workflow=";
47
49
  })(Key || (Key = {}));
48
- class WorkflowAction {
50
+ class WorkflowHandler {
49
51
  constructor() { }
50
- saveToDB(args) {
52
+ saveToDB() {
51
53
  return __awaiter(this, void 0, void 0, function* () {
52
- var _a, _b, _c;
53
54
  try {
54
- const docPath = (_b = (_a = args
55
- .find((str) => str.includes(Key.PATH))) === null || _a === void 0 ? void 0 : _a.replace(Key.PATH + '=', '')) !== null && _b !== void 0 ? _b : '';
56
- const secretKey = ((_c = args.find((str) => str.includes(Key.SECRET_KEY))) !== null && _c !== void 0 ? _c : '').replace(Key.SECRET_KEY + '=', '');
55
+ const args = process.argv;
56
+ const dirPath = (0, utils_1.getValueFromArgs)(args, Key.PATH);
57
+ const workflowPath = dirPath.replace(/\//g, '__');
58
+ const file = (0, utils_1.getValueFromArgs)(args, Key.FILE);
59
+ const secretKey = (0, utils_1.getValueFromArgs)(args, Key.SECRET_KEY);
60
+ if (!file) {
61
+ utils_1.customLog.error(`Parameter file is required`);
62
+ return;
63
+ }
64
+ if (!secretKey) {
65
+ utils_1.customLog.error(`Parameter secret is required`);
66
+ return;
67
+ }
68
+ const filePath = path_1.default.resolve(file);
69
+ const workflowJSON = fs.readFileSync(filePath, 'utf8');
57
70
  let query = '';
58
- if (docPath) {
59
- query += `path=${docPath}`;
71
+ if (workflowPath) {
72
+ query += `path=${workflowPath}`;
60
73
  }
61
74
  if (query.length) {
62
75
  query = '?' + query;
63
76
  }
64
77
  const url = `${constant_1.MSTATE_URL}/workflow/config/new${query}`;
65
- const filePath = path_1.default.resolve(args[0]);
66
- const workflowJSON = fs.readFileSync(filePath, 'utf8');
67
78
  const responseJSON = yield fetch(url, {
68
79
  method: 'POST',
69
80
  headers: {
70
81
  'secret-key': secretKey,
71
82
  'Content-Type': 'application/json',
72
83
  },
73
- body: JSON.stringify(JSON.parse(workflowJSON)),
84
+ body: workflowJSON,
74
85
  });
75
86
  const response = yield responseJSON.json();
76
87
  if (response === null || response === void 0 ? void 0 : response.errors) {
77
- console.error('Mstate error', 'Invalid Parameters: ', response === null || response === void 0 ? void 0 : response.errors);
88
+ utils_1.customLog.error('Invalid Parameters: ', response === null || response === void 0 ? void 0 : response.errors);
78
89
  }
79
90
  else {
80
- console.log('Workflow Added successfully', response === null || response === void 0 ? void 0 : response.data);
91
+ utils_1.customLog.success('Workflow Added successfully', response === null || response === void 0 ? void 0 : response.data);
81
92
  }
82
93
  }
83
94
  catch (error) {
84
- console.error(error.message);
95
+ utils_1.customLog.error(error.message);
85
96
  }
86
97
  });
87
98
  }
88
- updateWorkflowToDB(args) {
99
+ updateWorkflowToDB() {
89
100
  return __awaiter(this, void 0, void 0, function* () {
90
- var _a, _b, _c;
91
101
  try {
92
- const docPath = (_b = (_a = args
93
- .find((str) => str.includes(Key.PATH))) === null || _a === void 0 ? void 0 : _a.replace(Key.PATH + '=', '')) !== null && _b !== void 0 ? _b : '';
94
- const secretKey = ((_c = args.find((str) => str.includes(Key.SECRET_KEY))) !== null && _c !== void 0 ? _c : '').replace(Key.SECRET_KEY + '=', '');
102
+ const args = process.argv;
103
+ const dirPath = (0, utils_1.getValueFromArgs)(args, Key.WORKFLOW);
104
+ const workflowName = dirPath.replace(/\//g, '__');
105
+ const secretKey = (0, utils_1.getValueFromArgs)(args, Key.SECRET_KEY);
95
106
  if (!secretKey) {
96
- console.error(`Parameter secret is required`);
107
+ utils_1.customLog.error(`Parameter secret is required`);
97
108
  return;
98
109
  }
99
- let query = '';
100
- if (docPath) {
101
- query += `path=${docPath}`;
102
- }
103
- if (query.length) {
104
- query = '?' + query;
110
+ if (!workflowName) {
111
+ utils_1.customLog.error(`Parameter workflow is required`);
112
+ return;
105
113
  }
106
- const url = `${constant_1.MSTATE_URL}/workflow/config${query}`;
107
- const filePath = path_1.default.resolve(args[0]);
114
+ const folderPath = path_1.default.resolve(dirPath);
115
+ const filePath = path_1.default.join(folderPath, 'workflow.json');
108
116
  const workflowJSON = fs.readFileSync(filePath, 'utf8');
117
+ let query = `?workflow=${workflowName}`;
118
+ const url = `${constant_1.MSTATE_URL}/workflow/config${query}`;
109
119
  const responseJSON = yield fetch(url, {
110
120
  method: 'PUT',
111
121
  headers: {
112
122
  'secret-key': secretKey,
113
123
  'Content-Type': 'application/json',
114
124
  },
115
- body: JSON.stringify(JSON.parse(workflowJSON)),
125
+ body: workflowJSON,
116
126
  });
117
127
  const response = yield responseJSON.json();
118
128
  if (response === null || response === void 0 ? void 0 : response.errors) {
119
- console.error('Mstate error', 'Invalid Parameters: ', response === null || response === void 0 ? void 0 : response.errors);
129
+ utils_1.customLog.error('Invalid Parameters: ', response === null || response === void 0 ? void 0 : response.errors);
120
130
  }
121
131
  else {
122
- console.log('Workflow Uploaded successfully', response === null || response === void 0 ? void 0 : response.data);
132
+ utils_1.customLog.success('Workflow Uploaded successfully \n', response === null || response === void 0 ? void 0 : response.data);
123
133
  }
134
+ return response;
124
135
  }
125
136
  catch (error) {
126
- console.error(error.message);
137
+ utils_1.customLog.error(error.message);
127
138
  }
128
139
  });
129
140
  }
130
- linkToMobioffice() {
131
- return __awaiter(this, void 0, void 0, function* () { });
132
- }
133
- cloneWorkflow(args) {
141
+ cloneWorkflow() {
134
142
  return __awaiter(this, void 0, void 0, function* () {
135
- var _a, _b, _c;
136
143
  try {
137
- const workflowName = (_b = (_a = args
138
- .find((str) => str.includes(Key.WORKFLOW))) === null || _a === void 0 ? void 0 : _a.replace(Key.WORKFLOW + '=', '')) !== null && _b !== void 0 ? _b : '';
139
- const secretKey = ((_c = args.find((str) => str.includes(Key.SECRET_KEY))) !== null && _c !== void 0 ? _c : '').replace(Key.SECRET_KEY + '=', '');
144
+ const args = process.argv;
145
+ const dirPath = (0, utils_1.getValueFromArgs)(args, Key.WORKFLOW);
146
+ const workflowName = dirPath.replace(/\//g, '__');
147
+ const secretKey = (0, utils_1.getValueFromArgs)(args, Key.SECRET_KEY);
140
148
  if (!secretKey) {
141
- console.error(`Parameter secret is required`);
149
+ utils_1.customLog.error(`Parameter secret is required`);
142
150
  return;
143
151
  }
144
152
  if (!workflowName) {
145
- console.error(`Parameter workflow is required`);
153
+ utils_1.customLog.error(`Parameter workflow is required`);
146
154
  return;
147
155
  }
148
156
  const url = `${constant_1.MSTATE_URL}/workflow/config/${workflowName}`;
149
- console.log({ url });
150
157
  const responseJSON = yield fetch(url, {
158
+ method: 'GET',
151
159
  headers: {
160
+ 'Content-Type': 'application/json',
152
161
  'secret-key': secretKey,
153
162
  },
154
163
  });
155
164
  const response = yield responseJSON.json();
156
165
  if (response === null || response === void 0 ? void 0 : response.errors) {
157
- console.error('Mstate error', 'Invalid Parameters: ', response === null || response === void 0 ? void 0 : response.errors);
166
+ utils_1.customLog.error('Invalid Parameters: ', response === null || response === void 0 ? void 0 : response.errors);
158
167
  }
159
168
  else {
160
169
  const workflowConfig = response === null || response === void 0 ? void 0 : response.data;
161
- const filePath = path_1.default.resolve(workflowName);
170
+ const folderPath = path_1.default.resolve(dirPath);
171
+ fs.mkdirSync(folderPath, { recursive: true });
172
+ const filePath = path_1.default.join(folderPath, 'workflow.json');
162
173
  fs.writeFileSync(filePath, JSON.stringify(workflowConfig, null, 2));
163
- console.log(`Workflow "${workflowName}" is cloned successfully`);
174
+ utils_1.customLog.success(`Workflow cloned successfully`);
164
175
  }
165
176
  }
166
177
  catch (error) {
167
- console.error(error.message);
178
+ utils_1.customLog.error('Error in cloning workflow', error.message);
168
179
  }
169
180
  });
170
181
  }
171
182
  }
172
- exports.workflowAction = new WorkflowAction();
183
+ exports.workflowHandler = new WorkflowHandler();