prettier-plugin-pkg
Version:
An opinionated package.json formatter plugin for Prettier
211 lines (203 loc) • 4.72 kB
JavaScript
var babelParser = require('prettier/plugins/babel.js');
function alphabetSort(a, b) {
return a > b ? 1 : a < b ? -1 : 0;
}
const sortObject = (a, b) => alphabetSort(a.key.value, b.key.value);
const sortStringArray = (a, b) => alphabetSort(a.value, b.value);
const process$1 = (props) => {
const filesNode = props.find((prop) => prop.key.value === "files");
if (!filesNode) {
return props;
}
let readme;
let license;
const filesNodeValue = filesNode.value;
const [normals, negations] = filesNodeValue.elements.reduce(
(acc, node) => {
const value = node.value.toLowerCase();
if (value === "license") {
license = node;
return acc;
}
if (value === "readme" || value === "readme.md") {
readme = node;
return acc;
}
acc[+value.startsWith("!")].push(node);
return acc;
},
[[], []]
);
normals.sort(sortStringArray);
negations.sort(sortStringArray);
if (readme) {
normals.push(readme);
}
if (license) {
normals.push(license);
}
filesNodeValue.elements = [...normals, ...negations];
return props;
};
const process = (props, key) => {
const item = props.find((prop) => prop.key.value === key);
if (item) {
if ("elements" in item.value) {
item.value.elements.sort(sortStringArray);
} else if ("properties" in item.value) {
item.value.properties.sort(sortObject);
}
}
return props;
};
const dependencyNames = [
"bundledDependencies",
"peerDependencies",
"peerDependenciesMeta",
"dependencies",
"dependenciesMeta",
"optionalDependencies",
"devDependencies",
"overrides",
"resolutions"
];
const primary = [
// schema definition
"$schema",
// meta
"name",
"version",
"type",
"flat",
"displayName",
"description",
"categories",
"repository",
"homepage",
"bugs",
"author",
"publisher",
"maintainers",
"contributors",
"donate",
"funding",
"sponsor",
"license",
"preview",
"private",
"workspaces",
// constraints
"languageName",
"packageManager",
"engines",
"cpu",
"os",
// entries
"man",
"bin",
"main",
"module",
"imports",
"exports",
"esnext",
"es2020",
"esm2020",
"fesm2020",
"es2015",
"esm2015",
"fesm2015",
"es5",
"esm5",
"fesm5",
"browser",
"umd",
"jsdelivr",
"unpkg",
"types",
"typings",
"typesVersions",
// contents and utils
"directories",
"files",
"keywords",
"scripts",
"config",
// dependencies
...dependencyNames,
"publishConfig",
"sideEffects",
// vscode spec
"icon",
"badges",
"galleryBanner",
"activationEvents",
"contributes",
"markdown",
"qna",
"extensionPack",
"extensionDependencies",
"extensionKind"
];
const sort = (props) => {
const others = [];
const known = props.filter((prop) => {
if (primary.includes(prop.key.value)) {
return true;
}
others.push(prop);
return false;
});
known.sort(
(a, b) => alphabetSort(primary.indexOf(a.key.value), primary.indexOf(b.key.value))
);
others.sort(sortObject);
return [...known, ...others];
};
var __defProp = Object.defineProperty;
var __defProps = Object.defineProperties;
var __getOwnPropDescs = Object.getOwnPropertyDescriptors;
var __getOwnPropSymbols = Object.getOwnPropertySymbols;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __propIsEnum = Object.prototype.propertyIsEnumerable;
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
var __spreadValues = (a, b) => {
for (var prop in b || (b = {}))
if (__hasOwnProp.call(b, prop))
__defNormalProp(a, prop, b[prop]);
if (__getOwnPropSymbols)
for (var prop of __getOwnPropSymbols(b)) {
if (__propIsEnum.call(b, prop))
__defNormalProp(a, prop, b[prop]);
}
return a;
};
var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
const PKG_REG = /[/\\]?package\.json$/;
const {
json: { parse }
} = babelParser.parsers;
const format = (properties) => {
let props = ["engines", "scripts", ...dependencyNames].reduce(
(acc, item) => process(acc, item),
sort(properties)
);
props = process$1(props);
return props;
};
var index = {
name: "prettier-plugin-pkg",
parsers: {
"json-stringify": __spreadProps(__spreadValues({}, 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;
}
})
}
};
module.exports = index;
;