Skip to content

Commit

Permalink
fix: Unable to exclude files from app.asar
Browse files Browse the repository at this point in the history
Close #3446
  • Loading branch information
develar committed Nov 5, 2018
1 parent fdb7332 commit 5226c21
Show file tree
Hide file tree
Showing 17 changed files with 242 additions and 79 deletions.
8 changes: 4 additions & 4 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@ version: 2
jobs:
build:
docker:
- image: circleci/node:10.12.0
- image: circleci/node:10.13.0
steps:
- checkout
- restore_cache:
keys:
- deps-{{ checksum "yarn.lock" }}
- restore_cache:
keys:
- v-3.0.6-electron
- v-3.0.7-electron
- run:
command: yarn --frozen-lockfile
- run:
Expand All @@ -23,7 +23,7 @@ jobs:
- run:
command: node ./test/out/helpers/downloadElectron.js
- save_cache:
key: v-3.0.6-electron
key: v-3.0.7-electron
paths:
- ~/.cache/electron

Expand All @@ -41,7 +41,7 @@ jobs:
- deps-{{ checksum "yarn.lock" }}
- restore_cache:
keys:
- v-3.0.6-electron
- v-3.0.7-electron
# because in the build job we use circleci docker image and circleci restores cache to original user home
- run:
command: |
Expand Down
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
osx_image: xcode9.4
osx_image: xcode10.1
language: node_js

