Skip to content

Commit

Permalink
fix(build): fix bundling of overridden ApiDOM deps (#2853)
Browse files Browse the repository at this point in the history
Without this fix `npm ci` script in newer npm versions
has problem consolidating the dependency tree.
  • Loading branch information
char0n authored Feb 22, 2023
1 parent 9aa7046 commit 604cf87
Show file tree
Hide file tree
Showing 3 changed files with 126 additions and 73 deletions.
53 changes: 27 additions & 26 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

76 changes: 57 additions & 19 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@
"Buu Nguyen <[email protected]>",
"Josh Ponelat <[email protected]>",
"Kyle Shockey <[email protected]>",
"Sahar Jafari <[email protected]>"
"Sahar Jafari <[email protected]>",
"Vladimír Gorej <[email protected]>"
],
"files": [
"LICENSE",
Expand Down Expand Up @@ -138,15 +139,21 @@
],
"overrides": {
"@swagger-api/apidom-ast": {
"short-unique-id": "npm:[email protected]",
"@types/ramda": "npm:[email protected]"
"unraw": "npm:[email protected]",
"@types/ramda": {
".": "npm:[email protected]"
}
},
"@swagger-api/apidom-core": {
"short-unique-id": "npm:[email protected]",
"@types/ramda": "npm:[email protected]"
"@types/ramda": {
".": "npm:[email protected]"
}
},
"@swagger-api/apidom-ns-json-schema-draft-4": {
"@types/ramda": "npm:[email protected]"
"@types/ramda": {
".": "npm:[email protected]"
}
},
"@swagger-api/apidom-ns-openapi-3-0": {
"@types/ramda": "npm:[email protected]"
Expand All @@ -155,20 +162,51 @@
"@types/ramda": "npm:[email protected]"
},
"@swagger-api/apidom-reference": {
"@swagger-api/apidom-ns-asyncapi-2": "npm:[email protected]",
"@swagger-api/apidom-ns-api-design-systems": "npm:[email protected]",
"@swagger-api/apidom-parser-adapter-api-design-systems-json": "npm:[email protected]",
"@swagger-api/apidom-parser-adapter-api-design-systems-yaml": "npm:[email protected]",
"@swagger-api/apidom-parser-adapter-asyncapi-json-2": "npm:[email protected]",
"@swagger-api/apidom-parser-adapter-asyncapi-yaml-2": "npm:[email protected]",
"@swagger-api/apidom-parser-adapter-json": "npm:[email protected]",
"@swagger-api/apidom-parser-adapter-openapi-json-3-0": "npm:[email protected]",
"@swagger-api/apidom-parser-adapter-openapi-json-3-1": "npm:[email protected]",
"@swagger-api/apidom-parser-adapter-openapi-yaml-3-0": "npm:[email protected]",
"@swagger-api/apidom-parser-adapter-openapi-yaml-3-1": "npm:[email protected]",
"@swagger-api/apidom-parser-adapter-yaml-1-2": "npm:[email protected]",
"axios": "npm:[email protected]",
"@types/ramda": "npm:[email protected]"
"@swagger-api/apidom-ns-asyncapi-2": {
".": "npm:[email protected]"
},
"@swagger-api/apidom-ns-api-design-systems": {
".": "npm:[email protected]"
},
"@swagger-api/apidom-parser-adapter-api-design-systems-json": {
".": "npm:[email protected]"
},
"@swagger-api/apidom-parser-adapter-api-design-systems-yaml": {
".": "npm:[email protected]"
},
"@swagger-api/apidom-parser-adapter-asyncapi-json-2": {
".": "npm:[email protected]"
},
"@swagger-api/apidom-parser-adapter-asyncapi-yaml-2": {
".": "npm:[email protected]"
},
"@swagger-api/apidom-parser-adapter-json": {
".": "npm:[email protected]"
},
"@swagger-api/apidom-parser-adapter-openapi-json-3-0": {
".": "npm:[email protected]"
},
"@swagger-api/apidom-parser-adapter-openapi-json-3-1": {
".": "npm:[email protected]"
},
"@swagger-api/apidom-parser-adapter-openapi-yaml-3-0": {
".": "npm:[email protected]"
},
"@swagger-api/apidom-parser-adapter-openapi-yaml-3-1": {
".": "npm:[email protected]"
},
"@swagger-api/apidom-parser-adapter-yaml-1-2": {
".": "npm:[email protected]"
},
"axios": {
".": "npm:[email protected]"
},
"@types/ramda": {
".": "npm:[email protected]"
},
"minimatch": {
".": "*"
}
}
}
}
70 changes: 42 additions & 28 deletions scripts/overrides.js
Original file line number Diff line number Diff line change
@@ -1,35 +1,49 @@
/**
* This script simulates `overrides` package.json field
* in older npm versions that doesn't support it.
* This script uses package.json `overrides` field and remove all
* unnecessary dependencies of ApiDOM from npm bundling.
* The mechanism is fully idempotent.
*
* Older versions of npm match the package overrides by name and version,
* instead of just name (this is how new `override` package.json field works).
* Dependencies are only removed when using following override notation:
*
* ```
* "dep": {
* ".": "dep-override"
* }
* ```
*/
/* eslint-disable import/no-dynamic-require */

const fs = require('fs');
const path = require('path');

const rootPckg = require(path.join(__dirname, '..', 'package.json'));
const apidomReferencePckgPath = path.join(
__dirname,
'..',
'node_modules',
'@swagger-api',
'apidom-reference',
'package.json'
const fs = require('node:fs');
const path = require('node:path');

const rootPckgJSON = require(path.join(__dirname, '..', 'package.json')); // eslint-disable-line import/no-dynamic-require
const { overrides: rootOverrides } = rootPckgJSON;
const swaggerApiOverrides = Object.fromEntries(
Object.entries(rootOverrides).filter(([pckgName]) => pckgName.startsWith('@swagger-api'))
);
const apidomReferencePckg = require(apidomReferencePckgPath);

const {
overrides: { '@swagger-api/apidom-reference': overrides },
} = rootPckg;
const overridesList = Object.keys(overrides).filter((key) => key.startsWith('@swagger-api/'));
const readPckg = (pckgName) => {
const pckgPath = path.join(__dirname, '..', 'node_modules', pckgName, 'package.json');

overridesList.forEach((override) => {
if (Object.hasOwn(apidomReferencePckg.dependencies, override)) {
apidomReferencePckg.dependencies[override] = '=0.0.1';
}
});
return JSON.parse(fs.readFileSync(pckgPath, { encoding: 'utf-8' }));
};

const writePckg = (pckgName, pckgJSON) => {
const pckgPath = path.join(__dirname, '..', 'node_modules', pckgName, 'package.json');

return fs.writeFileSync(pckgPath, JSON.stringify(pckgJSON, null, 2));
};

fs.writeFileSync(apidomReferencePckgPath, JSON.stringify(apidomReferencePckg, null, 2));
const removeDeps = (pckgName, overrides) => {
const pckgJSON = readPckg(pckgName);

Object.entries(overrides).forEach(([dep, override]) => {
if (typeof override === 'object') {
delete pckgJSON?.dependencies[dep];
}
});

return writePckg(pckgName, pckgJSON);
};

Object.entries(swaggerApiOverrides).forEach(([pckgName]) => {
removeDeps(pckgName, swaggerApiOverrides[pckgName]);
});

0 comments on commit 604cf87

Please sign in to comment.