Skip to content

Commit d8b5fe0

Browse files
authored
Ensure node_modules is excluded in createTgz (#1289)
1 parent 0bb03b4 commit d8b5fe0

2 files changed

Lines changed: 14 additions & 6 deletions

File tree

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@definitelytyped/utils": patch
3+
---
4+
5+
Exclude node_modules from tarballs created by createTgz.

packages/utils/src/io.ts

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -272,22 +272,25 @@ function createTar(dir: string, onError: (error: any) => void): NodeJS.ReadableS
272272
const dirSegments = resolve(dir).split(sep);
273273
const parentDir = dirSegments.slice(0, dirSegments.length - 1).join(sep);
274274
const entryToAdd = dirSegments[dirSegments.length - 1];
275-
const packer = new Pack({ cwd: parentDir, filter: addDirectoryExecutablePermission });
275+
const packer = new Pack({ cwd: parentDir, filter: tarFilter });
276276
packer.on("error", onError);
277277
const stream = packer.add(entryToAdd);
278278
packer.end();
279279
// Shady, but minipass is compatible enough with ReadableStream for this use.
280280
return stream as unknown as NodeJS.ReadableStream;
281281
}
282282

283-
/**
284-
* Work around a bug where directories bundled on Windows do not have executable permission when extracted on Linux.
285-
* https://github.com/npm/node-tar/issues/7#issuecomment-17572926
286-
*/
287-
function addDirectoryExecutablePermission(_: string, stat: ReadEntry | fs.Stats): boolean {
283+
const nodeModulesPattern = /(?:^|[\\/])node_modules(?:[\\/]|$)/;
284+
285+
function tarFilter(path: string, stat: ReadEntry | fs.Stats): boolean {
288286
if (stat instanceof ReadEntry) {
289287
return true; // never happens?
290288
}
289+
if (nodeModulesPattern.test(path)) {
290+
return false;
291+
}
292+
// Work around a bug where directories bundled on Windows do not have executable permission when extracted on Linux.
293+
// https://github.com/npm/node-tar/issues/7#issuecomment-17572926
291294
if (stat.isDirectory()) {
292295
stat.mode = addExecutePermissionsFromReadPermissions(stat.mode);
293296
}

0 commit comments

Comments
 (0)