env:
Expand Down
4 changes: 1 addition & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@
"ejs": "^2.6.1",
"electron-is-dev": "^0.3.0",
"electron-osx-sign": "0.4.11",
"env-paths": "^1.0.0",
"fs-extra-p": "^7.0.0",
"hosted-git-info": "^2.7.1",
"iconv-lite": "^0.4.24",
Expand Down Expand Up @@ -108,14 +107,13 @@
"depcheck": "^0.6.11",
"develar-typescript-json-schema": "0.20.0",
"electron-builder-tslint-config": "^1.1.0",
"env-paths": "^1.0.0",
"globby": "^8.0.1",
"jest-cli": "^23.6.0",
"jest-junit": "^5.2.0",
"jsdoc-to-markdown": "^4.0.1",
"path-sort": "^0.1.0",
"sumchecker": "^2.0.2",
"ts-babel": "6.1.1",
"ts-babel": "6.1.2",
"ts-jsdoc": "^3.0.1",
"tslint": "^5.11.0",
"typescript": "^3.1.6",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,9 @@ export interface PlatformSpecificBuildOptions extends TargetSpecificOptions {
*/
readonly compression?: CompressionLevel | null

readonly files?: Array<FileSet | string> | FileSet | string | null
readonly extraResources?: Array<FileSet | string> | FileSet | string | null
readonly extraFiles?: Array<FileSet | string> | FileSet | string | null
files?: Array<FileSet | string> | FileSet | string | null
extraResources?: Array<FileSet | string> | FileSet | string | null
extraFiles?: Array<FileSet | string> | FileSet | string | null

/**
* Whether to package the application's source code into an archive, using [Electron's archive format](http://electron.atom.io/docs/tutorial/application-packaging/).
Expand Down
129 changes: 113 additions & 16 deletions packages/app-builder-lib/src/util/config.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { asArray, DebugLogger, InvalidConfigurationError, log, deepAssign } from "builder-util"
import { DebugLogger, deepAssign, InvalidConfigurationError, log } from "builder-util"
import { statOrNull } from "builder-util/out/fs"
import { readJson } from "fs-extra-p"
import { Lazy } from "lazy-val"
import * as path from "path"
import { getConfig as _getConfig, loadParentConfig, orNullIfFileNotExist, ReadConfigRequest, validateConfig as _validateConfig } from "read-config-file"
import { FileSet } from ".."
import { Configuration } from "../configuration"
import { reactCra } from "../presets/rectCra"

Expand Down Expand Up @@ -58,34 +59,130 @@ export async function getConfig(projectDir: string, configPath: string | null, c
}
}

if (extendsSpec == null) {
return deepAssign(getDefaultConfig(), config)
}

let parentConfig: Configuration | null
if (extendsSpec === "react-cra") {
parentConfig = await reactCra(projectDir)
log.info({preset: extendsSpec}, "loaded parent configuration")
}
else {
else if (extendsSpec != null) {
const parentConfigAndEffectiveFile = await loadParentConfig<Configuration>(configRequest, extendsSpec)
log.info({file: parentConfigAndEffectiveFile.configFile}, "loaded parent configuration")
parentConfig = parentConfigAndEffectiveFile.result
}
else {
parentConfig = null
}

return doMergeConfigs(config, parentConfig)
}

// normalize for easy merge
function normalizeFiles(configuration: Configuration, name: "files" | "extraFiles" | "extraResources") {
let value = configuration[name]
if (value == null) {
return
}

if (!Array.isArray(value)) {
value = [value]
}

itemLoop: for (let i = 0; i < value.length; i++) {
let item = value[i]
if (typeof item === "string") {
// merge with previous if possible
if (i !== 0) {
let prevItemIndex = i - 1
let prevItem: FileSet
do {
prevItem = value[prevItemIndex--] as FileSet
} while (prevItem == null)

if (prevItem.from == null && prevItem.to == null) {
if (prevItem.filter == null) {
prevItem.filter = [item]
}
else {
(prevItem.filter as Array<string>).push(item)
}
value[i] = null as any
continue itemLoop
}
}

item = {
filter: [item],
}
value[i] = item
}
else if (Array.isArray(item)) {
throw new Error(`${name} configuration is invalid, nested array not expected for index ${i}: ` + item)
}

// make sure that merge logic is not complex - unify different presentations
if (item.from === ".") {
item.from = undefined
}

// electron-webpack and electrify client config - want to exclude some files
// we add client files configuration to main parent file matcher
const files = config.files == null ? [] : (Array.isArray(config.files) ? config.files : (typeof config.files === "string" ? [config.files] : []))
if (parentConfig.files != null && files.length !== 0 && Array.isArray(parentConfig.files) && parentConfig.files.length > 0) {
const mainFileSet = parentConfig.files[0]
if (typeof mainFileSet === "object" && (mainFileSet.from == null || mainFileSet.from === ".")) {
mainFileSet.filter = asArray(mainFileSet.filter)
mainFileSet.filter.push(...asArray(config.files as any))
delete (config as any).files
if (item.to === ".") {
item.to = undefined
}

if (item.filter != null && typeof item.filter === "string") {
item.filter = [item.filter]
}
}

configuration[name] = value.filter(it => it != null)
}

function mergeFiles(configuration: Configuration, parentConfiguration: Configuration, mergedConfiguration: Configuration, name: "files" | "extraFiles" | "extraResources") {
const list = configuration[name] as Array<FileSet> | null
const parentList = parentConfiguration[name] as Array<FileSet> | null
if (list == null || parentList == null) {
return
}

const result = list.slice()
mergedConfiguration[name] = result

itemLoop: for (const item of parentConfiguration.files as Array<FileSet>) {
for (const existingItem of list) {
if (existingItem.from === item.from && existingItem.to === item.to) {
if (item.filter != null) {
if (existingItem.filter == null) {
existingItem.filter = item.filter.slice()
}
else {
existingItem.filter = (item.filter as Array<string>).concat(existingItem.filter)
}
}

continue itemLoop
}
}

// existing item not found, simply add
result.push(item)
}
}

export function doMergeConfigs(configuration: Configuration, parentConfiguration: Configuration | null) {
normalizeFiles(configuration, "files")
normalizeFiles(configuration, "extraFiles")
normalizeFiles(configuration, "extraResources")

if (parentConfiguration == null) {
return deepAssign(getDefaultConfig(), configuration)
}

normalizeFiles(parentConfiguration, "files")
normalizeFiles(parentConfiguration, "extraFiles")
normalizeFiles(parentConfiguration, "extraResources")

return deepAssign(getDefaultConfig(), parentConfig, config)
const result = deepAssign(getDefaultConfig(), parentConfiguration, configuration)
mergeFiles(configuration, parentConfiguration, result, "files")
return result
}

function getDefaultConfig(): Configuration {
Expand Down
2 changes: 1 addition & 1 deletion test/fixtures/test-app-build-sub/electron-builder.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
electronVersion: 3.0.6
electronVersion: 3.0.7
appId: org.electron-builder.testApp
compression: store
npmRebuild: false
Expand Down
2 changes: 1 addition & 1 deletion test/fixtures/test-app-one/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"author": "Foo Bar <foo@example.com>",
"license": "MIT",
"build": {
"electronVersion": "3.0.6",
"electronVersion": "3.0.7",
"appId": "org.electron-builder.testApp",
"compression": "store",
"npmRebuild": false,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"author": "Foo Bar <foo@example.com>",
"license": "MIT",
"build": {
"electronVersion": "3.0.6",
"electronVersion": "3.0.7",
"appId": "org.electron-builder.testApp",
"compression": "store",
"npmRebuild": false,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"author": "Foo Bar <foo@example.com>",
"license": "MIT",
"build": {
"electronVersion": "3.0.6",
"electronVersion": "3.0.7",
"appId": "org.electron-builder.testApp",
"compression": "store",
"npmRebuild": false,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"author": "Foo Bar <foo@example.com>",
"license": "MIT",
"build": {
"electronVersion": "3.0.6",
"electronVersion": "3.0.7",
"appId": "org.electron-builder.testApp",
"compression": "store",
"npmRebuild": false,
Expand Down
2 changes: 1 addition & 1 deletion test/fixtures/test-app/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"private": true,
"build": {
"electronVersion": "3.0.6",
"electronVersion": "3.0.7",
"appId": "org.electron-builder.testApp",
"compression": "store",
"npmRebuild": false,
Expand Down
64 changes: 64 additions & 0 deletions test/src/BuildTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { checkBuildRequestOptions } from "app-builder-lib"
import { readAsar } from "app-builder-lib/out/asar/asar"
import { move, outputJson, readFileSync } from "fs-extra-p"
import * as path from "path"
import { doMergeConfigs } from "app-builder-lib/out/util/config"
import { app, appTwo, appTwoThrows, assertPack, linuxDirTarget, modifyPackageJson, packageJson } from "./helpers/packTester"
import { ELECTRON_VERSION } from "./helpers/testConfig"

Expand Down Expand Up @@ -60,6 +61,69 @@ test("cli", async () => {
})
})

test("merge configurations", () => {
const result = doMergeConfigs({
files: [
"**/*",
"!webpack",
"!.*",
"!config/jsdoc.json",
"!package.*",
"!docs",
"!private"
],
}, {
files: [
{
from: ".",
filter: [
"package.json"
]
},
{
from: "dist/main"
},
{
from: "dist/renderer"
},
{
from: "dist/renderer-dll"
}
],
})

