Skip to content

Commit f0c3ef7

Browse files
committed
Prevent node_modules diving
1 parent 23aa33d commit f0c3ef7

1 file changed

Lines changed: 7 additions & 8 deletions

File tree

scripts/delete-temp-files.cjs

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -62,13 +62,13 @@ const shouldDeleteYalc = allFlag || yalcFlag;
6262

6363
const EXT_DIR_PATH = path.join(__dirname, '..');
6464
const CORE_DIR_PATH = path.join(EXT_DIR_PATH, '..', 'paranext-core');
65-
const SKIP_DIRS = new Set(['.yalc', 'dev-appdata', 'dev-packages', 'lib']);
65+
const SKIP_DIRS = new Set(['.yalc', 'dev-appdata', 'dev-packages', 'lib', 'node_modules']);
6666

6767
const BUILD_PATHS = [];
6868
if (shouldDeleteBuild) {
6969
// Add core build output subdirectories to `BUILD_PATHS`
70-
BUILD_PATHS.push(...findDirsNamed(CORE_DIR_PATH, 'dist', SKIP_DIRS));
71-
BUILD_PATHS.push(...findDirsNamed(CORE_DIR_PATH, 'temp-build', SKIP_DIRS));
70+
BUILD_PATHS.push(...findNamed(CORE_DIR_PATH, 'dist', SKIP_DIRS));
71+
BUILD_PATHS.push(...findNamed(CORE_DIR_PATH, 'temp-build', SKIP_DIRS));
7272
}
7373

7474
const CORE_PATHS = [path.join(CORE_DIR_PATH, 'dev-appdata')];
@@ -106,7 +106,7 @@ const NPM_PATHS = [
106106
];
107107
if (shouldDeleteNpm) {
108108
// Find core `node_modules` subdirectories and add them to `NPM_PATHS`
109-
NPM_PATHS.push(...findDirsNamed(CORE_DIR_PATH, 'node_modules', SKIP_DIRS));
109+
NPM_PATHS.push(...findNamed(CORE_DIR_PATH, 'node_modules', SKIP_DIRS));
110110
}
111111

112112
const TEST_PATHS = [path.join(EXT_DIR_PATH, 'coverage'), path.join(EXT_DIR_PATH, '.eslintcache')];
@@ -125,19 +125,18 @@ const YALC_PATHS = [
125125
* @param {Set<string>} skipDirs - Directory names to skip entirely
126126
* @returns {string[]} Absolute paths of every matching directory found
127127
*/
128-
function findDirsNamed(corePath, targetDir, skipDirs) {
128+
function findNamed(corePath, target, skipDirs) {
129129
let entries;
130130
try {
131131
entries = fs.readdirSync(corePath, { withFileTypes: true });
132132
} catch {
133133
return [];
134134
}
135135
return entries
136-
.filter((entry) => entry.isDirectory() && !skipDirs.has(entry.name))
136+
.filter((entry) => entry.name === target || (entry.isDirectory() && !skipDirs.has(entry.name)))
137137
.flatMap((entry) => {
138138
const fullPath = path.join(corePath, entry.name);
139-
if (entry.name === targetDir) return [fullPath];
140-
return findDirsNamed(fullPath, targetDir, skipDirs);
139+
return entry.name === target ? [fullPath] : findNamed(fullPath, target, skipDirs);
141140
});
142141
}
143142

0 commit comments

Comments
 (0)