Skip to content

Commit

Permalink
feat: add @bfra.me/prettier-plugins package (#168)
Browse files Browse the repository at this point in the history
  • Loading branch information
marcusrbrown committed Mar 27, 2024
1 parent a9b8656 commit 3a3d2e0
Show file tree
Hide file tree
Showing 13 changed files with 182 additions and 104 deletions.
6 changes: 6 additions & 0 deletions .changeset/honest-hats-switch.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@bfra.me/prettier-plugins": minor
"@bfra.me/prettier-config": minor
---

Add `@bfra.me/prettier-plugins` and move inline `package.json` plugin from `@bfra.me/prettier-config`
6 changes: 4 additions & 2 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,10 @@ jobs:
cache: pnpm
node-version-file: '.node-version'

- name: Install dependencies
run: pnpm install --frozen-lockfile
- name: Install dependencies and build packages
env:
HUSKY: 0
run: pnpm bootstrap

- name: Check formatting
run: pnpm run check-format
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"license": "MIT",
"author": "Marcus R. Brown <contact@bfra.me>",
"scripts": {
"bootstrap": "pnpm install --frozen-lockfile && pnpm -r build",
"bootstrap": "pnpm install && pnpm -r build",
"build": "pnpm -r run build",
"check-format": "prettier --check .",
"clean": "rimraf --glob \"packages/**/lib\" \"**/*.tsbuildinfo\"",
Expand Down
2 changes: 1 addition & 1 deletion packages/prettier-config/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
},
"types": "prettier.config.d.ts",
"dependencies": {
"prettier-package-json": "2.8.0"
"@bfra.me/prettier-plugins": "workspace:*"
},
"peerDependencies": {
"prettier": "^3.0.0"
Expand Down
28 changes: 3 additions & 25 deletions packages/prettier-config/prettier.config.js

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

32 changes: 4 additions & 28 deletions packages/prettier-config/prettier.config.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import type {Config, ParserOptions} from 'prettier'
import {parsers} from 'prettier/plugins/babel'
import {format, type Options as PrettierPackageJsonOptions} from 'prettier-package-json'
import {createRequire} from 'module'
import type {Config} from 'prettier'

const jsonStringifyParser = parsers['json-stringify']
const {resolve} = createRequire(import.meta.url)

/**
* Shared Prettier configuration for bfra.me projects.
Expand Down Expand Up @@ -69,30 +68,7 @@ const config: Config = {
},
],

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

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

export default config
60 changes: 60 additions & 0 deletions packages/prettier-plugins/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
{
"name": "@bfra.me/prettier-plugins",
"version": "0.0.0",
"description": "Plugins for Prettier used across bfra.me.",
"license": "MIT",
"author": "Marcus R. Brown <contact@bfra.me>",
"homepage": "https://github.com/bfra-me/works/tree/main/packages/prettier-plugins#readme",
"repository": {
"directory": "packages/prettier-plugins",
"type": "git",
"url": "https://github.com/bfra-me/works.git"
},
"bugs": "https://github.com/bfra-me/works/issues",
"type": "module",
"exports": {
".": {
"default": "./lib/index.js",
"types": "./lib/index.d.ts"
},
"./package-json": {
"default": "./lib/package-json/index.js",
"types": "./lib/package-json/index.d.ts"
},
"./package.json": "./package.json"
},
"main": "lib/index.js",
"files": [
"lib",
"!**/*.map"
],
"scripts": {
"build": "tsup"
},
"sideEffects": false,
"types": "lib/index.d.ts",
"dependencies": {
"prettier-package-json": "2.8.0"
},
"peerDependencies": {
"prettier": "^3.0.0"
},
"devDependencies": {
"@bfra.me/prettier-plugins": "workspace:*",
"@bfra.me/tsconfig": "workspace:*",
"prettier": "3.2.5",
"tsup": "8.0.2"
},
"keywords": [
"bfra.me",
"plugins",
"prettier",
"prettier-plugins",
"shared",
"works"
],
"publishConfig": {
"access": "public",
"provenance": true
}
}
35 changes: 35 additions & 0 deletions packages/prettier-plugins/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# @bfra.me/prettier-plugins

> Plugins for Prettier used across bfra.me.
## Install

### NPM

```sh
npm install --save-dev @bfra.me/prettier-plugins prettier
```

### PNPM

```sh
pnpm add --save-dev @bfra.me/prettier-plugins prettier
```

### Yarn

```sh
yarn add --dev @bfra.me/prettier-plugins prettier
```

## Usage

Add the following entry to your `.prettierrc` file:

```json
"plugins": ["@bfra.me/prettier-plugins/package-json"]
```

## License

[MIT](../../LICENSE.md)
1 change: 1 addition & 0 deletions packages/prettier-plugins/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './package-json'
25 changes: 25 additions & 0 deletions packages/prettier-plugins/src/package-json/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import type {Parser, ParserOptions} from 'prettier'
import {parsers as babelParsers} from 'prettier/plugins/babel'
import {format, type Options as PrettierPackageJsonOptions} from 'prettier-package-json'

const jsonStringifyParser = babelParsers['json-stringify']

export const parsers = {
'json-stringify': {
...jsonStringifyParser,

preprocess(text: string, options: ParserOptions) {
if (jsonStringifyParser.preprocess) {
text = jsonStringifyParser.preprocess(text, options)
}
if (/package.*json$/u.test(options.filepath)) {
text = format(JSON.parse(text), {
tabWidth: options.tabWidth,
useTabs: options.useTabs === true,
...((options['prettier-package-json'] ?? {}) as Partial<PrettierPackageJsonOptions>),
})
}
return text
},
},
} satisfies Record<string, Parser>
11 changes: 11 additions & 0 deletions packages/prettier-plugins/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"$schema": "http://json.schemastore.org/tsconfig",
"extends": "@bfra.me/tsconfig",
"compilerOptions": {
"composite": false,
"incremental": false,
"noEmit": true,
"outDir": "lib"
},
"exclude": ["lib", "node_modules"]
}
11 changes: 11 additions & 0 deletions packages/prettier-plugins/tsup.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import {defineConfig} from 'tsup'

export default defineConfig({
entry: ['src/**/*.ts'],
bundle: false,
clean: true,
dts: true,
format: 'esm',
outDir: 'lib',
sourcemap: true,
})
Loading

0 comments on commit 3a3d2e0

Please sign in to comment.