Skip to content

Commit

Permalink
feat: --config option
Browse files Browse the repository at this point in the history
  • Loading branch information
develar committed Feb 13, 2017
1 parent a7d2992 commit a9f2ad8
Show file tree
Hide file tree
Showing 8 changed files with 75 additions and 54 deletions.
44 changes: 24 additions & 20 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -165,7 +169,7 @@ and other distributable formats.
./node_modules/.bin/build --prepackaged <packed dir>
```

`--project` (the path to project directory) option also can be useful.
`--projectDir` (the path to project directory) option also can be useful.

## Community

Expand Down
30 changes: 14 additions & 16 deletions docs/Options.md
Original file line number Diff line number Diff line change
@@ -1,23 +1,21 @@
electron-builder configuration can be defined in the `package.json` file of your project or through the `--config <path/to/yml>` option (defaults to `electron-builder.yml`).
electron-builder configuration can be defined in the `package.json` file of your project or through the `--config <path/to/yml-or-json5>` 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"
}
]
}
```

Expand Down
3 changes: 0 additions & 3 deletions packages/electron-builder/src/builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
9 changes: 8 additions & 1 deletion packages/electron-builder/src/cli/cliOptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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"],
Expand Down Expand Up @@ -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()
Expand Down
11 changes: 9 additions & 2 deletions packages/electron-builder/src/packager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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")
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion packages/electron-builder/src/packagerApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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).
Expand Down
26 changes: 17 additions & 9 deletions packages/electron-builder/src/util/readPackageJson.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<Config | null> {
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
}
}
}

Expand Down
4 changes: 2 additions & 2 deletions test/src/helpers/packTester.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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 = <Config>packagerOptions.config
if (config != null && config.directories != null) {
throw new Error("unsupported")
}
Expand Down

0 comments on commit a9f2ad8

Please sign in to comment.