diff --git a/docs/src/pages/reference/cli-reference.md b/docs/src/pages/reference/cli-reference.md index 26b165f489..0665b35a04 100644 --- a/docs/src/pages/reference/cli-reference.md +++ b/docs/src/pages/reference/cli-reference.md @@ -27,7 +27,7 @@ This command is meant for local testing only, and is not designed to be run in p ### `astro check` -Runs diagnostics (such as type-checking) against your project and reports errors to the console. If any errors are found the process will exit with a code of __1__. +Runs diagnostics (such as type-checking) against your project and reports errors to the console. If any errors are found the process will exit with a code of **1**. This command is intended to be used in CI workflows. diff --git a/packages/astro/src/check.ts b/packages/astro/src/check.ts index 22bccd4f91..6df689d35c 100644 --- a/packages/astro/src/check.ts +++ b/packages/astro/src/check.ts @@ -7,25 +7,19 @@ import * as path from 'path'; import { pathToFileURL } from 'url'; import * as fs from 'fs'; -async function openAllDocuments( - workspaceUri: URL, - filePathsToIgnore: string[], - checker: AstroCheck -) { +async function openAllDocuments(workspaceUri: URL, filePathsToIgnore: string[], checker: AstroCheck) { const files = await glob('**/*.astro', { - cwd: workspaceUri.pathname, - ignore: ['node_modules/**'].concat(filePathsToIgnore.map((ignore) => `${ignore}/**`)) + cwd: workspaceUri.pathname, + ignore: ['node_modules/**'].concat(filePathsToIgnore.map((ignore) => `${ignore}/**`)), }); const absFilePaths = files.map((f) => path.resolve(workspaceUri.pathname, f)); for (const absFilePath of absFilePaths) { - const text = fs.readFileSync(absFilePath, 'utf-8'); - checker.upsertDocument( - { - uri: pathToFileURL(absFilePath).toString(), - text - } - ); + const text = fs.readFileSync(absFilePath, 'utf-8'); + checker.upsertDocument({ + uri: pathToFileURL(absFilePath).toString(), + text, + }); } } @@ -34,17 +28,17 @@ interface Result { warnings: number; } -function offsetAt({ line, character }: { line: number, character: number; }, text: string) { +function offsetAt({ line, character }: { line: number; character: number }, text: string) { let i = 0; let l = 0; let c = 0; - while(i < text.length) { - if(l === line && c === character) { + while (i < text.length) { + if (l === line && c === character) { break; } let char = text[i]; - switch(char) { + switch (char) { case '\n': { l++; c = 0; @@ -66,9 +60,7 @@ function pad(str: string, len: number) { return Array.from({ length: len }, () => str).join(''); } -export async function run() { - -} +export async function run() {} export async function check(astroConfig: AstroConfig) { const root = astroConfig.projectRoot; @@ -79,18 +71,18 @@ export async function check(astroConfig: AstroConfig) { let result: Result = { errors: 0, - warnings: 0 + warnings: 0, }; - diagnostics.forEach(diag => { - diag.diagnostics.forEach(d => { - switch(d.severity) { + diagnostics.forEach((diag) => { + diag.diagnostics.forEach((d) => { + switch (d.severity) { case DiagnosticSeverity.Error: { console.error(`${bold(cyan(path.relative(root.pathname, diag.filePath)))}:${bold(yellow(d.range.start.line))}:${bold(yellow(d.range.start.character))} - ${d.message}`); let startOffset = offsetAt({ line: d.range.start.line, character: 0 }, diag.text); let endOffset = offsetAt({ line: d.range.start.line + 1, character: 0 }, diag.text); let str = diag.text.substring(startOffset, endOffset - 1); - const lineNumStr = (d.range.start.line).toString(); + const lineNumStr = d.range.start.line.toString(); const lineNumLen = lineNumStr.length; console.error(`${bgWhite(black(lineNumStr))} ${str}`); let tildes = pad('~', d.range.end.character - d.range.start.character); @@ -107,10 +99,10 @@ export async function check(astroConfig: AstroConfig) { }); }); - if(result.errors) { - console.error(`Found ${result.errors} errors.`) + if (result.errors) { + console.error(`Found ${result.errors} errors.`); } const exitCode = result.errors ? 1 : 0; return exitCode; -} \ No newline at end of file +} diff --git a/packages/astro/src/cli.ts b/packages/astro/src/cli.ts index d49381520e..5c82f3af49 100644 --- a/packages/astro/src/cli.ts +++ b/packages/astro/src/cli.ts @@ -134,7 +134,7 @@ const cmdMap = new Map Promise>([ ['dev', devServer], ['preview', preview], ['reload', reloadAndExit], - ['check', checkAndExit] + ['check', checkAndExit], ]); /** The primary CLI action */