format-package
Advanced tools
Comparing version 4.5.1 to 5.0.0
@@ -1,5 +0,19 @@ | ||
function formatter(obj) { | ||
return [JSON.stringify(obj, null, 2), '\n'].join(''); | ||
async function formatter(obj) { | ||
const content = JSON.stringify(obj, null, 2); | ||
let prettier; | ||
try { | ||
prettier = require('prettier'); | ||
} catch (error) { | ||
return [content, '\n'].join(''); | ||
} | ||
const options = (await prettier.resolveConfig(process.cwd())) || {}; | ||
return prettier.format(content, { | ||
...options, | ||
parser: 'json', | ||
printWidth: 0, | ||
}); | ||
} | ||
module.exports = formatter; |
{ | ||
"name": "format-package", | ||
"version": "4.5.1", | ||
"version": "5.0.0", | ||
"description": "Sensible formatting and ordering of package.json", | ||
@@ -32,14 +32,10 @@ "license": "MIT", | ||
"format": "npm-run-all format:*", | ||
"format:js": "prettier --write \"**/*.{md,js,json}\"", | ||
"format:pkg": "./lib/cli/index.js -wp", | ||
"format:src": "prettier --write \"**/*.{md,js,json}\"", | ||
"gamut": "npm-run-all reset docs format lint test", | ||
"lint": "eslint \"**/*.js\"", | ||
"prepublishOnly": "yarn gamut", | ||
"reset": "cross-env rimraf node_modules && yarn install", | ||
"test": "jest" | ||
}, | ||
"husky": { | ||
"hooks": { | ||
"post-merge": "install-deps-postmerge", | ||
"pre-commit": "lint-staged", | ||
"pre-push": "yarn test" | ||
} | ||
}, | ||
"lint-staged": { | ||
@@ -63,18 +59,22 @@ "**/package.json": [ | ||
"convert-hrtime": "^3.0.0", | ||
"globby": "^9.2.0", | ||
"sort-scripts": "^1.0.0", | ||
"globby": "10.0.1", | ||
"sort-scripts": "^1.0.1", | ||
"yargs": "^13.2.4" | ||
}, | ||
"peerDependencies": { | ||
"prettier": "^1.6.0" | ||
}, | ||
"devDependencies": { | ||
"codecov": "^3.5.0", | ||
"eslint": "^5.16.0", | ||
"eslint-config-airbnb-base": "^13.1.0", | ||
"eslint-config-prettier": "^4.3.0", | ||
"cross-env": "^5.2.0", | ||
"eslint": "^6.0.1", | ||
"eslint-config-airbnb-base": "13.2.0", | ||
"eslint-config-prettier": "^6.0.0", | ||
"eslint-plugin-import": "^2.17.2", | ||
"execa": "^1.0.0", | ||
"execa": "2.0.3", | ||
"glob": "^7.1.4", | ||
"husky": "^2.3.0", | ||
"husky": "3.0.0", | ||
"install-deps-postmerge": "^1.0.3", | ||
"jest": "^24.8.0", | ||
"lint-staged": "^8.1.7", | ||
"lint-staged": "9.0.2", | ||
"markdown-magic": "^0.1.25", | ||
@@ -86,4 +86,4 @@ "markdown-magic-engines": "^1.0.2", | ||
"npm-run-all": "^4.1.5", | ||
"prettier": "^1.17.1" | ||
"prettier": "^1.18.2" | ||
} | ||
} |
@@ -8,3 +8,3 @@ # Format Package [![codecov](https://codecov.io/gh/camacho/format-package/branch/master/graph/badge.svg)](https://codecov.io/gh/camacho/format-package) | ||
```sh | ||
yarn add -D format-package | ||
yarn add -D format-package prettier@^1.6.0 | ||
``` | ||
@@ -321,3 +321,3 @@ | ||
By default, `JSON.stringify` is used: | ||
By default, the formatter will try to use [`prettier`](https://github.com/prettier/prettier) if it is installed, and will fallback to `JSON.stringify` if the [peer dependency](https://nodejs.org/es/blog/npm/peer-dependencies/) is not found: | ||
@@ -328,4 +328,18 @@ <!-- AUTO-GENERATED-CONTENT:START (CODE:src=./lib/defaults/formatter.js) --> | ||
```js | ||
function formatter(obj) { | ||
return [JSON.stringify(obj, null, 2), '\n'].join(''); | ||
async function formatter(obj) { | ||
const content = JSON.stringify(obj, null, 2); | ||
let prettier; | ||
try { | ||
prettier = require('prettier'); | ||
} catch (error) { | ||
return [content, '\n'].join(''); | ||
} | ||
const options = (await prettier.resolveConfig(process.cwd())) || {}; | ||
return prettier.format(content, { | ||
...options, | ||
parser: 'json', | ||
printWidth: 0, | ||
}); | ||
} | ||
@@ -338,22 +352,2 @@ | ||
An alternative would be to use [`prettier`](https://github.com/prettier/prettier): | ||
<!-- AUTO-GENERATED-CONTENT:START (PRETTIER) --> | ||
```js | ||
const formatPkg = require('format-package'); | ||
const prettier = require('prettier'); | ||
const pkg = require('./package.json'); | ||
const formatter = async content => { | ||
const options = await prettier.resolveConfig('./package.json'); | ||
// include `{ parser: 'json' }` in the options passed to prettier | ||
return prettier.format(content, { ...options, parser: 'json' }); | ||
}; | ||
formatPkg(pkg, { formatter }); | ||
``` | ||
<!-- AUTO-GENERATED-CONTENT:END *--> | ||
### CLI | ||
@@ -432,10 +426,13 @@ | ||
| Script | Description | | ||
| ------------ | --------------------------------------------------------------------------------------------------------------------- | | ||
| `docs` | updates any auto-generated-content blocks in [Markdown](https://guides.github.com/features/mastering-markdown/) files | | ||
| `format` | format the application code | | ||
| `format:js` | format JS using [prettier](https://github.com/prettier/prettier) | | ||
| `format:pkg` | format package.json | | ||
| `lint` | lint the application code | | ||
| `test` | run unit tests for the application | | ||
| Script | Description | | ||
| ---------------- | --------------------------------------------------------------------------------------------------------------------- | | ||
| `docs` | updates any auto-generated-content blocks in [Markdown](https://guides.github.com/features/mastering-markdown/) files | | ||
| `format` | format the application code | | ||
| `format:pkg` | format package.json | | ||
| `format:src` | format source content using [prettier](https://github.com/prettier/prettier) | | ||
| `gamut` | run the full gamut of checks - reset environment, generate docs, format and lint code, and run tests | | ||
| `lint` | lint the application code | | ||
| `prepublishOnly` | make sure the package is in good state before publishing | | ||
| `reset` | reset the `node_modules` dependencies | | ||
| `test` | run unit tests for the application | | ||
@@ -442,0 +439,0 @@ <!-- AUTO-GENERATED-CONTENT:END --> |
28407
304
6
19
437
+ Added@nodelib/fs.scandir@2.1.5(transitive)
+ Added@nodelib/fs.stat@2.0.5(transitive)
+ Added@nodelib/fs.walk@1.2.8(transitive)
+ Addedarray-union@2.1.0(transitive)
+ Addedbraces@3.0.3(transitive)
+ Addeddir-glob@3.0.1(transitive)
+ Addedfast-glob@3.3.3(transitive)
+ Addedfastq@1.19.0(transitive)
+ Addedfill-range@7.1.1(transitive)
+ Addedglob-parent@5.1.2(transitive)
+ Addedglobby@10.0.1(transitive)
+ Addedignore@5.3.2(transitive)
+ Addedis-number@7.0.0(transitive)
+ Addedmicromatch@4.0.8(transitive)
+ Addedpath-type@4.0.0(transitive)
+ Addedpicomatch@2.3.1(transitive)
+ Addedprettier@1.19.1(transitive)
+ Addedqueue-microtask@1.2.3(transitive)
+ Addedreusify@1.0.4(transitive)
+ Addedrun-parallel@1.2.0(transitive)
+ Addedslash@3.0.0(transitive)
+ Addedto-regex-range@5.0.1(transitive)
- Removed@mrmlnc/readdir-enhanced@2.2.1(transitive)
- Removed@nodelib/fs.stat@1.1.3(transitive)
- Removedarr-diff@4.0.0(transitive)
- Removedarr-flatten@1.1.0(transitive)
- Removedarr-union@3.1.0(transitive)
- Removedarray-union@1.0.2(transitive)
- Removedarray-uniq@1.0.3(transitive)
- Removedarray-unique@0.3.2(transitive)
- Removedassign-symbols@1.0.0(transitive)
- Removedatob@2.1.2(transitive)
- Removedbase@0.11.2(transitive)
- Removedbraces@2.3.2(transitive)
- Removedcache-base@1.0.1(transitive)
- Removedcall-me-maybe@1.0.2(transitive)
- Removedclass-utils@0.3.6(transitive)
- Removedcollection-visit@1.0.0(transitive)
- Removedcomponent-emitter@1.3.1(transitive)
- Removedcopy-descriptor@0.1.1(transitive)
- Removeddebug@2.6.9(transitive)
- Removeddecode-uri-component@0.2.2(transitive)
- Removeddefine-property@0.2.51.0.02.0.2(transitive)
- Removeddir-glob@2.2.2(transitive)
- Removedexpand-brackets@2.1.4(transitive)
- Removedextend-shallow@2.0.13.0.2(transitive)
- Removedextglob@2.0.4(transitive)
- Removedfast-glob@2.2.7(transitive)
- Removedfill-range@4.0.0(transitive)
- Removedfor-in@1.0.2(transitive)
- Removedfragment-cache@0.2.1(transitive)
- Removedfunction-bind@1.1.2(transitive)
- Removedget-value@2.0.6(transitive)
- Removedglob-parent@3.1.0(transitive)
- Removedglob-to-regexp@0.3.0(transitive)
- Removedglobby@9.2.0(transitive)
- Removedhas-value@0.3.11.0.0(transitive)
- Removedhas-values@0.1.41.0.0(transitive)
- Removedhasown@2.0.2(transitive)
- Removedignore@4.0.6(transitive)
- Removedis-accessor-descriptor@1.0.1(transitive)
- Removedis-buffer@1.1.6(transitive)
- Removedis-data-descriptor@1.0.1(transitive)
- Removedis-descriptor@0.1.71.0.3(transitive)
- Removedis-extendable@0.1.11.0.1(transitive)
- Removedis-glob@3.1.0(transitive)
- Removedis-number@3.0.0(transitive)
- Removedis-plain-object@2.0.4(transitive)
- Removedis-windows@1.0.2(transitive)
- Removedisarray@1.0.0(transitive)
- Removedisobject@2.1.03.0.1(transitive)
- Removedkind-of@3.2.24.0.06.0.3(transitive)
- Removedmap-cache@0.2.2(transitive)
- Removedmap-visit@1.0.0(transitive)
- Removedmicromatch@3.1.10(transitive)
- Removedmixin-deep@1.3.2(transitive)
- Removedms@2.0.0(transitive)
- Removednanomatch@1.2.13(transitive)
- Removedobject-copy@0.1.0(transitive)
- Removedobject-visit@1.0.1(transitive)
- Removedobject.pick@1.3.0(transitive)
- Removedpascalcase@0.1.1(transitive)
- Removedpath-dirname@1.0.2(transitive)
- Removedpath-type@3.0.0(transitive)
- Removedpify@3.0.04.0.1(transitive)
- Removedposix-character-classes@0.1.1(transitive)
- Removedregex-not@1.0.2(transitive)
- Removedrepeat-element@1.1.4(transitive)
- Removedrepeat-string@1.6.1(transitive)
- Removedresolve-url@0.2.1(transitive)
- Removedret@0.1.15(transitive)
- Removedsafe-regex@1.1.0(transitive)
- Removedset-value@2.0.1(transitive)
- Removedslash@2.0.0(transitive)
- Removedsnapdragon@0.8.2(transitive)
- Removedsnapdragon-node@2.1.1(transitive)
- Removedsnapdragon-util@3.0.1(transitive)
- Removedsource-map@0.5.7(transitive)
- Removedsource-map-resolve@0.5.3(transitive)
- Removedsource-map-url@0.4.1(transitive)
- Removedsplit-string@3.1.0(transitive)
- Removedstatic-extend@0.1.2(transitive)
- Removedto-object-path@0.3.0(transitive)
- Removedto-regex@3.0.2(transitive)
- Removedto-regex-range@2.1.1(transitive)
- Removedunion-value@1.0.1(transitive)
- Removedunset-value@1.0.0(transitive)
- Removedurix@0.1.0(transitive)
- Removeduse@3.1.1(transitive)
Updatedglobby@10.0.1
Updatedsort-scripts@^1.0.1