full site update
This commit is contained in:
62
node_modules/astro/dist/content/loaders/file.js
generated
vendored
62
node_modules/astro/dist/content/loaders/file.js
generated
vendored
@@ -1,43 +1,72 @@
|
||||
import { promises as fs, existsSync } from "node:fs";
|
||||
import { existsSync, promises as fs } from "node:fs";
|
||||
import { fileURLToPath } from "node:url";
|
||||
import yaml from "js-yaml";
|
||||
import toml from "smol-toml";
|
||||
import { FileGlobNotSupported, FileParserNotFound } from "../../core/errors/errors-data.js";
|
||||
import { AstroError } from "../../core/errors/index.js";
|
||||
import { posixRelative } from "../utils.js";
|
||||
function file(fileName) {
|
||||
function file(fileName, options) {
|
||||
if (fileName.includes("*")) {
|
||||
throw new Error("Glob patterns are not supported in `file` loader. Use `glob` loader instead.");
|
||||
throw new AstroError(FileGlobNotSupported);
|
||||
}
|
||||
let parse = null;
|
||||
const ext = fileName.split(".").at(-1);
|
||||
if (ext === "json") {
|
||||
parse = JSON.parse;
|
||||
} else if (ext === "yml" || ext === "yaml") {
|
||||
parse = (text) => yaml.load(text, {
|
||||
filename: fileName
|
||||
});
|
||||
} else if (ext === "toml") {
|
||||
parse = toml.parse;
|
||||
}
|
||||
if (options?.parser) parse = options.parser;
|
||||
if (parse === null) {
|
||||
throw new AstroError({
|
||||
...FileParserNotFound,
|
||||
message: FileParserNotFound.message(fileName)
|
||||
});
|
||||
}
|
||||
async function syncData(filePath, { logger, parseData, store, config }) {
|
||||
let json;
|
||||
let data;
|
||||
try {
|
||||
const data = await fs.readFile(filePath, "utf-8");
|
||||
json = JSON.parse(data);
|
||||
const contents = await fs.readFile(filePath, "utf-8");
|
||||
data = parse(contents);
|
||||
} catch (error) {
|
||||
logger.error(`Error reading data from ${fileName}`);
|
||||
logger.debug(error.message);
|
||||
return;
|
||||
}
|
||||
const normalizedFilePath = posixRelative(fileURLToPath(config.root), filePath);
|
||||
if (Array.isArray(json)) {
|
||||
if (json.length === 0) {
|
||||
if (Array.isArray(data)) {
|
||||
if (data.length === 0) {
|
||||
logger.warn(`No items found in ${fileName}`);
|
||||
}
|
||||
logger.debug(`Found ${json.length} item array in ${fileName}`);
|
||||
logger.debug(`Found ${data.length} item array in ${fileName}`);
|
||||
store.clear();
|
||||
for (const rawItem of json) {
|
||||
const idList = /* @__PURE__ */ new Set();
|
||||
for (const rawItem of data) {
|
||||
const id = (rawItem.id ?? rawItem.slug)?.toString();
|
||||
if (!id) {
|
||||
logger.error(`Item in ${fileName} is missing an id or slug field.`);
|
||||
continue;
|
||||
}
|
||||
const data = await parseData({ id, data: rawItem, filePath });
|
||||
store.set({ id, data, filePath: normalizedFilePath });
|
||||
if (idList.has(id)) {
|
||||
logger.warn(
|
||||
`Duplicate id "${id}" found in ${fileName}. Later items with the same id will overwrite earlier ones.`
|
||||
);
|
||||
}
|
||||
idList.add(id);
|
||||
const parsedData = await parseData({ id, data: rawItem, filePath });
|
||||
store.set({ id, data: parsedData, filePath: normalizedFilePath });
|
||||
}
|
||||
} else if (typeof json === "object") {
|
||||
const entries = Object.entries(json);
|
||||
} else if (typeof data === "object") {
|
||||
const entries = Object.entries(data);
|
||||
logger.debug(`Found object with ${entries.length} entries in ${fileName}`);
|
||||
store.clear();
|
||||
for (const [id, rawItem] of entries) {
|
||||
const data = await parseData({ id, data: rawItem, filePath });
|
||||
store.set({ id, data, filePath: normalizedFilePath });
|
||||
const parsedData = await parseData({ id, data: rawItem, filePath });
|
||||
store.set({ id, data: parsedData, filePath: normalizedFilePath });
|
||||
}
|
||||
} else {
|
||||
logger.error(`Invalid data in ${fileName}. Must be an array or object.`);
|
||||
@@ -55,6 +84,7 @@ function file(fileName) {
|
||||
}
|
||||
const filePath = fileURLToPath(url);
|
||||
await syncData(filePath, context);
|
||||
watcher?.add(filePath);
|
||||
watcher?.on("change", async (changedPath) => {
|
||||
if (changedPath === filePath) {
|
||||
logger.info(`Reloading data from ${fileName}`);
|
||||
|
Reference in New Issue
Block a user