Skip to content
This repository was archived by the owner on Jan 11, 2023. It is now read-only.

Commit 69f5b9c

Browse files
committed
implement --open - fixes #186
1 parent ad14320 commit 69f5b9c

File tree

3 files changed

+18
-4
lines changed

3 files changed

+18
-4
lines changed

src/cli.ts

+4-2
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@ const prog = sade('sapper').version(pkg.version);
1313
prog.command('dev')
1414
.describe('Start a development server')
1515
.option('-p, --port', 'Specify a port')
16-
.action(async (opts: { port: number }) => {
16+
.option('-o, --open', 'Open a browser window')
17+
.action(async (opts: { port: number, open: boolean }) => {
1718
const { dev } = await import('./cli/dev');
1819
dev(opts);
1920
});
@@ -40,7 +41,8 @@ prog.command('build [dest]')
4041
prog.command('start [dir]')
4142
.describe('Start your app')
4243
.option('-p, --port', 'Specify a port')
43-
.action(async (dir = 'build', opts: { port: number }) => {
44+
.option('-o, --open', 'Open a browser window')
45+
.action(async (dir = 'build', opts: { port: number, open: boolean }) => {
4446
const { start } = await import('./cli/start');
4547
start(dir, opts);
4648
});

src/cli/dev.ts

+9-1
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ function create_hot_update_server(port: number, interval = 10000) {
7070
return { send };
7171
}
7272

73-
export async function dev(opts: { port: number }) {
73+
export async function dev(opts: { port: number, open: boolean }) {
7474
process.env.NODE_ENV = 'development';
7575

7676
let port = opts.port || +process.env.PORT;
@@ -241,6 +241,8 @@ export async function dev(opts: { port: number }) {
241241
}
242242
});
243243

244+
let first = true;
245+
244246
watch(compilers.client, {
245247
name: 'client',
246248

@@ -263,6 +265,12 @@ export async function dev(opts: { port: number }) {
263265
hot_update_server.send({
264266
status: 'completed'
265267
});
268+
269+
if (first) {
270+
first = false;
271+
console.log(`${clorox.bold.cyan(`> Listening on localhost:${port}`)}`);
272+
if (opts.open) child_process.exec(`open http://localhost:${port}`);
273+
}
266274
});
267275

268276
create_serviceworker_manifest({

src/cli/start.ts

+5-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import * as child_process from 'child_process';
44
import * as clorox from 'clorox';
55
import * as ports from 'port-authority';
66

7-
export async function start(dir: string, opts: { port: number }) {
7+
export async function start(dir: string, opts: { port: number, open: boolean }) {
88
let port = opts.port || +process.env.PORT;
99

1010
const resolved = path.resolve(dir);
@@ -32,4 +32,8 @@ export async function start(dir: string, opts: { port: number }) {
3232
SAPPER_DEST: dir
3333
}, process.env)
3434
});
35+
36+
await ports.wait(port);
37+
console.log(`${clorox.bold.cyan(`> Listening on localhost:${port}`)}`);
38+
if (opts.open) child_process.exec(`open http://localhost:${port}`);
3539
}

0 commit comments

Comments
 (0)