gitea push

This commit is contained in:
2026-05-09 12:19:29 -06:00
parent 06113c95b8
commit 429461e985
1481 changed files with 74306 additions and 52475 deletions
+42 -34
View File
@@ -19,49 +19,57 @@ const stripJsonComments = require("./strip-json-comments");
const _stripCommentsCache = new WeakMap();
/**
* Read and parse JSON file (supports JSONC with comments)
* @template T
* Read and parse JSON file (supports JSONC with comments).
* Callback-based so a synchronous `fileSystem` stays synchronous all the
* way through — Promise wrapping would defer resolution by a Promise tick
* and break `resolveSync` when `tsconfig` is used together with
* `useSyncFileSystemCalls: true`.
* @param {FileSystem} fileSystem the file system
* @param {string} jsonFilePath absolute path to JSON file
* @param {ReadJsonOptions} options Options
* @returns {Promise<T>} parsed JSON content
* @param {(err: NodeJS.ErrnoException | Error | null, content?: JsonObject) => void} callback callback
* @returns {void}
*/
async function readJson(fileSystem, jsonFilePath, options = {}) {
function readJson(fileSystem, jsonFilePath, options, callback) {
const { stripComments = false } = options;
const { readJson } = fileSystem;
if (readJson && !stripComments) {
return new Promise((resolve, reject) => {
readJson(jsonFilePath, (err, content) => {
if (err) return reject(err);
resolve(/** @type {T} */ (content));
});
const { readJson: fsReadJson } = fileSystem;
if (fsReadJson && !stripComments) {
fsReadJson(jsonFilePath, (err, content) => {
if (err) return callback(err);
callback(null, /** @type {JsonObject} */ (content));
});
return;
}
const buf = await new Promise((resolve, reject) => {
fileSystem.readFile(jsonFilePath, (err, data) => {
if (err) return reject(err);
resolve(data);
});
fileSystem.readFile(jsonFilePath, (err, data) => {
if (err) return callback(err);
const buf = /** @type {Buffer} */ (data);
if (stripComments) {
const cached = _stripCommentsCache.get(buf);
if (cached !== undefined) return callback(null, cached);
}
let result;
try {
const jsonText = buf.toString();
const jsonWithoutComments = stripComments
? stripJsonComments(jsonText, {
trailingCommas: true,
whitespace: true,
})
: jsonText;
result = JSON.parse(jsonWithoutComments);
} catch (parseErr) {
return callback(/** @type {Error} */ (parseErr));
}
if (stripComments) {
_stripCommentsCache.set(buf, result);
}
callback(null, result);
});
if (stripComments) {
const cached = _stripCommentsCache.get(buf);
if (cached !== undefined) return /** @type {T} */ (cached);
}
const jsonText = /** @type {string} */ (buf.toString());
// Strip comments to support JSONC (e.g., tsconfig.json with comments)
const jsonWithoutComments = stripComments
? stripJsonComments(jsonText, { trailingCommas: true, whitespace: true })
: jsonText;
const result = JSON.parse(jsonWithoutComments);
if (stripComments) {
_stripCommentsCache.set(buf, result);
}
return result;
}
module.exports.readJson = readJson;