From af0a1339ba1ee68c269691ac9e3aba77a837bad9 Mon Sep 17 00:00:00 2001 From: Thomas Halwax Date: Wed, 20 Sep 2023 13:12:05 +0200 Subject: [PATCH] disable self-update with environment variable --- src/main/environment.js | 12 ++++++++++++ src/main/environment.test.js | 38 ++++++++++++++++++++++++++++++++++++ src/main/main.js | 7 +++++-- 3 files changed, 55 insertions(+), 2 deletions(-) create mode 100644 src/main/environment.js create mode 100644 src/main/environment.test.js diff --git a/src/main/environment.js b/src/main/environment.js new file mode 100644 index 00000000..2f5fa7a3 --- /dev/null +++ b/src/main/environment.js @@ -0,0 +1,12 @@ +/** + * + * @param {String} name The environment variable name to check + * @param {Boolean} defaultValue Can either be true or false. If omitted the default value is always true + * @returns {Boolean} Either the value of the environment variable or the default value + */ +export const isEnabled = (name, defaultValue = true) => { + if (!process.env[name]) return defaultValue + if (process.env[name].toUpperCase() === 'TRUE') return true + if (process.env[name] === '1') return true + return false +} diff --git a/src/main/environment.test.js b/src/main/environment.test.js new file mode 100644 index 00000000..d1a5609f --- /dev/null +++ b/src/main/environment.test.js @@ -0,0 +1,38 @@ +import assert from 'assert' +import { isEnabled } from './environment' + +describe('environment isEnabled', function () { + it('return default=false if not set', function () { + const value = isEnabled('TEST_SELF_UPDATE', false) + assert.equal(value, false) + }) + + it('return default=true if not set', function () { + const value = isEnabled('TEST_SELF_UPDATE', true) + assert.equal(value, true) + }) + + it('return true if value=TRUE', function () { + process.env.TEST_SELF_UPDATE = 'true' + const value = isEnabled('TEST_SELF_UPDATE', false) + assert.equal(value, true) + }) + + it('return true if value=1', function () { + process.env.TEST_SELF_UPDATE = '1' + const value = isEnabled('TEST_SELF_UPDATE', false) + assert.equal(value, true) + }) + + it('return true if value=FALSE', function () { + process.env.TEST_SELF_UPDATE = 'false' + const value = isEnabled('TEST_SELF_UPDATE', true) + assert.equal(value, false) + }) + + it('return true if value=0', function () { + process.env.TEST_SELF_UPDATE = '0' + const value = isEnabled('TEST_SELF_UPDATE', true) + assert.equal(value, false) + }) +}) diff --git a/src/main/main.js b/src/main/main.js index 80ca3b45..f39896e0 100644 --- a/src/main/main.js +++ b/src/main/main.js @@ -13,6 +13,7 @@ import { exportLayer } from './export.js' import { ipc } from './ipc' import * as dotenv from 'dotenv' import SelfUpdate from './SelfUpdate' +import { isEnabled } from './environment' /** * Emitted once, when Electron has finished initializing. @@ -107,8 +108,10 @@ const ready = async () => { await session.restore() await menu.show() - const selfUpdate = new SelfUpdate() - selfUpdate.checkForUpdates() + if (isEnabled('ODIN_SELF_UPDATE', true)) { + const selfUpdate = new SelfUpdate() + selfUpdate.checkForUpdates() + } }