Skip to content

Commit 5dc6dce

Browse files
author
cod1k
committed
Filter files in copyToTemp using .gitignore rules
Updated the `copyToTemp` function to respect ignore patterns defined in `.gitignore` files and exclude unwanted files like `node_modules`, `dist`, and `build`. This ensures that only relevant files are copied to the temporary directory, improving efficiency and alignment with project standards.
1 parent 51277ce commit 5dc6dce

File tree

1 file changed

+24
-3
lines changed

1 file changed

+24
-3
lines changed

dev-packages/e2e-tests/lib/copyToTemp.ts

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,32 @@
11
/* eslint-disable no-console */
2-
import { readFileSync, writeFileSync } from 'fs';
2+
import { existsSync, readFileSync, writeFileSync } from 'fs';
33
import { cp } from 'fs/promises';
4-
import { join } from 'path';
4+
import ignore from 'ignore';
5+
import { dirname, join, relative } from 'path';
56

67
export async function copyToTemp(originalPath: string, tmpDirPath: string): Promise<void> {
78
// copy files to tmp dir
8-
await cp(originalPath, tmpDirPath, { recursive: true });
9+
const ig = ignore();
10+
const ignoreFileDirs = [
11+
originalPath,
12+
dirname(__dirname)
13+
]
14+
ig.add(['.gitignore', 'node_modules', 'dist', 'build']);
15+
for(const dir of ignoreFileDirs) {
16+
const ignore_file = join(dir, '.gitignore');
17+
if (existsSync(ignore_file)) {
18+
ig.add(readFileSync(ignore_file, 'utf8'));
19+
}
20+
}
21+
22+
await cp(originalPath, tmpDirPath, {
23+
recursive: true,
24+
filter: src => {
25+
const relPath = relative(originalPath, src);
26+
if (!relPath) return true;
27+
return !ig.ignores(relPath);
28+
},
29+
});
930

1031
fixPackageJson(tmpDirPath);
1132
}

0 commit comments

Comments
 (0)