Add PORT env var to be used (#952)
* Add PORT env var to be used * Format * Create few-carpets-sing.md * Update few-carpets-sing.md
This commit is contained in:
parent
fcc56a8672
commit
939b9d01a4
2 changed files with 42 additions and 7 deletions
5
.changeset/few-carpets-sing.md
Normal file
5
.changeset/few-carpets-sing.md
Normal file
|
@ -0,0 +1,5 @@
|
||||||
|
---
|
||||||
|
"astro": patch
|
||||||
|
---
|
||||||
|
|
||||||
|
Allow dev server port to be set by `PORT` environment variable
|
|
@ -35,14 +35,44 @@ interface CLIState {
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type TypeOf = 'string' | 'boolean' | 'number';
|
||||||
|
// Validates property types of object
|
||||||
|
const validateOptions = (opts: Record<string, any>, specs: Record<string, TypeOf | [(v: any) => unknown, TypeOf]>) =>
|
||||||
|
Object.entries(specs).reduce<Record<string, any>>((options, [k, spec]) => {
|
||||||
|
const v = opts[k];
|
||||||
|
|
||||||
|
if (typeof spec === 'string' && typeof v === spec) {
|
||||||
|
options[k] = v;
|
||||||
|
} else if (Array.isArray(spec)) {
|
||||||
|
const [coercion, test] = spec;
|
||||||
|
const result = coercion(v);
|
||||||
|
if (typeof result === test) {
|
||||||
|
options[k] = result;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return options;
|
||||||
|
}, {});
|
||||||
|
|
||||||
/** Determine which action the user requested */
|
/** Determine which action the user requested */
|
||||||
function resolveArgs(flags: Arguments): CLIState {
|
function resolveArgs(flags: Arguments): CLIState {
|
||||||
const options: CLIState['options'] = {
|
const { PORT } = process.env;
|
||||||
projectRoot: typeof flags.projectRoot === 'string' ? flags.projectRoot : undefined,
|
|
||||||
site: typeof flags.site === 'string' ? flags.site : undefined,
|
// Merge options (Flags take priority)
|
||||||
sitemap: typeof flags.sitemap === 'boolean' ? flags.sitemap : undefined,
|
const options = {
|
||||||
port: typeof flags.port === 'number' ? flags.port : undefined,
|
...validateOptions(
|
||||||
config: typeof flags.config === 'string' ? flags.config : undefined,
|
{ port: PORT },
|
||||||
|
{
|
||||||
|
port: [parseInt, 'number'],
|
||||||
|
}
|
||||||
|
),
|
||||||
|
...validateOptions(flags, {
|
||||||
|
projectRoot: 'string',
|
||||||
|
site: 'string',
|
||||||
|
sitemap: 'boolean',
|
||||||
|
port: 'number',
|
||||||
|
config: 'string',
|
||||||
|
}),
|
||||||
};
|
};
|
||||||
|
|
||||||
if (flags.version) {
|
if (flags.version) {
|
||||||
|
@ -101,7 +131,7 @@ function mergeCLIFlags(astroConfig: AstroConfig, flags: CLIState['options']) {
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Handle `astro run` command */
|
/** Handle `astro run` command */
|
||||||
async function runCommand(rawRoot: string, cmd: (a: AstroConfig, options: any) => Promise<void>, options: CLIState['options']) {
|
async function runCommand(rawRoot: string, cmd: (a: AstroConfig, opts: any) => Promise<void>, options: CLIState['options']) {
|
||||||
try {
|
try {
|
||||||
const projectRoot = options.projectRoot || rawRoot;
|
const projectRoot = options.projectRoot || rawRoot;
|
||||||
const astroConfig = await loadConfig(projectRoot, options.config);
|
const astroConfig = await loadConfig(projectRoot, options.config);
|
||||||
|
|
Loading…
Reference in a new issue