-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathdev-vite.ts
More file actions
161 lines (139 loc) · 4.23 KB
/
Copy pathdev-vite.ts
File metadata and controls
161 lines (139 loc) · 4.23 KB
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
#!/usr/bin/env bun
import { spawn } from "bun";
import { join } from "path";
import { detectDevInstance, resolveDevPort } from "./dev-instance";
const ROOT_DIR = join(import.meta.dir, "..");
const IS_WINDOWS = process.platform === "win32";
const BIN_EXT = IS_WINDOWS ? ".exe" : "";
const VITE_BIN = join(ROOT_DIR, `node_modules/.bin/vite${BIN_EXT}`);
type Options = {
config: string;
defaultPort: number;
label: string;
portEnv?: string;
open?: string;
strictPort: boolean;
passThrough: string[];
};
function printUsage(): void {
console.log(`Usage: bun run scripts/dev-vite.ts --config <path> --default-port <port> [options]
Options:
--label <name> Name shown in logs.
--port-env <name> Environment variable that can override the port.
--open <path> Path or URL to pass to Vite --open.
--no-strict-port Allow Vite to choose the next free port.
-- Pass the remaining args through to Vite.`);
}
function readArg(argv: string[], index: number, name: string): string {
const value = argv[index + 1];
if (!value || value.startsWith("--")) {
throw new Error(`Missing value for ${name}`);
}
return value;
}
function parseArgs(argv: string[]): Options {
const options: Partial<Options> = {
strictPort: true,
passThrough: [],
};
for (let index = 0; index < argv.length; index += 1) {
const arg = argv[index];
if (arg === "--") {
options.passThrough = argv.slice(index + 1);
break;
}
switch (arg) {
case "--help":
case "-h":
printUsage();
process.exit(0);
case "--config":
options.config = readArg(argv, index, arg);
index += 1;
break;
case "--default-port": {
const rawPort = readArg(argv, index, arg);
const port = Number(rawPort);
if (!Number.isInteger(port) || port < 1 || port > 65535) {
throw new Error(`Invalid --default-port: ${rawPort}`);
}
options.defaultPort = port;
index += 1;
break;
}
case "--label":
options.label = readArg(argv, index, arg);
index += 1;
break;
case "--port-env":
options.portEnv = readArg(argv, index, arg);
index += 1;
break;
case "--open":
options.open = readArg(argv, index, arg);
index += 1;
break;
case "--no-strict-port":
options.strictPort = false;
break;
default:
throw new Error(`Unknown option: ${arg}`);
}
}
if (!options.config) throw new Error("Missing --config");
if (!options.defaultPort) throw new Error("Missing --default-port");
return {
config: options.config,
defaultPort: options.defaultPort,
label: options.label || options.config,
portEnv: options.portEnv,
open: options.open,
strictPort: options.strictPort ?? true,
passThrough: options.passThrough || [],
};
}
function describePort(options: Options, port: number): void {
const instance = detectDevInstance(ROOT_DIR);
if (instance) {
console.log(`🔢 ${instance.source} detected (${instance.label}): ${options.label} port=${port}`);
return;
}
if (options.portEnv && process.env[options.portEnv]) {
console.log(`🔌 Starting ${options.label} on ${options.portEnv}=${port}`);
return;
}
console.log(`🔌 Starting ${options.label} on port ${port}`);
}
async function main(): Promise<void> {
const options = parseArgs(process.argv.slice(2));
const resolvedPort = resolveDevPort(ROOT_DIR, options.defaultPort, options.portEnv);
const port = String(resolvedPort.port);
if (options.portEnv) {
process.env[options.portEnv] ||= port;
}
describePort(options, resolvedPort.port);
const cmd = [
VITE_BIN,
"dev",
"--config",
options.config,
"--port",
port,
...(options.strictPort ? ["--strictPort"] : []),
...(options.open ? ["--open", options.open] : []),
...options.passThrough,
];
const proc = spawn({
cmd,
cwd: ROOT_DIR,
stdin: "inherit",
stdout: "inherit",
stderr: "inherit",
env: process.env as Record<string, string>,
});
process.exit(await proc.exited);
}
main().catch((error) => {
console.error(error instanceof Error ? error.message : String(error));
process.exit(1);
});