-
Notifications
You must be signed in to change notification settings - Fork 344
/
Copy pathfile-filter.js
127 lines (111 loc) · 3.19 KB
/
file-filter.js
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
120
121
122
123
124
125
126
127
/* @flow */
import path from 'path';
import multimatch from 'multimatch';
import {createLogger} from './logger';
const log = createLogger(__filename);
// check if target is a sub directory of src
export const isSubPath = (src: string, target: string): boolean => {
const relate = path.relative(src, target);
// same dir
if (!relate) {
return false;
}
if (relate === '..') {
return false;
}
return !relate.startsWith(`..${path.sep}`);
};
// FileFilter types and implementation.
export type FileFilterOptions = {|
baseIgnoredPatterns?: Array<string>,
ignoreFiles?: Array<string>,
sourceDir: string,
artifactsDir?: string,
|};
/*
* Allows or ignores files.
*/
export class FileFilter {
filesToIgnore: Array<string>;
sourceDir: string;
constructor({
baseIgnoredPatterns = [
'**/*.xpi',
'**/*.zip',
'**/.*', // any hidden file and folder
'**/.*/**/*', // and the content inside hidden folder
'**/node_modules',
'**/node_modules/**/*',
],
ignoreFiles = [],
sourceDir,
artifactsDir,
}: FileFilterOptions = {}) {
sourceDir = path.resolve(sourceDir);
this.filesToIgnore = [];
this.sourceDir = sourceDir;
this.addToIgnoreList(baseIgnoredPatterns);
if (ignoreFiles) {
this.addToIgnoreList(ignoreFiles);
}
if (artifactsDir && isSubPath(sourceDir, artifactsDir)) {
artifactsDir = path.resolve(artifactsDir);
log.debug(
`Ignoring artifacts directory "${artifactsDir}" ` +
'and all its subdirectories'
);
this.addToIgnoreList([
artifactsDir,
path.join(artifactsDir, '**', '*'),
]);
}
}
/**
* Resolve relative path to absolute path with sourceDir.
*/
resolveWithSourceDir(file: string): string {
const resolvedPath = path.resolve(this.sourceDir, file);
log.debug(
`Resolved path ${file} with sourceDir ${this.sourceDir} ` +
`to ${resolvedPath}`
);
return resolvedPath;
}
/**
* Insert more files into filesToIgnore array.
*/
addToIgnoreList(files: Array<string>) {
for (const file of files) {
if (file.charAt(0) === '!') {
const resolvedFile = this.resolveWithSourceDir(file.substr(1));
this.filesToIgnore.push(`!${resolvedFile}`);
} else {
this.filesToIgnore.push(this.resolveWithSourceDir(file));
}
}
}
/*
* Returns true if the file is wanted.
*
* If filePath does not start with a slash, it will be treated as a path
* relative to sourceDir when matching it against all configured
* ignore-patterns.
*
* Example: this is called by zipdir as wantFile(filePath) for each
* file in the folder that is being archived.
*/
wantFile(filePath: string): boolean {
const resolvedPath = this.resolveWithSourceDir(filePath);
const matches = multimatch(resolvedPath, this.filesToIgnore);
if (matches.length > 0) {
log.debug(`FileFilter: ignoring file ${resolvedPath}`);
return false;
}
return true;
}
}
// a helper function to make mocking easier
export const createFileFilter = (
(params: FileFilterOptions): FileFilter => new FileFilter(params)
);
export type FileFilterCreatorFn = typeof createFileFilter;