-
Notifications
You must be signed in to change notification settings - Fork 345
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: Added --ignore-files option and build now ignores artifacts folder #753
Changes from all commits
19504b1
cd70980
b48244d
c497465
07cc1a1
e9e583a
dd3188b
619342d
0cb2a43
cae516e
30254c5
f51841b
de6ecfd
6e4d8ae
173333b
fc02fc2
452e382
87f30bc
21e582a
4b8fc84
ea48a6f
7d7a83e
bd331ce
cf64c51
1d86e35
c51c936
e03c90f
6512fe4
f30e931
71e3da4
4113539
000b7f2
72f0808
e95b538
0d35950
6f61733
1ad7c5b
1795c0e
214e09e
09bee46
70db989
6e45154
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
/* @flow */ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good call on moving this to its own file. You also need to move the tests out of test.build.js. The new There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I found some bugs when writing tests for FileFilter.
It might be fine for But it cause problem for There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not sure how should we fix it. Just explicitly ignore all content inside the folder? filesToIgnore = [
'**/*.xpi',
'**/*.zip',
'**/.*', // any hidden file
+ '**/.*/**/*', // any content in the hidden folder
'**/node_modules',
+ '**/node_modules/**/*',
], There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. These issues are both resolved in the patch now. |
||
import path from 'path'; | ||
|
||
import minimatch from 'minimatch'; | ||
|
||
import {createLogger} from './logger'; | ||
|
||
const log = createLogger(__filename); | ||
|
||
// Use this function to mimic path.resolve without resolving to absolute path. | ||
export const normalizeResolve = (file: string): string => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't fully understand why we need this function. Is it just for convenience so that someone can use an ignore path of There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. https://github.com/eight04/web-ext/blob/71e3da42208cbca3fc98296c2e16c2a18f82d6fb/src/util/file-filter.js#L65
This make There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ok, that makes sense. Consistency is good. |
||
// normalize | ||
file = path.normalize(file); | ||
|
||
// trim trailing slash | ||
if (path.parse(file).base && file.endsWith(path.sep)) { | ||
return file.slice(0, -1); | ||
} | ||
return file; | ||
}; | ||
|
||
// 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 = {}) { | ||
|
||
this.filesToIgnore = []; | ||
this.sourceDir = sourceDir; | ||
|
||
this.addToIgnoreList(baseIgnoredPatterns); | ||
if (ignoreFiles) { | ||
this.addToIgnoreList(ignoreFiles); | ||
} | ||
if (artifactsDir) { | ||
this.addToIgnoreList([ | ||
artifactsDir, | ||
path.join(artifactsDir, '**', '*'), | ||
]); | ||
} | ||
} | ||
|
||
/** | ||
* Resolve relative path to absolute path if sourceDir is setted. | ||
*/ | ||
resolve(file: string): string { | ||
if (this.sourceDir) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I am a little bit worried about pre-pending log.debug(`Adding sourceDir ${this.sourceDir} to the beginning of file ${file}`); There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Prepending only happens if There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks, the debug statement now shows if the path is relative or not. |
||
const resolvedPath = path.resolve(this.sourceDir, file); | ||
log.debug( | ||
`Resolved path ${file} with sourceDir ${this.sourceDir || ''} ` + | ||
`to ${resolvedPath}` | ||
); | ||
return resolvedPath; | ||
} | ||
return normalizeResolve(file); | ||
} | ||
|
||
/** | ||
* Insert more files into filesToIgnore array. | ||
*/ | ||
addToIgnoreList(files: Array<string>) { | ||
for (const file of files) { | ||
this.filesToIgnore.push(this.resolve(file)); | ||
} | ||
} | ||
|
||
/* | ||
* Returns true if the file is wanted. | ||
* | ||
* If path does not start with a slash, it will be treated as a path | ||
* relative to sourceDir when matching it against all configured | ||
* ignore-patterns. | ||
* | ||
* 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); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I've been thinking more about this and I don't think it's right to always assume There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not sure what is the problem described here. We normalize the path here is to make sure it can match normalized patterns, so There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ah, whoops, |
||
for (const test of this.filesToIgnore) { | ||
if (minimatch(path, test)) { | ||
log.debug(`FileFilter: ignoring file ${path} (it matched ${test})`); | ||
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; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this options should require an argument (by adding the
requiresArg: true
to the yargs properties for the option).There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It seems that setting
requiresArg
would force yargs to validateignoreFiles
is not a empty list. Since theignoreFiles
defaults to an empty list, this make yargs always argueMissing argument value: ignore-files
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This comment is supposed to be part of the last review comments but it has not been submitted by github, and so I added it here as a separate comment.
@eight04 yargs can be very annoying sometimes :-)
Anyway, it looks like that we can use a combination of yargs option config properties to make it work as expected:
Basically it seems that we have to:
demand: false
config to allow the --ignore-files option to be undefined (which is going to mean "that the option has not been specified")requiresArg: true
which force "--ignore-files" to have a value when specifiedI briefly tried the above config locally and it seems to work as expected,
let me know if it doesn't for you.