Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature 21 add toggle method #121

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
15 changes: 14 additions & 1 deletion src/index.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -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 ###


Expand Down Expand Up @@ -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)
@opts.appName = @opts.appName.substr(0, @opts.appName.length - '.app'.length)
71 changes: 71 additions & 0 deletions tests/index.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -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', ->
Expand Down