Skip to content

Commit

Permalink
feat: packageSortOrder option
Browse files Browse the repository at this point in the history
  • Loading branch information
hyoban committed Apr 11, 2024
1 parent b5fa154 commit 084e11e
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 1 deletion.
43 changes: 43 additions & 0 deletions lib/__snapshots__/index.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,46 @@ exports[`format should be correct 1`] = `
}
"
`;

exports[`format should follow custom order 1`] = `
"{
"version": "",
"name": "",
"description": "",
"keywords": [
"C",
"B",
"A"
],
"license": "",
"author": "",
"scripts": {
"prebuild": "",
"build": "",
"postbuild": "",
"dev": "",
"lint": "",
"start": "",
"pretest": "",
"test": "",
"posttest": ""
},
"dependencies": {
"A": "*",
"B": "*"
},
"devDependencies": {
"A": "*",
"B": "*"
},
"peerDependencies": {
"A": "*",
"B": "*"
},
"optionalDependencies": {
"A": "*",
"B": "*"
}
}
"
`;
21 changes: 20 additions & 1 deletion lib/index.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,27 @@ exports.parsers = {
if (parser.preprocess) {
text = parser.preprocess(text, options)
}
const sortPackageJsonOptions =
options.packageSortOrder.length !== 0
? {
sortOrder: options.packageSortOrder,
}
: undefined

return testPath(options.filepath) ? sortPackageJson.default(text) : text
return testPath(options.filepath)
? sortPackageJson.default(text, sortPackageJsonOptions)
: text
},
},
}

/** @type {import('prettier').Plugin['options']} */
exports.options = {
packageSortOrder: {
category: 'Global',
type: 'string',
array: true,
default: [{ value: [] }],
description: 'Custom ordering array',
},
}
11 changes: 11 additions & 0 deletions lib/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,4 +71,15 @@ describe('format', () => {

delete parsers['json-stringify'].preprocess
})

it('should follow custom order', () =>
expect(
Promise.resolve(
prettier.format(uglyJson, {
filepath: 'package.json',
plugins: ['./lib/index.cjs'],
packageSortOrder: ['version', 'name'],
}),
),
).resolves.toMatchSnapshot())
})

0 comments on commit 084e11e

Please sign in to comment.