implement --open - fixes #186

This commit is contained in:
Rich Harris
2018-03-11 16:01:13 -04:00
parent ad14320dc3
commit 69f5b9cac7
3 changed files with 18 additions and 4 deletions

View File

@@ -13,7 +13,8 @@ const prog = sade('sapper').version(pkg.version);
prog.command('dev') prog.command('dev')
.describe('Start a development server') .describe('Start a development server')
.option('-p, --port', 'Specify a port') .option('-p, --port', 'Specify a port')
.action(async (opts: { port: number }) => { .option('-o, --open', 'Open a browser window')
.action(async (opts: { port: number, open: boolean }) => {
const { dev } = await import('./cli/dev'); const { dev } = await import('./cli/dev');
dev(opts); dev(opts);
}); });
@@ -40,7 +41,8 @@ prog.command('build [dest]')
prog.command('start [dir]') prog.command('start [dir]')
.describe('Start your app') .describe('Start your app')
.option('-p, --port', 'Specify a port') .option('-p, --port', 'Specify a port')
.action(async (dir = 'build', opts: { port: number }) => { .option('-o, --open', 'Open a browser window')
.action(async (dir = 'build', opts: { port: number, open: boolean }) => {
const { start } = await import('./cli/start'); const { start } = await import('./cli/start');
start(dir, opts); start(dir, opts);
}); });

View File

@@ -70,7 +70,7 @@ function create_hot_update_server(port: number, interval = 10000) {
return { send }; return { send };
} }
export async function dev(opts: { port: number }) { export async function dev(opts: { port: number, open: boolean }) {
process.env.NODE_ENV = 'development'; process.env.NODE_ENV = 'development';
let port = opts.port || +process.env.PORT; let port = opts.port || +process.env.PORT;
@@ -241,6 +241,8 @@ export async function dev(opts: { port: number }) {
} }
}); });
let first = true;
watch(compilers.client, { watch(compilers.client, {
name: 'client', name: 'client',
@@ -263,6 +265,12 @@ export async function dev(opts: { port: number }) {
hot_update_server.send({ hot_update_server.send({
status: 'completed' status: 'completed'
}); });
if (first) {
first = false;
console.log(`${clorox.bold.cyan(`> Listening on localhost:${port}`)}`);
if (opts.open) child_process.exec(`open http://localhost:${port}`);
}
}); });
create_serviceworker_manifest({ create_serviceworker_manifest({

View File

@@ -4,7 +4,7 @@ import * as child_process from 'child_process';
import * as clorox from 'clorox'; import * as clorox from 'clorox';
import * as ports from 'port-authority'; import * as ports from 'port-authority';
export async function start(dir: string, opts: { port: number }) { export async function start(dir: string, opts: { port: number, open: boolean }) {
let port = opts.port || +process.env.PORT; let port = opts.port || +process.env.PORT;
const resolved = path.resolve(dir); const resolved = path.resolve(dir);
@@ -32,4 +32,8 @@ export async function start(dir: string, opts: { port: number }) {
SAPPER_DEST: dir SAPPER_DEST: dir
}, process.env) }, process.env)
}); });
await ports.wait(port);
console.log(`${clorox.bold.cyan(`> Listening on localhost:${port}`)}`);
if (opts.open) child_process.exec(`open http://localhost:${port}`);
} }