feat: add support for package.yaml config (#16157)
Co-authored-by: fisker Cheung <lionkay@gmail.com>
This commit is contained in:
1 parent
a23ea601f2
commit
da772ff1f0
7 files changed
+71
-5
No files matched your search
@@ -0,0 +1,10 @@
|
||||
#### Add support for `package.yaml` config (#16157 by @danielbayley)
|
||||
|
||||
Enable support for reading `prettier` configuration from [`package.yaml`](https://github.com/pnpm/pnpm/pull/1799).
|
||||
|
||||
```yaml
|
||||
# package.yaml
|
||||
prettier:
|
||||
semi: false
|
||||
singleQuote: true
|
||||
```
|
||||
@@ -5,7 +5,7 @@ title: Configuration File
|
||||
|
||||
You can configure Prettier via (in order of precedence):
|
||||
|
||||
- A `"prettier"` key in your `package.json` file.
|
||||
- A `"prettier"` key in your `package.json`, or [`package.yaml`](https://github.com/pnpm/pnpm/pull/1799) file.
|
||||
- A `.prettierrc` file written in JSON or YAML.
|
||||
- A `.prettierrc.json`, `.prettierrc.yml`, `.prettierrc.yaml`, or `.prettierrc.json5` file.
|
||||
- A `.prettierrc.js`, or `prettier.config.js` file that exports an object using `export default` or `module.exports` (depends on the [`type`](https://nodejs.org/api/packages.html#type) value in your `package.json`).
|
||||
|
||||
@@ -1,9 +1,13 @@
|
||||
import isFile from "../../utils/is-file.js";
|
||||
import Searcher from "../searcher.js";
|
||||
import { loadConfigFromPackageJson } from "./loaders.js";
|
||||
import {
|
||||
loadConfigFromPackageJson,
|
||||
loadConfigFromPackageYaml,
|
||||
} from "./loaders.js";
|
||||
|
||||
const CONFIG_FILE_NAMES = [
|
||||
"package.json",
|
||||
"package.yaml",
|
||||
".prettierrc",
|
||||
".prettierrc.json",
|
||||
".prettierrc.yaml",
|
||||
@@ -31,6 +35,14 @@ async function filter({ name, path: file }) {
|
||||
}
|
||||
}
|
||||
|
||||
if (name === "package.yaml") {
|
||||
try {
|
||||
return Boolean(await loadConfigFromPackageYaml(file));
|
||||
} catch {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
@@ -1,14 +1,19 @@
|
||||
import path from "node:path";
|
||||
|
||||
import loadExternalConfig from "./load-external-config.js";
|
||||
import loaders, { loadConfigFromPackageJson } from "./loaders.js";
|
||||
import loaders, {
|
||||
loadConfigFromPackageJson,
|
||||
loadConfigFromPackageYaml,
|
||||
} from "./loaders.js";
|
||||
|
||||
async function loadConfig(configFile) {
|
||||
const { base: fileName, ext: extension } = path.parse(configFile);
|
||||
const load =
|
||||
fileName === "package.json"
|
||||
? loadConfigFromPackageJson
|
||||
: loaders[extension];
|
||||
: fileName === "package.yaml"
|
||||
? loadConfigFromPackageYaml
|
||||
: loaders[extension];
|
||||
|
||||
if (!load) {
|
||||
throw new Error(
|
||||
|
||||
@@ -27,6 +27,11 @@ async function loadConfigFromPackageJson(file) {
|
||||
return prettier;
|
||||
}
|
||||
|
||||
async function loadConfigFromPackageYaml(file) {
|
||||
const { prettier } = await loadYaml(file);
|
||||
return prettier;
|
||||
}
|
||||
|
||||
async function loadYaml(file) {
|
||||
const content = await readFile(file);
|
||||
try {
|
||||
@@ -67,4 +72,4 @@ const loaders = {
|
||||
};
|
||||
|
||||
export default loaders;
|
||||
export { loadConfigFromPackageJson, readJson };
|
||||
export { loadConfigFromPackageJson, loadConfigFromPackageYaml, readJson };
|
||||
@@ -419,3 +419,33 @@ test("Search from directory, not treat file as directory", async () => {
|
||||
"directory/.prettierrc",
|
||||
);
|
||||
});
|
||||
|
||||
test("package.json/package.yaml", async () => {
|
||||
await expect(
|
||||
prettier.resolveConfig(
|
||||
new URL("../cli/config/package/file.js", import.meta.url),
|
||||
),
|
||||
).resolves.toMatchInlineSnapshot(`
|
||||
{
|
||||
"tabWidth": 3,
|
||||
}
|
||||
`);
|
||||
await expect(
|
||||
prettier.resolveConfig(
|
||||
new URL("../cli/config/package/file.ts", import.meta.url),
|
||||
),
|
||||
).resolves.toMatchInlineSnapshot(`
|
||||
{
|
||||
"tabWidth": 5,
|
||||
}
|
||||
`);
|
||||
await expect(
|
||||
prettier.resolveConfig(
|
||||
new URL("../cli/config/package-yaml/file.ts", import.meta.url),
|
||||
),
|
||||
).resolves.toMatchInlineSnapshot(`
|
||||
{
|
||||
"printWidth": 101,
|
||||
}
|
||||
`);
|
||||
});
|
||||
@@ -0,0 +1,4 @@
|
||||
name: my-package
|
||||
version: 1.0.0
|
||||
prettier:
|
||||
printWidth: 101
|
||||
Reference in new issue
Block a user