[ci] format

This commit is contained in:
matthewp 2022-11-15 15:04:33 +00:00 committed by fredkbot
parent 40226dd14d
commit a93aa4ef3f
3 changed files with 31 additions and 28 deletions

View file

@ -1,9 +1,9 @@
/* eslint-disable no-console */ /* eslint-disable no-console */
import fs from 'fs';
import * as colors from 'kleur/colors'; import * as colors from 'kleur/colors';
import type { Arguments as Flags } from 'yargs-parser'; import type { Arguments as Flags } from 'yargs-parser';
import yargs from 'yargs-parser'; import yargs from 'yargs-parser';
import { z } from 'zod'; import { z } from 'zod';
import fs from 'fs';
import { import {
createSettings, createSettings,
openConfig, openConfig,
@ -174,7 +174,9 @@ async function runCommand(cmd: string, flags: yargs.Arguments) {
const { default: devServer } = await import('../core/dev/index.js'); const { default: devServer } = await import('../core/dev/index.js');
const configFlag = resolveFlags(flags).config; const configFlag = resolveFlags(flags).config;
const configFlagPath = configFlag ? await resolveConfigPath({ cwd: root, flags, fs }) : undefined; const configFlagPath = configFlag
? await resolveConfigPath({ cwd: root, flags, fs })
: undefined;
await devServer(settings, { await devServer(settings, {
configFlag, configFlag,

View file

@ -164,7 +164,7 @@ export async function resolveConfigPath(
const config = await loadConfigWithVite({ const config = await loadConfigWithVite({
configPath: userConfigPath, configPath: userConfigPath,
root, root,
fs: configOptions.fs fs: configOptions.fs,
}); });
return config.filePath; return config.filePath;
} catch (e) { } catch (e) {
@ -220,7 +220,7 @@ async function tryLoadConfig(
let configPath = await resolveConfigPath({ let configPath = await resolveConfigPath({
cwd: configOptions.cwd, cwd: configOptions.cwd,
flags: configOptions.flags, flags: configOptions.flags,
fs: fsMod fs: fsMod,
}); });
if (!configPath) return undefined; if (!configPath) return undefined;
if (configOptions.isRestart) { if (configOptions.isRestart) {
@ -242,12 +242,12 @@ async function tryLoadConfig(
}; };
configPath = tempConfigPath; configPath = tempConfigPath;
} }
// Create a vite server to load the config // Create a vite server to load the config
const config = await loadConfigWithVite({ const config = await loadConfigWithVite({
configPath, configPath,
fs: fsMod, fs: fsMod,
root root,
}); });
return config as TryLoadConfigResult; return config as TryLoadConfigResult;
} finally { } finally {

View file

@ -1,7 +1,7 @@
import * as vite from 'vite'; import type fsType from 'fs';
import npath from 'path'; import npath from 'path';
import { pathToFileURL } from 'url'; import { pathToFileURL } from 'url';
import type fsType from 'fs'; import * as vite from 'vite';
import { AstroError, AstroErrorData } from '../errors/index.js'; import { AstroError, AstroErrorData } from '../errors/index.js';
// Fallback for legacy // Fallback for legacy
@ -25,8 +25,8 @@ async function createViteLoader(root: string): Promise<ViteLoader> {
// NOTE: Vite doesn't externalize linked packages by default. During testing locally, // NOTE: Vite doesn't externalize linked packages by default. During testing locally,
// these dependencies trip up Vite's dev SSR transform. In the future, we should // these dependencies trip up Vite's dev SSR transform. In the future, we should
// avoid `vite.createServer` and use `loadConfigFromFile` instead. // avoid `vite.createServer` and use `loadConfigFromFile` instead.
external: ['@astrojs/tailwind', '@astrojs/mdx', '@astrojs/react'] external: ['@astrojs/tailwind', '@astrojs/mdx', '@astrojs/react'],
} },
}); });
return { return {
@ -40,7 +40,7 @@ async function stat(fs: typeof fsType, configPath: string, mustExist: boolean):
await fs.promises.stat(configPath); await fs.promises.stat(configPath);
return true; return true;
} catch { } catch {
if(mustExist) { if (mustExist) {
throw new AstroError({ throw new AstroError({
...AstroErrorData.ConfigNotFound, ...AstroErrorData.ConfigNotFound,
message: AstroErrorData.ConfigNotFound.message(configPath), message: AstroErrorData.ConfigNotFound.message(configPath),
@ -57,13 +57,13 @@ async function search(fs: typeof fsType, root: string) {
'astro.config.ts', 'astro.config.ts',
'astro.config.mts', 'astro.config.mts',
'astro.config.cjs', 'astro.config.cjs',
'astro.config.cjs' 'astro.config.cjs',
].map(path => npath.join(root, path)); ].map((path) => npath.join(root, path));
for(const file of paths) { for (const file of paths) {
// First verify the file event exists // First verify the file event exists
const exists = await stat(fs, file, false); const exists = await stat(fs, file, false);
if(exists) { if (exists) {
return file; return file;
} }
} }
@ -75,35 +75,38 @@ interface LoadConfigWithViteOptions {
fs: typeof fsType; fs: typeof fsType;
} }
export async function loadConfigWithVite({ configPath, fs, root }: LoadConfigWithViteOptions): Promise<{ export async function loadConfigWithVite({
configPath,
fs,
root,
}: LoadConfigWithViteOptions): Promise<{
value: Record<string, any>; value: Record<string, any>;
filePath?: string; filePath?: string;
}> { }> {
let file: string; let file: string;
if(configPath) { if (configPath) {
// Go ahead and check if the file exists and throw if not. // Go ahead and check if the file exists and throw if not.
await stat(fs, configPath, true); await stat(fs, configPath, true);
file = configPath; file = configPath;
} else { } else {
const found = await search(fs, root); const found = await search(fs, root);
if(!found) { if (!found) {
// No config file found, return an empty config that will be populated with defaults // No config file found, return an empty config that will be populated with defaults
return { return {
value: {}, value: {},
filePath: undefined filePath: undefined,
}; };
} else { } else {
file = found; file = found;
} }
} }
// Try loading with Node import() // Try loading with Node import()
if(/\.[cm]?js$/.test(file)) { if (/\.[cm]?js$/.test(file)) {
const config = await import(pathToFileURL(file).toString()); const config = await import(pathToFileURL(file).toString());
return { return {
value: config.default ?? {}, value: config.default ?? {},
filePath: file filePath: file,
}; };
} }
@ -114,10 +117,9 @@ export async function loadConfigWithVite({ configPath, fs, root }: LoadConfigWit
const mod = await loader.viteServer.ssrLoadModule(file); const mod = await loader.viteServer.ssrLoadModule(file);
return { return {
value: mod.default ?? {}, value: mod.default ?? {},
filePath: file filePath: file,
} };
} catch { } catch {
// Try loading with Proload // Try loading with Proload
// TODO deprecate - this is only for legacy compatibility // TODO deprecate - this is only for legacy compatibility
const res = await load('astro', { const res = await load('astro', {
@ -127,11 +129,10 @@ export async function loadConfigWithVite({ configPath, fs, root }: LoadConfigWit
}); });
return { return {
value: res?.value ?? {}, value: res?.value ?? {},
filePath: file filePath: file,
}; };
} finally { } finally {
if(loader) { if (loader) {
await loader.viteServer.close(); await loader.viteServer.close();
} }
} }