mirror of
https://github.com/withastro/astro.git
synced 2024-12-23 21:53:55 -05:00
[ci] format
This commit is contained in:
parent
126cd8e83f
commit
50e5e576bc
7 changed files with 60 additions and 76 deletions
|
@ -1,7 +1,6 @@
|
|||
/* eslint-disable no-console */
|
||||
import * as colors from 'kleur/colors';
|
||||
import { pathToFileURL } from 'url';
|
||||
import { normalizePath } from 'vite';
|
||||
import type { Arguments as Flags } from 'yargs-parser';
|
||||
import yargs from 'yargs-parser';
|
||||
import { z } from 'zod';
|
||||
|
@ -12,7 +11,6 @@ import {
|
|||
openConfig,
|
||||
resolveConfigPath,
|
||||
resolveFlags,
|
||||
resolveRoot,
|
||||
} from '../core/config/index.js';
|
||||
import { ASTRO_VERSION } from '../core/constants.js';
|
||||
import devServer from '../core/dev/index.js';
|
||||
|
@ -21,7 +19,6 @@ import { createSafeError } from '../core/errors/index.js';
|
|||
import { debug, error, info, LogOptions } from '../core/logger/core.js';
|
||||
import { enableVerboseLogging, nodeLogDestination } from '../core/logger/node.js';
|
||||
import { formatConfigErrorMessage, formatErrorMessage, printHelp } from '../core/messages.js';
|
||||
import { appendForwardSlash } from '../core/path.js';
|
||||
import preview from '../core/preview/index.js';
|
||||
import * as event from '../events/index.js';
|
||||
import { eventConfigError, eventError, telemetry } from '../events/index.js';
|
||||
|
@ -178,9 +175,7 @@ async function runCommand(cmd: string, flags: yargs.Arguments) {
|
|||
switch (cmd) {
|
||||
case 'dev': {
|
||||
const configFlag = resolveFlags(flags).config;
|
||||
const configFlagPath = configFlag
|
||||
? await resolveConfigPath({ cwd: root, flags })
|
||||
: undefined;
|
||||
const configFlagPath = configFlag ? await resolveConfigPath({ cwd: root, flags }) : undefined;
|
||||
|
||||
await devServer(settings, {
|
||||
configFlag,
|
||||
|
@ -190,7 +185,7 @@ async function runCommand(cmd: string, flags: yargs.Arguments) {
|
|||
handleConfigError(e) {
|
||||
handleConfigError(e, { cwd: root, flags, logging });
|
||||
info(logging, 'astro', 'Continuing with previous valid configuration\n');
|
||||
}
|
||||
},
|
||||
});
|
||||
return await new Promise(() => {}); // lives forever
|
||||
}
|
||||
|
|
|
@ -106,7 +106,7 @@ export function resolveFlags(flags: Partial<Flags>): CLIFlags {
|
|||
}
|
||||
|
||||
export function resolveRoot(cwd?: string | URL): string {
|
||||
if(cwd instanceof URL) {
|
||||
if (cwd instanceof URL) {
|
||||
cwd = fileURLToPath(cwd);
|
||||
}
|
||||
return cwd ? path.resolve(cwd) : process.cwd();
|
||||
|
|
|
@ -7,15 +7,15 @@ import * as vite from 'vite';
|
|||
import {
|
||||
runHookConfigDone,
|
||||
runHookConfigSetup,
|
||||
runHookServerDone,
|
||||
runHookServerSetup,
|
||||
runHookServerStart,
|
||||
runHookServerDone
|
||||
} from '../../integrations/index.js';
|
||||
import { createDefaultDevSettings, resolveRoot } from '../config/index.js';
|
||||
import { createVite } from '../create-vite.js';
|
||||
import { LogOptions } from '../logger/core.js';
|
||||
import { appendForwardSlash } from '../path.js';
|
||||
import { nodeLogDestination } from '../logger/node.js';
|
||||
import { appendForwardSlash } from '../path.js';
|
||||
import { apply as applyPolyfill } from '../polyfill.js';
|
||||
|
||||
const defaultLogging: LogOptions = {
|
||||
|
@ -108,17 +108,13 @@ export async function createContainer(params: CreateContainerParams = {}): Promi
|
|||
// TODO deprecate and remove
|
||||
close() {
|
||||
return closeContainer(container);
|
||||
}
|
||||
},
|
||||
};
|
||||
|
||||
return container;
|
||||
}
|
||||
|
||||
async function closeContainer({
|
||||
viteServer,
|
||||
settings,
|
||||
logging
|
||||
}: Container) {
|
||||
async function closeContainer({ viteServer, settings, logging }: Container) {
|
||||
await viteServer.close();
|
||||
await runHookServerDone({
|
||||
config: settings.config,
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
import type { AstroTelemetry } from '@astrojs/telemetry';
|
||||
import type { AddressInfo } from 'net';
|
||||
import type http from 'http';
|
||||
import type { AddressInfo } from 'net';
|
||||
import { performance } from 'perf_hooks';
|
||||
import * as vite from 'vite';
|
||||
import type { AstroSettings } from '../../@types/astro';
|
||||
|
@ -43,7 +43,7 @@ export default async function dev(
|
|||
settings,
|
||||
logging: options.logging,
|
||||
isRestart: options.isRestart,
|
||||
}
|
||||
},
|
||||
});
|
||||
|
||||
// Start listening to the port
|
||||
|
|
|
@ -1,9 +1,3 @@
|
|||
export {
|
||||
createContainer,
|
||||
runInContainer,
|
||||
startContainer
|
||||
} from './container.js';
|
||||
export {
|
||||
createContainerWithAutomaticRestart
|
||||
} from './restart.js';
|
||||
export { createContainer, runInContainer, startContainer } from './container.js';
|
||||
export { default } from './dev.js';
|
||||
export { createContainerWithAutomaticRestart } from './restart.js';
|
||||
|
|
|
@ -1,19 +1,16 @@
|
|||
import type { AstroSettings } from '../../@types/astro';
|
||||
import type { Container, CreateContainerParams } from './container';
|
||||
import * as vite from 'vite';
|
||||
import type { AstroSettings } from '../../@types/astro';
|
||||
import { createSettings, openConfig } from '../config/index.js';
|
||||
import { info } from '../logger/core.js';
|
||||
import { createContainer, isStarted, startContainer } from './container.js';
|
||||
import { createSafeError } from '../errors/index.js';
|
||||
import { info } from '../logger/core.js';
|
||||
import type { Container, CreateContainerParams } from './container';
|
||||
import { createContainer, isStarted, startContainer } from './container.js';
|
||||
|
||||
async function createRestartedContainer(container: Container, settings: AstroSettings): Promise<Container> {
|
||||
const {
|
||||
logging,
|
||||
fs,
|
||||
resolvedRoot,
|
||||
configFlag,
|
||||
configFlagPath
|
||||
} = container;
|
||||
async function createRestartedContainer(
|
||||
container: Container,
|
||||
settings: AstroSettings
|
||||
): Promise<Container> {
|
||||
const { logging, fs, resolvedRoot, configFlag, configFlagPath } = container;
|
||||
const needsStart = isStarted(container);
|
||||
const newContainer = await createContainer({
|
||||
isRestart: true,
|
||||
|
@ -25,26 +22,24 @@ async function createRestartedContainer(container: Container, settings: AstroSet
|
|||
configFlagPath,
|
||||
});
|
||||
|
||||
if(needsStart) {
|
||||
if (needsStart) {
|
||||
await startContainer(newContainer);
|
||||
}
|
||||
|
||||
return newContainer;
|
||||
}
|
||||
|
||||
export function shouldRestartContainer({
|
||||
settings,
|
||||
configFlag,
|
||||
configFlagPath,
|
||||
restartInFlight
|
||||
}: Container, changedFile: string): boolean {
|
||||
if(restartInFlight) return false;
|
||||
export function shouldRestartContainer(
|
||||
{ settings, configFlag, configFlagPath, restartInFlight }: Container,
|
||||
changedFile: string
|
||||
): boolean {
|
||||
if (restartInFlight) return false;
|
||||
|
||||
let shouldRestart = false;
|
||||
|
||||
// If the config file changed, reload the config and restart the server.
|
||||
if(configFlag) {
|
||||
if(!!configFlagPath) {
|
||||
if (configFlag) {
|
||||
if (!!configFlagPath) {
|
||||
shouldRestart = vite.normalizePath(configFlagPath) === vite.normalizePath(changedFile);
|
||||
}
|
||||
}
|
||||
|
@ -78,19 +73,14 @@ export async function restartContainer({
|
|||
flags,
|
||||
logMsg,
|
||||
handleConfigError,
|
||||
beforeRestart
|
||||
}: RestartContainerParams): Promise<{ container: Container, error: Error | null }> {
|
||||
const {
|
||||
logging,
|
||||
close,
|
||||
resolvedRoot,
|
||||
settings: existingSettings
|
||||
} = container;
|
||||
beforeRestart,
|
||||
}: RestartContainerParams): Promise<{ container: Container; error: Error | null }> {
|
||||
const { logging, close, resolvedRoot, settings: existingSettings } = container;
|
||||
container.restartInFlight = true;
|
||||
|
||||
//console.clear(); // TODO move this
|
||||
if(beforeRestart) {
|
||||
beforeRestart()
|
||||
if (beforeRestart) {
|
||||
beforeRestart();
|
||||
}
|
||||
try {
|
||||
const newConfig = await openConfig({
|
||||
|
@ -107,7 +97,7 @@ export async function restartContainer({
|
|||
await close();
|
||||
return {
|
||||
container: await createRestartedContainer(container, settings),
|
||||
error: null
|
||||
error: null,
|
||||
};
|
||||
} catch (_err) {
|
||||
const error = createSafeError(_err);
|
||||
|
@ -116,7 +106,7 @@ export async function restartContainer({
|
|||
info(logging, 'astro', 'Continuing with previous valid configuration\n');
|
||||
return {
|
||||
container: await createRestartedContainer(container, existingSettings),
|
||||
error
|
||||
error,
|
||||
};
|
||||
}
|
||||
}
|
||||
|
@ -137,11 +127,11 @@ export async function createContainerWithAutomaticRestart({
|
|||
flags,
|
||||
handleConfigError = (_e: Error) => {},
|
||||
beforeRestart,
|
||||
params
|
||||
params,
|
||||
}: CreateContainerWithAutomaticRestart): Promise<Restart> {
|
||||
const initialContainer = await createContainer(params);
|
||||
let resolveRestart: (value: Error | null) => void;
|
||||
let restartComplete = new Promise<Error | null>(resolve => {
|
||||
let restartComplete = new Promise<Error | null>((resolve) => {
|
||||
resolveRestart = resolve;
|
||||
});
|
||||
|
||||
|
@ -149,14 +139,14 @@ export async function createContainerWithAutomaticRestart({
|
|||
container: initialContainer,
|
||||
restarted() {
|
||||
return restartComplete;
|
||||
}
|
||||
},
|
||||
};
|
||||
|
||||
function handleServerRestart(logMsg: string) {
|
||||
// eslint-disable-next-line @typescript-eslint/no-shadow
|
||||
const container = restart.container;
|
||||
return async function(changedFile: string) {
|
||||
if(shouldRestartContainer(container, changedFile)) {
|
||||
return async function (changedFile: string) {
|
||||
if (shouldRestartContainer(container, changedFile)) {
|
||||
const { container: newContainer, error } = await restartContainer({
|
||||
beforeRestart,
|
||||
container,
|
||||
|
@ -169,20 +159,20 @@ export async function createContainerWithAutomaticRestart({
|
|||
type: 'error',
|
||||
err: {
|
||||
message: err.message,
|
||||
stack: err.stack || ''
|
||||
}
|
||||
stack: err.stack || '',
|
||||
},
|
||||
});
|
||||
}
|
||||
},
|
||||
});
|
||||
restart.container = newContainer;
|
||||
// Add new watches because this is a new container with a new Vite server
|
||||
addWatches();
|
||||
resolveRestart(error);
|
||||
restartComplete = new Promise<Error | null>(resolve => {
|
||||
restartComplete = new Promise<Error | null>((resolve) => {
|
||||
resolveRestart = resolve;
|
||||
});
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
// Set up watches
|
||||
|
|
|
@ -1,7 +1,10 @@
|
|||
import { expect } from 'chai';
|
||||
import * as cheerio from 'cheerio';
|
||||
|
||||
import { createContainerWithAutomaticRestart, runInContainer } from '../../../dist/core/dev/index.js';
|
||||
import {
|
||||
createContainerWithAutomaticRestart,
|
||||
runInContainer,
|
||||
} from '../../../dist/core/dev/index.js';
|
||||
import { createFs, createRequestAndResponse } from '../test-utils.js';
|
||||
|
||||
const root = new URL('../../fixtures/alias/', import.meta.url);
|
||||
|
@ -20,13 +23,13 @@ describe('dev container restarts', () => {
|
|||
`,
|
||||
'/astro.config.mjs': `
|
||||
|
||||
`
|
||||
`,
|
||||
},
|
||||
root
|
||||
);
|
||||
|
||||
let restart = await createContainerWithAutomaticRestart({
|
||||
params: { fs, root }
|
||||
params: { fs, root },
|
||||
});
|
||||
|
||||
try {
|
||||
|
@ -39,13 +42,16 @@ describe('dev container restarts', () => {
|
|||
const $ = cheerio.load(html);
|
||||
expect(r.res.statusCode).to.equal(200);
|
||||
expect($('h1')).to.have.a.lengthOf(1);
|
||||
|
||||
|
||||
// Create an error
|
||||
let restartComplete = restart.restarted();
|
||||
fs.writeFileFromRootSync('/astro.config.mjs', 'const foo = bar');
|
||||
|
||||
// Vite watches the real filesystem, so we have to mock this part. It's not so bad.
|
||||
restart.container.viteServer.watcher.emit('change', fs.getFullyResolvedPath('/astro.config.mjs'));
|
||||
restart.container.viteServer.watcher.emit(
|
||||
'change',
|
||||
fs.getFullyResolvedPath('/astro.config.mjs')
|
||||
);
|
||||
|
||||
// Wait for the restart to finish
|
||||
let hmrError = await restartComplete;
|
||||
|
@ -57,7 +63,10 @@ describe('dev container restarts', () => {
|
|||
fs.writeFileFromRootSync('/astro.config.mjs', 'const foo = bar2');
|
||||
|
||||
// Vite watches the real filesystem, so we have to mock this part. It's not so bad.
|
||||
restart.container.viteServer.watcher.emit('change', fs.getFullyResolvedPath('/astro.config.mjs'));
|
||||
restart.container.viteServer.watcher.emit(
|
||||
'change',
|
||||
fs.getFullyResolvedPath('/astro.config.mjs')
|
||||
);
|
||||
|
||||
hmrError = await restartComplete;
|
||||
expect(hmrError).to.not.be.a('undefined');
|
||||
|
|
Loading…
Reference in a new issue