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

[Backport 2.x] Remove examples and other unwanted artifacts from installed dependencies #4905

Merged
merged 1 commit into from
Sep 5, 2023
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
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
"author": "opensearch-project",
"scripts": {
"preinstall": "scripts/use_node ./preinstall_check",
"postinstall": "scripts/use_node scripts/postinstall",
"osd": "scripts/use_node scripts/osd",
"opensearch": "scripts/use_node scripts/opensearch",
"test": "grunt test",
Expand Down
41 changes: 41 additions & 0 deletions scripts/postinstall.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
* Copyright OpenSearch Contributors
* SPDX-License-Identifier: Apache-2.0
*/

/* eslint no-restricted-syntax: 0 */

const fs = require('fs/promises');

/**
* Some libraries pack their demos and examples into their release artifacts.
* This unwanted content makes our release artifacts larger but more importantly,
* some contain in-browser references to outdated and vulnerable versions of
* libraries that are not even mentioned in the dependency tree. This is a
* problem when vulnerability scanners point them out, and we have no way to fix
* them. This function looks for folders that are unwanted and deletes them.
*/
const removeUnwantedFolders = async (root, unwantedNames) => {
const items = await fs.readdir(root, { withFileTypes: true });
const promises = [];
for (const item of items) {
if (!item.isDirectory()) continue;

if (unwantedNames.includes(item.name)) {
promises.push(fs.rm(`${root}/${item.name}`, { recursive: true, force: true }));
} else {
promises.push(...(await removeUnwantedFolders(`${root}/${item.name}`, unwantedNames)));
}
}

return promises;
};
const run = async () => {
const promises = await removeUnwantedFolders('node_modules', ['demo', 'example', 'examples']);
await Promise.all(promises);
};

run().catch((err) => {
console.error(err);
process.exit(1);
});
Loading