forked from novuhq/novu
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjarvis.js
347 lines (295 loc) · 10.1 KB
/
jarvis.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
const fs = require('fs');
const shell = require('shelljs');
const nodeModulesExist = fs.existsSync('node_modules');
const envInitialized = fs.existsSync('apps/api/src/.env');
async function reInstallProject() {
const inquirer = require('inquirer');
const questions = [
{
type: 'list',
name: 'reinstall',
message:
'Are you changing branches like socks? just came from another branch? Do you wish us to reinstall the project for you?',
choices: ['Yes', 'No'],
},
];
await inquirer.prompt(questions).then(async (answers) => {
if (answers.reinstall === 'No') {
return;
}
return await setupProject();
});
}
const RUN_PROJECT = 'Run the project';
const TEST_PROJECT = 'Test the project';
const API_AND_WORKER_ONLY = 'API & Worker only';
const API_TESTS = 'API tests';
const API_E2E_TESTS = 'API E2E tests';
const API_INTEGRATION_TESTS = 'API integration tests';
const DEV_ENVIRONMENT_SETUP = 'Development environment setup';
const WEB_PROJECT_AND_WIDGET = 'Web project and Widget app';
const WEB_PROJECT = 'Web project (Web, API, Worker, WS)';
const WEB_TESTS = 'WEB tests';
const RUN_PLAYWRIGHT_UI = 'Open Playwright UI';
const RUN_PLAYWRIGHT_CLI = 'Run Playwright tests - CLI';
async function setupRunner() {
const ora = require('ora');
const shell = require('shelljs');
const waitPort = require('wait-port');
const inquirer = require('inquirer');
const questions = [
{
type: 'list',
name: 'action',
message: 'How can I help you today?',
choices: [RUN_PROJECT, TEST_PROJECT, DEV_ENVIRONMENT_SETUP],
},
{
type: 'list',
name: 'runConfiguration',
message: 'What section of the project you want to run?',
choices: [WEB_PROJECT, WEB_PROJECT_AND_WIDGET, API_AND_WORKER_ONLY],
when(answers) {
return answers.action === RUN_PROJECT;
},
},
{
type: 'list',
name: 'runConfiguration',
message: 'What section of the project you want to run?',
choices: [WEB_TESTS, API_TESTS],
when(answers) {
return answers.action === TEST_PROJECT;
},
},
{
type: 'list',
name: 'runApiConfiguration',
message: 'What section of the project you want to run?',
choices: [API_INTEGRATION_TESTS, API_E2E_TESTS],
when(answers) {
return answers.runConfiguration === API_TESTS;
},
},
{
type: 'list',
name: 'runWebConfiguration',
message: 'What section of the project you want to run?',
choices: [RUN_PLAYWRIGHT_UI, RUN_PLAYWRIGHT_CLI],
when(answers) {
return answers.runConfiguration === WEB_TESTS;
},
},
];
inquirer.prompt(questions).then(async (answers) => {
if (answers.action === DEV_ENVIRONMENT_SETUP) {
shell.exec('npm run dev-environment-setup');
} else if (answers.runConfiguration === WEB_PROJECT_AND_WIDGET) {
shell.exec('nx run-many --target=build --projects=@novu/api-service,@novu/worker');
shell.exec('npm run start:dev', { async: true });
await waitPort({
host: 'localhost',
port: 3000,
});
await waitPort({
host: 'localhost',
port: 3004,
});
await waitPort({
host: 'localhost',
port: 4500,
});
await waitPort({
host: 'localhost',
port: 4200,
});
// eslint-disable-next-line no-console
console.log(`
Everything is running 🎊
Web: http://127.0.0.1:4200
Widget: http://127.0.0.1:4500
API: http://127.0.0.1:3000
Worker: http://127.0.0.1:3004
`);
} else if (answers.runConfiguration === WEB_PROJECT) {
try {
shell.exec('nx run-many --target=build --projects=@novu/api-service,@novu/worker,@novu/ws');
shell.exec('npm run start:api', { async: true });
shell.exec('npm run start:ws', { async: true });
shell.exec('npm run start:worker', { async: true });
await waitPort({
host: 'localhost',
port: 3000,
});
await waitPort({
host: 'localhost',
port: 3002,
});
await waitPort({
host: 'localhost',
port: 3004,
});
shell.exec('npm run start:web', { async: true });
await new Promise((resolve) => setTimeout(resolve, 3000));
// eslint-disable-next-line no-console
console.log(`
Everything is running 🎊
Web: http://127.0.0.1:4200
API: http://127.0.0.1:3000
WS: http://127.0.0.1:3002
Worker: http://127.0.0.1:3004
`);
} catch (e) {
console.error(`Failed to spin up the project ❌`, e);
}
} else if (answers.runConfiguration === API_AND_WORKER_ONLY) {
shell.exec('nx run-many --target=build --projects=@novu/api-service,@novu/worker');
shell.exec('npm run start:api', { async: true });
shell.exec('npm run start:worker', { async: true });
await waitPort({
host: 'localhost',
port: 3000,
});
await waitPort({
host: 'localhost',
port: 3004,
});
console.log(`
Everything is running 🎊
API: http://127.0.0.1:3000
Worker: http://127.0.0.1:3004
`);
} else if (answers.runApiConfiguration === API_INTEGRATION_TESTS) {
shell.exec('nx run-many --target=build --projects=@novu/api-service,@novu/worker');
shell.exec('npm run start:worker:test', { async: true });
await waitPort({
host: 'localhost',
port: 1342,
});
shell.exec('npm run start:integration:api', { async: true });
} else if (answers.runApiConfiguration === API_E2E_TESTS) {
shell.exec('nx run-many --target=build --projects=@novu/api-service,@novu/worker');
shell.exec('npm run start:worker:test', { async: true });
await waitPort({
host: 'localhost',
port: 1342,
});
shell.exec('npm run start:e2e:api', { async: true });
} else if ([RUN_PLAYWRIGHT_CLI, RUN_PLAYWRIGHT_UI].includes(answers.runWebConfiguration)) {
shell.exec('nx run-many --target=build --projects=@novu/api-service,@novu/worker,@novu/ws');
shell.exec('cd apps/web && npm run build:test');
shell.exec('npm run start:api:test', { async: true });
shell.exec('npm run start:worker:test', { async: true });
shell.exec('npm run start:ws:test', { async: true });
shell.exec('cd apps/web && npm run start:test', { async: true });
await waitPort({
host: 'localhost',
port: 1336,
});
await waitPort({
host: 'localhost',
port: 1340,
});
await waitPort({
host: 'localhost',
port: 1342,
});
await waitPort({
host: 'localhost',
port: 4200,
});
shell.exec('cd apps/web && npm run test:e2e:install');
if (answers.runWebConfiguration === RUN_PLAYWRIGHT_UI) {
shell.exec('cd apps/web && npm run test:e2e:ui');
} else if (answers.runWebConfiguration === RUN_PLAYWRIGHT_CLI) {
shell.exec('cd apps/web && npm run test:e2e');
}
}
return true;
});
}
const informAboutInitialSetup = () => {
const rlp = require('readline');
return new Promise((resolve, reject) => {
const rl = rlp.createInterface({
input: process.stdin,
output: process.stdout,
});
rl.question(
'Looks like its the first time running this project on your machine. We will start by installing pnpm dependencies. ' +
'\nDo you want to continue? Yes/No\n',
function (answer) {
if (answer.toLowerCase() === 'yes' || answer.toLowerCase() === 'y') {
rl.close();
resolve();
return;
}
reject('exit.. because dependencies are mandatory.');
}
);
});
};
const setupProject = () =>
new Promise((resolve, reject) => {
const { spawn } = require('child_process');
const isWindows = process.platform === 'win32';
const cmd = isWindows ? 'cmd' : 'npm';
const args = isWindows ? ['/c', 'npm run setup:project'] : ['run setup:project'];
const command = spawn(cmd, args, {
shell: true,
stdio: 'inherit',
});
function onExit() {
command.kill('SIGINT');
}
process.on('SIGTERM', onExit);
process.on('SIGINT', onExit);
command.on('exit', (exitCode) => {
if (parseInt(exitCode) !== 0) {
return reject(new Error(exitCode));
}
// eslint-disable-next-line no-console
console.log('Finished installing building project!');
resolve();
});
});
async function main() {
if (!nodeModulesExist || !envInitialized) {
await informAboutInitialSetup();
await setupProject();
}
showWelcomeScreen();
await setupRunner();
}
main().catch((rej) => {
// eslint-disable-next-line no-console
console.log(rej);
process.kill(process.pid, 'SIGTERM');
});
function showWelcomeScreen() {
const chalk = require('chalk');
const gradient = require('gradient-string');
const textGradient = gradient('#0099F7', '#ff3432');
const logoGradient = gradient('#212121', '#ec0f0d');
const logo = `
@@@@@@@@@@@@@
@@@ @@@@@@@@@@@
@@@@@@@@ @@@@@@@@
@@@@@@@@@@@@ @@@@@@ @@
@@@@@@@@@@@@@@@@ @@@@ @@@
@@@@@@@@@@@@@@@@@@@ @ @@@
@@@@@ @@@@@@@@ @@@@
@@@ @ @@@@@@@@@@@@@@@@@@
@@@ @@@@ @@@@@@@@@@@@@@@@
@@ @@@@@@ @@@@@@@@@@@@
@@@@@@@@ @@@@@@@@
@@@@@@@@@@@ @@@
@@@@@@@@@@@@@
`;
const items = logo.split('\n').map((row) => logoGradient(row));
/* eslint-disable no-console */
console.log(chalk.bold(items.join('\n')));
console.log(chalk.bold(` Hi, I'm Jarvis by NOVU!`));
console.log(chalk.bold(textGradient(` Welcome to the codebase of the open-source notification infrastructure\n`)));
/* eslint-enable no-console */
}