/
cli.js
executable file
·259 lines (218 loc) · 7.1 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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
import sade from 'sade';
import colors from 'kleur';
import fs from 'fs';
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';
import { logger } from './core/utils.js';
/** @param {unknown} e */
function handle_error(e) {
const error = coalesce_to_error(e);
if (error.name === 'SyntaxError') throw error;
console.error(colors.bold().red(`> ${error.message}`));
if (error.stack) {
console.error(colors.gray(error.stack.split('\n').slice(1).join('\n')));
}
process.exit(1);
}
/**
* @param {number} port
* @param {boolean} https
* @param {string} base
*/
async function launch(port, https, base) {
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}${base}`);
}
const prog = sade('svelte-kit').version('__VERSION__');
prog
.command('dev')
.describe('Start a development server')
.option('-p, --port', 'Port')
.option('-o, --open', 'Open a browser tab')
.option('--host', 'Host (only use this on trusted networks)')
.option('--https', 'Use self-signed HTTPS certificate')
.option('-H', 'no longer supported, use --https instead') // TODO remove for 1.0
.action(async ({ port, host, https, open, H }) => {
try {
if (H) throw new Error('-H is no longer supported — use --https instead');
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,
base: config.kit.paths.base,
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 log = logger({ verbose });
const { build } = await import('./core/build/index.js');
const { build_data, prerendered } = await build(config, { log });
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, prerendered, { log });
// 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('-o, --open', 'Open a browser tab', false)
.option('--host', 'Host (only use this on trusted networks)', 'localhost')
.option('--https', 'Use self-signed HTTPS certificate', false)
.option('-H', 'no longer supported, use --https instead') // TODO remove for 1.0
.action(async ({ port, host, https, open, H }) => {
try {
if (H) throw new Error('-H is no longer supported — use --https instead');
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, base: config.kit.paths.base });
} catch (error) {
handle_error(error);
}
});
prog
.command('package')
.describe('Create a 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
.command('sync')
.describe('Synchronise generated files')
.action(async () => {
if (!fs.existsSync('svelte.config.js')) {
console.warn('Missing svelte.config.js — skipping');
return;
}
try {
const config = await load_config();
const sync = await import('./core/sync/sync.js');
sync.all(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.error(colors.bold().red(`Port ${port} is occupied`));
const n = await ports.blame(port);
if (n) {
// prettier-ignore
console.error(
`Terminate process ${colors.bold(n)} or specify a different port with ${colors.bold('--port')}\n`
);
} else {
// prettier-ignore
console.error(
`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;
* base: string;
* loose?: boolean;
* allow?: string[];
* cwd?: string;
* }} param0
*/
function welcome({ port, host, https, open, base, loose, allow, cwd }) {
if (open) launch(port, https, base);
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');
}