Minor improvement to config loader (#17343)
This commit is contained in:
1 parent
f3486cfbc5
commit
db1a46aac5
1 file changed
+36
-29
@@ -20,7 +20,7 @@ async function importModuleDefault(file) {
|
||||
return module.default;
|
||||
}
|
||||
|
||||
async function readPackageJson(file) {
|
||||
async function readBunPackageJson(file) {
|
||||
try {
|
||||
return await readJson(file);
|
||||
} catch (error) {
|
||||
@@ -28,22 +28,25 @@ async function readPackageJson(file) {
|
||||
// Bun supports comments and trialing comma in `package.json`
|
||||
// And it can load via `import()`
|
||||
// https://bun.sh/blog/bun-v1.2#jsonc-support-in-package-json
|
||||
if (process.versions.bun) {
|
||||
try {
|
||||
return await importModuleDefault(file);
|
||||
} catch {
|
||||
// No op
|
||||
}
|
||||
try {
|
||||
return await importModuleDefault(file);
|
||||
} catch {
|
||||
// No op
|
||||
}
|
||||
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
async function loadConfigFromPackageJson(file) {
|
||||
const { prettier } = await readPackageJson(file);
|
||||
return prettier;
|
||||
}
|
||||
const loadConfigFromPackageJson = process.versions.bun
|
||||
? async function loadConfigFromBunPackageJson(file) {
|
||||
const { prettier } = await readBunPackageJson(file);
|
||||
return prettier;
|
||||
}
|
||||
: async function loadConfigFromPackageJson(file) {
|
||||
const { prettier } = await readJson(file);
|
||||
return prettier;
|
||||
};
|
||||
|
||||
async function loadConfigFromPackageYaml(file) {
|
||||
const { prettier } = await loadYaml(file);
|
||||
@@ -60,25 +63,29 @@ async function loadYaml(file) {
|
||||
}
|
||||
}
|
||||
|
||||
async function loadToml(file) {
|
||||
const content = await readFile(file);
|
||||
try {
|
||||
return parseToml(content);
|
||||
} catch (/** @type {any} */ error) {
|
||||
error.message = `TOML Error in ${file}:\n${error.message}`;
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
async function loadJson5(file) {
|
||||
const content = await readFile(file);
|
||||
try {
|
||||
return json5.parse(content);
|
||||
} catch (/** @type {any} */ error) {
|
||||
error.message = `JSON5 Error in ${file}:\n${error.message}`;
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
const loaders = {
|
||||
async ".toml"(file) {
|
||||
const content = await readFile(file);
|
||||
try {
|
||||
return parseToml(content);
|
||||
} catch (/** @type {any} */ error) {
|
||||
error.message = `TOML Error in ${file}:\n${error.message}`;
|
||||
throw error;
|
||||
}
|
||||
},
|
||||
async ".json5"(file) {
|
||||
const content = await readFile(file);
|
||||
try {
|
||||
return json5.parse(content);
|
||||
} catch (/** @type {any} */ error) {
|
||||
error.message = `JSON5 Error in ${file}:\n${error.message}`;
|
||||
throw error;
|
||||
}
|
||||
},
|
||||
".toml": loadToml,
|
||||
".json5": loadJson5,
|
||||
".json": readJson,
|
||||
".js": importModuleDefault,
|
||||
".mjs": importModuleDefault,
|
||||
|
||||
Reference in new issue
Block a user