This repository has been archived by the owner on Jan 10, 2023. It is now read-only.
forked from atom/tree-view
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathnsync-helper.coffee
203 lines (159 loc) · 5.72 KB
/
nsync-helper.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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
fs = require 'fs-plus'
_ = require 'underscore-plus'
_path = require 'path'
nsync = require 'nsync-fs'
remote = require 'remote'
{dialog} = require('electron').remote
atomHelper = require './atom-helper'
executeCustomCommand = require './custom-commands'
{CompositeDisposable} = require 'event-kit'
logFile = _path.join(atom.getConfigDirPath(), 'learn-ide.log')
require('dotenv').config
path: _path.join(__dirname, '..', '..', '.env')
silent: true
require('dotenv').config
path: _path.join(atom.getConfigDirPath(), '.env')
silent: true
WS_SERVER_URL = (->
config = _.defaults
host: process.env['IDE_WS_HOST']
port: process.env['IDE_WS_PORT']
path: process.env['IDE_WS_FS_PATH']
,
host: 'ile.learn.co',
port: 443,
path: 'fs_server'
{host, port, path} = config
protocol = if port is 443 then 'wss' else 'ws'
"#{protocol}://#{host}:#{port}/#{path}"
)()
convertEOL = (text) ->
text.replace(/\r\n|\n|\r/g, '\n')
unimplemented = ({type}) ->
command = type.replace(/^learn-ide:/, '').replace(/-/g, ' ')
atomHelper.warn 'Learn IDE: coming soon!', {detail: "Sorry, '#{command}' isn't available yet."}
onRefresh = ->
atomHelper.resetPackage()
onSave = ({target}) ->
editor = atomHelper.findTextEditorByElement(target)
path = editor.getPath()
if not editor.getPath()?
# TODO: untitled editor is saved
return console.warn 'Cannot save file without path'
text = convertEOL(editor.getText())
content = new Buffer(text).toString('base64')
nsync.save(path, content)
onImport = ->
dialog.showOpenDialog
title: 'Import Files',
properties: ['openFile', 'multiSelections']
, (paths) ->
importLocalPaths(paths)
importLocalPaths = (localPaths) ->
localPaths = [localPaths] if typeof localPaths is 'string'
targetPath = atomHelper.selectedPath()
targetNode = nsync.getNode(targetPath)
localPaths.forEach (path) ->
fs.readFile path, 'base64', (err, data) ->
if err?
return console.error 'Unable to import file:', path, err
base = _path.basename(path)
newPath = _path.posix.join(targetNode.path, base)
if nsync.hasPath(newPath)
atomHelper.warn 'Learn IDE: cannot save file',
detail: "There is already an existing remote file with path: #{newPath}"
return
nsync.save(newPath, data)
onEditorSave = ({path}) ->
node = nsync.getNode(path)
node.determineSync().then (shouldSync) ->
if shouldSync
atomHelper.findOrCreateBuffer(path).then (textBuffer) ->
text = convertEOL(textBuffer.getText())
content = new Buffer(text).toString('base64')
nsync.save(node.path, content)
onFindAndReplace = (path) ->
fs.readFile path, 'utf8', (err, data) ->
if err
return console.error 'Project Replace Error', err
text = convertEOL(data)
content = new Buffer(text).toString('base64')
nsync.save(path, content)
waitForFile = (localPath, seconds) ->
setTimeout ->
fs.stat localPath, (err, stats) ->
if err? and nsync.hasPath(localPath)
waitForFile(localPath, seconds * 2)
else
atomHelper.resolveOpen(localPath)
, seconds * 1000
module.exports = helper = (activationState) ->
composite = new CompositeDisposable
disposables = [
atomHelper.addCommands
'learn-ide:save': onSave
'learn-ide:save-as': unimplemented
'learn-ide:save-all': unimplemented
'learn-ide:import': onImport
'learn-ide:file-open': unimplemented
'learn-ide:add-project': unimplemented
'learn-ide-tree:refresh': onRefresh
nsync.onDidConfigure ->
atomHelper.addOpener (uri) ->
fs.stat uri, (err, stats) ->
if err? and nsync.hasPath(uri)
atomHelper.loadingFile(uri)
nsync.open(uri)
waitForFile(uri, 1)
nsync.onDidOpen ({localPath}) ->
atomHelper.resolveOpen(localPath)
nsync.onDidSetPrimary ({localPath, expansionState}) ->
atomHelper.updateProject(localPath, expansionState)
nsync.onWillLoad ->
secondsTillNotifying = 2
setTimeout ->
unless nsync.hasPrimaryNode()
atomHelper.loading()
, secondsTillNotifying * 1000
nsync.onDidDisconnect (detail) ->
if detail?
atomHelper.error 'Learn IDE: you are not connected!', {detail}
else
atomHelper.disconnected()
atomHelper.emit('learn-ide-tree:connection', {isConnected: false})
nsync.onWillConnect ->
atomHelper.connecting()
nsync.onDidConnect ->
atomHelper.emit('learn-ide-tree:connection', {isConnected: true})
atomHelper.connected()
nsync.onDidReceiveCustomCommand (payload) ->
executeCustomCommand(payload)
nsync.onDidChange (path) ->
parent = _path.dirname(path)
atomHelper.reloadTreeView(parent, path)
atomHelper.updateTitle()
nsync.onDidUpdate (path) ->
atomHelper.saveEditor(path)
atomHelper.observeTextEditors (editor) ->
composite.add editor.onDidSave (e) ->
onEditorSave(e)
atomHelper.on 'learn-ide:logout', ->
pkg = atom.packages.loadPackage('learn-ide-tree')
pkg.mainModule.preventCache = true
nsync.flushCache()
atomHelper.onDidActivatePackage (pkg) ->
if pkg.name is 'find-and-replace'
projectFindView = pkg.mainModule.projectFindView
resultModel = projectFindView.model
composite.add resultModel.onDidReplacePath ({filePath}) ->
onFindAndReplace(filePath)
]
disposables.forEach (disposable) -> composite.add(disposable)
atomHelper.getToken().then (token) ->
nsync.configure
expansionState: activationState.directoryExpansionStates
localRoot: _path.join(atom.configDirPath, '.learn-ide')
logFile: logFile
connection:
url: "#{WS_SERVER_URL}?token=#{token}"
return composite