0
Fork 0
mirror of https://github.com/withastro/astro.git synced 2024-12-30 22:03:56 -05:00

[ci] npm run format

This commit is contained in:
matthewp 2021-04-05 18:18:39 +00:00 committed by GitHub Actions
parent c9bc6ffef7
commit 71e5de76f1
4 changed files with 32 additions and 29 deletions

View file

@ -45,8 +45,8 @@ async function writeFilep(outPath: URL, bytes: string | Buffer, encoding: 'utf-8
/** Utility for writing a build result to disk */
async function writeResult(result: LoadResult, outPath: URL, encoding: null | 'utf-8') {
if (result.statusCode === 500 || result.statusCode === 404) {
error(logging, 'build', result.error || result.statusCode);
} else if(result.statusCode !== 200) {
error(logging, 'build', result.error || result.statusCode);
} else if (result.statusCode !== 200) {
error(logging, 'build', `Unexpected load result (${result.statusCode}) for ${outPath.pathname}`);
} else {
const bytes = result.contents;

View file

@ -26,7 +26,7 @@ type LoadResultSuccess = {
contentType?: string | false;
};
type LoadResultNotFound = { statusCode: 404; error: Error };
type LoadResultRedirect = { statusCode: 301 | 302; location: string; };
type LoadResultRedirect = { statusCode: 301 | 302; location: string };
type LoadResultError = { statusCode: 500 } & ({ type: 'parse-error'; error: CompileError } | { type: 'unknown'; error: Error });
export type LoadResult = LoadResultSuccess | LoadResultNotFound | LoadResultRedirect | LoadResultError;
@ -45,7 +45,7 @@ async function load(config: RuntimeConfig, rawPathname: string | undefined): Pro
info(logging, 'access', reqPath);
const searchResult = searchForPage(fullurl, astroRoot);
if(searchResult.statusCode === 404) {
if (searchResult.statusCode === 404) {
try {
const result = await frontendSnowpack.loadUrl(reqPath);
@ -65,7 +65,7 @@ async function load(config: RuntimeConfig, rawPathname: string | undefined): Pro
}
}
if(searchResult.statusCode === 301) {
if (searchResult.statusCode === 301) {
return { statusCode: 301, location: searchResult.pathname };
}

View file

@ -6,54 +6,57 @@ interface PageLocation {
}
function findAnyPage(candidates: Array<string>, astroRoot: URL): PageLocation | false {
for(let candidate of candidates) {
for (let candidate of candidates) {
const url = new URL(`./pages/${candidate}`, astroRoot);
if(existsSync(url)) {
if (existsSync(url)) {
return {
fileURL: url,
snowpackURL: `/_astro/pages/${candidate}.js`
snowpackURL: `/_astro/pages/${candidate}.js`,
};
}
}
return false;
}
type SearchResult = {
statusCode: 200;
location: PageLocation;
pathname: string;
} | {
statusCode: 301;
location: null;
pathname: string;
} | {
statusCode: 404;
};
type SearchResult =
| {
statusCode: 200;
location: PageLocation;
pathname: string;
}
| {
statusCode: 301;
location: null;
pathname: string;
}
| {
statusCode: 404;
};
export function searchForPage(url: URL, astroRoot: URL): SearchResult {
const reqPath = decodeURI(url.pathname);
const base = reqPath.substr(1);
// Try to find index.astro/md paths
if(reqPath.endsWith('/')) {
if (reqPath.endsWith('/')) {
const candidates = [`${base}index.astro`, `${base}index.md`];
const location = findAnyPage(candidates, astroRoot);
if(location) {
if (location) {
return {
statusCode: 200,
location,
pathname: reqPath
pathname: reqPath,
};
}
} else {
// Try to find the page by its name.
const candidates = [`${base}.astro`, `${base}.md`];
let location = findAnyPage(candidates, astroRoot);
if(location) {
if (location) {
return {
statusCode: 200,
location,
pathname: reqPath
pathname: reqPath,
};
}
}
@ -61,15 +64,15 @@ export function searchForPage(url: URL, astroRoot: URL): SearchResult {
// Try to find name/index.astro/md
const candidates = [`${base}/index.astro`, `${base}/index.md`];
const location = findAnyPage(candidates, astroRoot);
if(location) {
if (location) {
return {
statusCode: 301,
location: null,
pathname: reqPath + '/'
pathname: reqPath + '/',
};
}
return {
statusCode: 404
statusCode: 404,
};
}