Skip to content

Commit

Permalink
feat(prettier-config): add inline plugin to format package.json (#159)
Browse files Browse the repository at this point in the history
  • Loading branch information
marcusrbrown authored Mar 22, 2024
1 parent 1afc21c commit 72838eb
Show file tree
Hide file tree
Showing 7 changed files with 56 additions and 24 deletions.
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.

0 comments on commit 72838eb

Please sign in to comment.