UNPKG

syncpack

Version:

Manage multiple package.json files, such as in Lerna Monorepos and Yarn/Pnpm Workspaces

50 lines (49 loc) 1.93 kB
"use strict"; exports.__esModule = true; exports.formatToDisk = exports.format = void 0; var constants_1 = require("../constants"); var get_wrappers_1 = require("./lib/get-wrappers"); var write_if_changed_1 = require("./lib/write-if-changed"); var sortObject = function (sortedKeys, obj) { sortedKeys.forEach(function (key) { var value = obj[key]; delete obj[key]; obj[key] = value; }); }; var sortAlphabetically = function (value) { if (Array.isArray(value)) { value.sort(); } else if (value && typeof value === 'object') { sortObject(Object.keys(value).sort(), value); } }; exports.format = function (wrapper, _a) { var _b = _a === void 0 ? {} : _a, _c = _b.sortAz, sortAz = _c === void 0 ? constants_1.SORT_AZ : _c, _d = _b.sortFirst, sortFirst = _d === void 0 ? constants_1.SORT_FIRST : _d; var contents = wrapper.contents; var sortedKeys = Object.keys(contents).sort(); var keys = new Set(sortFirst.concat(sortedKeys)); if (contents.bugs && typeof contents.bugs === 'object' && contents.bugs.url) { contents.bugs = contents.bugs.url; } if (contents.repository && typeof contents.repository === 'object' && contents.repository.url) { if (contents.repository.url.includes('github.com')) { contents.repository = contents.repository.url.replace(/^.+github\.com\//, ''); } else { contents.repository = contents.repository.url; } } sortAz.forEach(function (key) { return sortAlphabetically(contents[key]); }); sortObject(keys, contents); return contents; }; exports.formatToDisk = function (_a) { var indent = _a.indent, source = _a.source; get_wrappers_1.getWrappers({ source: source }).forEach(function (wrapper) { write_if_changed_1.writeIfChanged(indent, wrapper, function () { exports.format(wrapper); }); }); };