|
1 | 1 | /* eslint-disable no-console */
|
2 |
| -import { readFileSync, writeFileSync } from 'fs'; |
| 2 | +import { existsSync, readFileSync, writeFileSync } from 'fs'; |
3 | 3 | import { cp } from 'fs/promises';
|
4 |
| -import { join } from 'path'; |
| 4 | +import ignore from 'ignore'; |
| 5 | +import { dirname, join, relative } from 'path'; |
5 | 6 |
|
6 | 7 | export async function copyToTemp(originalPath: string, tmpDirPath: string): Promise<void> {
|
7 | 8 | // 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 | + }); |
9 | 30 |
|
10 | 31 | fixPackageJson(tmpDirPath);
|
11 | 32 | }
|
|
0 commit comments