Skip to content

Commit

Permalink
build: check order of deps keys in package.json files
Browse files Browse the repository at this point in the history
Add a new step to `bin/check-package-metadata.js` to ensure that all
public packages list deps in the following order:
1. `peerDependencies`
2. `dependencies
3. `devDependencies`

Signed-off-by: Miroslav Bajtoš <[email protected]>
  • Loading branch information
bajtos committed Nov 20, 2020
1 parent d9d5eaf commit d718080
Showing 1 changed file with 26 additions and 0 deletions.
26 changes: 26 additions & 0 deletions bin/check-package-metadata.js
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,8 @@ async function checkPkgsPackageJson(packages, rootPkg) {
if (!isRepositoryDirectoryExist) {
errors.push(`${p.name} directory doesn't exist in the monorepo`);
}

checkDepsOrder(p, pkg, errors);
}

return errors;
Expand Down Expand Up @@ -202,6 +204,30 @@ async function checkPackagesMetadata() {
}
}

function checkDepsOrder(lernaPkg, pkgJson, errors) {
const actualOrder = Object.keys(pkgJson).filter(k =>
['dependencies', 'devDependencies', 'peerDependencies'].includes(k),
);

const expectedOrder = [
'peerDependencies',
'dependencies',
'devDependencies',
].filter(k => actualOrder.includes(k));

const actualStr = actualOrder.join(' ');
const expectedStr = expectedOrder.join(' ');

if (actualStr !== expectedStr) {
const pkgPath = path.relative(lernaPkg.rootPath, lernaPkg.location);
errors.push(
`${pkgPath}/package.json has incorrect order of keys.\n` +
` Actual: ${actualStr}\n` +
` Expected: ${expectedStr}`,
);
}
}

module.exports = checkPackagesMetadata;

runMain(module, checkPackagesMetadata);

0 comments on commit d718080

Please sign in to comment.