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

add a fork command #187

Merged
merged 1 commit into from
Mar 5, 2016
Merged
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
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,9 @@ View your online backup using the following command:
Check the latest backup is applied:
* `sync-settings:check-backup`

You can also fork existing settings from a different GitHub user using the following command:
* `sync-settings:fork`
* In the following input field enter the Gist ID to fork

## Running the tests

Expand Down
40 changes: 40 additions & 0 deletions lib/fork-gistid-input-view.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
{CompositeDisposable} = require 'atom'
{$, TextEditorView, View} = require 'atom-space-pen-views'

oldView = null

module.exports =
class ForkGistIdInputView extends View
@content: ->
@div class: 'command-palette', =>
@subview 'selectEditor', new TextEditorView(mini: true, placeholderText: 'Gist ID to fork')

initialize: ->
oldView?.destroy()
oldView = this

@disposables = new CompositeDisposable
@disposables.add atom.commands.add 'atom-text-editor', 'core:confirm', => @confirm()
@disposables.add atom.commands.add 'atom-text-editor', 'core:cancel', => @destroy()
@attach()

destroy: ->
@disposables.dispose()
@detach()

attach: ->
@panel ?= atom.workspace.addModalPanel(item: this)
@panel.show()
@selectEditor.focus()

detach: ->
@panel.destroy()
super

confirm: ->
gistId = @selectEditor.getText()
@callbackInstance.forkGistId(gistId)
@destroy()

setCallbackInstance: (callbackInstance) ->
@callbackInstance = callbackInstance
31 changes: 31 additions & 0 deletions lib/sync-settings.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
fs = require 'fs'
_ = require 'underscore-plus'
[GitHubApi, PackageManager, Tracker] = []
ForkGistIdInputView = null

# constants
DESCRIPTION = 'Atom configuration storage operated by http://atom.io/packages/sync-settings'
Expand Down Expand Up @@ -31,6 +32,8 @@ SyncSettings =
atom.commands.add 'atom-workspace', "sync-settings:check-backup", =>
@checkForUpdate()
@tracker.track 'Check backup'
atom.commands.add 'atom-workspace', "sync-settings:fork", =>
@inputForkGistId()

mandatorySettingsApplied = @checkMandatorySettings()
@checkForUpdate() if atom.config.get('sync-settings.checkForUpdatedBackup') and mandatorySettingsApplied
Expand All @@ -40,6 +43,7 @@ SyncSettings =
@tracker.trackActivate()

deactivate: ->
@inputView?.destroy()
@tracker.trackDeactivate()

serialize: ->
Expand Down Expand Up @@ -274,4 +278,31 @@ SyncSettings =
console.error "Error reading file #{filePath}. Probably doesn't exist.", e
null

inputForkGistId: ->
ForkGistIdInputView ?= require './fork-gistid-input-view'
@inputView = new ForkGistIdInputView()
@inputView.setCallbackInstance(this)

forkGistId: (forkId) ->
@tracker.track 'Fork'
@createClient().gists.fork
id: forkId
, (err, res) =>
if err
try
message = JSON.parse(err.message).message
message = "Gist ID Not Found" if message is "Not Found"
catch SyntaxError
message = err.message
atom.notifications.addError "sync-settings: Error forking settings. ("+message+")"
return cb?()

if res.id
atom.config.set "sync-settings.gistId", res.id
atom.notifications.addSuccess "sync-settings: Forked successfully to the new Gist ID " + res.id + " which has been saved to your config."
else
atom.notifications.addError "sync-settings: Error forking settings"

cb?()

module.exports = SyncSettings
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
},
"dependencies": {
"analytics-node": "^1.2.2",
"atom-space-pen-views": "^2.2.0",
"emissary": "1.x",
"github4": "^0.4.0",
"loophole": "^1.0.0",
Expand Down