select random ports if defaults are taken (#695)

* select random port if default ports are taken

* add changeset
This commit is contained in:
Mihkel Eidast 2021-07-15 00:00:21 +03:00 committed by GitHub
parent 5814f2faca
commit fb8bf7ec43
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 17 additions and 6 deletions

View file

@ -0,0 +1,5 @@
---
'astro': patch
---
Allow multiple Astro servers to be running simultaneously by choosing random ports if the defaults are taken.

View file

@ -68,6 +68,7 @@
"fast-xml-parser": "^3.19.0",
"fdir": "^5.0.0",
"find-up": "^5.0.0",
"get-port": "^5.1.1",
"gzip-size": "^6.0.0",
"hast-to-hyperscript": "~9.0.0",
"kleur": "^4.1.4",

View file

@ -1,4 +1,5 @@
const { readFile } = require('fs').promises;
const getPort = require('get-port');
// Snowpack plugins must be CommonJS :(
const transformPromise = import('./dist/compiler/index.js');
@ -48,9 +49,12 @@ module.exports = (snowpackConfig, options = {}) => {
configManager.markDirty();
}
},
config(snowpackConfig) {
async config(snowpackConfig) {
if(!isNaN(snowpackConfig.devOptions.hmrPort)) {
hmrPort = snowpackConfig.devOptions.hmrPort;
} else {
hmrPort = await getPort({ port: DEFAULT_HMR_PORT, host: snowpackConfig.devOptions.hostname });
snowpackConfig.devOptions.hmrPort = hmrPort;
}
},
async load({ filePath }) {

View file

@ -1,6 +1,7 @@
import type { AstroConfig } from './@types/astro';
import path from 'path';
import { existsSync } from 'fs';
import getPort from 'get-port';
/** Type util */
const type = (thing: any): string => (Array.isArray(thing) ? 'Array' : typeof thing);
@ -48,7 +49,7 @@ function validateConfig(config: any): void {
}
/** Set default config values */
function configDefaults(userConfig?: any): any {
async function configDefaults(userConfig?: any): Promise<any> {
const config: any = { ...(userConfig || {}) };
if (!config.projectRoot) config.projectRoot = '.';
@ -57,7 +58,7 @@ function configDefaults(userConfig?: any): any {
if (!config.dist) config.dist = './dist';
if (!config.public) config.public = './public';
if (!config.devOptions) config.devOptions = {};
if (!config.devOptions.port) config.devOptions.port = 3000;
if (!config.devOptions.port) config.devOptions.port = await getPort({ port: getPort.makeRange(3000, 3050) });
if (!config.buildOptions) config.buildOptions = {};
if (!config.markdownOptions) config.markdownOptions = {};
if (typeof config.buildOptions.sitemap === 'undefined') config.buildOptions.sitemap = true;
@ -86,9 +87,9 @@ export async function loadConfig(rawRoot: string | undefined, configFileName = '
// load
let config: any;
if (existsSync(astroConfigPath)) {
config = configDefaults((await import(astroConfigPath.href)).default);
config = await configDefaults((await import(astroConfigPath.href)).default);
} else {
config = configDefaults();
config = await configDefaults();
}
// validate

View file

@ -4988,7 +4988,7 @@ get-pkg-repo@^1.0.0:
get-port@^5.1.1:
version "5.1.1"
resolved "https://registry.npmjs.org/get-port/-/get-port-5.1.1.tgz"
resolved "https://registry.yarnpkg.com/get-port/-/get-port-5.1.1.tgz#0469ed07563479de6efb986baf053dcd7d4e3193"
integrity sha512-g/Q1aTSDOxFpchXC4i8ZWvxA1lnPqx/JHqcpIw0/LX9T8x/GBbi6YnlN5nhaKIFkT8oFsscUKgDJYxfwfS6QsQ==
get-stdin@^4.0.1: