forked from electron-userland/electron-builder
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
WIP: NSIS Auto-Update electron-userland#529
- Loading branch information
Showing
18 changed files
with
434 additions
and
206 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
{ | ||
"name": "nsis-auto-updater", | ||
"version": "0.0.1", | ||
"description": "NSIS Auto Updater", | ||
"main": "out/nsis-auto-updater/src/nsis-updater.js", | ||
"scripts": { | ||
}, | ||
"author": "Vladimir Krivosheev", | ||
"license": "MIT", | ||
"files": [ | ||
"out" | ||
], | ||
"dependencies": { | ||
"bluebird": "^3.4.1", | ||
"fs-extra-p": "^1.0.6" | ||
}, | ||
"bundledDependencies": [ | ||
"fs-extra-p", | ||
"bluebird" | ||
], | ||
"devDependencies": { | ||
"@types/electron": "^0.37.14", | ||
"@types/node": "^4.0.30" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
import { EventEmitter } from "events" | ||
import { app } from "electron" | ||
import { spawn } from "child_process" | ||
import * as path from "path" | ||
import { tmpdir } from "os" | ||
|
||
class NsisUpdater extends EventEmitter { | ||
private setupPath = path.join(tmpdir(), 'innobox-upgrade.exe') | ||
|
||
private updateAvailable = false | ||
private quitAndInstallCalled = false | ||
|
||
constructor(public updateUrl?: string) { | ||
super() | ||
} | ||
|
||
getFeedURL(): string | null | undefined { | ||
return this.updateUrl | ||
} | ||
|
||
setFeedURL(value: string) { | ||
this.updateUrl = value | ||
} | ||
|
||
checkForUpdates(): void { | ||
if (this.updateUrl == null) { | ||
this.emitError("Update URL is not set") | ||
return | ||
} | ||
|
||
this.emit("checking-for-update") | ||
} | ||
|
||
quitAndInstall(): void { | ||
if (!this.updateAvailable) { | ||
this.emitError("No update available, can't quit and install") | ||
return | ||
} | ||
|
||
if (this.quitAndInstallCalled) { | ||
return | ||
} | ||
|
||
// prevent calling several times | ||
this.quitAndInstallCalled = true | ||
|
||
spawn(this.setupPath, ["/S"], { | ||
detached: true, | ||
stdio: "ignore", | ||
}).unref() | ||
|
||
app.quit() | ||
} | ||
|
||
// emit both error object and message, this is to keep compatibility with old APIs | ||
private emitError (message: string) { | ||
return this.emit("error", new Error(message), message) | ||
} | ||
} | ||
|
||
const updater = new NsisUpdater() | ||
export= updater |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
{ | ||
"compilerOptions": { | ||
"module": "commonjs", | ||
"target": "es6", | ||
"noImplicitAny": true, | ||
"outDir": "out", | ||
"newLine": "LF", | ||
"noResolve": true, | ||
"noEmitOnError": true, | ||
"inlineSources": true, | ||
"sourceMap": true, | ||
"noImplicitReturns": true, | ||
"strictNullChecks": true, | ||
"noEmitHelpers": true, | ||
"noFallthroughCasesInSwitch": true, | ||
"skipLibCheck": true | ||
}, | ||
"files": [ | ||
"../node_modules/fs-extra-p/index.d.ts", | ||
"../node_modules/fs-extra-p/bluebird.d.ts", | ||
"../src/util/httpRequest.ts" | ||
], | ||
"include": [ | ||
"src/**/*.ts", | ||
"node_modules/@types/**/*.d.ts" | ||
], | ||
"exclude": [ | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
import { Publisher, PublishOptions } from "./publisher" | ||
import { Promise as BluebirdPromise } from "bluebird" | ||
import { bintrayRequest, HttpError, doApiRequest, uploadFile } from "./gitHubRequest" | ||
import { log } from "../util/log" | ||
import { debug } from "../util/util" | ||
import { basename } from "path" | ||
import { stat } from "fs-extra-p" | ||
|
||
//noinspection JSUnusedLocalSymbols | ||
const __awaiter = require("../util/awaiter") | ||
|
||
//noinspection ReservedWordAsName | ||
interface Version { | ||
readonly name: string | ||
readonly package: string | ||
} | ||
|
||
export class BintrayPublisher implements Publisher { | ||
private _versionPromise: BluebirdPromise<Version> | ||
private readonly auth: string | ||
|
||
private basePath: string | ||
|
||
constructor(private user: string, apiKey: string, private version: string, private packageName: string, private repo: string = "generic", private options: PublishOptions = {}) { | ||
this.auth = `Basic ${new Buffer(`${user}:${apiKey}`).toString("base64")}` | ||
this.basePath = `/packages/${this.user}/${this.repo}/${this.packageName}` | ||
this._versionPromise = <BluebirdPromise<Version>>this.init() | ||
} | ||
|
||
private async init(): Promise<Version | null> { | ||
try { | ||
return await bintrayRequest<Version>(`${this.basePath}/versions/${this.version}`, this.auth) | ||
} | ||
catch (e) { | ||
if (e instanceof HttpError && e.response.statusCode === 404) { | ||
if (this.options.publish !== "onTagOrDraft") { | ||
log(`Version ${this.version} doesn't exist, creating one`) | ||
return this.createVersion() | ||
} | ||
else { | ||
log(`Version ${this.version} doesn't exist, artifacts will be not published`) | ||
} | ||
} | ||
|
||
throw e | ||
} | ||
} | ||
|
||
private createVersion() { | ||
return bintrayRequest<Version>(`${this.basePath}/versions`, this.auth, { | ||
name: this.version, | ||
}) | ||
} | ||
|
||
async upload(file: string, artifactName?: string): Promise<any> { | ||
const fileName = artifactName || basename(file) | ||
const version = await this._versionPromise | ||
if (version == null) { | ||
debug(`Version ${this.version} doesn't exist and is not created, artifact ${fileName} is not published`) | ||
return | ||
} | ||
|
||
const fileStat = await stat(file) | ||
let badGatewayCount = 0 | ||
for (let i = 0; i < 3; i++) { | ||
try { | ||
return await doApiRequest<any>({ | ||
hostname: "api.bintray.com", | ||
path: `/content/${this.user}/${this.repo}/${this.packageName}/${version.name}/${fileName}`, | ||
method: "PUT", | ||
headers: { | ||
"User-Agent": "electron-builder", | ||
"Content-Length": fileStat.size, | ||
"X-Bintray-Override": "1", | ||
"X-Bintray-Publish": "1", | ||
} | ||
}, this.auth, uploadFile.bind(this, file, fileStat, fileName)) | ||
} | ||
catch (e) { | ||
if (e instanceof HttpError && e.response.statusCode === 502 && badGatewayCount++ < 3) { | ||
continue | ||
} | ||
|
||
throw e | ||
} | ||
} | ||
} | ||
|
||
//noinspection JSUnusedGlobalSymbols | ||
deleteRelease(): Promise<any> { | ||
if (!this._versionPromise.isFulfilled()) { | ||
return BluebirdPromise.resolve() | ||
} | ||
|
||
const version = this._versionPromise.value() | ||
if (version == null) { | ||
return BluebirdPromise.resolve() | ||
} | ||
|
||
return bintrayRequest<Version>(`/packages/${this.user}/${this.repo}/${this.packageName}/versions/${version.name}`, this.auth, null, "DELETE") | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.