Skip to content

Commit

Permalink
feat: accept multiple default app dirs
Browse files Browse the repository at this point in the history
www checked in addition to app.

Closes #344
  • Loading branch information
demetris-manikas authored and develar committed Apr 20, 2016
1 parent 9b043d6 commit ea5f842
Show file tree
Hide file tree
Showing 13 changed files with 170 additions and 69 deletions.
1 change: 1 addition & 0 deletions docs/Options.md
Original file line number Diff line number Diff line change
Expand Up @@ -97,5 +97,6 @@ See all [windows-installer options](https://github.com/electron/windows-installe
| --- | ---
| buildResources | <a name="MetadataDirectories-buildResources"></a>The path to build resources, default `build`.
| output | <a name="MetadataDirectories-output"></a>The output directory, default `dist`.
| app | <a name="MetadataDirectories-app"></a>The application directory (containing the application package.json), default `app`, `www` or working directory.

<!-- end of generated block -->
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
"dependencies": {
"7zip-bin": "^0.0.4",
"bluebird": "^3.3.5",
"chalk": "^1.1.3",
"command-line-args": "^2.1.6",
"deep-assign": "^2.0.0",
"electron-packager": "^7.0.0",
Expand Down
17 changes: 12 additions & 5 deletions src/build-cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
import { PackagerOptions } from "./platformPackager"
import { build } from "./builder"
import { PublishOptions } from "./gitHubPublisher"
import { commonArgs } from "./util"
import { printErrorAndExit } from "./promise"
import cla = require("command-line-args")
import { readFileSync } from "fs"
import * as path from "path"
import { warn } from "./util"

//noinspection JSUnusedLocalSymbols
const __awaiter = require("./awaiter")
Expand All @@ -16,26 +16,33 @@ interface CliOptions extends PackagerOptions, PublishOptions {
help: boolean
}

const cli = cla(commonArgs.concat(
const cli = cla([
{name: "dist", type: Boolean, alias: "d", description: "Whether to package in a distributable format (e.g. DMG, windows installer, NuGet package)."},
{name: "publish", type: String, alias: "p", description: "Publish artifacts (to GitHub Releases): onTag (on tag push only) or onTagOrDraft (on tag push or if draft release exists)."},
{name: "platform", type: String, multiple: true, description: "darwin, linux, win32 or all. Current platform (" + process.platform + ") by default."},
{name: "arch", type: String, description: "ia32, x64 or all. Defaults to architecture you're running on."},
{name: "target", type: String, multiple: true, description: "Installer or package type."},
{name: "sign", type: String},
{name: "help", alias: "h", type: Boolean, description: "Display this usage guide."}
))
{name: "help", alias: "h", type: Boolean, description: "Display this usage guide."},
{name: "appDir", type: String}
])

const args: CliOptions = cli.parse()

if (args.help) {
const version = process.env.npm_package_version || JSON.parse(readFileSync(path.join(__dirname, "..", "package.json"), "utf8")).version
console.log(cli.getUsage({
title: "electron-builder " + version,
footer: "Project home: [underline]{https://github.com/electron-userland/electron-builder}"
footer: "Project home: [underline]{https://github.com/electron-userland/electron-builder}",
hide: ["appDir"],
}))
}
else {
if (args.appDir) {
warn(`-appDir CLI parameter is deprecated, please configure build.directories.app instead
See https://github.com/electron-userland/electron-builder/wiki/Options#MetadataDirectories-app`)
}

build(args)
.catch(printErrorAndExit)
}
32 changes: 22 additions & 10 deletions src/install-app-deps.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,33 @@
#! /usr/bin/env node

import { DEFAULT_APP_DIR_NAME, installDependencies, commonArgs, getElectronVersion, readPackageJson } from "./util"
import { computeDefaultAppDirectory, installDependencies, getElectronVersion, readPackageJson, use } from "./util"
import { printErrorAndExit } from "./promise"
import * as path from "path"
import cla = require("command-line-args")
import { Promise as BluebirdPromise } from "bluebird"
import { DevMetadata } from "./metadata";

//noinspection JSUnusedLocalSymbols
const __awaiter = require("./awaiter")

const args = cla(commonArgs.concat({
name: "arch",
type: String,
})).parse()
const args = cla([{name: "arch", type: String}, {name: "appDir", type: String}]).parse()

const devPackageFile = path.join(process.cwd(), "package.json")
const appDir = args.appDir || DEFAULT_APP_DIR_NAME
const projectDir = process.cwd()
const devPackageFile = path.join(projectDir, "package.json")

readPackageJson(devPackageFile)
.then(async (it) => installDependencies(path.join(process.cwd(), appDir), await getElectronVersion(it, devPackageFile), args.arch))
.catch(printErrorAndExit)
async function main() {
const devMetadata: DevMetadata = await readPackageJson(devPackageFile)
const results: Array<string> = await BluebirdPromise.all([
computeDefaultAppDirectory(projectDir, use(devMetadata.directories, it => it.app) || args.appDir),
getElectronVersion(devMetadata, devPackageFile)
])

await installDependencies(results[0], results[1], args.arch)
}

try {
main()
}
catch (e) {
printErrorAndExit(e)
}
7 changes: 5 additions & 2 deletions src/linuxPackager.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import * as path from "path"
import { Promise as BluebirdPromise } from "bluebird"
import { PlatformPackager, BuildInfo, use } from "./platformPackager"
import { PlatformPackager, BuildInfo } from "./platformPackager"
import { Platform, LinuxBuildOptions } from "./metadata"
import { dir as _tpmDir, TmpOptions } from "tmp"
import { exec, log } from "./util"
import { exec, log, use } from "./util"
import { outputFile, readFile, readdir } from "fs-extra-p"
const template = require("lodash.template")

Expand Down Expand Up @@ -107,6 +107,9 @@ Icon=${this.metadata.name}
}

const promises: Array<Promise<any>> = [resize(24), resize(96)]
if (!output.includes("is32")) {
promises.push(resize(16))
}
if (!output.includes("ih32")) {
promises.push(resize(48))
}
Expand Down
5 changes: 5 additions & 0 deletions src/metadata.ts
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,11 @@ export interface MetadataDirectories {
The output directory, default `dist`.
*/
readonly output?: string

/*
The application directory (containing the application package.json), default `app`, `www` or working directory.
*/
readonly app?: string
}

export interface PlatformSpecificBuildOptions {
Expand Down
48 changes: 12 additions & 36 deletions src/packager.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
import { accessSync } from "fs"
import * as path from "path"
import { DEFAULT_APP_DIR_NAME, installDependencies, log, getElectronVersion, readPackageJson } from "./util"
import { computeDefaultAppDirectory, installDependencies, log, getElectronVersion, readPackageJson, use, warn } from "./util"
import { all, executeFinally } from "./promise"
import { EventEmitter } from "events"
import { Promise as BluebirdPromise } from "bluebird"
import { InfoRetriever } from "./repositoryInfo"
import { AppMetadata, DevMetadata, Platform } from "./metadata"
import { PackagerOptions, PlatformPackager, BuildInfo, ArtifactCreated, use } from "./platformPackager"
import { PackagerOptions, PlatformPackager, BuildInfo, ArtifactCreated } from "./platformPackager"
import MacPackager from "./macPackager"
import { WinPackager } from "./winPackager"
import * as errorMessages from "./errorMessages"
Expand All @@ -22,7 +21,7 @@ function addHandler(emitter: EventEmitter, event: string, handler: Function) {

export class Packager implements BuildInfo {
readonly projectDir: string
readonly appDir: string
appDir: string

metadata: AppMetadata
devMetadata: DevMetadata
Expand All @@ -36,7 +35,9 @@ export class Packager implements BuildInfo {
//noinspection JSUnusedGlobalSymbols
constructor(public options: PackagerOptions, public repositoryInfo: InfoRetriever = null) {
this.projectDir = options.projectDir == null ? process.cwd() : path.resolve(options.projectDir)
this.appDir = this.computeAppDirectory()
if (this.appDir === this.projectDir) {
this.isTwoPackageJsonProjectLayoutUsed = false
}
}

artifactCreated(handler: (event: ArtifactCreated) => void): Packager {
Expand All @@ -50,12 +51,12 @@ export class Packager implements BuildInfo {

async build(): Promise<any> {
const devPackageFile = this.devPackageFile
const appPackageFile = this.projectDir === this.appDir ? devPackageFile : path.join(this.appDir, "package.json")
const platforms = this.options.platform

const metadataList = await BluebirdPromise.map(Array.from(new Set([devPackageFile, appPackageFile])), readPackageJson)
this.metadata = metadataList[metadataList.length - 1]
this.devMetadata = deepAssign(metadataList[0], this.options.devMetadata)
this.devMetadata = deepAssign(await readPackageJson(devPackageFile), this.options.devMetadata)
this.appDir = await computeDefaultAppDirectory(this.projectDir, use(this.devMetadata.directories, it => it.app) || this.options.appDir)
const appPackageFile = this.projectDir === this.appDir ? devPackageFile : path.join(this.appDir, "package.json")
this.metadata = appPackageFile === devPackageFile ? this.devMetadata : await readPackageJson(appPackageFile)
this.checkMetadata(appPackageFile, devPackageFile, platforms)

this.electronVersion = await getElectronVersion(this.devMetadata, devPackageFile)
Expand Down Expand Up @@ -110,31 +111,6 @@ export class Packager implements BuildInfo {
}
}

// Auto-detect app/ (two package.json project layout (development and app)) or fallback to use working directory if not explicitly specified
private computeAppDirectory(): string {
let customAppPath = this.options.appDir
let required = true
if (customAppPath == null) {
customAppPath = DEFAULT_APP_DIR_NAME
required = false
}

const absoluteAppPath = path.join(this.projectDir, customAppPath)
try {
accessSync(absoluteAppPath)
}
catch (e) {
if (required) {
throw new Error(customAppPath + " doesn't exists, " + e.message)
}
else {
this.isTwoPackageJsonProjectLayoutUsed = false
return this.projectDir
}
}
return absoluteAppPath
}

private checkMetadata(appPackageFile: string, devAppPackageFile: string, platforms: Array<Platform>): void {
const reportError = (missedFieldName: string) => {
throw new Error("Please specify '" + missedFieldName + "' in the application package.json ('" + appPackageFile + "')")
Expand All @@ -156,10 +132,10 @@ export class Packager implements BuildInfo {
}

if (this.devMetadata.homepage != null) {
console.warn("homepage in the development package.json is deprecated, please move to the application package.json")
warn("homepage in the development package.json is deprecated, please move to the application package.json")
}
if (this.devMetadata.license != null) {
console.warn("license in the development package.json is deprecated, please move to the application package.json")
warn("license in the development package.json is deprecated, please move to the application package.json")
}
}

Expand Down
9 changes: 3 additions & 6 deletions src/platformPackager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import * as path from "path"
import packager = require("electron-packager")
import globby = require("globby")
import { copy } from "fs-extra-p"
import { statOrNull } from "./util"
import { statOrNull, use } from "./util"
import { Packager } from "./packager"
import deepAssign = require("deep-assign")

Expand All @@ -26,6 +26,7 @@ export interface PackagerOptions {
platform?: Array<Platform>
target?: Array<string>

// deprecated
appDir?: string

projectDir?: string
Expand Down Expand Up @@ -147,7 +148,7 @@ export abstract class PlatformPackager<DC extends PlatformSpecificBuildOptions>
throw new Error(`Output directory ${appOutDir} does not exists. Seems like a wrong configuration.`)
}
else if (!outStat.isDirectory()) {
throw new Error(`Output directory ${appOutDir} is a file. Seems like a wrong configuration.`)
throw new Error(`Output directory ${appOutDir} is not a directory. Seems like a wrong configuration.`)
}
}

Expand Down Expand Up @@ -213,8 +214,4 @@ export interface ArtifactCreated {
readonly artifactName?: string

readonly platform: Platform
}

export function use<T, R>(value: T, task: (it: T) => R): R {
return value == null ? null : task(value)
}
42 changes: 34 additions & 8 deletions src/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,23 @@ import readPackageJsonAsync = require("read-package-json")
import * as os from "os"
import * as path from "path"
import { readJson, stat } from "fs-extra-p"
import { yellow } from "chalk"

//noinspection JSUnusedLocalSymbols
const __awaiter = require("./awaiter")

export const log = console.log

export const DEFAULT_APP_DIR_NAME = "app"
export function warn(message: string) {
console.warn(yellow(message))
}

export const commonArgs: any[] = [{
name: "appDir",
type: String,
description: "Relative (to the working directory) path to the folder containing the application package.json. Working directory or app/ by default."
}]
const DEFAULT_APP_DIR_NAMES = ["app", "www"]

export const readPackageJson = BluebirdPromise.promisify(readPackageJsonAsync)

export function installDependencies(appDir: string, electronVersion: string, arch: string = process.arch, command: string = "install"): BluebirdPromise<any> {
log("Installing app dependencies for arch %s to %s", arch, appDir)
log((command === "install" ? "Installing" : "Rebuilding") + " app dependencies for arch %s to %s", arch, appDir)
const gypHome = path.join(os.homedir(), ".electron-gyp")
const env = Object.assign({}, process.env, {
npm_config_disturl: "https://atom.io/download/atom-shell",
Expand Down Expand Up @@ -102,7 +101,7 @@ export async function getElectronVersion(packageData: any, packageJsonPath: stri
}
catch (e) {
if (e.code !== "ENOENT") {
console.warn("Cannot read electron version from electron-prebuilt package.json" + e.message)
warn("Cannot read electron version from electron-prebuilt package.json" + e.message)
}
}

Expand Down Expand Up @@ -133,4 +132,31 @@ export async function statOrNull(file: string) {
throw e
}
}
}

export async function computeDefaultAppDirectory(projectDir: string, userAppDir: string): Promise<string> {
if (userAppDir != null) {
const absolutePath = path.join(projectDir, userAppDir)
const stat = await statOrNull(absolutePath)
if (stat == null) {
throw new Error(`Application directory ${userAppDir} doesn't exists`)
}
else if (!stat.isDirectory()) {
throw new Error(`Application directory ${userAppDir} is not a directory`)
}
return absolutePath
}

for (let dir of DEFAULT_APP_DIR_NAMES) {
const absolutePath = path.join(projectDir, dir)
const stat = await statOrNull(absolutePath)
if (stat != null && stat.isDirectory()) {
return absolutePath
}
}
return projectDir
}

export function use<T, R>(value: T, task: (it: T) => R): R {
return value == null ? null : task(value)
}
4 changes: 2 additions & 2 deletions src/winPackager.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { downloadCertificate } from "./codeSign"
import { Promise as BluebirdPromise } from "bluebird"
import { PlatformPackager, BuildInfo, use } from "./platformPackager"
import { PlatformPackager, BuildInfo } from "./platformPackager"
import { Platform, WinBuildOptions } from "./metadata"
import * as path from "path"
import { log, statOrNull } from "./util"
import { log, statOrNull, use } from "./util"
import { readFile, deleteFile, stat, rename, copy, emptyDir, writeFile, open, close, read } from "fs-extra-p"
import { sign } from "signcode"

Expand Down
1 change: 1 addition & 0 deletions test/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
"../typings/main/ambient/mime/mime.d.ts",
"../typings/main/ambient/progress/progress.d.ts",
"../typings/main/ambient/tmp/tmp.d.ts",
"../typings/main/definitions/chalk/index.d.ts",
"../typings/main/definitions/source-map-support/source-map-support.d.ts",
"../typings/node.d.ts",
"../typings/progress-stream.d.ts",
Expand Down
1 change: 1 addition & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
"typings/main/ambient/mime/mime.d.ts",
"typings/main/ambient/progress/progress.d.ts",
"typings/main/ambient/tmp/tmp.d.ts",
"typings/main/definitions/chalk/index.d.ts",
"typings/main/definitions/source-map-support/source-map-support.d.ts",
"typings/node.d.ts",
"typings/progress-stream.d.ts",
Expand Down
Loading

0 comments on commit ea5f842

Please sign in to comment.