-
Notifications
You must be signed in to change notification settings - Fork 86
/
dev.ts
executable file
·98 lines (80 loc) · 2.77 KB
/
dev.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
91
92
93
94
95
96
97
98
#!/usr/bin/env ts-node-script
import ora from 'ora';
import execa from 'execa';
import TscWatchClient from 'tsc-watch/client';
import chalk from 'chalk';
import path from 'path';
type CatchFn<TResult = never> = (reason: any) => TResult | PromiseLike<TResult>;
function handleError<TResult = never>(spinner?: ora.Ora, message?: string): CatchFn<TResult>;
function handleError<TResult = never>(message?: string): CatchFn<TResult>;
function handleError<TResult = never>(messageOrSpinner?: ora.Ora | string, message?: string): CatchFn<TResult> {
return (err: any) => {
if (messageOrSpinner) {
if (typeof messageOrSpinner === 'string') {
console.error(messageOrSpinner);
} else {
messageOrSpinner.fail(message);
}
}
if (err) console.error(err);
process.exit(1);
};
}
async function getTSConfigs() {
const spinner = ora('Determining TypeScript projects to build for development...').start();
const [tsconfigs, allPkgs] = await Promise.all([
execa('yarn', ['--silent', 'paths', 'tsconfig.json', 'tsconfig.cjs.json']).then((subprocess) =>
subprocess.stdout.split('\n'),
),
execa('yarn', ['--silent', 'paths']).then((subprocess) => subprocess.stdout.split('\n')),
])
.then((res) => {
spinner.succeed('Found TypeScript projects to build for development!');
return res;
})
.catch(handleError(spinner, 'Failed to discover TypeScript project to build.'));
return { tsconfigs, allPkgs };
}
async function injectENV(allPkgs: string[]) {
await execa(`${process.env.INIT_CWD}/scripts/inject-env.ts`, [...allPkgs]).then(() => {
console.log('Environment variables successfully injected!');
});
}
async function runDevServer(tsconfigs: string[], allPkgs: string[]) {
const spinner = ora();
[
[5, 0],
[4, 1000],
[3, 2000],
[2, 3000],
[1, 4000],
].forEach(([countdown, timeout]) => {
if (!spinner.isSpinning) spinner.start();
setTimeout(() => {
spinner.text = `Starting TypeScript development server in ${countdown}...`;
}, timeout);
});
setTimeout(() => {
spinner.succeed('Starting TypeScript development server...');
const watch = new TscWatchClient();
watch.on('success', async () => injectENV(allPkgs));
watch.start('-b', ...tsconfigs);
}, 5000);
}
async function main() {
const { tsconfigs, allPkgs } = await getTSConfigs();
console.log(
tsconfigs
.slice()
.sort()
.map((cfgPath) => {
const prefix = 'packages/';
const basename = `/${path.basename(cfgPath)}`;
return cfgPath.replace(prefix, chalk`{gray ${prefix}}`).replace(basename, chalk`{gray ${basename}}`);
})
.reduce((prev, next) => chalk`${prev}\n {gray -} ${next}`, ''),
'\n',
);
await runDevServer(tsconfigs, allPkgs);
}
main();