Skip to content

Commit

Permalink
feat: set the same order for all json packages
Browse files Browse the repository at this point in the history
Signed-off-by: Francisco Buceta <frbuceta@gmail.com>
  • Loading branch information
frbuceta committed May 3, 2021
1 parent 0bdd523 commit ee7c179
Showing 1 changed file with 57 additions and 1 deletion.
58 changes: 57 additions & 1 deletion packages/monorepo/lib/update-package-json.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,32 @@ const {
runMain,
} = require('./script-util');

const orderedPkgProperties = [
'name',
'description',
'version',
'keywords',
'private',
'license',
'bin',
'main',
'unpkg',
'types',
'author',
'copyright.owner',
'homepage',
'repository',
'bugs',
'engines',
'scripts',
'publishConfig',
'files',
'peerDependencies',
'dependencies',
'devDependencies',
'config',
];

/**
* Check all required fields of package.json for each package on the matching
* with root package.json
Expand All @@ -40,7 +66,7 @@ async function updatePackageJsonFiles(options) {
for (const p of packages) {
debug('Checking package.json for %s@%s', p.name, p.version);
const pkgFile = p.manifestLocation;
const pkg = cloneJson(p.toJSON());
let pkg = cloneJson(p.toJSON());

if (isTypeScriptPackage(p) && !isMonorepoPackage(p)) {
pkg.main = 'dist/index.js';
Expand Down Expand Up @@ -69,6 +95,36 @@ async function updatePackageJsonFiles(options) {
pkg.engines = rootPkg.engines;
}

const unknownProperties = [];
pkg = Object.fromEntries(
Object.entries(pkg).sort(([a], [b]) => {
const ai = orderedPkgProperties.indexOf(a);
const bi = orderedPkgProperties.indexOf(b);
// add the properties that are not in the list before peerDependencies
if (ai === -1) {
if (unknownProperties.indexOf(a) === -1) unknownProperties.push(a);
return (
orderedPkgProperties.indexOf('peerDependencies') -
orderedPkgProperties.indexOf(b)
);
}
if (bi === -1) {
if (unknownProperties.indexOf(b) === -1) unknownProperties.push(b);
return (
orderedPkgProperties.indexOf(a) -
orderedPkgProperties.indexOf('peerDependencies')
);
}
return ai - orderedPkgProperties.indexOf(b);
}),
);

if (unknownProperties.length) {
console.group(`Properties are not recognized in ${p.name}:`);
unknownProperties.forEach(key => console.log('-', key));
console.groupEnd();
}

if (!isJsonEqual(pkg, p.toJSON())) {
if (isDryRun(options)) {
printJson(pkg);
Expand Down

0 comments on commit ee7c179

Please sign in to comment.