Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix order of deps-related fields on package.json + add automated check #6810

Merged
merged 3 commits into from
Nov 23, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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}`,
);
}
}
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Example output:

Error: Some of the packages are not following loopback-next guidelines:                                |Error: Some of the packages are not following loopback-next guidelines:
                                                                                                       |
- docs/package.json has incorrect order of keys.                                                       |- docs/package.json has incorrect order of keys.
  Actual:   devDependencies dependencies                                                               |  Actual:   devDependencies dependencies
  Expected: dependencies devDependencies                                                               |  Expected: dependencies devDependencies
- examples/greeter-extension/package.json has incorrect order of keys.                                 |- examples/greeter-extension/package.json has incorrect order of keys.
  Actual:   devDependencies dependencies                                                               |  Actual:   devDependencies dependencies
  Expected: dependencies devDependencies


module.exports = checkPackagesMetadata;

runMain(module, checkPackagesMetadata);
56 changes: 56 additions & 0 deletions bin/fix-monorepo.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,19 @@
*/
'use strict';

const path = require('path');

const syncDevDeps = require('./sync-dev-deps');
const updateMonorepo = require('./update-monorepo-file');

const {
isJsonEqual,
loadLernaRepo,
runMain,
updatePackageDeps,
updatePackageJson,
updateTsProjectRefs,
writeJsonSync,
} = require('../packages/monorepo');

async function fixMonorepo() {
Expand All @@ -31,6 +36,57 @@ async function fixMonorepo() {
await updateMonorepo();
// Ensure TypeScript project references are up to date
await updateTsProjectRefs();
// Ensure consistent order of keys in package.json
await updateOrderOfPackageJsonFields();
}

runMain(module, fixMonorepo);

async function updateOrderOfPackageJsonFields() {
const {packages} = await loadLernaRepo();
for (const pkg of packages.filter(p => !p.private)) {
const manifest = pkg.toJSON();

// Keys to update in the desired order,
// filtered to those present in `package.json` only.
const depKeys = [
'peerDependencies',
'dependencies',
'devDependencies',
].filter(k => k in manifest);

// Get all top-level keys like "name", "version", "dependencies"
let keys = Object.keys(manifest);
// Remove "*dependencies"
keys = keys.filter(k => !depKeys.includes(k));

// Find the position of "publishConfig" (if present) or "license"
let ix = keys.indexOf('publishConfig');
if (ix === -1) {
ix = keys.indexOf('license');
if (ix === -1) {
const relPath = path.relative(pkg.rootPath, pkg.manifestLocation);
throw new Error(
`Fatal error: ${relPath} is missing required "license" field.`,
);
}
}

// Insert back "*dependencies" after "publishConfig" or "license"
keys.splice(
ix + 1, // index at which to start changing the array.
0, // number of elements to remove
...depKeys, // elements to insert
);

// Build a new manifest with the keys in the correct order
const updated = {};
for (const k of keys) {
updated[k] = manifest[k];
}

if (!isJsonEqual(updated, manifest)) {
writeJsonSync(pkg.manifestLocation, updated);
}
}
}
8 changes: 4 additions & 4 deletions bodyparsers/rest-msgpack/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@
"src",
"!*/__tests__"
],
"copyright.owner": "IBM Corp.",
"publishConfig": {
"access": "public"
},
"peerDependencies": {
"@loopback/core": "^2.13.0",
"@loopback/rest": "^9.1.0"
Expand All @@ -45,9 +49,5 @@
"@types/node": "^10.17.35",
"@types/type-is": "^1.6.3",
"typescript": "~4.1.2"
},
"copyright.owner": "IBM Corp.",
"publishConfig": {
"access": "public"
}
}
14 changes: 7 additions & 7 deletions docs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,13 @@
"publishConfig": {
"access": "public"
},
"dependencies": {
"fs-extra": "^9.0.1",
"tslib": "^2.0.3"
},
"devDependencies": {
"@loopback/build": "^6.2.7"
},
"engines": {
"node": "^10.16 || 12 || 14"
},
Expand All @@ -26,16 +33,9 @@
"version": "node ./bin/copy-readmes.js && node ./bin/copy-changelogs.js && cd .. && npm run tsdocs",
"clean": "lb-clean loopback-docs*.tgz package apidocs site/readmes site/changelogs site/apidocs"
},
"devDependencies": {
"@loopback/build": "^6.2.7"
},
"repository": {
"type": "git",
"url": "https://github.com/strongloop/loopback-next.git",
"directory": "docs"
},
"dependencies": {
"fs-extra": "^9.0.1",
"tslib": "^2.0.3"
}
}
54 changes: 27 additions & 27 deletions examples/access-control-migration/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,33 @@
"publishConfig": {
"access": "public"
},
"dependencies": {
"@loopback/authentication": "^7.0.4",
"@loopback/authorization": "^0.7.4",
"@loopback/boot": "^3.1.1",
"@loopback/core": "^2.13.0",
"@loopback/repository": "^3.2.1",
"@loopback/rest": "^9.1.0",
"@loopback/rest-explorer": "^3.0.4",
"@loopback/security": "^0.3.4",
"@loopback/service-proxy": "^3.0.4",
"@types/bcryptjs": "2.4.2",
"bcryptjs": "^2.4.3",
"casbin": "^5.2.1",
"jsonwebtoken": "^8.5.1",
"loopback-connector-rest": "^4.0.1"
},
"devDependencies": {
"@loopback/build": "^6.2.7",
"@loopback/eslint-config": "^10.0.3",
"@loopback/http-caching-proxy": "^2.1.18",
"@loopback/testlab": "^3.2.9",
"@types/lodash": "^4.14.165",
"@types/node": "^10.17.35",
"eslint": "^7.14.0",
"lodash": "^4.17.20",
"typescript": "~4.1.2"
},
"scripts": {
"build": "lb-tsc",
"build:watch": "lb-tsc --watch",
Expand Down Expand Up @@ -41,33 +68,6 @@
"url": "https://github.com/strongloop/loopback-next.git",
"directory": "examples/access-control-migration"
},
"dependencies": {
"@loopback/authentication": "^7.0.4",
"@loopback/authorization": "^0.7.4",
"@loopback/boot": "^3.1.1",
"@loopback/core": "^2.13.0",
"@loopback/repository": "^3.2.1",
"@loopback/rest": "^9.1.0",
"@loopback/rest-explorer": "^3.0.4",
"@loopback/security": "^0.3.4",
"@loopback/service-proxy": "^3.0.4",
"@types/bcryptjs": "2.4.2",
"bcryptjs": "^2.4.3",
"casbin": "^5.2.1",
"jsonwebtoken": "^8.5.1",
"loopback-connector-rest": "^4.0.1"
},
"devDependencies": {
"@loopback/build": "^6.2.7",
"@loopback/eslint-config": "^10.0.3",
"@loopback/http-caching-proxy": "^2.1.18",
"@loopback/testlab": "^3.2.9",
"@types/lodash": "^4.14.165",
"@types/node": "^10.17.35",
"eslint": "^7.14.0",
"lodash": "^4.17.20",
"typescript": "~4.1.2"
},
"keywords": [
"loopback",
"LoopBack",
Expand Down
12 changes: 6 additions & 6 deletions examples/binding-resolution/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,6 @@
"publishConfig": {
"access": "public"
},
"files": [
"README.md",
"dist",
"src",
"!*/__tests__"
],
"dependencies": {
"@loopback/boot": "^3.1.1",
"@loopback/core": "^2.13.0",
Expand All @@ -59,5 +53,11 @@
"source-map-support": "^0.5.19",
"typescript": "~4.1.2"
},
"files": [
"README.md",
"dist",
"src",
"!*/__tests__"
],
"copyright.owner": "IBM Corp."
}
24 changes: 12 additions & 12 deletions examples/context/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,18 @@
"publishConfig": {
"access": "public"
},
"dependencies": {
"@loopback/context": "^3.13.1",
"tslib": "^2.0.3"
},
"devDependencies": {
"@loopback/build": "^6.2.7",
"@loopback/eslint-config": "^10.0.3",
"@loopback/testlab": "^3.2.9",
"@types/node": "^10.17.35",
"eslint": "^7.14.0",
"typescript": "~4.1.2"
},
"scripts": {
"acceptance": "lb-mocha \"dist/__tests__/acceptance/**/*.js\"",
"build": "lb-tsc",
Expand All @@ -39,18 +51,6 @@
"url": "https://github.com/strongloop/loopback-next.git",
"directory": "examples/context"
},
"dependencies": {
"@loopback/context": "^3.13.1",
"tslib": "^2.0.3"
},
"devDependencies": {
"@loopback/build": "^6.2.7",
"@loopback/eslint-config": "^10.0.3",
"@loopback/testlab": "^3.2.9",
"@types/node": "^10.17.35",
"eslint": "^7.14.0",
"typescript": "~4.1.2"
},
"keywords": [
"loopback",
"LoopBack",
Expand Down
36 changes: 18 additions & 18 deletions examples/file-transfer/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,24 @@
"publishConfig": {
"access": "public"
},
"dependencies": {
"@loopback/boot": "^3.1.1",
"@loopback/core": "^2.13.0",
"@loopback/rest": "^9.1.0",
"@loopback/rest-explorer": "^3.0.4",
"multer": "^1.4.2",
"tslib": "^2.0.3"
},
"devDependencies": {
"@loopback/build": "^6.2.7",
"@loopback/eslint-config": "^10.0.3",
"@loopback/testlab": "^3.2.9",
"@types/express-serve-static-core": "^4.17.13",
"@types/multer": "^1.4.4",
"@types/node": "^10.17.35",
"eslint": "^7.14.0",
"typescript": "~4.1.2"
},
"scripts": {
"acceptance": "lb-mocha \"dist/__tests__/acceptance/**/*.js\"",
"build": "lb-tsc",
Expand All @@ -38,24 +56,6 @@
"url": "https://github.com/strongloop/loopback-next.git",
"directory": "examples/file-transfer"
},
"dependencies": {
"@loopback/boot": "^3.1.1",
"@loopback/core": "^2.13.0",
"@loopback/rest": "^9.1.0",
"@loopback/rest-explorer": "^3.0.4",
"multer": "^1.4.2",
"tslib": "^2.0.3"
},
"devDependencies": {
"@loopback/build": "^6.2.7",
"@loopback/eslint-config": "^10.0.3",
"@loopback/testlab": "^3.2.9",
"@types/express-serve-static-core": "^4.17.13",
"@types/multer": "^1.4.4",
"@types/node": "^10.17.35",
"eslint": "^7.14.0",
"typescript": "~4.1.2"
},
"keywords": [
"loopback",
"LoopBack",
Expand Down
Loading