-
Notifications
You must be signed in to change notification settings - Fork 761
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(build): fix bundling of overridden ApiDOM deps (#2853)
Without this fix `npm ci` script in newer npm versions has problem consolidating the dependency tree.
- Loading branch information
Showing
3 changed files
with
126 additions
and
73 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]); | ||
}); |