mirror of
https://github.com/velopack/velopack.git
synced 2025-10-25 15:19:22 +00:00
Add custom log callback and wire up to jest
This commit is contained in:
1
Cargo.lock
generated
1
Cargo.lock
generated
@@ -2215,6 +2215,7 @@ dependencies = [
|
||||
name = "velopack_nodeffi"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"lazy_static",
|
||||
"log",
|
||||
"neon",
|
||||
"semver",
|
||||
|
||||
@@ -15,6 +15,7 @@ serde_json = "1"
|
||||
velopack = { path = "../../../lib-rust" }
|
||||
semver = "1.0"
|
||||
log = "0.4"
|
||||
lazy_static = "1.4"
|
||||
|
||||
[build-dependencies]
|
||||
ts-rs = "9"
|
||||
|
||||
@@ -236,11 +236,16 @@ fn js_appbuilder_run(mut cx: FunctionContext) -> JsResult<JsUndefined> {
|
||||
Ok(undefined)
|
||||
}
|
||||
|
||||
fn js_set_logger_callback(mut cx: FunctionContext) -> JsResult<JsUndefined> {
|
||||
let arg_cb = cx.argument::<JsFunction>(0)?;
|
||||
let cb_root = arg_cb.root(&mut cx);
|
||||
logger::set_logger_callback(Some(cb_root), &mut cx);
|
||||
Ok(cx.undefined())
|
||||
}
|
||||
|
||||
#[neon::main]
|
||||
fn main(mut cx: ModuleContext) -> NeonResult<()> {
|
||||
let mut log_channel = Channel::new(&mut cx);
|
||||
log_channel.unref(&mut cx);
|
||||
logger::init_logger_callback(log_channel);
|
||||
logger::init_logger(&mut cx);
|
||||
|
||||
cx.export_function("js_new_update_manager", js_new_update_manager)?;
|
||||
cx.export_function("js_get_current_version", js_get_current_version)?;
|
||||
@@ -252,5 +257,6 @@ fn main(mut cx: ModuleContext) -> NeonResult<()> {
|
||||
cx.export_function("js_download_update_async", js_download_update_async)?;
|
||||
cx.export_function("js_wait_exit_then_apply_update", js_wait_exit_then_apply_update)?;
|
||||
cx.export_function("js_appbuilder_run", js_appbuilder_run)?;
|
||||
cx.export_function("js_set_logger_callback", js_set_logger_callback)?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
@@ -1,10 +1,20 @@
|
||||
use std::{sync::{Arc, Mutex}, thread::{self, ThreadId}};
|
||||
|
||||
use lazy_static::lazy_static;
|
||||
use log::{Level, Log, Metadata, Record};
|
||||
use neon::{event::Channel, prelude::*};
|
||||
|
||||
struct LoggerImpl {
|
||||
channel: Channel,
|
||||
static LOGGER: LoggerImpl = LoggerImpl {};
|
||||
|
||||
lazy_static! {
|
||||
static ref LOGGER_CB: Arc<Mutex<Option<Root<JsFunction>>>> = Arc::new(Mutex::new(None));
|
||||
static ref LOGGER_CONSOLE_LOG: Arc<Mutex<Option<Root<JsFunction>>>> = Arc::new(Mutex::new(None));
|
||||
static ref LOGGER_CHANNEL: Arc<Mutex<Option<Channel>>> = Arc::new(Mutex::new(None));
|
||||
// static ref LOGGER_MAIN_THREADID: Arc<Mutex<Option<ThreadId>>> = Arc::new(Mutex::new(None));
|
||||
}
|
||||
|
||||
struct LoggerImpl {}
|
||||
|
||||
impl Log for LoggerImpl {
|
||||
fn enabled(&self, metadata: &Metadata) -> bool {
|
||||
metadata.level() <= log::max_level()
|
||||
@@ -25,20 +35,69 @@ impl Log for LoggerImpl {
|
||||
Level::Trace => "trace",
|
||||
};
|
||||
|
||||
self.channel.send(move |mut cx| {
|
||||
let console = cx.global::<JsObject>("console")?;
|
||||
let log_fn: Handle<JsFunction> = console.get(&mut cx, level)?;
|
||||
let args = vec![cx.string(text).upcast()];
|
||||
log_fn.call(&mut cx, console, args.clone())?;
|
||||
if let Ok(channel_opt) = LOGGER_CHANNEL.lock() {
|
||||
if channel_opt.is_some() {
|
||||
let channel = channel_opt.as_ref().unwrap();
|
||||
|
||||
channel.send(move |mut cx| {
|
||||
// If custom callback exists, then use that.
|
||||
if let Ok(cb_lock) = LOGGER_CB.lock() {
|
||||
if let Some(cb) = &*cb_lock {
|
||||
let undefined = cx.undefined();
|
||||
let args = vec![cx.string(level).upcast(), cx.string(text).upcast()];
|
||||
cb.to_inner(&mut cx).call(&mut cx, undefined, args)?;
|
||||
return Ok(());
|
||||
}
|
||||
}
|
||||
|
||||
// If no custom callback, then use console.log
|
||||
if let Ok(console) = LOGGER_CONSOLE_LOG.lock() {
|
||||
if let Some(cb) = &*console {
|
||||
let undefined = cx.undefined();
|
||||
let args = vec![cx.string(level).upcast(), cx.string(text).upcast()];
|
||||
cb.to_inner(&mut cx).call(&mut cx, undefined, args)?;
|
||||
return Ok(());
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn flush(&self) {}
|
||||
}
|
||||
|
||||
pub fn init_logger_callback(channel: Channel) {
|
||||
let logger = LoggerImpl { channel };
|
||||
let _ = log::set_boxed_logger(Box::new(logger));
|
||||
pub fn init_logger(cx: &mut ModuleContext) {
|
||||
let _ = log::set_logger(&LOGGER);
|
||||
log::set_max_level(log::LevelFilter::Trace);
|
||||
|
||||
// if let Ok(mut ch) = LOGGER_MAIN_THREADID.lock() {
|
||||
// *ch = Some(thread::current().id());
|
||||
// }
|
||||
|
||||
if let Ok(mut ch) = LOGGER_CONSOLE_LOG.lock() {
|
||||
if let Ok(console) = cx.global::<JsObject>("console") {
|
||||
if let Ok(log_fn) = console.get::<JsFunction, ModuleContext, &str>(cx, "log") {
|
||||
*ch = Some(log_fn.root(cx));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if let Ok(mut ch) = LOGGER_CHANNEL.lock() {
|
||||
// let mut log_channel = Channel::new(cx);
|
||||
let mut log_channel = cx.channel();
|
||||
log_channel.unref(cx); // Unref the channel so that the event loop can exit while this channel is open
|
||||
*ch = Some(log_channel);
|
||||
}
|
||||
}
|
||||
|
||||
pub fn set_logger_callback(callback: Option<Root<JsFunction>>, cx: &mut FunctionContext) {
|
||||
if let Ok(mut cb_lock) = LOGGER_CB.lock() {
|
||||
let cb_taken = cb_lock.take();
|
||||
if let Some(cb_exist) = cb_taken {
|
||||
cb_exist.drop(cx);
|
||||
}
|
||||
*cb_lock = callback;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,7 +0,0 @@
|
||||
/** @type {import('ts-jest').JestConfigWithTsJest} **/
|
||||
module.exports = {
|
||||
testEnvironment: "node",
|
||||
transform: {
|
||||
"^.+.ts$": ["ts-jest", {}],
|
||||
},
|
||||
};
|
||||
20
src/lib-nodejs/jest.config.ts
Normal file
20
src/lib-nodejs/jest.config.ts
Normal file
@@ -0,0 +1,20 @@
|
||||
import type { Config } from "jest";
|
||||
|
||||
const config: Config = {
|
||||
verbose: true,
|
||||
testEnvironment: "node",
|
||||
transform: {
|
||||
"^.+.ts$": ["ts-jest", {}],
|
||||
},
|
||||
reporters: ["default", ["github-actions", { silent: true }], "summary"],
|
||||
};
|
||||
|
||||
export default config;
|
||||
|
||||
// /** @type {import('ts-jest').JestConfigWithTsJest} **/
|
||||
// module.exports = {
|
||||
// testEnvironment: "node",
|
||||
// transform: {
|
||||
// "^.+.ts$": ["ts-jest", {}],
|
||||
// },
|
||||
// };
|
||||
150
src/lib-nodejs/package-lock.json
generated
150
src/lib-nodejs/package-lock.json
generated
@@ -21,6 +21,7 @@
|
||||
"jest": "^29.7.0",
|
||||
"prettier": "3.3.3",
|
||||
"ts-jest": "^29.2.5",
|
||||
"ts-node": "^10.9.2",
|
||||
"typescript": "^5.3.3"
|
||||
},
|
||||
"optionalDependencies": {
|
||||
@@ -647,6 +648,28 @@
|
||||
"win32"
|
||||
]
|
||||
},
|
||||
"node_modules/@cspotcode/source-map-support": {
|
||||
"version": "0.8.1",
|
||||
"resolved": "https://registry.npmjs.org/@cspotcode/source-map-support/-/source-map-support-0.8.1.tgz",
|
||||
"integrity": "sha512-IchNf6dN4tHoMFIn/7OE8LWZ19Y6q/67Bmf6vnGREv8RSbBVb9LPJxEcnwrcwX6ixSvaiGoomAUvu4YSxXrVgw==",
|
||||
"dev": true,
|
||||
"dependencies": {
|
||||
"@jridgewell/trace-mapping": "0.3.9"
|
||||
},
|
||||
"engines": {
|
||||
"node": ">=12"
|
||||
}
|
||||
},
|
||||
"node_modules/@cspotcode/source-map-support/node_modules/@jridgewell/trace-mapping": {
|
||||
"version": "0.3.9",
|
||||
"resolved": "https://registry.npmjs.org/@jridgewell/trace-mapping/-/trace-mapping-0.3.9.tgz",
|
||||
"integrity": "sha512-3Belt6tdc8bPgAtbcmdtNJlirVoTmEb5e2gC94PnkwEW9jI6CAHUeoG85tjWP5WquqfavoMtMwiG4P926ZKKuQ==",
|
||||
"dev": true,
|
||||
"dependencies": {
|
||||
"@jridgewell/resolve-uri": "^3.0.3",
|
||||
"@jridgewell/sourcemap-codec": "^1.4.10"
|
||||
}
|
||||
},
|
||||
"node_modules/@istanbuljs/load-nyc-config": {
|
||||
"version": "1.1.0",
|
||||
"resolved": "https://registry.npmjs.org/@istanbuljs/load-nyc-config/-/load-nyc-config-1.1.0.tgz",
|
||||
@@ -1536,6 +1559,30 @@
|
||||
"@sinonjs/commons": "^3.0.0"
|
||||
}
|
||||
},
|
||||
"node_modules/@tsconfig/node10": {
|
||||
"version": "1.0.11",
|
||||
"resolved": "https://registry.npmjs.org/@tsconfig/node10/-/node10-1.0.11.tgz",
|
||||
"integrity": "sha512-DcRjDCujK/kCk/cUe8Xz8ZSpm8mS3mNNpta+jGCA6USEDfktlNvm1+IuZ9eTcDbNk41BHwpHHeW+N1lKCz4zOw==",
|
||||
"dev": true
|
||||
},
|
||||
"node_modules/@tsconfig/node12": {
|
||||
"version": "1.0.11",
|
||||
"resolved": "https://registry.npmjs.org/@tsconfig/node12/-/node12-1.0.11.tgz",
|
||||
"integrity": "sha512-cqefuRsh12pWyGsIoBKJA9luFu3mRxCA+ORZvA4ktLSzIuCUtWVxGIuXigEwO5/ywWFMZ2QEGKWvkZG1zDMTag==",
|
||||
"dev": true
|
||||
},
|
||||
"node_modules/@tsconfig/node14": {
|
||||
"version": "1.0.3",
|
||||
"resolved": "https://registry.npmjs.org/@tsconfig/node14/-/node14-1.0.3.tgz",
|
||||
"integrity": "sha512-ysT8mhdixWK6Hw3i1V2AeRqZ5WfXg1G43mqoYlM2nc6388Fq5jcXyr5mRsqViLx/GJYdoL0bfXD8nmF+Zn/Iow==",
|
||||
"dev": true
|
||||
},
|
||||
"node_modules/@tsconfig/node16": {
|
||||
"version": "1.0.4",
|
||||
"resolved": "https://registry.npmjs.org/@tsconfig/node16/-/node16-1.0.4.tgz",
|
||||
"integrity": "sha512-vxhUy4J8lyeyinH7Azl1pdd43GJhZH/tP2weN8TntQblOY+A0XbT8DJk1/oCPuOOyg/Ja757rG0CgHcWC8OfMA==",
|
||||
"dev": true
|
||||
},
|
||||
"node_modules/@tsconfig/node20": {
|
||||
"version": "20.1.4",
|
||||
"resolved": "https://registry.npmjs.org/@tsconfig/node20/-/node20-20.1.4.tgz",
|
||||
@@ -1668,6 +1715,30 @@
|
||||
"integrity": "sha512-I4q9QU9MQv4oEOz4tAHJtNz1cwuLxn2F3xcc2iV5WdqLPpUnj30aUuxt1mAxYTG+oe8CZMV/+6rU4S4gRDzqtQ==",
|
||||
"dev": true
|
||||
},
|
||||
"node_modules/acorn": {
|
||||
"version": "8.12.1",
|
||||
"resolved": "https://registry.npmjs.org/acorn/-/acorn-8.12.1.tgz",
|
||||
"integrity": "sha512-tcpGyI9zbizT9JbV6oYE477V6mTlXvvi0T0G3SNIYE2apm/G5huBa1+K89VGeovbg+jycCrfhl3ADxErOuO6Jg==",
|
||||
"dev": true,
|
||||
"bin": {
|
||||
"acorn": "bin/acorn"
|
||||
},
|
||||
"engines": {
|
||||
"node": ">=0.4.0"
|
||||
}
|
||||
},
|
||||
"node_modules/acorn-walk": {
|
||||
"version": "8.3.3",
|
||||
"resolved": "https://registry.npmjs.org/acorn-walk/-/acorn-walk-8.3.3.tgz",
|
||||
"integrity": "sha512-MxXdReSRhGO7VlFe1bRG/oI7/mdLV9B9JJT0N8vZOhF7gFRR5l3M8W9G8JxmKV+JC5mGqJ0QvqfSOLsCPa4nUw==",
|
||||
"dev": true,
|
||||
"dependencies": {
|
||||
"acorn": "^8.11.0"
|
||||
},
|
||||
"engines": {
|
||||
"node": ">=0.4.0"
|
||||
}
|
||||
},
|
||||
"node_modules/aggregate-error": {
|
||||
"version": "4.0.1",
|
||||
"resolved": "https://registry.npmjs.org/aggregate-error/-/aggregate-error-4.0.1.tgz",
|
||||
@@ -1745,6 +1816,12 @@
|
||||
"node": ">= 8"
|
||||
}
|
||||
},
|
||||
"node_modules/arg": {
|
||||
"version": "4.1.3",
|
||||
"resolved": "https://registry.npmjs.org/arg/-/arg-4.1.3.tgz",
|
||||
"integrity": "sha512-58S9QDqG0Xx27YwPSt9fJxivjYl432YCwfDMfZ+71RAqUrZef7LrKQZ3LHLOwCS4FLNBplP533Zx895SeOCHvA==",
|
||||
"dev": true
|
||||
},
|
||||
"node_modules/argparse": {
|
||||
"version": "1.0.10",
|
||||
"resolved": "https://registry.npmjs.org/argparse/-/argparse-1.0.10.tgz",
|
||||
@@ -2329,6 +2406,12 @@
|
||||
"node": ">=8"
|
||||
}
|
||||
},
|
||||
"node_modules/create-require": {
|
||||
"version": "1.1.1",
|
||||
"resolved": "https://registry.npmjs.org/create-require/-/create-require-1.1.1.tgz",
|
||||
"integrity": "sha512-dcKFX3jn0MpIaXjisoRvexIJVEKzaq7z2rZKxf+MSr9TkdmHmsU4m2lcLojrj/FHl8mk5VxMmYA+ftRkP/3oKQ==",
|
||||
"dev": true
|
||||
},
|
||||
"node_modules/cross-env": {
|
||||
"version": "7.0.3",
|
||||
"resolved": "https://registry.npmjs.org/cross-env/-/cross-env-7.0.3.tgz",
|
||||
@@ -2498,6 +2581,15 @@
|
||||
"node": ">=8"
|
||||
}
|
||||
},
|
||||
"node_modules/diff": {
|
||||
"version": "4.0.2",
|
||||
"resolved": "https://registry.npmjs.org/diff/-/diff-4.0.2.tgz",
|
||||
"integrity": "sha512-58lmxKSA4BNyLz+HHMUzlOEpg09FV+ev6ZMe3vJihgdxzgcwZ8VoEEPmALCZG9LmqfVoNMMKpttIYTVG6uDY7A==",
|
||||
"dev": true,
|
||||
"engines": {
|
||||
"node": ">=0.3.1"
|
||||
}
|
||||
},
|
||||
"node_modules/diff-sequences": {
|
||||
"version": "29.6.3",
|
||||
"resolved": "https://registry.npmjs.org/diff-sequences/-/diff-sequences-29.6.3.tgz",
|
||||
@@ -6038,6 +6130,49 @@
|
||||
"node": ">=12"
|
||||
}
|
||||
},
|
||||
"node_modules/ts-node": {
|
||||
"version": "10.9.2",
|
||||
"resolved": "https://registry.npmjs.org/ts-node/-/ts-node-10.9.2.tgz",
|
||||
"integrity": "sha512-f0FFpIdcHgn8zcPSbf1dRevwt047YMnaiJM3u2w2RewrB+fob/zePZcrOyQoLMMO7aBIddLcQIEK5dYjkLnGrQ==",
|
||||
"dev": true,
|
||||
"dependencies": {
|
||||
"@cspotcode/source-map-support": "^0.8.0",
|
||||
"@tsconfig/node10": "^1.0.7",
|
||||
"@tsconfig/node12": "^1.0.7",
|
||||
"@tsconfig/node14": "^1.0.0",
|
||||
"@tsconfig/node16": "^1.0.2",
|
||||
"acorn": "^8.4.1",
|
||||
"acorn-walk": "^8.1.1",
|
||||
"arg": "^4.1.0",
|
||||
"create-require": "^1.1.0",
|
||||
"diff": "^4.0.1",
|
||||
"make-error": "^1.1.1",
|
||||
"v8-compile-cache-lib": "^3.0.1",
|
||||
"yn": "3.1.1"
|
||||
},
|
||||
"bin": {
|
||||
"ts-node": "dist/bin.js",
|
||||
"ts-node-cwd": "dist/bin-cwd.js",
|
||||
"ts-node-esm": "dist/bin-esm.js",
|
||||
"ts-node-script": "dist/bin-script.js",
|
||||
"ts-node-transpile-only": "dist/bin-transpile.js",
|
||||
"ts-script": "dist/bin-script-deprecated.js"
|
||||
},
|
||||
"peerDependencies": {
|
||||
"@swc/core": ">=1.2.50",
|
||||
"@swc/wasm": ">=1.2.50",
|
||||
"@types/node": "*",
|
||||
"typescript": ">=2.7"
|
||||
},
|
||||
"peerDependenciesMeta": {
|
||||
"@swc/core": {
|
||||
"optional": true
|
||||
},
|
||||
"@swc/wasm": {
|
||||
"optional": true
|
||||
}
|
||||
}
|
||||
},
|
||||
"node_modules/type-detect": {
|
||||
"version": "4.0.8",
|
||||
"resolved": "https://registry.npmjs.org/type-detect/-/type-detect-4.0.8.tgz",
|
||||
@@ -6108,6 +6243,12 @@
|
||||
"browserslist": ">= 4.21.0"
|
||||
}
|
||||
},
|
||||
"node_modules/v8-compile-cache-lib": {
|
||||
"version": "3.0.1",
|
||||
"resolved": "https://registry.npmjs.org/v8-compile-cache-lib/-/v8-compile-cache-lib-3.0.1.tgz",
|
||||
"integrity": "sha512-wa7YjyUGfNZngI/vtK0UHAN+lgDCxBPCylVXGp0zu59Fz5aiGtNXaq3DhIov063MorB+VfufLh3JlF2KdTK3xg==",
|
||||
"dev": true
|
||||
},
|
||||
"node_modules/v8-to-istanbul": {
|
||||
"version": "9.3.0",
|
||||
"resolved": "https://registry.npmjs.org/v8-to-istanbul/-/v8-to-istanbul-9.3.0.tgz",
|
||||
@@ -6276,6 +6417,15 @@
|
||||
"node": ">=12"
|
||||
}
|
||||
},
|
||||
"node_modules/yn": {
|
||||
"version": "3.1.1",
|
||||
"resolved": "https://registry.npmjs.org/yn/-/yn-3.1.1.tgz",
|
||||
"integrity": "sha512-Ux4ygGWsu2c7isFWe8Yu1YluJmqVhxqK2cLXNQA5AcC3QfbGNpM7fu0Y8b/z16pXLnFxZYvWhd3fhBY9DLmC6Q==",
|
||||
"dev": true,
|
||||
"engines": {
|
||||
"node": ">=6"
|
||||
}
|
||||
},
|
||||
"node_modules/yocto-queue": {
|
||||
"version": "0.1.0",
|
||||
"resolved": "https://registry.npmjs.org/yocto-queue/-/yocto-queue-0.1.0.tgz",
|
||||
|
||||
@@ -4,7 +4,7 @@
|
||||
"description": "",
|
||||
"main": "./lib/index.cjs",
|
||||
"scripts": {
|
||||
"test": "npm run debug && cross-env VELOPACK_RESTART=true VELOPACK_DEBUG=true jest --silent=false",
|
||||
"test": "npm run debug && cross-env VELOPACK_RESTART=true VELOPACK_DEBUG=true jest",
|
||||
"debug": "tsc -p tsconfig.build.json && cargo build && neon dist -f ../../target/debug/velopack_nodeffi.dll && npm run lint",
|
||||
"build": "tsc -p tsconfig.build.json && cargo build --release && neon dist -f ../../target/release/velopack_nodeffi.dll",
|
||||
"clean": "cargo clean && del-cli platforms/**/index.node index.node lib",
|
||||
@@ -44,6 +44,7 @@
|
||||
"jest": "^29.7.0",
|
||||
"prettier": "3.3.3",
|
||||
"ts-jest": "^29.2.5",
|
||||
"ts-node": "^10.9.2",
|
||||
"typescript": "^5.3.3"
|
||||
},
|
||||
"dependencies": {
|
||||
|
||||
@@ -43,6 +43,10 @@ declare module "./load" {
|
||||
customArgs: string[] | null,
|
||||
locator: string | null,
|
||||
): void;
|
||||
|
||||
function js_set_logger_callback(
|
||||
cb: (loglevel: LogLevel, msg: string) => void,
|
||||
): void;
|
||||
}
|
||||
|
||||
type VelopackHookType =
|
||||
@@ -55,6 +59,8 @@ type VelopackHookType =
|
||||
|
||||
type VelopackHook = (version: string) => void;
|
||||
|
||||
type LogLevel = "info" | "warn" | "error" | "debug" | "trace";
|
||||
|
||||
export class VelopackApp {
|
||||
private _hooks = new Map<VelopackHookType, VelopackHook>();
|
||||
private _customArgs: string[] | null = null;
|
||||
@@ -140,6 +146,14 @@ export class VelopackApp {
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set a custom logger callback to receive log messages from Velopack. The default behavior is to log to console.log.
|
||||
*/
|
||||
setLogger(callback: (loglevel: LogLevel, msg: string) => void): VelopackApp {
|
||||
addon.js_set_logger_callback(callback);
|
||||
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.
|
||||
|
||||
@@ -1,10 +1,20 @@
|
||||
import { copyFileSync, existsSync } from "fs";
|
||||
import { UpdateManager, UpdateOptions, VelopackLocator } from "../src";
|
||||
import {
|
||||
UpdateManager,
|
||||
UpdateOptions,
|
||||
VelopackApp,
|
||||
VelopackLocator,
|
||||
} from "../src";
|
||||
import path from "path";
|
||||
import { tempd3, fixture, updateExe } from "./helper";
|
||||
import { tempd3, fixture, updateExe, shortDelay } from "./helper";
|
||||
|
||||
test("UpdateManager detects local update", () => {
|
||||
return tempd3(async (tmpDir, packagesDir, rootDir) => {
|
||||
VelopackApp.build()
|
||||
.setLogger((level, msg) => {
|
||||
console.log(level, msg);
|
||||
})
|
||||
.run();
|
||||
const locator: VelopackLocator = {
|
||||
ManifestPath: "../../test/fixtures/Test.Squirrel-App.nuspec",
|
||||
PackagesDir: packagesDir,
|
||||
@@ -30,12 +40,17 @@ test("UpdateManager detects local update", () => {
|
||||
expect(update?.TargetFullRelease?.FileName).toBe(
|
||||
"AvaloniaCrossPlat-1.0.11-full.nupkg",
|
||||
);
|
||||
await shortDelay();
|
||||
});
|
||||
});
|
||||
|
||||
test("UpdateManager downloads full update", () => {
|
||||
return tempd3(async (feedDir, packagesDir, rootDir) => {
|
||||
|
||||
VelopackApp.build()
|
||||
.setLogger((level, msg) => {
|
||||
console.log(level, msg);
|
||||
})
|
||||
.run();
|
||||
const locator: VelopackLocator = {
|
||||
ManifestPath: "../../test/fixtures/Test.Squirrel-App.nuspec",
|
||||
PackagesDir: packagesDir,
|
||||
@@ -60,10 +75,15 @@ test("UpdateManager downloads full update", () => {
|
||||
);
|
||||
|
||||
const update = await um.checkForUpdatesAsync();
|
||||
await um.downloadUpdateAsync(update!, () => { });
|
||||
|
||||
console.log(
|
||||
`about to download update from ${feedDir} to ${packagesDir} ...`,
|
||||
);
|
||||
await um.downloadUpdateAsync(update!, () => {});
|
||||
|
||||
expect(
|
||||
existsSync(path.join(packagesDir, "AvaloniaCrossPlat-1.0.11-full.nupkg")),
|
||||
).toBe(true);
|
||||
await shortDelay();
|
||||
});
|
||||
});
|
||||
|
||||
@@ -23,41 +23,41 @@ export function makeId(length: number): string {
|
||||
return result;
|
||||
}
|
||||
|
||||
export function tempd1<T>(cb: (dir: string) => T): T {
|
||||
const id = makeId(10);
|
||||
const dir = path.join(os.tmpdir(), id);
|
||||
fs.mkdirSync(dir);
|
||||
try {
|
||||
return cb(dir);
|
||||
} finally {
|
||||
fs.rmSync(dir, { recursive: true });
|
||||
}
|
||||
}
|
||||
// export function tempd1<T>(cb: (dir: string) => T): T {
|
||||
// const id = makeId(10);
|
||||
// const dir = path.join(os.tmpdir(), id);
|
||||
// fs.mkdirSync(dir);
|
||||
// try {
|
||||
// return cb(dir);
|
||||
// } finally {
|
||||
// fs.rmSync(dir, { recursive: true });
|
||||
// }
|
||||
// }
|
||||
|
||||
export function tempd2<T>(cb: (dir1: string, dir2: string) => T): T {
|
||||
const dir1 = path.join(os.tmpdir(), makeId(10));
|
||||
const dir2 = path.join(os.tmpdir(), makeId(10));
|
||||
fs.mkdirSync(dir1);
|
||||
fs.mkdirSync(dir2);
|
||||
try {
|
||||
return cb(dir1, dir2);
|
||||
} finally {
|
||||
fs.rmSync(dir1, { recursive: true });
|
||||
fs.rmSync(dir2, { recursive: true });
|
||||
}
|
||||
}
|
||||
// export function tempd2<T>(cb: (dir1: string, dir2: string) => T): T {
|
||||
// const dir1 = path.join(os.tmpdir(), makeId(10));
|
||||
// const dir2 = path.join(os.tmpdir(), makeId(10));
|
||||
// fs.mkdirSync(dir1);
|
||||
// fs.mkdirSync(dir2);
|
||||
// try {
|
||||
// return cb(dir1, dir2);
|
||||
// } finally {
|
||||
// fs.rmSync(dir1, { recursive: true });
|
||||
// fs.rmSync(dir2, { recursive: true });
|
||||
// }
|
||||
// }
|
||||
|
||||
export function tempd3<T>(
|
||||
export async function tempd3<T>(
|
||||
cb: (dir1: string, dir2: string, dir3: string) => T,
|
||||
): T {
|
||||
const dir1 = path.join(os.tmpdir(), makeId(10));
|
||||
const dir2 = path.join(os.tmpdir(), makeId(10));
|
||||
const dir3 = path.join(os.tmpdir(), makeId(10));
|
||||
): Promise<T> {
|
||||
const dir1 = path.join(os.tmpdir(), makeId(16));
|
||||
const dir2 = path.join(os.tmpdir(), makeId(16));
|
||||
const dir3 = path.join(os.tmpdir(), makeId(16));
|
||||
fs.mkdirSync(dir1);
|
||||
fs.mkdirSync(dir2);
|
||||
fs.mkdirSync(dir3);
|
||||
try {
|
||||
return cb(dir1, dir2, dir3);
|
||||
return await cb(dir1, dir2, dir3);
|
||||
} finally {
|
||||
fs.rmSync(dir1, { recursive: true });
|
||||
fs.rmSync(dir2, { recursive: true });
|
||||
@@ -86,6 +86,10 @@ export function updateExe(): string {
|
||||
throw new Error("Update.exe not found");
|
||||
}
|
||||
|
||||
export function shortDelay(): Promise<void> {
|
||||
return new Promise((resolve) => setTimeout(resolve, 100));
|
||||
}
|
||||
|
||||
// export function copyUpdateExeTo(dir: string, filename?: string): string {
|
||||
// const exe = updateExe();
|
||||
// const dest = path.join(dir, filename ?? path.basename(exe));
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
import { VelopackApp, VelopackLocator } from "../src/index";
|
||||
import { shortDelay } from "./helper";
|
||||
|
||||
class HookTester {
|
||||
public afterInstall = false;
|
||||
@@ -36,11 +37,14 @@ class HookTester {
|
||||
tester.firstRun = true;
|
||||
tester.version = ver;
|
||||
});
|
||||
builder.setLogger((level, msg) => {
|
||||
console.log(level, msg);
|
||||
});
|
||||
return [builder, tester];
|
||||
}
|
||||
}
|
||||
|
||||
test("VelopackApp should handle restarted event", () => {
|
||||
test("VelopackApp should handle restarted event", async () => {
|
||||
let [builder, tester] = HookTester.build();
|
||||
let locator: VelopackLocator = {
|
||||
ManifestPath: "../../test/fixtures/Test.Squirrel-App.nuspec",
|
||||
@@ -57,9 +61,10 @@ test("VelopackApp should handle restarted event", () => {
|
||||
expect(tester.restarted).toBe(true);
|
||||
expect(tester.firstRun).toBe(false);
|
||||
expect(tester.version).toBe("1.0.0");
|
||||
await shortDelay();
|
||||
});
|
||||
|
||||
test("VelopackApp should handle after-install hook", () => {
|
||||
test("VelopackApp should handle after-install hook", async () => {
|
||||
let [builder, tester] = HookTester.build();
|
||||
builder.setArgs(["--veloapp-install", "1.2.3-test.4"]).run();
|
||||
|
||||
@@ -70,9 +75,10 @@ test("VelopackApp should handle after-install hook", () => {
|
||||
expect(tester.restarted).toBe(false);
|
||||
expect(tester.firstRun).toBe(false);
|
||||
expect(tester.version).toBe("1.2.3-test.4");
|
||||
await shortDelay();
|
||||
});
|
||||
|
||||
test("VelopackApp should handle before-uninstall hook", () => {
|
||||
test("VelopackApp should handle before-uninstall hook", async () => {
|
||||
let [builder, tester] = HookTester.build();
|
||||
builder.setArgs(["--veloapp-uninstall", "1.2.3-test"]).run();
|
||||
|
||||
@@ -83,9 +89,10 @@ test("VelopackApp should handle before-uninstall hook", () => {
|
||||
expect(tester.restarted).toBe(false);
|
||||
expect(tester.firstRun).toBe(false);
|
||||
expect(tester.version).toBe("1.2.3-test");
|
||||
await shortDelay();
|
||||
});
|
||||
|
||||
test("VelopackApp should handle after-update hook", () => {
|
||||
test("VelopackApp should handle after-update hook", async () => {
|
||||
let [builder, tester] = HookTester.build();
|
||||
builder.setArgs(["--veloapp-updated", "1.2.3"]).run();
|
||||
|
||||
@@ -96,9 +103,10 @@ test("VelopackApp should handle after-update hook", () => {
|
||||
expect(tester.restarted).toBe(false);
|
||||
expect(tester.firstRun).toBe(false);
|
||||
expect(tester.version).toBe("1.2.3");
|
||||
await shortDelay();
|
||||
});
|
||||
|
||||
test("VelopackApp should handle before-update hook", () => {
|
||||
test("VelopackApp should handle before-update hook", async () => {
|
||||
let [builder, tester] = HookTester.build();
|
||||
builder.setArgs(["--veloapp-obsolete", "1.2.3-test.4"]).run();
|
||||
|
||||
@@ -109,4 +117,5 @@ test("VelopackApp should handle before-update hook", () => {
|
||||
expect(tester.restarted).toBe(false);
|
||||
expect(tester.firstRun).toBe(false);
|
||||
expect(tester.version).toBe("1.2.3-test.4");
|
||||
await shortDelay();
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user