Skip to content

Commit

Permalink
fix: normalize relative path and correctly ignore a directory
Browse files Browse the repository at this point in the history
  • Loading branch information
eight04 committed Jan 17, 2017
1 parent de6ecfd commit 6e4d8ae
Showing 1 changed file with 30 additions and 13 deletions.
43 changes: 30 additions & 13 deletions src/util/file-filter.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,21 @@ import {createLogger} from './logger';

const log = createLogger(__filename);

// Use this function to mimic path.resolve without resolving to absolute path.
const normalizeResolve = (file: string): string => {
// normalize
file = path.normalize(file);

// trim trailing slash
if (!path.parse(file).base) {
return file;
}
if (file.endsWith(path.sep)) {
return file.slice(0, -1);
}
return file;
};

// FileFilter types and implementation.

export type FileFilterOptions = {|
Expand All @@ -27,27 +42,24 @@ export class FileFilter {
filesToIgnore = [
'**/*.xpi',
'**/*.zip',
'**/.*', // any hidden file
'**/node_modules',
'**/.*/', // any hidden file and folder
'**/node_modules/',
],
ignoreFiles = [],
sourceDir = '',
artifactsDir,
}: FileFilterOptions = {}) {

this.filesToIgnore = filesToIgnore;
this.filesToIgnore = [];
this.sourceDir = sourceDir;

this.addToIgnoreList(filesToIgnore);
if (ignoreFiles) {
this.filesToIgnore.push(...ignoreFiles);
this.addToIgnoreList(ignoreFiles);
}
if (artifactsDir) {
this.filesToIgnore.push(artifactsDir);
this.addToIgnoreList([artifactsDir], true);
}

this.filesToIgnore = this.filesToIgnore.map(
(file) => this.resolve(file)
);
}

/**
Expand All @@ -57,15 +69,20 @@ export class FileFilter {
if (this.sourceDir) {
return path.resolve(this.sourceDir, file);
}
return file;
return normalizeResolve(file);
}

/**
* Insert more files into filesToIgnore array.
*/
addToIgnoreList(files: Array<string>) {
files = files.map((file) => this.resolve(file));
this.filesToIgnore.push(...files);
addToIgnoreList(files: Array<string>, isDir: boolean = false) {
for (const file of files) {
this.filesToIgnore.push(this.resolve(file));
// If file is a directory, ignore its content too.
if (/([/\\]\.{0,2})$/.test(file) || isDir) {
this.filesToIgnore.push(this.resolve(path.join(file, '**/*')));
}
}
}

/*
Expand Down

0 comments on commit 6e4d8ae

Please sign in to comment.