Skip to content

Commit 51309bf

Browse files
committed
fix: pattern **/*.js still copies all files
Closes #701
1 parent a6b7573 commit 51309bf

File tree

6 files changed

+175
-161
lines changed

6 files changed

+175
-161
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@
7373
"electron-download": "^2.1.2",
7474
"electron-osx-sign": "^0.4.0-beta4",
7575
"extract-zip": "^1.5.0",
76-
"fs-extra-p": "^1.0.6",
76+
"fs-extra-p": "^1.1.7",
7777
"hosted-git-info": "^2.1.5",
7878
"image-size": "^0.5.0",
7979
"isbinaryfile": "^3.0.1",

src/asarUtil.ts

Lines changed: 20 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import * as path from "path"
99
import { log } from "./util/log"
1010
import { Minimatch } from "minimatch"
1111
import { deepAssign } from "./util/deepAssign"
12+
import { Filter } from "./util/filter"
1213

1314
const isBinaryFile: any = BluebirdPromise.promisify(require("isbinaryfile"))
1415
const pickle = require ("chromium-pickle-js")
@@ -22,27 +23,25 @@ const MAX_FILE_REQUESTS = 32
2223
const concurrency = {concurrency: MAX_FILE_REQUESTS}
2324
const NODE_MODULES_PATTERN = path.sep + "node_modules" + path.sep
2425

25-
export function walk(dirPath: string, consumer?: (file: string, stat: Stats) => void, filter?: (file: string) => boolean, addRootToResult?: boolean): BluebirdPromise<Array<string>> {
26+
export function walk(dirPath: string, consumer?: (file: string, stat: Stats) => void, filter?: Filter, addRootToResult?: boolean): BluebirdPromise<Array<string>> {
2627
return readdir(dirPath)
27-
.then(names => {
28-
return BluebirdPromise.map(names, name => {
29-
const filePath = dirPath + path.sep + name
30-
if (filter != null && !filter(filePath)) {
31-
return <any>null
32-
}
28+
.then(names => BluebirdPromise.map(names, name => {
29+
const filePath = dirPath + path.sep + name
30+
return lstat(filePath)
31+
.then((stat): any => {
32+
if (filter != null && !filter(filePath, stat)) {
33+
return null
34+
}
3335

34-
return lstat(filePath)
35-
.then((stat): any => {
36-
if (consumer != null) {
37-
consumer(filePath, stat)
38-
}
39-
if (stat.isDirectory()) {
40-
return walk(filePath, consumer, filter, true)
41-
}
42-
return filePath
43-
})
44-
}, concurrency)
45-
})
36+
if (consumer != null) {
37+
consumer(filePath, stat)
38+
}
39+
if (stat.isDirectory()) {
40+
return walk(filePath, consumer, filter, true)
41+
}
42+
return filePath
43+
})
44+
}, concurrency))
4645
.then(list => {
4746
list.sort((a, b) => {
4847
// files before directories
@@ -75,7 +74,7 @@ export function walk(dirPath: string, consumer?: (file: string, stat: Stats) =>
7574
})
7675
}
7776

78-
export async function createAsarArchive(src: string, resourcesPath: string, options: AsarOptions, filter: (file: string) => boolean): Promise<any> {
77+
export async function createAsarArchive(src: string, resourcesPath: string, options: AsarOptions, filter: Filter): Promise<any> {
7978
// sort files to minimize file change (i.e. asar file is not changed dramatically on small change)
8079
await new AsarPackager(src, resourcesPath, options).pack(filter)
8180
}
@@ -96,7 +95,7 @@ class AsarPackager {
9695
this.outFile = path.join(this.resourcesPath, "app.asar")
9796
}
9897

99-
async pack(filter: (file: string) => boolean) {
98+
async pack(filter: Filter) {
10099
const metadata = new Map<string, Stats>()
101100
const files = await walk(this.src, (it, stat) => {
102101
metadata.set(it, stat)

src/fileMatcher.ts

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
import * as path from "path"
2+
import { createFilter, hasMagic, Filter } from "./util/filter"
3+
import { Minimatch } from "minimatch"
4+
import { asArray } from "./util/util"
5+
6+
export interface FilePattern {
7+
from?: string
8+
to?: string
9+
filter?: Array<string> | string
10+
}
11+
12+
export interface FileMatchOptions {
13+
arch: string,
14+
os: string
15+
}
16+
17+
export class FileMatcher {
18+
readonly from: string
19+
readonly to: string
20+
21+
readonly patterns: Array<string>
22+
23+
constructor(from: string, to: string, private options: FileMatchOptions, patterns?: Array<string> | string | n) {
24+
this.from = this.expandPattern(from)
25+
this.to = this.expandPattern(to)
26+
this.patterns = asArray(patterns)
27+
}
28+
29+
addPattern(pattern: string) {
30+
this.patterns.push(pattern)
31+
}
32+
33+
isEmpty() {
34+
return this.patterns.length === 0
35+
}
36+
37+
getParsedPatterns(fromDir?: string): Array<Minimatch> {
38+
const minimatchOptions = {}
39+
40+
const parsedPatterns: Array<Minimatch> = []
41+
const pathDifference = fromDir ? path.relative(fromDir, this.from) : null
42+
43+
for (let i = 0; i < this.patterns.length; i++) {
44+
let expandedPattern = this.expandPattern(this.patterns[i])
45+
if (pathDifference) {
46+
expandedPattern = path.join(pathDifference, expandedPattern)
47+
}
48+
49+
const parsedPattern = new Minimatch(expandedPattern, minimatchOptions)
50+
parsedPatterns.push(parsedPattern)
51+
52+
if (!hasMagic(parsedPattern)) {
53+
// https://github.com/electron-userland/electron-builder/issues/545
54+
// add **/*
55+
parsedPatterns.push(new Minimatch(`${expandedPattern}/**/*`, minimatchOptions))
56+
}
57+
}
58+
59+
return parsedPatterns
60+
}
61+
62+
createFilter(ignoreFiles?: Set<string>, rawFilter?: (file: string) => boolean, excludePatterns?: Array<Minimatch> | n): Filter {
63+
return createFilter(this.from, this.getParsedPatterns(), ignoreFiles, rawFilter, excludePatterns)
64+
}
65+
66+
private expandPattern(pattern: string): string {
67+
return pattern
68+
.replace(/\$\{arch}/g, this.options.arch)
69+
.replace(/\$\{os}/g, this.options.os)
70+
.replace(/\$\{\/\*}/g, "{,/**/*,/**/.*}")
71+
}
72+
}
73+
74+
export function deprecatedUserIgnoreFilter(ignore: any, appDir: string) {
75+
let ignoreFunc: any
76+
if (typeof (ignore) === "function") {
77+
ignoreFunc = function (file: string) { return !ignore(file) }
78+
}
79+
else {
80+
if (!Array.isArray(ignore)) {
81+
ignore = [ignore]
82+
}
83+
84+
ignoreFunc = function (file: string) {
85+
for (let i = 0; i < ignore.length; i++) {
86+
if (file.match(ignore[i])) {
87+
return false
88+
}
89+
}
90+
91+
return true
92+
}
93+
}
94+
95+
return function filter(file: string) {
96+
let name = file.split(path.resolve(appDir))[1]
97+
if (path.sep === "\\") {
98+
// convert slashes so unix-format ignores work
99+
name = name.replace(/\\/g, "/")
100+
}
101+
return ignoreFunc(name)
102+
}
103+
}

0 commit comments

Comments
 (0)