-
-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathcopy-filter.ts
119 lines (101 loc) · 3.04 KB
/
copy-filter.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
import {
baseTempDir,
debug,
ensureArray,
generateFinalBasename,
normalizePath,
} from './common';
import junk from 'junk';
import path from 'path';
import { isModule, Pruner } from './prune';
import { officialPlatformArchCombos } from './targets';
import { ComboOptions, Options } from './types';
import { CopyFilterAsync } from 'fs-extra';
const DEFAULT_IGNORES = [
'/package-lock\\.json$',
'/yarn\\.lock$',
'/\\.git($|/)',
'/node_modules/\\.bin($|/)',
'\\.o(bj)?$',
'/node_gyp_bins($|/)',
];
export function populateIgnoredPaths(opts: Options) {
(opts as Options & { originalIgnore: Options['ignore'] }).originalIgnore =
opts.ignore;
if (typeof opts.ignore !== 'function') {
if (opts.ignore) {
opts.ignore = [...ensureArray(opts.ignore), ...DEFAULT_IGNORES];
} else {
opts.ignore = [...DEFAULT_IGNORES];
}
if (process.platform === 'linux') {
opts.ignore.push(baseTempDir(opts));
}
debug('Ignored path regular expressions:', opts.ignore);
}
}
export function generateIgnoredOutDirs(opts: ComboOptions): string[] {
const normalizedOut = opts.out ? path.resolve(opts.out) : null;
const ignoredOutDirs: string[] = [];
if (normalizedOut === null || normalizedOut === process.cwd()) {
for (const [platform, archs] of Object.entries(
officialPlatformArchCombos,
)) {
for (const arch of archs) {
const basenameOpts = {
arch: arch,
name: opts.name,
platform: platform,
};
ignoredOutDirs.push(
path.join(process.cwd(), generateFinalBasename(basenameOpts)),
);
}
}
} else {
ignoredOutDirs.push(normalizedOut);
}
debug('Ignored paths based on the out param:', ignoredOutDirs);
return ignoredOutDirs;
}
function generateFilterFunction(
ignore: Exclude<ComboOptions['ignore'], undefined>,
): (file: string) => boolean {
if (typeof ignore === 'function') {
return (file) => !ignore(file);
} else {
const ignoredRegexes = ensureArray(ignore);
return function filterByRegexes(file) {
return !ignoredRegexes.some((regex) => file.match(regex));
};
}
}
export function userPathFilter(opts: ComboOptions): CopyFilterAsync {
const filterFunc = generateFilterFunction(opts.ignore || []);
const ignoredOutDirs = generateIgnoredOutDirs(opts);
const pruner = opts.prune ? new Pruner(opts.dir, Boolean(opts.quiet)) : null;
return async function filter(file) {
const fullPath = path.resolve(file);
if (ignoredOutDirs.includes(fullPath)) {
return false;
}
if (opts.junk !== false) {
// defaults to true
if (junk.is(path.basename(fullPath))) {
return false;
}
}
let name = fullPath.split(path.resolve(opts.dir))[1];
if (path.sep === '\\') {
name = normalizePath(name);
}
if (pruner && name.startsWith('/node_modules/')) {
if (await isModule(file)) {
return pruner.pruneModule(name);
} else {
return filterFunc(name);
}
}
return filterFunc(name);
} as CopyFilterAsync;
}