UNPKG

velopack

Version:

Velopack is an installation and auto-update framework for cross-platform applications. It's opinionated, extremely easy to use with zero config needed. With just one command you can be up and running with an installable application, and it's lightning fas

238 lines (237 loc) 9.61 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || (function () { var ownKeys = function(o) { ownKeys = Object.getOwnPropertyNames || function (o) { var ar = []; for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k; return ar; }; return ownKeys(o); }; return function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]); __setModuleDefault(result, mod); return result; }; })(); Object.defineProperty(exports, "__esModule", { value: true }); exports.UpdateManager = exports.VelopackApp = void 0; const addon = __importStar(require("./load")); /** * VelopackApp helps you to handle app activation events correctly. * This should be used as early as possible in your application startup code. * (eg. the beginning of main() or wherever your entry point is) */ class VelopackApp { _hooks = new Map(); _customArgs = null; _customLocator = null; _autoApply = true; /** * Build a new VelopackApp instance. */ static build() { return new VelopackApp(); } /** * WARNING: FastCallback hooks are run during critical stages of Velopack operations. * Your code will be run and then the process will exit. * If your code has not completed within 30 seconds, it will be terminated. * Only supported on windows; On other operating systems, this will never be called. */ onAfterInstallFastCallback(callback) { this._hooks.set("after-install", callback); return this; } /** * WARNING: FastCallback hooks are run during critical stages of Velopack operations. * Your code will be run and then the process will exit. * If your code has not completed within 30 seconds, it will be terminated. * Only supported on windows; On other operating systems, this will never be called. */ onBeforeUninstallFastCallback(callback) { this._hooks.set("before-uninstall", callback); return this; } /** * WARNING: FastCallback hooks are run during critical stages of Velopack operations. * Your code will be run and then the process will exit. * If your code has not completed within 15 seconds, it will be terminated. * Only supported on windows; On other operating systems, this will never be called. */ onBeforeUpdateFastCallback(callback) { this._hooks.set("before-update", callback); return this; } /** * WARNING: FastCallback hooks are run during critical stages of Velopack operations. * Your code will be run and then the process will exit. * If your code has not completed within 15 seconds, it will be terminated. * Only supported on windows; On other operating systems, this will never be called. */ onAfterUpdateFastCallback(callback) { this._hooks.set("after-update", callback); return this; } /** * This hook is triggered when the application is restarted by Velopack after installing updates. */ onRestarted(callback) { this._hooks.set("restarted", callback); return this; } /** * This hook is triggered when the application is started for the first time after installation. */ onFirstRun(callback) { this._hooks.set("first-run", callback); return this; } /** * Override the command line arguments used by VelopackApp. (by default this is env::args().skip(1)) */ setArgs(args) { this._customArgs = args; return this; } /** * VelopackLocator provides some utility functions for locating the current app important paths (eg. path to packages, update binary, and so forth). */ setLocator(locator) { this._customLocator = locator; return this; } /** * Set a callback to receive log messages from VelopackApp. */ setLogger(callback) { addon.js_set_logger_callback(callback); return this; } /** * Set whether to automatically apply downloaded updates on startup. This is ON by default. */ setAutoApplyOnStartup(autoApply) { this._autoApply = autoApply; return this; } /** * Runs the Velopack startup logic. This should be the first thing to run in your app. * In some circumstances it may terminate/restart the process to perform tasks. */ run() { addon.js_appbuilder_run((hook_name, current_version) => { let hook = this._hooks.get(hook_name); if (hook) { hook(current_version); } }, this._customArgs, this._customLocator ? JSON.stringify(this._customLocator) : null, this._autoApply); } } exports.VelopackApp = VelopackApp; /** * Provides functionality for checking for updates, downloading updates, and applying updates to the current application. */ class UpdateManager { opaque; /** * Create a new UpdateManager instance. * @param urlOrPath Location of the update server or path to the local update directory. * @param options Optional extra configuration for update manager. * @param locator Override the default locator configuration (usually used for testing / mocks). */ constructor(urlOrPath, options, locator) { this.opaque = addon.js_new_update_manager(urlOrPath, options ? JSON.stringify(options) : "", locator ? JSON.stringify(locator) : null); } /** * Returns the currently installed version of the app. */ getCurrentVersion() { return addon.js_get_current_version(this.opaque); } /** * Returns the currently installed app id. */ getAppId() { return addon.js_get_app_id(this.opaque); } /** * Returns whether the app is in portable mode. On Windows this can be true or false. * On MacOS and Linux this will always be true. */ isPortable() { return addon.js_is_portable(this.opaque); } /** * Returns an VelopackAsset object if there is an update downloaded which still needs to be applied. * You can pass the VelopackAsset object to waitExitThenApplyUpdate to apply the update. */ getUpdatePendingRestart() { let json = addon.js_update_pending_restart(this.opaque); if (json && json.length > 0) { return JSON.parse(json); } return null; } /** * Checks for updates, returning None if there are none available. If there are updates available, this method will return an * UpdateInfo object containing the latest available release, and any delta updates that can be applied if they are available. */ checkForUpdatesAsync() { let json = addon.js_check_for_updates_async(this.opaque); return json.then((json) => { if (json && json.length > 0) { return JSON.parse(json); } return null; }); } /** * Downloads the specified updates to the local app packages directory. Progress is reported back to the caller via an optional Sender. * This function will acquire a global update lock so may fail if there is already another update operation in progress. * - If the update contains delta packages and the delta feature is enabled * this method will attempt to unpack and prepare them. * - If there is no delta update available, or there is an error preparing delta * packages, this method will fall back to downloading the full version of the update. */ downloadUpdateAsync(update, progress) { if (!update) { throw new Error("update is required"); } return addon.js_download_update_async(this.opaque, JSON.stringify(update), progress ?? (() => { })); } /** * This will launch the Velopack updater and tell it to wait for this program to exit gracefully. * You should then clean up any state and exit your app. The updater will apply updates and then * optionally restart your app. The updater will only wait for 60 seconds before giving up. */ waitExitThenApplyUpdate(update, silent = false, restart = true, restartArgs = []) { if (!update) { throw new Error("update is required"); } // the backend API only accepts VelopackAsset, so we need to extract it from UpdateInfo if ("TargetFullRelease" in update && typeof update.TargetFullRelease === "object") { update = update.TargetFullRelease; } addon.js_wait_exit_then_apply_update(this.opaque, JSON.stringify(update), silent, restart, restartArgs); } } exports.UpdateManager = UpdateManager;