console.log("BOO: " + JSON.stringify(result, null, 2))
expect(result).toMatchObject({
directories: {
output: "dist",
buildResources: "build"
},
files: [
{
filter: [
"package.json",
"**/*",
"!webpack",
"!.*",
"!config/jsdoc.json",
"!package.*",
"!docs",
"!private"
]
},
{
from: "dist/main"
},
{
from: "dist/renderer"
},
{
from: "dist/renderer-dll"
}
]
})
})

test("build in the app package.json", appTwoThrows({targets: linuxDirTarget}, {
projectDirCreated: it => modifyPackageJson(it, data => {
data.build = {
Expand Down
5 changes: 3 additions & 2 deletions test/src/ExtraBuildTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { move } from "fs-extra-p"
import * as path from "path"
import { assertThat } from "./helpers/fileAssert"
import { app, assertPack, linuxDirTarget, modifyPackageJson } from "./helpers/packTester"
import { getElectronCacheDir } from "./helpers/testConfig"
import { expectUpdateMetadata } from "./helpers/winHelper"

function createBuildResourcesTest(packagerOptions: PackagerOptions) {
Expand Down Expand Up @@ -120,10 +121,10 @@ test.ifAll.ifDevOrWinCi("override targets in the config - only arch", app({
// test on all CI to check path separators
test.ifAll("do not exclude build entirely (respect files)", () => assertPack("test-app-build-sub", {targets: linuxDirTarget}))

test.ifAll("electronDist as path to local folder with electron builds zipped ", app({
test.ifNotWindows("electronDist as path to local folder with electron builds zipped ", app({
targets: linuxDirTarget,
config: {
electronDist: require("env-paths")("electron", {suffix: ""}).cache,
electronDist: getElectronCacheDir(),
},
}))

Expand Down
Loading

0 comments on commit 5226c21

Please sign in to comment.