-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
/
cli.js
executable file
·229 lines (193 loc) · 6.21 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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
import sade from 'sade';
import colors from 'kleur';
import { relative } from 'path';
import * as ports from 'port-authority';
import { load_config } from './core/config/index.js';
import { networkInterfaces, release } from 'os';
import { coalesce_to_error } from './utils/error.js';
/** @param {unknown} e */
function handle_error(e) {
const error = coalesce_to_error(e);
if (error.name === 'SyntaxError') throw error;
console.log(colors.bold().red(`> ${error.message}`));
if (error.stack) {
console.log(colors.gray(error.stack.split('\n').slice(1).join('\n')));
}
process.exit(1);
}
/**
* @param {number} port
* @param {boolean} https
*/
async function launch(port, https) {
const { exec } = await import('child_process');
let cmd = 'open';
if (process.platform == 'win32') {
cmd = 'start';
} else if (process.platform == 'linux') {
if (/microsoft/i.test(release())) {
cmd = 'cmd.exe /c start';
} else {
cmd = 'xdg-open';
}
}
exec(`${cmd} ${https ? 'https' : 'http'}://localhost:${port}`);
}
const prog = sade('svelte-kit').version('__VERSION__');
prog
.command('dev')
.describe('Start a development server')
.option('-p, --port', 'Port')
.option('-h, --host', 'Host (only use this on trusted networks)')
.option('-H, --https', 'Use self-signed HTTPS certificate')
.option('-o, --open', 'Open a browser tab')
.action(async ({ port, host, https, open }) => {
try {
process.env.NODE_ENV = process.env.NODE_ENV || 'development';
const config = await load_config();
const { dev } = await import('./core/dev/index.js');
const cwd = process.cwd();
const { address_info, server_config } = await dev({
cwd,
port,
host,
https,
config
});
welcome({
port: address_info.port,
host: address_info.address,
https: !!(https || server_config.https),
open: open || !!server_config.open,
loose: server_config.fs.strict === false,
allow: server_config.fs.allow,
cwd
});
} catch (error) {
handle_error(error);
}
});
prog
.command('build')
.describe('Create a production build of your app')
.option('--verbose', 'Log more stuff', false)
.action(async ({ verbose }) => {
try {
process.env.NODE_ENV = process.env.NODE_ENV || 'production';
const config = await load_config();
const { build } = await import('./core/build/index.js');
const build_data = await build(config);
console.log(
`\nRun ${colors.bold().cyan('npm run preview')} to preview your production build locally.`
);
if (config.kit.adapter) {
const { adapt } = await import('./core/adapt/index.js');
await adapt(config, build_data, { verbose });
// this is necessary to close any open db connections, etc
process.exit(0);
}
console.log(colors.bold().yellow('\nNo adapter specified'));
// prettier-ignore
console.log(
`See ${colors.bold().cyan('https://kit.svelte.dev/docs#adapters')} to learn how to configure your app to run on the platform of your choosing`
);
} catch (error) {
handle_error(error);
}
});
prog
.command('preview')
.describe('Serve an already-built app')
.option('-p, --port', 'Port', 3000)
.option('-h, --host', 'Host (only use this on trusted networks)', 'localhost')
.option('-H, --https', 'Use self-signed HTTPS certificate', false)
.option('-o, --open', 'Open a browser tab', false)
.action(async ({ port, host, https, open }) => {
try {
await check_port(port);
process.env.NODE_ENV = process.env.NODE_ENV || 'production';
const config = await load_config();
const { preview } = await import('./core/preview/index.js');
await preview({ port, host, config, https });
welcome({ port, host, https, open });
} catch (error) {
handle_error(error);
}
});
prog
.command('package')
.describe('Create a package')
.option('-d, --dir', 'Destination directory', 'package')
.action(async () => {
try {
const config = await load_config();
const { make_package } = await import('./packaging/index.js');
await make_package(config);
} catch (error) {
handle_error(error);
}
});
prog.parse(process.argv, { unknown: (arg) => `Unknown option: ${arg}` });
/** @param {number} port */
async function check_port(port) {
if (await ports.check(port)) {
return;
}
console.log(colors.bold().red(`Port ${port} is occupied`));
const n = await ports.blame(port);
if (n) {
// prettier-ignore
console.log(
`Terminate process ${colors.bold(n)} or specify a different port with ${colors.bold('--port')}\n`
);
} else {
// prettier-ignore
console.log(
`Terminate the process occupying the port or specify a different port with ${colors.bold('--port')}\n`
);
}
process.exit(1);
}
/**
* @param {{
* open: boolean;
* host: string;
* https: boolean;
* port: number;
* loose?: boolean;
* allow?: string[];
* cwd?: string;
* }} param0
*/
function welcome({ port, host, https, open, loose, allow, cwd }) {
if (open) launch(port, https);
console.log(colors.bold().cyan(`\n SvelteKit v${'__VERSION__'}\n`));
const protocol = https ? 'https:' : 'http:';
const exposed = typeof host !== 'undefined' && host !== 'localhost' && host !== '127.0.0.1';
Object.values(networkInterfaces()).forEach((interfaces) => {
if (!interfaces) return;
interfaces.forEach((details) => {
if (details.family !== 'IPv4') return;
// prettier-ignore
if (details.internal) {
console.log(` ${colors.gray('local: ')} ${protocol}//${colors.bold(`localhost:${port}`)}`);
} else {
if (details.mac === '00:00:00:00:00:00') return;
if (exposed) {
console.log(` ${colors.gray('network:')} ${protocol}//${colors.bold(`${details.address}:${port}`)}`);
if (loose) {
console.log(`\n ${colors.yellow('Serving with vite.server.fs.strict: false. Note that all files on your machine will be accessible to anyone on your network.')}`);
} else if (allow?.length && cwd) {
console.log(`\n ${colors.yellow('Note that all files in the following directories will be accessible to anyone on your network: ' + allow.map(a => relative(cwd, a)).join(', '))}`);
}
} else {
console.log(` ${colors.gray('network: not exposed')}`);
}
}
});
});
if (!exposed) {
console.log('\n Use --host to expose server to other devices on this network');
}
console.log('\n');
}