From c6a2a4fe18ace39c20ccf3870bf7599c1594c53c Mon Sep 17 00:00:00 2001 From: develar Date: Thu, 1 Nov 2018 09:31:47 +0100 Subject: [PATCH] feat(electron-publisher): Allow pass configuration to custom electron-publisher provider Close #3261 --- package.json | 2 +- .../src/publish/PublishManager.ts | 24 ++++++++++++++----- .../src/publishOptions.ts | 9 +++++-- .../__snapshots__/PublishManagerTest.js.snap | 11 +++++++++ test/src/ArtifactPublisherTest.ts | 4 ++-- test/src/PublishManagerTest.ts | 20 ++++++++++++++++ yarn.lock | 8 +++---- 7 files changed, 63 insertions(+), 15 deletions(-) diff --git a/package.json b/package.json index 8e744d4bdd9..f4b35f3f8e9 100644 --- a/package.json +++ b/package.json @@ -118,7 +118,7 @@ "ts-babel": "6.1.0", "ts-jsdoc": "^3.0.1", "tslint": "^5.11.0", - "typescript": "^3.1.4", + "typescript": "^3.1.5", "whitespace": "^2.1.0", "worker-farm": "^1.6.0" }, diff --git a/packages/app-builder-lib/src/publish/PublishManager.ts b/packages/app-builder-lib/src/publish/PublishManager.ts index 9248ebbb400..6c2f8e88151 100644 --- a/packages/app-builder-lib/src/publish/PublishManager.ts +++ b/packages/app-builder-lib/src/publish/PublishManager.ts @@ -191,7 +191,7 @@ export class PublishManager implements PublishContext { const providerCacheKey = safeStringifyJson(publishConfig) let publisher = this.nameToPublisher.get(providerCacheKey) if (publisher == null) { - publisher = createPublisher(this, appInfo.version, publishConfig, this.publishOptions) + publisher = createPublisher(this, appInfo.version, publishConfig, this.publishOptions, this.packager) this.nameToPublisher.set(providerCacheKey, publisher) log.info({publisher: publisher!!.toString()}, "publishing") } @@ -259,7 +259,7 @@ export async function getPublishConfigsForUpdateInfo(packager: PlatformPackager< return publishConfigs } -export function createPublisher(context: PublishContext, version: string, publishConfig: PublishConfiguration, options: PublishOptions): Publisher | null { +export function createPublisher(context: PublishContext, version: string, publishConfig: PublishConfiguration, options: PublishOptions, packager: Packager): Publisher | null { if (debug.enabled) { debug(`Create publisher: ${safeStringifyJson(publishConfig)}`) } @@ -276,12 +276,12 @@ export function createPublisher(context: PublishContext, version: string, publis return null default: - const clazz = requireProviderClass(provider) + const clazz = requireProviderClass(provider, packager) return clazz == null ? null : new clazz(context, publishConfig) } } -function requireProviderClass(provider: string): any | null { +function requireProviderClass(provider: string, packager: Packager): any | null { switch (provider) { case "github": return GitHubPublisher @@ -299,7 +299,19 @@ function requireProviderClass(provider: string): any | null { return SpacesPublisher default: - return require(`electron-publisher-${provider}`).default + const name = `electron-publisher-${provider}` + let module: any = null + try { + module = require(path.join(packager.buildResourcesDir, name + ".js")) + } + catch (ignored) { + console.log(ignored) + } + + if (module == null) { + module = require(name) + } + return module.default || module } } @@ -429,7 +441,7 @@ async function getResolvedPublishConfig(platformPackager: PlatformPackager return options } - const providerClass = requireProviderClass(options.provider) + const providerClass = requireProviderClass(options.provider, packager) if (providerClass != null && providerClass.checkAndResolveOptions != null) { await providerClass.checkAndResolveOptions(options, channelFromAppVersion, errorIfCannot) return options diff --git a/packages/builder-util-runtime/src/publishOptions.ts b/packages/builder-util-runtime/src/publishOptions.ts index a2856f25357..71eef0fc320 100644 --- a/packages/builder-util-runtime/src/publishOptions.ts +++ b/packages/builder-util-runtime/src/publishOptions.ts @@ -1,7 +1,7 @@ -export type PublishProvider = "github" | "bintray" | "s3" | "spaces" | "generic" +export type PublishProvider = "github" | "bintray" | "s3" | "spaces" | "generic" | "custom" // typescript-json-schema generates only PublishConfiguration if it is specified in the list, so, it is not added here -export type AllPublishOptions = string | GithubOptions | S3Options | SpacesOptions | GenericServerOptions | BintrayOptions +export type AllPublishOptions = string | GithubOptions | S3Options | SpacesOptions | GenericServerOptions | BintrayOptions | CustomPublishOptions // https://github.com/YousefED/typescript-json-schema/issues/80 export type Publish = AllPublishOptions | Array | null @@ -27,6 +27,11 @@ export interface PublishConfiguration { readonly publishAutoUpdate?: boolean } +// https://github.com/electron-userland/electron-builder/issues/3261 +export interface CustomPublishOptions extends PublishConfiguration { + [index: string]: any +} + /** * [GitHub](https://help.github.com/articles/about-releases/) options. * diff --git a/test/out/__snapshots__/PublishManagerTest.js.snap b/test/out/__snapshots__/PublishManagerTest.js.snap index 14bb229fac1..ade20b5ee50 100644 --- a/test/out/__snapshots__/PublishManagerTest.js.snap +++ b/test/out/__snapshots__/PublishManagerTest.js.snap @@ -1,5 +1,16 @@ // Jest Snapshot v1, https://goo.gl/fbAQLP +exports[`custom provider 1`] = ` +Object { + "linux": Array [ + Object { + "arch": "x64", + "file": "TestApp-1.1.0.zip", + }, + ], +} +`; + exports[`dotted s3 bucket 1`] = ` Object { "linux": Array [ diff --git a/test/src/ArtifactPublisherTest.ts b/test/src/ArtifactPublisherTest.ts index fd315e448f3..7fab913b0c1 100644 --- a/test/src/ArtifactPublisherTest.ts +++ b/test/src/ArtifactPublisherTest.ts @@ -111,7 +111,7 @@ testAndIgnoreApiRate("GitHub upload", async () => { if (process.env.AWS_ACCESS_KEY_ID != null && process.env.AWS_SECRET_ACCESS_KEY != null) { test("S3 upload", async () => { - const publisher = createPublisher(publishContext, "0.0.1", {provider: "s3", bucket: "electron-builder-test"} as S3Options, {})!! + const publisher = createPublisher(publishContext, "0.0.1", {provider: "s3", bucket: "electron-builder-test"} as S3Options, {}, {} as any)!! await publisher.upload({file: iconPath, arch: Arch.x64}) // test overwrite await publisher.upload({file: iconPath, arch: Arch.x64}) @@ -125,7 +125,7 @@ if (process.env.DO_KEY_ID != null && process.env.DO_SECRET_KEY != null) { name: "electron-builder-test", region: "nyc3", } - const publisher = createPublisher(publishContext, "0.0.1", configuration, {})!! + const publisher = createPublisher(publishContext, "0.0.1", configuration, {}, {} as any)!! await publisher.upload({file: iconPath, arch: Arch.x64}) // test overwrite await publisher.upload({file: iconPath, arch: Arch.x64}) diff --git a/test/src/PublishManagerTest.ts b/test/src/PublishManagerTest.ts index 5bc49f4d35f..75451d8debf 100644 --- a/test/src/PublishManagerTest.ts +++ b/test/src/PublishManagerTest.ts @@ -1,4 +1,5 @@ import { createTargets, Platform } from "electron-builder" +import { outputFile } from "fs-extra-p" import * as path from "path" import { assertThat } from "./helpers/fileAssert" import { app, checkDirContents } from "./helpers/packTester" @@ -106,4 +107,23 @@ test.ifAll.ifNotWindows("dotted s3 bucket", app({ }, }, { publish: "never" +})) + +// https://github.com/electron-userland/electron-builder/issues/3261 +test.ifAll.ifNotWindows("custom provider", app({ + targets: createTargets([Platform.LINUX], "zip"), + config: { + publish: { + provider: "custom", + boo: "foo", + }, + }, +}, { + publish: "never", + projectDirCreated: projectDir => outputFile(path.join(projectDir, "build/electron-publisher-custom.js"), `class Publisher { + async upload(task) { + } + } + + module.exports = Publisher`) })) \ No newline at end of file diff --git a/yarn.lock b/yarn.lock index a9232fd56e0..0aab54db4ae 100644 --- a/yarn.lock +++ b/yarn.lock @@ -6356,10 +6356,10 @@ type-check@~0.3.2: dependencies: prelude-ls "~1.1.2" -typescript@^3.1.4: - version "3.1.4" - resolved "https://registry.yarnpkg.com/typescript/-/typescript-3.1.4.tgz#c74ef7b3c2da65beff548b903022cb8c3cd997ed" - integrity sha512-JZHJtA6ZL15+Q3Dqkbh8iCUmvxD3iJ7ujXS+fVkKnwIVAdHc5BJTDNM0aTrnr2luKulFjU7W+SRhDZvi66Ru7Q== +typescript@^3.1.5: + version "3.1.5" + resolved "https://registry.yarnpkg.com/typescript/-/typescript-3.1.5.tgz#93d8b6864375325a91177372cb370ae0ae3a0703" + integrity sha512-muYNWV9j5+3mXoKD6oPONKuGUmYiFX14gfo9lWm9ZXRHOqVDQiB4q1CzFPbF4QLV2E9TZXH6oK55oQ94rn3PpA== typescript@~2.6.2: version "2.6.2"