forked from yongkangchen/remote-sync
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.coffee
118 lines (93 loc) · 3.57 KB
/
index.coffee
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
CompositeDisposable = null
path = null
$ = null
getEventPath = (e)->
$ ?= require('atom-space-pen-views').$
target = $(e.target).closest('.file, .directory, .tab')[0]
target ?= atom.workspace.getActiveTextEditor()
fullPath = target?.getPath()
return [] unless fullPath
[projectPath, relativePath] = atom.project.relativizePath(fullPath)
return [projectPath, fullPath]
projectDict = null
disposables = null
RemoteSync = null
initProject = (projectPaths)->
disposes = []
for projectPath of projectDict
disposes.push projectPath if projectPaths.indexOf(projectPath) == -1
for projectPath in disposes
projectDict[projectPath].dispose()
delete projectDict[projectPath]
for projectPath in projectPaths
RemoteSync ?= require "./lib/RemoteSync"
projectDict[projectPath] ?= RemoteSync.create(projectPath)
handleEvent = (e, cmd)->
[projectPath, fullPath] = getEventPath(e)
return unless projectPath
projectObj = projectDict[projectPath]
projectObj[cmd]?(fullPath)
reload = (projectPath)->
projectDict[projectPath]?.dispose()
projectDict[projectPath] = RemoteSync.create(projectPath)
configure = (e)->
[projectPath] = getEventPath(e)
return unless projectPath
RemoteSync ?= require "./lib/RemoteSync"
RemoteSync.configure projectPath, -> reload(projectPath)
module.exports =
config:
logToConsole:
type: 'boolean'
default: false
title: 'Log to console'
description: 'Log messages to the console instead of the status view at the bottom of the window'
difftoolCommand:
type: 'string'
default: ''
title: 'Diff tool command'
description: 'The command to run for your diff tool'
configFileName:
type: 'string'
default: '.remote-sync.json'
activate: (state) ->
projectDict = {}
initProject(atom.project.getPaths())
CompositeDisposable ?= require('atom').CompositeDisposable
disposables = new CompositeDisposable
disposables.add atom.commands.add('atom-workspace', {
'remote-sync:upload-folder': (e)-> handleEvent(e, "uploadFolder")
'remote-sync:upload-file': (e)-> handleEvent(e, "uploadFile")
'remote-sync:download-file': (e)-> handleEvent(e, "downloadFile")
'remote-sync:download-folder': (e)-> handleEvent(e, "downloadFolder")
'remote-sync:diff-file': (e)-> handleEvent(e, "diffFile")
'remote-sync:diff-folder': (e)-> handleEvent(e, "diffFolder")
'remote-sync:upload-git-change': (e)-> handleEvent(e, "uploadGitChange")
'remote-sync:configure': configure
})
disposables.add atom.project.onDidChangePaths (projectPaths)->
initProject(projectPaths)
disposables.add atom.workspace.observeTextEditors (editor) ->
onDidSave = editor.onDidSave (e) ->
fullPath = e.path
[projectPath, relativePath] = atom.project.relativizePath(fullPath)
return unless projectPath
projectObj = projectDict[projectPath]
return unless projectObj
if fullPath == projectObj.configPath
projectObj = reload(projectPath)
return unless projectObj.host.uploadOnSave
projectObj.uploadFile(fullPath)
onDidDestroy = editor.onDidDestroy ->
disposables.remove onDidSave
disposables.remove onDidDestroy
onDidDestroy.dispose()
onDidSave.dispose()
disposables.add onDidSave
disposables.add onDidDestroy
deactivate: ->
disposables.dispose()
disposables = null
for projectPath, obj of projectDict
obj.dispose()
projectDict = null