🎉 initiate project *astro_rewrite*
This commit is contained in:
parent
ffd4d5e86c
commit
2ba37bfbe3
8658 changed files with 2268794 additions and 2538 deletions
83
node_modules/astro/dist/cli/check/index.d.ts
generated
vendored
Normal file
83
node_modules/astro/dist/cli/check/index.d.ts
generated
vendored
Normal file
|
@ -0,0 +1,83 @@
|
|||
/// <reference types="node" />
|
||||
import { AstroCheck } from '@astrojs/language-server';
|
||||
import fs from 'node:fs';
|
||||
import type { Arguments as Flags } from 'yargs-parser';
|
||||
import type { AstroSettings } from '../../@types/astro';
|
||||
import type { LogOptions } from '../../core/logger/core.js';
|
||||
import type { ProcessExit, SyncOptions } from '../../core/sync';
|
||||
export type CheckPayload = {
|
||||
/**
|
||||
* Flags passed via CLI
|
||||
*/
|
||||
flags: Flags;
|
||||
/**
|
||||
* Logging options
|
||||
*/
|
||||
logging: LogOptions;
|
||||
};
|
||||
/**
|
||||
*
|
||||
* Types of response emitted by the checker
|
||||
*/
|
||||
export declare enum CheckResult {
|
||||
/**
|
||||
* Operation finished without errors
|
||||
*/
|
||||
ExitWithSuccess = 0,
|
||||
/**
|
||||
* Operation finished with errors
|
||||
*/
|
||||
ExitWithError = 1,
|
||||
/**
|
||||
* The consumer should not terminate the operation
|
||||
*/
|
||||
Listen = 2
|
||||
}
|
||||
/**
|
||||
* Checks `.astro` files for possible errors.
|
||||
*
|
||||
* If the `--watch` flag is provided, the command runs indefinitely and provides diagnostics
|
||||
* when `.astro` files are modified.
|
||||
*
|
||||
* Every time an astro files is modified, content collections are also generated.
|
||||
*
|
||||
* @param {CheckPayload} options Options passed {@link AstroChecker}
|
||||
* @param {Flags} options.flags Flags coming from the CLI
|
||||
* @param {LogOptions} options.logging Logging options
|
||||
*/
|
||||
export declare function check({ logging, flags }: CheckPayload): Promise<AstroChecker | undefined>;
|
||||
type CheckerConstructor = {
|
||||
diagnosticChecker: AstroCheck;
|
||||
isWatchMode: boolean;
|
||||
syncCli: (settings: AstroSettings, options: SyncOptions) => Promise<ProcessExit>;
|
||||
settings: Readonly<AstroSettings>;
|
||||
logging: Readonly<LogOptions>;
|
||||
fileSystem: typeof fs;
|
||||
};
|
||||
/**
|
||||
* Responsible to check files - classic or watch mode - and report diagnostics.
|
||||
*
|
||||
* When in watch mode, the class does a whole check pass, and then starts watching files.
|
||||
* When a change occurs to an `.astro` file, the checker builds content collections again and lint all the `.astro` files.
|
||||
*/
|
||||
export declare class AstroChecker {
|
||||
#private;
|
||||
constructor({ diagnosticChecker, isWatchMode, syncCli, settings, fileSystem, logging, }: CheckerConstructor);
|
||||
/**
|
||||
* Check all `.astro` files once and then finishes the operation.
|
||||
*/
|
||||
check(): Promise<CheckResult>;
|
||||
/**
|
||||
* Check all `.astro` files and then start watching for changes.
|
||||
*/
|
||||
watch(): Promise<CheckResult>;
|
||||
/**
|
||||
* Stops the watch. It terminates the inner server.
|
||||
*/
|
||||
stop(): Promise<void>;
|
||||
/**
|
||||
* Whether the checker should run in watch mode
|
||||
*/
|
||||
get isWatchMode(): boolean;
|
||||
}
|
||||
export {};
|
270
node_modules/astro/dist/cli/check/index.js
generated
vendored
Normal file
270
node_modules/astro/dist/cli/check/index.js
generated
vendored
Normal file
|
@ -0,0 +1,270 @@
|
|||
import {
|
||||
AstroCheck,
|
||||
DiagnosticSeverity
|
||||
} from "@astrojs/language-server";
|
||||
import glob from "fast-glob";
|
||||
import { bold, dim, red, yellow } from "kleur/colors";
|
||||
import { createRequire } from "module";
|
||||
import fs from "node:fs";
|
||||
import { join } from "node:path";
|
||||
import { fileURLToPath, pathToFileURL } from "node:url";
|
||||
import ora from "ora";
|
||||
import { debug, info } from "../../core/logger/core.js";
|
||||
import { printHelp } from "../../core/messages.js";
|
||||
import { loadSettings } from "../load-settings.js";
|
||||
import { printDiagnostic } from "./print.js";
|
||||
var CheckResult = /* @__PURE__ */ ((CheckResult2) => {
|
||||
CheckResult2[CheckResult2["ExitWithSuccess"] = 0] = "ExitWithSuccess";
|
||||
CheckResult2[CheckResult2["ExitWithError"] = 1] = "ExitWithError";
|
||||
CheckResult2[CheckResult2["Listen"] = 2] = "Listen";
|
||||
return CheckResult2;
|
||||
})(CheckResult || {});
|
||||
const ASTRO_GLOB_PATTERN = "**/*.astro";
|
||||
async function check({ logging, flags }) {
|
||||
if (flags.help || flags.h) {
|
||||
printHelp({
|
||||
commandName: "astro check",
|
||||
usage: "[...flags]",
|
||||
tables: {
|
||||
Flags: [
|
||||
["--watch", "Watch Astro files for changes and re-run checks."],
|
||||
["--help (-h)", "See all available flags."]
|
||||
]
|
||||
},
|
||||
description: `Runs diagnostics against your project and reports errors to the console.`
|
||||
});
|
||||
return;
|
||||
}
|
||||
const settings = await loadSettings({ cmd: "check", flags, logging });
|
||||
if (!settings)
|
||||
return;
|
||||
const checkFlags = parseFlags(flags);
|
||||
if (checkFlags.watch) {
|
||||
info(logging, "check", "Checking files in watch mode");
|
||||
} else {
|
||||
info(logging, "check", "Checking files");
|
||||
}
|
||||
const { syncCli } = await import("../../core/sync/index.js");
|
||||
const root = settings.config.root;
|
||||
const require2 = createRequire(import.meta.url);
|
||||
const diagnosticChecker = new AstroCheck(
|
||||
root.toString(),
|
||||
require2.resolve("typescript/lib/tsserverlibrary.js", {
|
||||
paths: [root.toString()]
|
||||
})
|
||||
);
|
||||
return new AstroChecker({
|
||||
syncCli,
|
||||
settings,
|
||||
fileSystem: fs,
|
||||
logging,
|
||||
diagnosticChecker,
|
||||
isWatchMode: checkFlags.watch
|
||||
});
|
||||
}
|
||||
class AstroChecker {
|
||||
#diagnosticsChecker;
|
||||
#shouldWatch;
|
||||
#syncCli;
|
||||
#settings;
|
||||
#logging;
|
||||
#fs;
|
||||
#watcher;
|
||||
#filesCount;
|
||||
#updateDiagnostics;
|
||||
constructor({
|
||||
diagnosticChecker,
|
||||
isWatchMode,
|
||||
syncCli,
|
||||
settings,
|
||||
fileSystem,
|
||||
logging
|
||||
}) {
|
||||
this.#diagnosticsChecker = diagnosticChecker;
|
||||
this.#shouldWatch = isWatchMode;
|
||||
this.#syncCli = syncCli;
|
||||
this.#logging = logging;
|
||||
this.#settings = settings;
|
||||
this.#fs = fileSystem;
|
||||
this.#filesCount = 0;
|
||||
}
|
||||
/**
|
||||
* Check all `.astro` files once and then finishes the operation.
|
||||
*/
|
||||
async check() {
|
||||
return await this.#checkAllFiles(true);
|
||||
}
|
||||
/**
|
||||
* Check all `.astro` files and then start watching for changes.
|
||||
*/
|
||||
async watch() {
|
||||
await this.#checkAllFiles(true);
|
||||
await this.#watch();
|
||||
return 2 /* Listen */;
|
||||
}
|
||||
/**
|
||||
* Stops the watch. It terminates the inner server.
|
||||
*/
|
||||
async stop() {
|
||||
var _a;
|
||||
await ((_a = this.#watcher) == null ? void 0 : _a.close());
|
||||
}
|
||||
/**
|
||||
* Whether the checker should run in watch mode
|
||||
*/
|
||||
get isWatchMode() {
|
||||
return this.#shouldWatch;
|
||||
}
|
||||
async #openDocuments() {
|
||||
this.#filesCount = await openAllDocuments(
|
||||
this.#settings.config.root,
|
||||
[],
|
||||
this.#diagnosticsChecker
|
||||
);
|
||||
}
|
||||
/**
|
||||
* Lint all `.astro` files, and report the result in console. Operations executed, in order:
|
||||
* 1. Compile content collections.
|
||||
* 2. Optionally, traverse the file system for `.astro` files and saves their paths.
|
||||
* 3. Get diagnostics for said files and print the result in console.
|
||||
*
|
||||
* @param openDocuments Whether the operation should open all `.astro` files
|
||||
*/
|
||||
async #checkAllFiles(openDocuments) {
|
||||
const processExit = await this.#syncCli(this.#settings, {
|
||||
logging: this.#logging,
|
||||
fs: this.#fs
|
||||
});
|
||||
if (processExit === 1)
|
||||
return processExit;
|
||||
let spinner = ora(
|
||||
` Getting diagnostics for Astro files in ${fileURLToPath(this.#settings.config.root)}\u2026`
|
||||
).start();
|
||||
if (openDocuments) {
|
||||
await this.#openDocuments();
|
||||
}
|
||||
let diagnostics = await this.#diagnosticsChecker.getDiagnostics();
|
||||
spinner.succeed();
|
||||
let brokenDownDiagnostics = this.#breakDownDiagnostics(diagnostics);
|
||||
this.#logDiagnosticsSeverity(brokenDownDiagnostics);
|
||||
return brokenDownDiagnostics.errors > 0 ? 1 /* ExitWithError */ : 0 /* ExitWithSuccess */;
|
||||
}
|
||||
#checkForDiagnostics() {
|
||||
clearTimeout(this.#updateDiagnostics);
|
||||
this.#updateDiagnostics = setTimeout(async () => await this.#checkAllFiles(false), 500);
|
||||
}
|
||||
/**
|
||||
* This function is responsible to attach events to the server watcher
|
||||
*/
|
||||
async #watch() {
|
||||
const { default: chokidar } = await import("chokidar");
|
||||
this.#watcher = chokidar.watch(
|
||||
join(fileURLToPath(this.#settings.config.root), ASTRO_GLOB_PATTERN),
|
||||
{
|
||||
ignored: ["**/node_modules/**"],
|
||||
ignoreInitial: true
|
||||
}
|
||||
);
|
||||
this.#watcher.on("add", (file) => {
|
||||
this.#addDocument(file);
|
||||
this.#filesCount += 1;
|
||||
this.#checkForDiagnostics();
|
||||
});
|
||||
this.#watcher.on("change", (file) => {
|
||||
this.#addDocument(file);
|
||||
this.#checkForDiagnostics();
|
||||
});
|
||||
this.#watcher.on("unlink", (file) => {
|
||||
this.#diagnosticsChecker.removeDocument(file);
|
||||
this.#filesCount -= 1;
|
||||
this.#checkForDiagnostics();
|
||||
});
|
||||
}
|
||||
/**
|
||||
* Add a document to the diagnostics checker
|
||||
* @param filePath Path to the file
|
||||
*/
|
||||
#addDocument(filePath) {
|
||||
const text = fs.readFileSync(filePath, "utf-8");
|
||||
this.#diagnosticsChecker.upsertDocument({
|
||||
uri: pathToFileURL(filePath).toString(),
|
||||
text
|
||||
});
|
||||
}
|
||||
/**
|
||||
* Logs the result of the various diagnostics
|
||||
*
|
||||
* @param result Result emitted by AstroChecker.#breakDownDiagnostics
|
||||
*/
|
||||
#logDiagnosticsSeverity(result) {
|
||||
info(
|
||||
this.#logging,
|
||||
"diagnostics",
|
||||
[
|
||||
bold(`Result (${this.#filesCount} file${this.#filesCount === 1 ? "" : "s"}): `),
|
||||
bold(red(`${result.errors} ${result.errors === 1 ? "error" : "errors"}`)),
|
||||
bold(yellow(`${result.warnings} ${result.warnings === 1 ? "warning" : "warnings"}`)),
|
||||
dim(`${result.hints} ${result.hints === 1 ? "hint" : "hints"}
|
||||
`)
|
||||
].join(`
|
||||
${dim("-")} `)
|
||||
);
|
||||
}
|
||||
/**
|
||||
* It loops through all diagnostics and break down diagnostics that are errors, warnings or hints.
|
||||
*/
|
||||
#breakDownDiagnostics(diagnostics) {
|
||||
let result = {
|
||||
errors: 0,
|
||||
warnings: 0,
|
||||
hints: 0
|
||||
};
|
||||
diagnostics.forEach((diag) => {
|
||||
diag.diagnostics.forEach((d) => {
|
||||
info(this.#logging, "diagnostics", `
|
||||
${printDiagnostic(diag.fileUri, diag.text, d)}`);
|
||||
switch (d.severity) {
|
||||
case DiagnosticSeverity.Error: {
|
||||
result.errors++;
|
||||
break;
|
||||
}
|
||||
case DiagnosticSeverity.Warning: {
|
||||
result.warnings++;
|
||||
break;
|
||||
}
|
||||
case DiagnosticSeverity.Hint: {
|
||||
result.hints++;
|
||||
break;
|
||||
}
|
||||
}
|
||||
});
|
||||
});
|
||||
return result;
|
||||
}
|
||||
}
|
||||
async function openAllDocuments(workspaceUri, filePathsToIgnore, checker) {
|
||||
const files = await glob(ASTRO_GLOB_PATTERN, {
|
||||
cwd: fileURLToPath(workspaceUri),
|
||||
ignore: ["node_modules/**"].concat(filePathsToIgnore.map((ignore) => `${ignore}/**`)),
|
||||
absolute: true
|
||||
});
|
||||
for (const file of files) {
|
||||
debug("check", `Adding file ${file} to the list of files to check.`);
|
||||
const text = fs.readFileSync(file, "utf-8");
|
||||
checker.upsertDocument({
|
||||
uri: pathToFileURL(file).toString(),
|
||||
text
|
||||
});
|
||||
}
|
||||
return files.length;
|
||||
}
|
||||
function parseFlags(flags) {
|
||||
return {
|
||||
watch: flags.watch ?? false
|
||||
};
|
||||
}
|
||||
export {
|
||||
AstroChecker,
|
||||
CheckResult,
|
||||
check
|
||||
};
|
2
node_modules/astro/dist/cli/check/print.d.ts
generated
vendored
Normal file
2
node_modules/astro/dist/cli/check/print.d.ts
generated
vendored
Normal file
|
@ -0,0 +1,2 @@
|
|||
import { type Diagnostic } from '@astrojs/language-server';
|
||||
export declare function printDiagnostic(filePath: string, text: string, diag: Diagnostic): string;
|
95
node_modules/astro/dist/cli/check/print.js
generated
vendored
Normal file
95
node_modules/astro/dist/cli/check/print.js
generated
vendored
Normal file
|
@ -0,0 +1,95 @@
|
|||
import { DiagnosticSeverity, offsetAt } from "@astrojs/language-server";
|
||||
import {
|
||||
bgRed,
|
||||
bgWhite,
|
||||
bgYellow,
|
||||
black,
|
||||
bold,
|
||||
cyan,
|
||||
gray,
|
||||
red,
|
||||
white,
|
||||
yellow
|
||||
} from "kleur/colors";
|
||||
import { fileURLToPath } from "node:url";
|
||||
import stringWidth from "string-width";
|
||||
function printDiagnostic(filePath, text, diag) {
|
||||
let result = [];
|
||||
const realStartLine = diag.range.start.line + 1;
|
||||
const realStartCharacter = diag.range.start.character + 1;
|
||||
const IDEFilePath = `${bold(cyan(fileURLToPath(filePath)))}:${bold(yellow(realStartLine))}:${bold(
|
||||
yellow(realStartCharacter)
|
||||
)}`;
|
||||
result.push(
|
||||
`${IDEFilePath} ${bold(getColorForSeverity(diag, getStringForSeverity(diag)))}: ${diag.message}`
|
||||
);
|
||||
const previousLine = getLine(diag.range.start.line - 1, text);
|
||||
if (previousLine) {
|
||||
result.push(`${getPrintableLineNumber(realStartLine - 1)} ${gray(previousLine)}`);
|
||||
}
|
||||
const str = getLine(diag.range.start.line, text);
|
||||
const lineNumStr = realStartLine.toString().padStart(2, "0");
|
||||
const lineNumLen = lineNumStr.length;
|
||||
result.push(`${getBackgroundForSeverity(diag, lineNumStr)} ${str}`);
|
||||
const tildes = generateString("~", diag.range.end.character - diag.range.start.character);
|
||||
const beforeChars = stringWidth(str.substring(0, diag.range.start.character));
|
||||
const spaces = generateString(" ", beforeChars + lineNumLen - 1);
|
||||
result.push(` ${spaces}${bold(getColorForSeverity(diag, tildes))}`);
|
||||
const nextLine = getLine(diag.range.start.line + 1, text);
|
||||
if (nextLine) {
|
||||
result.push(`${getPrintableLineNumber(realStartLine + 1)} ${gray(nextLine)}`);
|
||||
}
|
||||
result.push("");
|
||||
return result.join("\n");
|
||||
}
|
||||
function generateString(str, len) {
|
||||
return Array.from({ length: len }, () => str).join("");
|
||||
}
|
||||
function getStringForSeverity(diag) {
|
||||
switch (diag.severity) {
|
||||
case DiagnosticSeverity.Error:
|
||||
return "Error";
|
||||
case DiagnosticSeverity.Warning:
|
||||
return "Warning";
|
||||
case DiagnosticSeverity.Hint:
|
||||
return "Hint";
|
||||
default:
|
||||
return "Unknown";
|
||||
}
|
||||
}
|
||||
function getColorForSeverity(diag, text) {
|
||||
switch (diag.severity) {
|
||||
case DiagnosticSeverity.Error:
|
||||
return red(text);
|
||||
case DiagnosticSeverity.Warning:
|
||||
return yellow(text);
|
||||
case DiagnosticSeverity.Hint:
|
||||
return gray(text);
|
||||
default:
|
||||
return text;
|
||||
}
|
||||
}
|
||||
function getBackgroundForSeverity(diag, text) {
|
||||
switch (diag.severity) {
|
||||
case DiagnosticSeverity.Error:
|
||||
return bgRed(white(text));
|
||||
case DiagnosticSeverity.Warning:
|
||||
return bgYellow(white(text));
|
||||
case DiagnosticSeverity.Hint:
|
||||
return bgWhite(black(text));
|
||||
default:
|
||||
return text;
|
||||
}
|
||||
}
|
||||
function getPrintableLineNumber(line) {
|
||||
return bgWhite(black(line.toString().padStart(2, "0")));
|
||||
}
|
||||
function getLine(line, text) {
|
||||
return text.substring(
|
||||
offsetAt({ line, character: 0 }, text),
|
||||
offsetAt({ line, character: Number.MAX_SAFE_INTEGER }, text)
|
||||
).replace(/\t/g, " ").trimEnd();
|
||||
}
|
||||
export {
|
||||
printDiagnostic
|
||||
};
|
Loading…
Add table
Add a link
Reference in a new issue