-
Notifications
You must be signed in to change notification settings - Fork 211
/
Copy pathcli.js
executable file
·203 lines (176 loc) · 6 KB
/
cli.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
#!/usr/bin/env node
const inquirer = require('inquirer').default;
const { spawn } = require('child_process');
const fs = require('fs');
const path = require('path');
const ngrok = require('ngrok');
const waitOn = require('wait-on');
const getProjectInfo = (folderPath, projectPath) => {
const packageJsonPath = path.join(__dirname, '..', folderPath, projectPath, 'package.json');
const packageJson = JSON.parse(fs.readFileSync(packageJsonPath, 'utf8'));
return {
name: packageJson.name || projectPath,
value: `${folderPath}/${projectPath}`,
};
};
const projectsAndApps = [
{ folder: 'services', items: ['workflows-service'] },
{ folder: 'apps', items: ['workflows-dashboard', 'kyb-app', 'backoffice-v2'] },
].flatMap(({ folder, items }) => items.map(projectPath => getProjectInfo(folder, projectPath)));
const logsDir = path.join(__dirname, '..', 'logs');
if (!fs.existsSync(logsDir)) {
fs.mkdirSync(logsDir);
}
const getEnvFiles = projectPath => {
const projectDir = path.join(__dirname, '..', projectPath);
return fs
.readdirSync(projectDir)
.filter(file => file.startsWith('.env'))
.map(file => ({ name: file, value: file }));
};
async function main() {
const { mode } = await inquirer.prompt([
{
type: 'list',
name: 'mode',
message: 'Choose mode:',
choices: [
{ name: 'Simple', value: 'simple' },
{ name: 'Advanced', value: 'advanced' },
],
},
]);
const { selectedItems, runMode } = await inquirer.prompt([
{
type: 'checkbox',
name: 'selectedItems',
message: 'Select projects and apps to run:',
choices: [{ name: 'All', value: 'all' }, new inquirer.Separator(), ...projectsAndApps],
default: projectsAndApps.map(item => item.value),
},
{
type: 'list',
name: 'runMode',
message: 'Choose run mode:',
choices: [
{ name: 'Start (no watch)', value: 'start' },
{ name: 'Dev (with watch)', value: 'dev' },
],
},
]);
let envFiles = {};
let useNgrok = false;
let resetDatabase = false;
// Convert 'all' selection to all projects
const finalSelectedItems = selectedItems.includes('all')
? projectsAndApps.map(item => item.value)
: selectedItems;
if (mode === 'advanced') {
const advancedOptions = await inquirer.prompt([
{
type: 'confirm',
name: 'useCommonEnv',
message: 'Use .env for all projects?',
default: true,
},
{
type: 'confirm',
name: 'useNgrok',
message: 'Run ngrok for webhook requests?',
default: false,
},
]);
useNgrok = advancedOptions.useNgrok;
if (advancedOptions.useCommonEnv) {
envFiles = Object.fromEntries(finalSelectedItems.map(item => [item, '.env']));
} else {
for (const item of finalSelectedItems) {
const projectEnvFiles = getEnvFiles(item);
const { envFile } = await inquirer.prompt([
{
type: 'list',
name: 'envFile',
message: `Choose .env file for ${item}:`,
choices: [{ name: '.env (default)', value: '.env' }, ...projectEnvFiles],
default: '.env',
},
]);
envFiles[item] = envFile;
}
}
} else {
envFiles = Object.fromEntries(finalSelectedItems.map(item => [item, '.env']));
}
// Check if workflows-service is selected
if (finalSelectedItems.some(item => item.includes('workflows-service'))) {
const { resetDatabaseConfirm } = await inquirer.prompt([
{
type: 'confirm',
name: 'resetDatabaseConfirm',
message: 'Do you want to reset the database?',
default: false,
},
]);
resetDatabase = resetDatabaseConfirm;
}
const projectFilter = finalSelectedItems.map(item => item.split('/')[1]).join(',');
const workflowsServiceIncluded = finalSelectedItems.some(item =>
item.includes('workflows-service'),
);
const appsIncluded = finalSelectedItems.some(item => item.includes('apps/'));
let command = `nx run-many --target=${runMode} --projects=${projectFilter
.split(',')
.map(project => `@ballerine/${project}`)
.join(',')}`;
if (resetDatabase) {
command = `nx run-many --target=db:reset:dev:with-data --projects=@ballerine/workflows-service --output-style=stream && ${command}`;
}
if (workflowsServiceIncluded && appsIncluded) {
// Remove workflows-service from the command since we'll run it separately
const filteredProjects = projectFilter
.split(',')
.filter(project => project !== 'workflows-service')
.map(project => `@ballerine/${project}`)
.join(',');
const filteredCommand = `nx run-many --target=${runMode} --projects=${filteredProjects}`;
command = `nx run @ballerine/workflows-service:${runMode} & wait-on http://localhost:3000/api/v1/_health/ready && ${filteredCommand}`;
}
const timestamp = new Date().toISOString().replace(/:/g, '-');
const logFile = path.join(logsDir, `nx_run_${timestamp}.log`);
const logStream = fs.createWriteStream(logFile, { flags: 'a' });
console.log('Using .env files:');
Object.entries(envFiles).forEach(([project, envFile]) => {
console.log(` ${project}: ${envFile}`);
});
console.log(`Logs: ${logFile}`);
if (useNgrok) {
console.log('Establishing ngrok tunnel...');
const url = await ngrok.connect(3000);
console.log(`ngrok tunnel established: ${url}`);
}
console.log(`Executing command: ${command}`);
const child = spawn(command, {
shell: true,
cwd: path.join(__dirname, '..'),
env: { ...process.env, PROJECT_ENV_FILES: JSON.stringify(envFiles) },
});
child.stdout.on('data', data => {
process.stdout.write(data);
logStream.write(data);
});
child.stderr.on('data', data => {
process.stderr.write(data);
logStream.write(data);
});
child.on('close', code => {
console.log(`Process exited with code ${code}`);
logStream.end();
if (useNgrok) {
ngrok.kill();
}
});
}
main().catch(error => {
console.error('An error occurred:', error);
process.exit(1);
});