-
Notifications
You must be signed in to change notification settings - Fork 0
/
development.ts
118 lines (91 loc) · 2.75 KB
/
development.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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
import chalk from 'chalk';
import { watch } from 'chokidar';
import { consola } from 'consola';
import { type ChildProcess, exec, execSync, spawn } from 'node:child_process';
import { rmSync } from 'node:fs';
import * as readline from 'node:readline';
import { promisify } from 'node:util';
import treeKill from 'tree-kill';
import { compileTypescript } from './compile.js';
readline.emitKeypressEvents(process.stdin);
const config = {
command: { name: 'pnpm', args: ['run', 'start'] },
watch: ['ts', 'hbs', 'css'],
ignored: ['node_modules', 'dist', '.git', 'development.ts'],
};
const kill = promisify(treeKill);
let running: ChildProcess;
/**
* Starts the process.
*/
async function spawnProcess() {
rmSync('dist', { recursive: true, force: true });
await compileTypescript();
consola.success('Successfully compiled TypeScript and CSS!');
running = spawn(config.command.name, config.command.args, { stdio: 'inherit', shell: true });
}
await spawnProcess();
/**
* Logs a message to the console.
* @param message The message(s) to log.
*/
function logMessage(...message: string[]) {
consola.log(`${chalk.blue('[Auto Reload]:')}`, ...message);
}
/**
* Restarts the process.
*/
async function restartProcess() {
logMessage('Restarting...');
await kill(running.pid!);
spawnProcess();
await new Promise((resolve) => setTimeout(resolve, 500));
}
/**
* Stops the process.
*/
function stopProcess() {
logMessage('Killing process...');
execSync('pnpm run remove-compiled');
process.exit(0); // eslint-disable-line unicorn/no-process-exit
}
/**
* Opens the website in the default browser.
*/
function openWebsite() {
logMessage('Opening website...');
exec(`open http://localhost:${process.env.PORT ?? 3000}`);
}
process.stdin.resume();
if (process.stdin.isTTY) process.stdin.setRawMode(true);
logMessage('Starting!');
logMessage('Press Ctrl+R to reload, Ctrl+C to stop, and Ctrl+O to open the website');
process.stdin.on('keypress', (string, key: { ctrl: boolean; name: string }) => {
if (key.ctrl)
switch (key.name) {
case 'c': {
return stopProcess();
}
case 'r': {
return restartProcess();
}
case 'o': {
return openWebsite();
}
}
});
let restarting = false;
const watcher = watch('.', {
ignored: (path, stats) => {
if (config.ignored.includes(path)) return true;
if (!stats?.isFile()) return false;
if (config.watch.some((extension) => path.endsWith(extension))) return false;
return true;
},
});
watcher.on('change', async () => {
if (restarting) return;
restarting = true;
await restartProcess();
restarting = false;
});