forked from mozilla/web-ext
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfile-filter.js
87 lines (74 loc) · 1.87 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
/* @flow */
import path from 'path';
import minimatch from 'minimatch';
import {createLogger} from './logger';
const log = createLogger(__filename);
// FileFilter types and implementation.
export type FileFilterOptions = {|
filesToIgnore?: Array<string>,
ignoreFiles?: Array<string>,
sourceDir?: string,
artifactsDir?: string,
|};
/*
* Allows or ignores files when creating a ZIP archive.
*/
export class FileFilter {
filesToIgnore: Array<string>;
sourceDir: string;
constructor({
filesToIgnore = [
'**/*.xpi',
'**/*.zip',
'**/.*', // any hidden file
'**/node_modules',
],
ignoreFiles = [],
sourceDir = '',
artifactsDir,
}: FileFilterOptions = {}) {
this.filesToIgnore = filesToIgnore;
this.sourceDir = sourceDir;
if (ignoreFiles) {
this.filesToIgnore.push(...ignoreFiles);
}
if (artifactsDir) {
this.filesToIgnore.push(artifactsDir);
}
this.filesToIgnore = this.filesToIgnore.map(
(file) => this.resolve(file)
);
}
/**
* Resolve relative path to absolute path if sourceDir is setted.
*/
resolve(file: string): string {
if (this.sourceDir) {
return path.resolve(this.sourceDir, file);
}
return file;
}
/**
* Insert more files into filesToIgnore array.
*/
addToIgnoreList(files: Array<string>) {
files = files.map((file) => this.resolve(file));
this.filesToIgnore.push(...files);
}
/*
* Returns true if the file is wanted for the ZIP archive.
*
* This is called by zipdir as wantFile(path, stat) for each
* file in the folder that is being archived.
*/
wantFile(path: string): boolean {
path = this.resolve(path);
for (const test of this.filesToIgnore) {
if (minimatch(path, test)) {
log.debug(`FileFilter: ignoring file ${path}`);
return false;
}
}
return true;
}
}