mirror of
https://github.com/kevin-DL/sapper.git
synced 2026-01-21 23:05:02 +00:00
use port-authority
This commit is contained in:
@@ -8,7 +8,7 @@ import mkdirp from 'mkdirp';
|
|||||||
import rimraf from 'rimraf';
|
import rimraf from 'rimraf';
|
||||||
import format_messages from 'webpack-format-messages';
|
import format_messages from 'webpack-format-messages';
|
||||||
import prettyMs from 'pretty-ms';
|
import prettyMs from 'pretty-ms';
|
||||||
import * as port_utils from './port-utils';
|
import * as ports from 'port-authority';
|
||||||
import { dest } from '../config';
|
import { dest } from '../config';
|
||||||
import { create_compilers, create_app, create_routes, create_serviceworker } from 'sapper/core.js';
|
import { create_compilers, create_app, create_routes, create_serviceworker } from 'sapper/core.js';
|
||||||
|
|
||||||
@@ -77,7 +77,7 @@ export default async function dev(port: number) {
|
|||||||
rimraf.sync(dir);
|
rimraf.sync(dir);
|
||||||
mkdirp.sync(dir);
|
mkdirp.sync(dir);
|
||||||
|
|
||||||
const dev_port = await port_utils.find(10000);
|
const dev_port = await ports.find(10000);
|
||||||
|
|
||||||
const routes = create_routes();
|
const routes = create_routes();
|
||||||
create_app({ routes, dev_port });
|
create_app({ routes, dev_port });
|
||||||
@@ -205,7 +205,7 @@ export default async function dev(port: number) {
|
|||||||
|
|
||||||
deferreds.client.promise.then(() => {
|
deferreds.client.promise.then(() => {
|
||||||
function restart() {
|
function restart() {
|
||||||
port_utils.wait(3000).then(deferreds.server.fulfil); // TODO control port
|
ports.wait(3000).then(deferreds.server.fulfil); // TODO control port
|
||||||
}
|
}
|
||||||
|
|
||||||
if (proc) {
|
if (proc) {
|
||||||
|
|||||||
@@ -5,7 +5,7 @@ import express from 'express';
|
|||||||
import cheerio from 'cheerio';
|
import cheerio from 'cheerio';
|
||||||
import URL from 'url-parse';
|
import URL from 'url-parse';
|
||||||
import fetch from 'node-fetch';
|
import fetch from 'node-fetch';
|
||||||
import * as port_utils from './port-utils';
|
import * as ports from 'port-authority';
|
||||||
import { dest } from '../config';
|
import { dest } from '../config';
|
||||||
|
|
||||||
const app = express();
|
const app = express();
|
||||||
@@ -27,7 +27,7 @@ export default async function exporter(export_dir: string) {
|
|||||||
sander.copyFileSync(build_dir, 'service-worker.js').to(export_dir, 'service-worker.js');
|
sander.copyFileSync(build_dir, 'service-worker.js').to(export_dir, 'service-worker.js');
|
||||||
}
|
}
|
||||||
|
|
||||||
const port = await port_utils.find(3000);
|
const port = await ports.find(3000);
|
||||||
|
|
||||||
const origin = `http://localhost:${port}`;
|
const origin = `http://localhost:${port}`;
|
||||||
|
|
||||||
@@ -36,6 +36,7 @@ export default async function exporter(export_dir: string) {
|
|||||||
env: {
|
env: {
|
||||||
PORT: port,
|
PORT: port,
|
||||||
NODE_ENV: 'production',
|
NODE_ENV: 'production',
|
||||||
|
SAPPER_DEST: build_dir,
|
||||||
SAPPER_EXPORT: 'true'
|
SAPPER_EXPORT: 'true'
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
@@ -88,7 +89,7 @@ export default async function exporter(export_dir: string) {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
return port_utils.wait(port)
|
return ports.wait(port)
|
||||||
.then(() => handle(new URL(origin))) // TODO all static routes
|
.then(() => handle(new URL(origin))) // TODO all static routes
|
||||||
.then(() => proc.kill());
|
.then(() => proc.kill());
|
||||||
}
|
}
|
||||||
@@ -9,7 +9,7 @@ import build from './build';
|
|||||||
import exporter from './export';
|
import exporter from './export';
|
||||||
import dev from './dev';
|
import dev from './dev';
|
||||||
import upgrade from './upgrade';
|
import upgrade from './upgrade';
|
||||||
import * as port_utils from './port-utils';
|
import * as ports from 'port-authority';
|
||||||
import { exists } from '../utils';
|
import { exists } from '../utils';
|
||||||
import * as pkg from '../../package.json';
|
import * as pkg from '../../package.json';
|
||||||
|
|
||||||
@@ -22,12 +22,12 @@ prog.command('dev')
|
|||||||
.option('-p, --port', 'Specify a port')
|
.option('-p, --port', 'Specify a port')
|
||||||
.action(async ({ port }: { port: number }) => {
|
.action(async ({ port }: { port: number }) => {
|
||||||
if (port) {
|
if (port) {
|
||||||
if (!await port_utils.check(port)) {
|
if (!await ports.check(port)) {
|
||||||
console.log(chalk.bold.red(`> Port ${port} is unavailable`));
|
console.log(chalk.bold.red(`> Port ${port} is unavailable`));
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
port = await port_utils.find(3000);
|
port = await ports.find(3000);
|
||||||
}
|
}
|
||||||
|
|
||||||
dev(port);
|
dev(port);
|
||||||
@@ -66,12 +66,12 @@ prog.command('start [dir]')
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (port) {
|
if (port) {
|
||||||
if (!await port_utils.check(port)) {
|
if (!await ports.check(port)) {
|
||||||
console.log(chalk.bold.red(`> Port ${port} is unavailable`));
|
console.log(chalk.bold.red(`> Port ${port} is unavailable`));
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
port = await port_utils.find(3000);
|
port = await ports.find(3000);
|
||||||
}
|
}
|
||||||
|
|
||||||
child_process.fork(server, [], {
|
child_process.fork(server, [], {
|
||||||
|
|||||||
@@ -1,66 +0,0 @@
|
|||||||
import * as net from 'net';
|
|
||||||
|
|
||||||
export function check(port: number) {
|
|
||||||
return new Promise(fulfil => {
|
|
||||||
const server = net.createServer();
|
|
||||||
|
|
||||||
server.unref();
|
|
||||||
|
|
||||||
server.on('error', () => {
|
|
||||||
fulfil(false);
|
|
||||||
});
|
|
||||||
|
|
||||||
server.listen({ port }, () => {
|
|
||||||
server.close(() => {
|
|
||||||
fulfil(true);
|
|
||||||
});
|
|
||||||
});
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
export function find(port: number): Promise<number> {
|
|
||||||
return new Promise((fulfil) => {
|
|
||||||
get_port(port, fulfil);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
function get_port(port: number, cb: (port: number) => void) {
|
|
||||||
const server = net.createServer();
|
|
||||||
|
|
||||||
server.unref();
|
|
||||||
|
|
||||||
server.on('error', () => {
|
|
||||||
get_port(port + 1, cb);
|
|
||||||
});
|
|
||||||
|
|
||||||
server.listen({ port }, () => {
|
|
||||||
server.close(() => {
|
|
||||||
cb(port);
|
|
||||||
});
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
export function wait(port: number, timeout = 5000) {
|
|
||||||
return new Promise((fulfil, reject) => {
|
|
||||||
get_connection(port, fulfil);
|
|
||||||
setTimeout(() => reject(new Error(`timed out waiting for connection`)), timeout);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
function get_connection(port: number, cb: () => void) {
|
|
||||||
const socket = net.connect(port, 'localhost', () => {
|
|
||||||
cb();
|
|
||||||
socket.destroy();
|
|
||||||
});
|
|
||||||
|
|
||||||
socket.on('error', err => {
|
|
||||||
setTimeout(() => {
|
|
||||||
get_connection(port, cb);
|
|
||||||
}, 10);
|
|
||||||
});
|
|
||||||
|
|
||||||
setTimeout(() => {
|
|
||||||
socket.destroy();
|
|
||||||
}, 1000);
|
|
||||||
}
|
|
||||||
|
|
||||||
Reference in New Issue
Block a user