From 6aaf709c9dcf8272861dfd9146a5c2f658398aa1 Mon Sep 17 00:00:00 2001 From: Daniel Stutemann <64223605+dstutemann@users.noreply.github.com> Date: Sat, 17 Feb 2024 16:11:17 +0100 Subject: [PATCH 1/2] Added toggle function, added tests for toggle function --- src/index.coffee | 15 +++++++++- tests/index.coffee | 71 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 85 insertions(+), 1 deletion(-) diff --git a/src/index.coffee b/src/index.coffee index 0d17d57..e743aaf 100644 --- a/src/index.coffee +++ b/src/index.coffee @@ -56,6 +56,19 @@ module.exports = class AutoLaunch isEnabled: => @api.isEnabled @opts.appName, @opts.mac + toggle: (value = undefined) => + return @isEnabled(@opts.appName).then (isEnabled) => + if isEnabled and (value == false or value == undefined) + # If auto launch is enabled and we want to disable it + return @disable(@opts.appName) + else if !isEnabled and (value == true or value == undefined) + # If auto launch is disabled and we want to enable it + return @enable(@opts) + else + # If the value is already what we want, return the current state + return isEnabled + + ### Private ### @@ -115,4 +128,4 @@ module.exports = class AutoLaunch @opts.appName = tempPath[tempPath.length - 1] # Remove ".app" from the appName if it exists if @opts.appName.indexOf('.app', @opts.appName.length - '.app'.length) isnt -1 - @opts.appName = @opts.appName.substr(0, @opts.appName.length - '.app'.length) \ No newline at end of file + @opts.appName = @opts.appName.substr(0, @opts.appName.length - '.app'.length) diff --git a/tests/index.coffee b/tests/index.coffee index 408236d..ff4664a 100644 --- a/tests/index.coffee +++ b/tests/index.coffee @@ -94,6 +94,77 @@ describe 'node-auto-launch', -> autoLaunch.disable().catch done return + describe '.toggle', -> + beforeEach -> + autoLaunchHelper.ensureDisabled() + + it 'should enable auto launch', (done) -> + autoLaunch.toggle(true) + .then () -> + autoLaunch.isEnabled() + .then (enabled) -> + expect(enabled).to.equal true + done() + .catch done + return + + it 'should disable auto launch', (done) -> + before -> + autoLaunchHelper.ensureEnabled() + autoLaunch.toggle(false) + .then () -> + autoLaunch.isEnabled() + .then (enabled) -> + expect(enabled).to.equal false + done() + .catch done + return + + it 'should toggle auto launch', (done) -> + autoLaunch.toggle() + .then () -> + autoLaunch.isEnabled() + .then (enabled) -> + expect(enabled).to.equal true + return autoLaunch.toggle() + .then () -> + autoLaunch.isEnabled() + .then (enabled) -> + expect(enabled).to.equal false + done() + .catch done + return + + it 'should do nothing if already enabled', (done) -> + autoLaunchHelper.ensureEnabled() + autoLaunch.toggle(true) + .then () -> + autoLaunch.isEnabled() + .then (enabled) -> + expect(enabled).to.equal true + done() + .catch done + return + + it 'should do nothing if already disabled', (done) -> + autoLaunchHelper.ensureDisabled() + autoLaunch.toggle(false) + .then () -> + autoLaunch.isEnabled() + .then (enabled) -> + expect(enabled).to.equal false + done() + .catch done + return + + it 'should catch errors', (done) -> + autoLaunchHelper.mockApi + disable: -> + Promise.reject() + + autoLaunch.disable().catch done + return + return unless followsXDG describe 'testing .appName', -> From 95d04d10902006e2c2a280ab641b56333236815e Mon Sep 17 00:00:00 2001 From: Daniel Stutemann <64223605+dstutemann@users.noreply.github.com> Date: Sat, 17 Feb 2024 16:11:39 +0100 Subject: [PATCH 2/2] Added documentation for toggle --- README.md | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/README.md b/README.md index 6e143f4..ba0f563 100644 --- a/README.md +++ b/README.md @@ -91,6 +91,12 @@ Disables your app from auto-launching at startup. Returns a Promise. Returns a Promise which resolves to a Boolean; `true` if your app is set to launch on startup. +### `.toggle(value)` + +**`value`** - (Optional) Boolean + +Toggles the auto-launch to the desired state. If value is not specified it will be toggled to the inverted state. + ## How does it work? ### Linux / FreeBSD