Skip to content

Commit

Permalink
fix: pattern **/*.js still copies all files
Browse files Browse the repository at this point in the history
Closes #701
  • Loading branch information
develar committed Aug 27, 2016
1 parent a6b7573 commit 51309bf
Show file tree
Hide file tree
Showing 6 changed files with 175 additions and 161 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@
"electron-download": "^2.1.2",
"electron-osx-sign": "^0.4.0-beta4",
"extract-zip": "^1.5.0",
"fs-extra-p": "^1.0.6",
"fs-extra-p": "^1.1.7",
"hosted-git-info": "^2.1.5",
"image-size": "^0.5.0",
"isbinaryfile": "^3.0.1",
Expand Down
41 changes: 20 additions & 21 deletions src/asarUtil.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import * as path from "path"
import { log } from "./util/log"
import { Minimatch } from "minimatch"
import { deepAssign } from "./util/deepAssign"
import { Filter } from "./util/filter"

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

export function walk(dirPath: string, consumer?: (file: string, stat: Stats) => void, filter?: (file: string) => boolean, addRootToResult?: boolean): BluebirdPromise<Array<string>> {
export function walk(dirPath: string, consumer?: (file: string, stat: Stats) => void, filter?: Filter, addRootToResult?: boolean): BluebirdPromise<Array<string>> {
return readdir(dirPath)
.then(names => {
return BluebirdPromise.map(names, name => {
const filePath = dirPath + path.sep + name
if (filter != null && !filter(filePath)) {
return <any>null
}
.then(names => BluebirdPromise.map(names, name => {
const filePath = dirPath + path.sep + name
return lstat(filePath)
.then((stat): any => {
if (filter != null && !filter(filePath, stat)) {
return null
}

return lstat(filePath)
.then((stat): any => {
if (consumer != null) {
consumer(filePath, stat)
}
if (stat.isDirectory()) {
return walk(filePath, consumer, filter, true)
}
return filePath
})
}, concurrency)
})
if (consumer != null) {
consumer(filePath, stat)
}
if (stat.isDirectory()) {
return walk(filePath, consumer, filter, true)
}
return filePath
})
}, concurrency))
.then(list => {
list.sort((a, b) => {
// files before directories
Expand Down Expand Up @@ -75,7 +74,7 @@ export function walk(dirPath: string, consumer?: (file: string, stat: Stats) =>
})
}

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

async pack(filter: (file: string) => boolean) {
async pack(filter: Filter) {
const metadata = new Map<string, Stats>()
const files = await walk(this.src, (it, stat) => {
metadata.set(it, stat)
Expand Down
103 changes: 103 additions & 0 deletions src/fileMatcher.ts
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)
}
}
Loading

0 comments on commit 51309bf

Please sign in to comment.