UNPKG

prettier-plugin-pkg

Version:

An opinionated package.json formatter plugin for Prettier

28 lines 995 B
import babelParser from 'prettier/plugins/babel.js'; import { files } from './rules/files.js'; import { object } from './rules/object.js'; import { dependencyNames, sort } from './rules/sort.js'; const PKG_REG = /[/\\]?package\.json$/; const { json: { parse }, } = babelParser.parsers; const format = (properties) => { let props = ['engines', 'scripts', ...dependencyNames].reduce((acc, item) => object(acc, item), sort(properties)); props = files(props); return props; }; export default { name: 'prettier-plugin-pkg', parsers: { 'json-stringify': { ...babelParser.parsers['json-stringify'], parse(text, options) { const { filepath } = options; const ast = parse(text, options); if (PKG_REG.test(filepath)) { ast.node.properties = format(ast.node.properties); } return ast; }, }, }, }; //# sourceMappingURL=index.js.map