From a9f2ad8ea9eb933e80ad569727cf7c3c4692c876 Mon Sep 17 00:00:00 2001 From: develar Date: Mon, 13 Feb 2017 07:27:02 +0100 Subject: [PATCH] feat: --config option --- README.md | 44 ++++++++++--------- docs/Options.md | 30 ++++++------- packages/electron-builder/src/builder.ts | 3 -- .../electron-builder/src/cli/cliOptions.ts | 9 +++- packages/electron-builder/src/packager.ts | 11 ++++- packages/electron-builder/src/packagerApi.ts | 2 +- .../src/util/readPackageJson.ts | 26 +++++++---- test/src/helpers/packTester.ts | 4 +- 8 files changed, 75 insertions(+), 54 deletions(-) diff --git a/README.md b/README.md index 799a65550b1..8f41b86b557 100755 --- a/README.md +++ b/README.md @@ -92,21 +92,24 @@ See the [Auto Update](https://github.com/electron-userland/electron-builder/wiki Execute `node_modules/.bin/build --help` to get the actual CLI usage guide. ``` Building: - --mac, -m, -o, --macos Build for macOS, accepts target list (see - https://goo.gl/HAnnq8). [array] - --linux, -l Build for Linux, accepts target list (see - https://goo.gl/O80IL2) [array] - --win, -w, --windows Build for Windows, accepts target list (see - https://goo.gl/dL4i8i) [array] - --x64 Build for x64 [boolean] - --ia32 Build for ia32 [boolean] - --armv7l Build for armv7l [boolean] - --dir Build unpacked dir. Useful to test. [boolean] - --extraMetadata, --em Inject properties to package.json (asar only) - --prepackaged, --pd The path to prepackaged app (to pack in a - distributable format) - --project The path to project directory. Defaults to current - working directory. + --mac, -m, -o, --macos Build for macOS, accepts target list (see + https://goo.gl/HAnnq8). [array] + --linux, -l Build for Linux, accepts target list (see + https://goo.gl/O80IL2) [array] + --win, -w, --windows Build for Windows, accepts target list (see + https://goo.gl/dL4i8i) [array] + --x64 Build for x64 [boolean] + --ia32 Build for ia32 [boolean] + --armv7l Build for armv7l [boolean] + --dir Build unpacked dir. Useful to test. [boolean] + --extraMetadata, --em Inject properties to package.json (asar only) + --prepackaged, --pd The path to prepackaged app (to pack in a + distributable format) + --projectDir, --project The path to project directory. Defaults to current + working directory. + --config, -c The path to an electron-builder config. Defaults to + `electron-builder.yml` (or `json`, or `json5`), see + https://goo.gl/YFRJOM Publishing: --publish, -p Publish artifacts (to GitHub Releases), see @@ -126,10 +129,11 @@ Other: --version Show version number [boolean] Examples: - build -mwl build for macOS, Windows and Linux - build --linux deb tar.xz build deb and tar.xz for Linux - build --win --ia32 build for Windows ia32 - build --em.foo=bar set package.json property `foo` to `bar` + build -mwl build for macOS, Windows and Linux + build --linux deb tar.xz build deb and tar.xz for Linux + build --win --ia32 build for Windows ia32 + build --em.foo=bar set package.json property `foo` to `bar` + build --config.nsis.unicode= configure unicode options for NSIS ``` ## Programmatic Usage @@ -165,7 +169,7 @@ and other distributable formats. ./node_modules/.bin/build --prepackaged ``` -`--project` (the path to project directory) option also can be useful. +`--projectDir` (the path to project directory) option also can be useful. ## Community diff --git a/docs/Options.md b/docs/Options.md index f0dccf8948d..6a4fc8810a0 100644 --- a/docs/Options.md +++ b/docs/Options.md @@ -1,23 +1,21 @@ -electron-builder configuration can be defined in the `package.json` file of your project or through the `--config ` option (defaults to `electron-builder.yml`). +electron-builder configuration can be defined in the `package.json` file of your project or through the `--config ` option (defaults to `electron-builder.yml` (or `json`, or [json5](http://json5.org))). If you'd like to use your package.json to store config, the `build` key should be used on the top level. For example, to change icon location for DMG: ```json -"build": { - "dmg": { - "contents": [ - { - "x": 130, - "y": 220 - }, - { - "x": 410, - "y": 220, - "type": "link", - "path": "/Applications" - } - ] - } +"dmg": { + "contents": [ + { + "x": 130, + "y": 220 + }, + { + "x": 410, + "y": 220, + "type": "link", + "path": "/Applications" + } + ] } ``` diff --git a/packages/electron-builder/src/builder.ts b/packages/electron-builder/src/builder.ts index 01945693389..8937cc108c5 100644 --- a/packages/electron-builder/src/builder.ts +++ b/packages/electron-builder/src/builder.ts @@ -159,9 +159,6 @@ export function normalizeOptions(args: CliOptions): BuildOptions { delete result.x64 delete result.armv7l - if (result.project != null) { - result.projectDir = result.project - } delete result.project return result } diff --git a/packages/electron-builder/src/cli/cliOptions.ts b/packages/electron-builder/src/cli/cliOptions.ts index 50588283962..8b6a92f934f 100644 --- a/packages/electron-builder/src/cli/cliOptions.ts +++ b/packages/electron-builder/src/cli/cliOptions.ts @@ -12,6 +12,7 @@ export function createYargs(): any { .example("build --linux deb tar.xz", "build deb and tar.xz for Linux") .example("build --win --ia32", "build for Windows ia32") .example("build --em.foo=bar", "set package.json property `foo` to `bar`") + .example("build --config.nsis.unicode=", "configure unicode options for NSIS") .option("mac", { group: buildGroup, alias: ["m", "o", "macos"], @@ -88,10 +89,16 @@ export function createYargs(): any { group: buildGroup, describe: "The path to prepackaged app (to pack in a distributable format)", }) - .option("project", { + .option("projectDir", { + alias: ["project"], group: buildGroup, describe: "The path to project directory. Defaults to current working directory.", }) + .option("config", { + alias: ["c"], + group: buildGroup, + describe: "The path to an electron-builder config. Defaults to `electron-builder.yml` (or `json`, or `json5`), see " + underline("https://goo.gl/YFRJOM"), + }) .strict() .group(["help", "version"], "Other:") .help() diff --git a/packages/electron-builder/src/packager.ts b/packages/electron-builder/src/packager.ts index e4c6e96b045..94393d5ca57 100644 --- a/packages/electron-builder/src/packager.ts +++ b/packages/electron-builder/src/packager.ts @@ -16,7 +16,7 @@ import { ArtifactCreated, BuildInfo, PackagerOptions, SourceRepositoryInfo } fro import { PlatformPackager } from "./platformPackager" import { getRepositoryInfo } from "./repositoryInfo" import { createTargets } from "./targets/targetFactory" -import { getElectronVersion, loadConfig, readPackageJson } from "./util/readPackageJson" +import { doLoadConfig, getElectronVersion, loadConfig, readPackageJson } from "./util/readPackageJson" import { WinPackager } from "./winPackager" import { getGypEnv, installOrRebuild } from "./yarn" import { CancellationToken } from "electron-builder-http/out/CancellationToken" @@ -91,7 +91,14 @@ export class Packager implements BuildInfo { warn("devMetadata is deprecated, please use config instead") } + let configPath: string | null = null let configFromOptions = this.options.config + if (typeof configFromOptions === "string") { + // it is a path to config file + configPath = configFromOptions + configFromOptions = null + } + if (devMetadataFromOptions != null) { if (configFromOptions != null) { throw new Error("devMetadata and config cannot be used in conjunction") @@ -100,7 +107,7 @@ export class Packager implements BuildInfo { } const projectDir = this.projectDir - const fileOrPackageConfig = await loadConfig(projectDir) + const fileOrPackageConfig = await (configPath == null ? loadConfig(projectDir) : doLoadConfig(configPath, projectDir)) const config = deepAssign({}, fileOrPackageConfig, configFromOptions) const extraMetadata = this.options.extraMetadata diff --git a/packages/electron-builder/src/packagerApi.ts b/packages/electron-builder/src/packagerApi.ts index da220b8ff7d..8a68d21adef 100644 --- a/packages/electron-builder/src/packagerApi.ts +++ b/packages/electron-builder/src/packagerApi.ts @@ -23,7 +23,7 @@ export interface PackagerOptions { */ readonly devMetadata?: Metadata - readonly config?: Config + readonly config?: Config | string | null /** * The same as [application package.json](https://github.com/electron-userland/electron-builder/wiki/Options#AppMetadata). diff --git a/packages/electron-builder/src/util/readPackageJson.ts b/packages/electron-builder/src/util/readPackageJson.ts index c67bfbfe397..1d05e0a0877 100644 --- a/packages/electron-builder/src/util/readPackageJson.ts +++ b/packages/electron-builder/src/util/readPackageJson.ts @@ -46,16 +46,24 @@ function getConfigFromPackageData(metadata: any) { return metadata.build } +export async function doLoadConfig(configFile: string, projectDir: string) { + const configPath = path.join(projectDir, configFile) + const result = safeLoad(await readFile(configPath, "utf8")) + + const relativePath = path.relative(projectDir, configPath) + log(`Using ${relativePath.startsWith("..") ? configFile : relativePath} configuration file`) + return result +} + export async function loadConfig(projectDir: string): Promise { - try { - const configPath = path.join(projectDir, "electron-builder.yml") - const result = safeLoad(await readFile(configPath, "utf8")) - log(`Using ${path.relative(projectDir, configPath)} configuration file`) - return result - } - catch (e) { - if (e.code !== "ENOENT") { - throw e + for (const configFile of ["electron-builder.yml", "electron-builder.json", "electron-builder.json5"]) { + try { + return await doLoadConfig(path.join(projectDir, configFile), projectDir) + } + catch (e) { + if (e.code !== "ENOENT") { + throw e + } } } diff --git a/test/src/helpers/packTester.ts b/test/src/helpers/packTester.ts index e2b969fe3ca..bc52ef5ee1e 100755 --- a/test/src/helpers/packTester.ts +++ b/test/src/helpers/packTester.ts @@ -4,7 +4,7 @@ import * as path from "path" import { parse as parsePlist } from "plist" import { CSC_LINK } from "./codeSignData" import { expectedLinuxContents, expectedWinContents } from "./expectedContents" -import { Packager, PackagerOptions, Platform, ArtifactCreated, Arch, DIR_TARGET, createTargets, getArchSuffix, MacOsTargetName, Target, MacOptions, BuildInfo } from "electron-builder" +import { Packager, PackagerOptions, Platform, ArtifactCreated, Arch, DIR_TARGET, createTargets, getArchSuffix, MacOsTargetName, Target, MacOptions, BuildInfo, Config } from "electron-builder" import { exec, spawn, getTempName } from "electron-builder-util" import { log, warn } from "electron-builder-util/out/log" import pathSorter from "path-sort" @@ -118,7 +118,7 @@ export async function assertPack(fixtureName: string, packagerOptions: PackagerO // never output to test fixture app if (!useTempDir) { dirToDelete = path.join(testDir, `${(tmpDirCounter++).toString(16)}`) - const config = packagerOptions.config + const config = packagerOptions.config if (config != null && config.directories != null) { throw new Error("unsupported") }