-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: pattern **/*.js still copies all files
Closes #701
- Loading branch information
Showing
6 changed files
with
175 additions
and
161 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
import * as path from "path" | ||
import { createFilter, hasMagic, Filter } from "./util/filter" | ||
import { Minimatch } from "minimatch" | ||
import { asArray } from "./util/util" | ||
|
||
export interface FilePattern { | ||
from?: string | ||
to?: string | ||
filter?: Array<string> | string | ||
} | ||
|
||
export interface FileMatchOptions { | ||
arch: string, | ||
os: string | ||
} | ||
|
||
export class FileMatcher { | ||
readonly from: string | ||
readonly to: string | ||
|
||
readonly patterns: Array<string> | ||
|
||
constructor(from: string, to: string, private options: FileMatchOptions, patterns?: Array<string> | string | n) { | ||
this.from = this.expandPattern(from) | ||
this.to = this.expandPattern(to) | ||
this.patterns = asArray(patterns) | ||
} | ||
|
||
addPattern(pattern: string) { | ||
this.patterns.push(pattern) | ||
} | ||
|
||
isEmpty() { | ||
return this.patterns.length === 0 | ||
} | ||
|
||
getParsedPatterns(fromDir?: string): Array<Minimatch> { | ||
const minimatchOptions = {} | ||
|
||
const parsedPatterns: Array<Minimatch> = [] | ||
const pathDifference = fromDir ? path.relative(fromDir, this.from) : null | ||
|
||
for (let i = 0; i < this.patterns.length; i++) { | ||
let expandedPattern = this.expandPattern(this.patterns[i]) | ||
if (pathDifference) { | ||
expandedPattern = path.join(pathDifference, expandedPattern) | ||
} | ||
|
||
const parsedPattern = new Minimatch(expandedPattern, minimatchOptions) | ||
parsedPatterns.push(parsedPattern) | ||
|
||
if (!hasMagic(parsedPattern)) { | ||
// https://github.com/electron-userland/electron-builder/issues/545 | ||
// add **/* | ||
parsedPatterns.push(new Minimatch(`${expandedPattern}/**/*`, minimatchOptions)) | ||
} | ||
} | ||
|
||
return parsedPatterns | ||
} | ||
|
||
createFilter(ignoreFiles?: Set<string>, rawFilter?: (file: string) => boolean, excludePatterns?: Array<Minimatch> | n): Filter { | ||
return createFilter(this.from, this.getParsedPatterns(), ignoreFiles, rawFilter, excludePatterns) | ||
} | ||
|
||
private expandPattern(pattern: string): string { | ||
return pattern | ||
.replace(/\$\{arch}/g, this.options.arch) | ||
.replace(/\$\{os}/g, this.options.os) | ||
.replace(/\$\{\/\*}/g, "{,/**/*,/**/.*}") | ||
} | ||
} | ||
|
||
export function deprecatedUserIgnoreFilter(ignore: any, appDir: string) { | ||
let ignoreFunc: any | ||
if (typeof (ignore) === "function") { | ||
ignoreFunc = function (file: string) { return !ignore(file) } | ||
} | ||
else { | ||
if (!Array.isArray(ignore)) { | ||
ignore = [ignore] | ||
} | ||
|
||
ignoreFunc = function (file: string) { | ||
for (let i = 0; i < ignore.length; i++) { | ||
if (file.match(ignore[i])) { | ||
return false | ||
} | ||
} | ||
|
||
return true | ||
} | ||
} | ||
|
||
return function filter(file: string) { | ||
let name = file.split(path.resolve(appDir))[1] | ||
if (path.sep === "\\") { | ||
// convert slashes so unix-format ignores work | ||
name = name.replace(/\\/g, "/") | ||
} | ||
return ignoreFunc(name) | ||
} | ||
} |
Oops, something went wrong.