-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathstart.ts
90 lines (77 loc) · 2.29 KB
/
start.ts
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
const fs = require('fs');
const path = require('path');
const util = require('node:util');
const execFile = util.promisify(require('node:child_process').execFile);
const { spawn } = require('child_process');
const { execSync } = require('child_process');
const prompts = require('prompts');
const pkgsPath = path.join(__dirname, '..', 'apps');
const dirs = fs.readdirSync(pkgsPath);
const choices = [];
enum AppType {
API = '🧰',
CMS = '📃',
FRONTEND = '🖥️',
}
function exec(command, appType: AppType) {
command = command.replace(/\\?\n/g, ''); // need to merge multi-line commands into one string
const spawn = require('child_process').spawn;
const childProcess = spawn(command, {
stdio: 'pipe',
shell: true,
});
return new Promise((resolve, reject) => {
let stdout = '';
childProcess.stdout.on('data', (data) => {
console.log(`${appType}: ${data.toString()}`);
});
childProcess.on('error', function (error) {
console.log(error.toString());
reject({ code: 1, error: error });
});
childProcess.on('close', function (code) {
console.log('Command exited with code ' + code);
if (code > 0) {
reject('Command failed with code ' + code);
} else {
resolve({ code: code, data: stdout });
}
});
}).catch((err) => {
console.error(err);
throw new Error(err);
});
}
/**
* Get config data for all app packages and prompt for which one to use in dev instance.
*/
(async () => {
// Do not include 'cms' or 'api' package
const dirsFiltered = dirs.filter((name: string) => {
return name !== 'cms' && name !== 'api';
});
dirsFiltered.forEach((name: string) => {
if (fs.statSync(path.join(pkgsPath, name)).isDirectory()) {
// Obj for usage in choices
choices.push({
title: name,
description: name,
value: name,
});
}
});
const response = await prompts({
type: 'select',
name: 'value',
message: 'Pick an app to run:',
choices,
});
try {
exec('npm run watch', AppType.FRONTEND);
exec('cd apps/api; npm run dev', AppType.API);
exec(`cd apps/cms; yarn dev --app ${response.value}`, AppType.CMS);
exec(`cd apps/${response.value}; yarn dev`, AppType.FRONTEND);
} catch (error) {
console.error(error);
}
})();