Skip to content

Commit

Permalink
Merge pull request #3 from matzkoh/topic-use-external-sort-lib
Browse files Browse the repository at this point in the history
BREAKING CHANGE: replace sort logic with sort-package-json
  • Loading branch information
matzkoh authored Jul 6, 2019
2 parents 2245070 + 241059d commit af01d0b
Show file tree
Hide file tree
Showing 7 changed files with 110 additions and 181 deletions.
50 changes: 10 additions & 40 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,48 +1,18 @@
# Prettier plugin for package.json

[![CircleCI](https://circleci.com/gh/matzkoh/prettier-plugin-packagejson.svg?style=shield)](https://circleci.com/gh/matzkoh/prettier-plugin-packagejson)
[![Renovate](https://badges.renovateapi.com/github/matzkoh/prettier-plugin-packagejson)](https://renovatebot.com/)
[![npm](https://img.shields.io/npm/v/prettier-plugin-packagejson.svg)](https://www.npmjs.com/package/prettier-plugin-packagejson)

## Installation

```bash
$ npm i -D prettier prettier-plugin-packagejson
```

`overrides` is required because native json parser takes precedence over this plugin.
# prettier-plugin-packagejson

```json
{
"semi": false,
"singleQuote": true,
"trailingComma": "all",
This is [Prettier] plugin to make the keys of package.json well sorted order.

"overrides": [
{
"files": "package.json",
"options": {
"parser": "package-json"
}
}
]
}
```
Using [sort-package-json].

## Rules
[Prettier]: https://github.com/prettier/prettier
[sort-package-json]: https://github.com/keithamus/sort-package-json

- Primary (ordered to top)
1. name
2. description
3. version
4. author
5. license
- Others
- Sorted alphabetically
- Sort children
- dependencies
- devDependencies
- peerDependencies
- optionalDependencies
- keywords
## Installation

ToDo: Add more tweaks
```bash
$ npm i -D prettier prettier-plugin-packagejson
```
17 changes: 8 additions & 9 deletions lib/index.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
const { parsers } = require('prettier/parser-babylon')
const sortPackageJson = require('sort-package-json')
const parser = parsers['json-stringify']
const printer = require('./printer')

exports.parsers = {
'package-json': {
'json-stringify': {
...parser,
astFormat: 'package-json',
},
}

exports.printers = {
'package-json': {
...printer,
preprocess(text, options) {
if (parser.preprocess) {
text = parser.preprocess(text, options)
}
return sortPackageJson(text)
},
},
}
90 changes: 0 additions & 90 deletions lib/printer.js

This file was deleted.

22 changes: 21 additions & 1 deletion package-lock.json

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

34 changes: 26 additions & 8 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,21 +1,39 @@
{
"name": "prettier-plugin-packagejson",
"description": "Prettier package.json plugin to make the order of properties nice.",
"version": "0.0.0-development",
"license": "MIT",
"dependencies": {
"prettier": "1.18.2"
},
"files": [
"lib"
],
"description": "Prettier package.json plugin to make the order of properties nice.",
"keywords": [
"package.json",
"plugin",
"prettier"
],
"homepage": "https://github.com/matzkoh/prettier-plugin-packagejson#readme",
"bugs": {
"url": "https://github.com/matzkoh/prettier-plugin-packagejson/issues"
},
"repository": {
"type": "git",
"url": "git+https://github.com/matzkoh/prettier-plugin-packagejson.git"
},
"license": "MIT",
"files": [
"lib"
],
"main": "lib",
"directories": {
"lib": "lib",
"test": "test"
},
"scripts": {
"test": "node test"
},
"dependencies": {
"sort-package-json": "1.22.1"
},
"devDependencies": {
"prettier": "1.18.2"
},
"peerDependencies": {
"prettier": ">= 1.13"
}
}
8 changes: 0 additions & 8 deletions prettier.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,4 @@ module.exports = {
singleQuote: true,
trailingComma: 'all',
plugins: ['.'],
overrides: [
{
files: 'package.json',
options: {
parser: 'package-json',
},
},
],
}
70 changes: 45 additions & 25 deletions test/index.js
Original file line number Diff line number Diff line change
@@ -1,41 +1,66 @@
const prettier = require('prettier')

const code = `{
const input = `{
"license": "",
"author": "",
"version": "",
"devDependencies": {
"B": "*",
"A": "*"
},
"description": "",
"name": "",
"keywords": [
"C",
"B",
"A",
],
"dependencies": {
"B": "*",
"A": "*"
},
"devDependencies": {
"scripts": {
"test": "",
"posttest": "",
"start": "",
"pretest": "",
"build": "",
"postbuild": "",
"prebuild": "",
"lint": ""
},
"peerDependencies": {
"B": "*",
"A": "*"
},
"author": "",
"name": "",
"optionalDependencies": {
"B": "*",
"A": "*"
},
"peerDependencies": {
"B": "*",
"A": "*"
}
"keywords": [
"C",
"B",
"A"
]
}
`

const expected = `{
"name": "",
"description": "",
"version": "",
"author": "",
"description": "",
"keywords": [
"A",
"B",
"C"
],
"license": "",
"author": "",
"scripts": {
"build": "",
"lint": "",
"postbuild": "",
"prebuild": "",
"start": "",
"pretest": "",
"test": "",
"posttest": ""
},
"dependencies": {
"A": "*",
"B": "*"
Expand All @@ -44,26 +69,21 @@ const expected = `{
"A": "*",
"B": "*"
},
"keywords": [
"A",
"B",
"C"
],
"optionalDependencies": {
"peerDependencies": {
"A": "*",
"B": "*"
},
"peerDependencies": {
"optionalDependencies": {
"A": "*",
"B": "*"
}
}
`

const formatted = prettier.format(code, {
parser: 'package-json',
const output = prettier.format(input, {
filepath: 'package.json',
plugins: ['.'],
})

console.assert(formatted === expected)
console.assert(output === expected)
console.log('\033[32mpassed!\033[39m')

0 comments on commit af01d0b

Please sign in to comment.