mstate-cli 0.2.9 → 0.4.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/.prettierrc +5 -5
- package/Readme.md +50 -50
- package/dist/index.js +298 -0
- package/package.json +46 -45
- package/rollup.config.mjs +34 -34
- package/src/common/constant.ts +19 -26
- package/src/common/enum.ts +26 -16
- package/src/common/helpers.ts +58 -42
- package/src/common/utils.ts +67 -67
- package/src/handlers/action.handler.ts +94 -131
- package/src/handlers/company.handler.ts +202 -274
- package/src/handlers/environment.handler.ts +77 -111
- package/src/handlers/help.handler.ts +80 -48
- package/src/handlers/mobioffice.handler.ts +43 -78
- package/src/handlers/workflow.handler.ts +171 -238
- package/src/index.ts +85 -85
- package/src/interface.d.ts +43 -43
- package/tsconfig.json +18 -18
@@ -1,274 +1,202 @@
|
|
1
|
-
import { MSTATE_DOMAIN, MSTATE_URL } from '../common/constant';
|
2
|
-
import {
|
3
|
-
customLog,
|
4
|
-
getSecretKey,
|
5
|
-
handleUpdateSecretKey,
|
6
|
-
} from '../common/utils';
|
7
|
-
import { getValueFromArgs } from '../common/helpers';
|
8
|
-
|
9
|
-
import open from 'open';
|
10
|
-
import express from 'express';
|
11
|
-
import cors from 'cors';
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
const
|
30
|
-
const
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
const
|
56
|
-
const
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
}
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
if (!
|
95
|
-
customLog.changeAndThrow(`Parameter
|
96
|
-
return;
|
97
|
-
}
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
|
122
|
-
|
123
|
-
|
124
|
-
|
125
|
-
|
126
|
-
|
127
|
-
|
128
|
-
|
129
|
-
|
130
|
-
|
131
|
-
|
132
|
-
|
133
|
-
|
134
|
-
|
135
|
-
|
136
|
-
|
137
|
-
|
138
|
-
|
139
|
-
|
140
|
-
|
141
|
-
|
142
|
-
|
143
|
-
|
144
|
-
|
145
|
-
|
146
|
-
|
147
|
-
|
148
|
-
|
149
|
-
|
150
|
-
|
151
|
-
|
152
|
-
|
153
|
-
|
154
|
-
|
155
|
-
|
156
|
-
|
157
|
-
|
158
|
-
|
159
|
-
|
160
|
-
|
161
|
-
|
162
|
-
|
163
|
-
|
164
|
-
|
165
|
-
|
166
|
-
|
167
|
-
|
168
|
-
|
169
|
-
|
170
|
-
|
171
|
-
|
172
|
-
|
173
|
-
|
174
|
-
|
175
|
-
|
176
|
-
|
177
|
-
|
178
|
-
|
179
|
-
|
180
|
-
|
181
|
-
|
182
|
-
|
183
|
-
|
184
|
-
|
185
|
-
|
186
|
-
|
187
|
-
|
188
|
-
|
189
|
-
|
190
|
-
|
191
|
-
|
192
|
-
|
193
|
-
|
194
|
-
}
|
195
|
-
|
196
|
-
|
197
|
-
|
198
|
-
|
199
|
-
|
200
|
-
|
201
|
-
|
202
|
-
|
203
|
-
console.log('Opening browser for authentication...');
|
204
|
-
|
205
|
-
// Start a local server to capture the token
|
206
|
-
|
207
|
-
const startServer = (port: number) => {
|
208
|
-
return app.listen(port, () => {
|
209
|
-
console.log(`Waiting for browser response...`);
|
210
|
-
});
|
211
|
-
};
|
212
|
-
|
213
|
-
const server = startServer(port);
|
214
|
-
|
215
|
-
server.on('error', (err: any) => {
|
216
|
-
if (err.code === 'EADDRINUSE') {
|
217
|
-
console.log(`Port ${port} is in use. Trying another port...`);
|
218
|
-
|
219
|
-
startServer(++port);
|
220
|
-
} else {
|
221
|
-
console.error('Server error:', err);
|
222
|
-
}
|
223
|
-
});
|
224
|
-
|
225
|
-
// Capture the OAuth callback
|
226
|
-
app.get('/cli/mstate/login', (req, res) => {
|
227
|
-
try {
|
228
|
-
const { token } = req.query;
|
229
|
-
|
230
|
-
if (token) {
|
231
|
-
console.log('Authentication successful!');
|
232
|
-
|
233
|
-
// Save the token locally
|
234
|
-
handleUpdateSecretKey(token as string);
|
235
|
-
|
236
|
-
res.send('<h1>Login successful! You can close this window.</h1>');
|
237
|
-
} else {
|
238
|
-
console.error('Authentication failed.');
|
239
|
-
res.send('<h1>Authentication failed. Please try again.</h1>');
|
240
|
-
}
|
241
|
-
} catch (error) {
|
242
|
-
} finally {
|
243
|
-
server.close(); // Stop the server after handling the response
|
244
|
-
}
|
245
|
-
});
|
246
|
-
}
|
247
|
-
|
248
|
-
// Command handling
|
249
|
-
login()
|
250
|
-
.then(async () => {
|
251
|
-
await open(
|
252
|
-
MSTATE_DOMAIN +
|
253
|
-
`/oauth?redirect=${encodeURIComponent(
|
254
|
-
'cli/mstate/login',
|
255
|
-
)}&open=${port}&scope=profile`,
|
256
|
-
);
|
257
|
-
})
|
258
|
-
.catch((err) => {
|
259
|
-
console.error('Error during login:', err.message);
|
260
|
-
process.exit(1);
|
261
|
-
});
|
262
|
-
}
|
263
|
-
|
264
|
-
handleUpdateSavedSecretKey() {
|
265
|
-
handleUpdateSecretKey(getSecretKey());
|
266
|
-
}
|
267
|
-
|
268
|
-
logToken() {
|
269
|
-
customLog.info('Token', getSecretKey());
|
270
|
-
customLog.info('MSTATE_URL', MSTATE_URL);
|
271
|
-
}
|
272
|
-
}
|
273
|
-
|
274
|
-
export const companyHandler = new CompanyHandler();
|
1
|
+
import { MSTATE_DOMAIN, MSTATE_URL } from '../common/constant';
|
2
|
+
import {
|
3
|
+
customLog,
|
4
|
+
getSecretKey,
|
5
|
+
handleUpdateSecretKey,
|
6
|
+
} from '../common/utils';
|
7
|
+
import { getValueFromArgs } from '../common/helpers';
|
8
|
+
|
9
|
+
import open from 'open';
|
10
|
+
import express from 'express';
|
11
|
+
import cors from 'cors';
|
12
|
+
import { Variables } from 'src/common/enum';
|
13
|
+
|
14
|
+
class CompanyHandler {
|
15
|
+
constructor() {}
|
16
|
+
|
17
|
+
async addToken() {
|
18
|
+
const pString = getValueFromArgs(Variables.PERMISSIONS).trim();
|
19
|
+
const permissions = pString.split(',').map((s) => s.trim());
|
20
|
+
const secretKey = getSecretKey();
|
21
|
+
const user = getValueFromArgs(Variables.USER_ID, true);
|
22
|
+
const companyID = getValueFromArgs(Variables.COMPANY_ID, true);
|
23
|
+
|
24
|
+
if (!permissions.length && permissions[0]?.length) {
|
25
|
+
customLog.changeAndThrow(`Parameter permissions is required`);
|
26
|
+
return;
|
27
|
+
}
|
28
|
+
|
29
|
+
const url = `${MSTATE_URL}/company/permission`;
|
30
|
+
const responseJSON = await fetch(url, {
|
31
|
+
method: 'POST',
|
32
|
+
headers: {
|
33
|
+
'secret-key': secretKey,
|
34
|
+
'Content-Type': 'application/json',
|
35
|
+
},
|
36
|
+
body: JSON.stringify({
|
37
|
+
user,
|
38
|
+
permissions,
|
39
|
+
company: companyID,
|
40
|
+
}),
|
41
|
+
});
|
42
|
+
|
43
|
+
const response = await responseJSON.json();
|
44
|
+
if (response?.errors) {
|
45
|
+
customLog.changeAndThrow(
|
46
|
+
'Invalid Parameters for Company: ',
|
47
|
+
...response?.errors,
|
48
|
+
);
|
49
|
+
} else {
|
50
|
+
customLog.success('Company updated successfully \n', response?.data);
|
51
|
+
}
|
52
|
+
}
|
53
|
+
|
54
|
+
async addUser() {
|
55
|
+
const secretKey = getSecretKey();
|
56
|
+
const name = getValueFromArgs(Variables.USER_ID, true);
|
57
|
+
const email = getValueFromArgs(Variables.EMAIL, true);
|
58
|
+
const phone = getValueFromArgs(Variables.PHONE, true);
|
59
|
+
|
60
|
+
const url = `${MSTATE_URL}/um/user`;
|
61
|
+
const responseJSON = await fetch(url, {
|
62
|
+
method: 'POST',
|
63
|
+
headers: {
|
64
|
+
'secret-key': secretKey,
|
65
|
+
'Content-Type': 'application/json',
|
66
|
+
},
|
67
|
+
body: JSON.stringify({
|
68
|
+
name,
|
69
|
+
username: name,
|
70
|
+
email,
|
71
|
+
password: '123',
|
72
|
+
phone,
|
73
|
+
}),
|
74
|
+
});
|
75
|
+
|
76
|
+
const response = await responseJSON.json();
|
77
|
+
if (response?.errors) {
|
78
|
+
customLog.changeAndThrow(
|
79
|
+
'Invalid Parameters for Company: ',
|
80
|
+
...response?.errors,
|
81
|
+
);
|
82
|
+
} else {
|
83
|
+
customLog.success('User Updated successfully \n', response?.data);
|
84
|
+
}
|
85
|
+
}
|
86
|
+
|
87
|
+
async revokePermission() {
|
88
|
+
const pString = getValueFromArgs(Variables.PERMISSIONS);
|
89
|
+
const permissions = pString.split(',').map((s) => s.trim());
|
90
|
+
const secretKey = getSecretKey();
|
91
|
+
const user = getValueFromArgs(Variables.USER_ID, true);
|
92
|
+
const companyID = getValueFromArgs(Variables.COMPANY_ID, true);
|
93
|
+
|
94
|
+
if (!permissions.length && permissions[0]?.length) {
|
95
|
+
customLog.changeAndThrow(`Parameter permissions is required`);
|
96
|
+
return;
|
97
|
+
}
|
98
|
+
|
99
|
+
const url = `${MSTATE_URL}/company/permission`;
|
100
|
+
const responseJSON = await fetch(url, {
|
101
|
+
method: 'DELETE',
|
102
|
+
headers: {
|
103
|
+
'secret-key': secretKey,
|
104
|
+
'Content-Type': 'application/json',
|
105
|
+
},
|
106
|
+
body: JSON.stringify({
|
107
|
+
company: companyID,
|
108
|
+
user,
|
109
|
+
permissions,
|
110
|
+
}),
|
111
|
+
});
|
112
|
+
|
113
|
+
const response = await responseJSON.json();
|
114
|
+
if (response?.errors) {
|
115
|
+
customLog.changeAndThrow(
|
116
|
+
'Invalid Parameters for Company: ',
|
117
|
+
...response?.errors,
|
118
|
+
);
|
119
|
+
} else {
|
120
|
+
customLog.success('Company updated successfully \n', response?.data);
|
121
|
+
}
|
122
|
+
}
|
123
|
+
|
124
|
+
async handleLogin() {
|
125
|
+
// Start the login process
|
126
|
+
let port = 49152;
|
127
|
+
const app = express();
|
128
|
+
app.use(cors({ origin: '*' }));
|
129
|
+
|
130
|
+
async function login() {
|
131
|
+
console.log('Opening browser for authentication...');
|
132
|
+
|
133
|
+
// Start a local server to capture the token
|
134
|
+
|
135
|
+
const startServer = (port: number) => {
|
136
|
+
return app.listen(port, () => {
|
137
|
+
console.log(`Waiting for browser response...`);
|
138
|
+
});
|
139
|
+
};
|
140
|
+
|
141
|
+
const server = startServer(port);
|
142
|
+
|
143
|
+
server.on('error', (err: any) => {
|
144
|
+
if (err.code === 'EADDRINUSE') {
|
145
|
+
console.log(`Port ${port} is in use. Trying another port...`);
|
146
|
+
|
147
|
+
startServer(++port);
|
148
|
+
} else {
|
149
|
+
console.error('Server error:', err);
|
150
|
+
}
|
151
|
+
});
|
152
|
+
|
153
|
+
// Capture the OAuth callback
|
154
|
+
app.get('/cli/mstate/login', (req, res) => {
|
155
|
+
try {
|
156
|
+
const { token } = req.query;
|
157
|
+
|
158
|
+
if (token) {
|
159
|
+
console.log('Authentication successful!');
|
160
|
+
|
161
|
+
// Save the token locally
|
162
|
+
handleUpdateSecretKey(token as string);
|
163
|
+
|
164
|
+
res.send('<h1>Login successful! You can close this window.</h1>');
|
165
|
+
} else {
|
166
|
+
console.error('Authentication failed.');
|
167
|
+
res.send('<h1>Authentication failed. Please try again.</h1>');
|
168
|
+
}
|
169
|
+
} catch (error) {
|
170
|
+
} finally {
|
171
|
+
server.close(); // Stop the server after handling the response
|
172
|
+
}
|
173
|
+
});
|
174
|
+
}
|
175
|
+
|
176
|
+
// Command handling
|
177
|
+
login()
|
178
|
+
.then(async () => {
|
179
|
+
await open(
|
180
|
+
MSTATE_DOMAIN +
|
181
|
+
`/oauth?redirect=${encodeURIComponent(
|
182
|
+
'cli/mstate/login',
|
183
|
+
)}&open=${port}&scope=profile`,
|
184
|
+
);
|
185
|
+
})
|
186
|
+
.catch((err) => {
|
187
|
+
console.error('Error during login:', err.message);
|
188
|
+
process.exit(1);
|
189
|
+
});
|
190
|
+
}
|
191
|
+
|
192
|
+
handleUpdateSavedSecretKey() {
|
193
|
+
handleUpdateSecretKey(getSecretKey());
|
194
|
+
}
|
195
|
+
|
196
|
+
logToken() {
|
197
|
+
customLog.info('MSTATE_URL', MSTATE_URL);
|
198
|
+
customLog.info('Token', getSecretKey());
|
199
|
+
}
|
200
|
+
}
|
201
|
+
|
202
|
+
export const companyHandler = new CompanyHandler();
|
@@ -1,111 +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
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
const
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
const
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
customLog.changeAndThrow(
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
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
|
-
type,
|
93
|
-
}),
|
94
|
-
});
|
95
|
-
|
96
|
-
const response = await responseJSON.json();
|
97
|
-
if (response?.errors) {
|
98
|
-
customLog.changeAndThrow(
|
99
|
-
`Invalid Parameters for Environment: `,
|
100
|
-
...response?.errors,
|
101
|
-
);
|
102
|
-
} else {
|
103
|
-
customLog.success(`Environments Updated successfully`);
|
104
|
-
}
|
105
|
-
} catch (error: any) {
|
106
|
-
customLog.changeAndThrow(error.message);
|
107
|
-
}
|
108
|
-
}
|
109
|
-
}
|
110
|
-
|
111
|
-
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}`;
|
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();
|