Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(prettier-config): add inline plugin to format package.json #159

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/fast-ways-bow.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@bfra.me/prettier-config": minor
---

Add an inline Prettier plugin to format `package.json` files using [prettier-package-json](https://github.com/cameronhunter/prettier-package-json)
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,5 @@ yarn.lock
node_modules
oclif.manifest.json
*.tsbuildinfo
*.d.ts.map
*.js.map
2 changes: 2 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@

"cSpell.words": ["bfra", "frameworkers"],

"prettier.enable": true,

"typescript.enablePromptUseWorkspaceTsdk": true,
"typescript.tsdk": "node_modules/typescript/lib"
}
23 changes: 22 additions & 1 deletion packages/prettier-config/prettier.config.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

26 changes: 25 additions & 1 deletion packages/prettier-config/prettier.config.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import type {Config} from 'prettier'
import type {Config, ParserOptions} from 'prettier'
import {parsers} from 'prettier/plugins/babel'
import {format, type Options as PrettierPackageJsonOptions} from 'prettier-package-json'

/**
* Shared Prettier configuration for bfra.me projects.
Expand Down Expand Up @@ -60,6 +62,28 @@ const config: Config = {
},
},
],

plugins: [
{
parsers: {
'json-stringify': {
...parsers['json-stringify'],

preprocess(text: string, options: ParserOptions) {
if (/package.*json$/u.test(options.filepath)) {
return format(JSON.parse(text), {
tabWidth: options.tabWidth,
useTabs: options.useTabs === true,
...((options['prettier-package-json'] ??
{}) as Partial<PrettierPackageJsonOptions>),
})
}
return text
},
},
},
},
],
}

export default config
2 changes: 0 additions & 2 deletions packages/prettier-config/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@
"$schema": "http://json.schemastore.org/tsconfig",
"extends": "@bfra.me/tsconfig",
"compilerOptions": {
"inlineSourceMap": true,
"sourceMap": false,
"outDir": ".",
"rootDir": "."
},
Expand Down
20 changes: 0 additions & 20 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.