-
Notifications
You must be signed in to change notification settings - Fork 5
/
main.js
141 lines (109 loc) · 4.25 KB
/
main.js
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
/* global define, $, brackets */
define(function (require, exports, module) {
'use strict';
var CommandManager = brackets.getModule('command/CommandManager'),
ExtensionUtils = brackets.getModule('utils/ExtensionUtils'),
NodeConnection = brackets.getModule('utils/NodeConnection'),
FileSystem = brackets.getModule('filesystem/FileSystem'),
ProjectManager = brackets.getModule('project/ProjectManager'),
COMMAND_ID = 'outofme.bracketsLivereload.enable',
AppInit = brackets.getModule('utils/AppInit'),
isRunning = false,
nodeConnection,
port = 35729,
lrDomain,
$lrIcon;
function toggleServer() {
if (!isRunning) {
lrDomain.startServer(port).done(function () {
isRunning = true;
activateListeners();
}).fail(function (err) {
isRunning = false;
deactivateListeners(err);
});
} else {
lrDomain.stopServer().done(function () {
isRunning = false;
deactivateListeners();
}).fail(function (err) {
isRunning = false;
deactivateListeners(err);
});
}
}
function changeHandler(err, changedThing) {
if (ProjectManager.getProjectRoot()._path === changedThing._path.substr(0, ProjectManager.getProjectRoot()._path.length)) {
if (changedThing && isRunning) {
lrDomain.trigger([changedThing.name]);
}
}
}
function activateListeners() {
$lrIcon
.removeClass('error')
.addClass('active')
.attr('title', 'LiveReload: active');
// Listening to file system changes
FileSystem.on('change', changeHandler);
}
function deactivateListeners(err) {
if (!err) {
$lrIcon
.removeClass('active error')
.attr('title', 'LiveReload');
} else {
$lrIcon
.removeClass('active')
.addClass('error')
.attr('title', 'LiveReload: ' + (err.code === 'EADDRINUSE' ? 'Port ' + port + ' is already in use.' : err.message));
}
// Listening to file system changes
FileSystem.off('change', changeHandler);
}
function initLrDomain(callback) {
// Creating new node connection
nodeConnection = new NodeConnection();
var connectionPromise = nodeConnection.connect(true),
errorHandler = function (err) {
nodeConnection = null;
$lrIcon.addClass('error');
$lrIcon.attr('title', 'LiveReload is having a problem to connect to node process: ' + err);
console.log('[brackets-livereload] failed to load domain:', err);
callback(err);
};
connectionPromise.done(function () {
var path = ExtensionUtils.getModulePath(module, 'node/index'),
loadPromise = nodeConnection.loadDomains([path], true);
loadPromise.done(function () {
lrDomain = nodeConnection.domains.livereload;
callback();
}).fail(errorHandler);
}).fail(errorHandler);
}
// Register extension
CommandManager.register('Livereload', COMMAND_ID, toggleServer);
AppInit.appReady(function () {
// Load stylesheet.
ExtensionUtils.loadStyleSheet(module, 'livereload.css');
// Add icon to toolbar.
$lrIcon = $('<a href="#" title="LiveReload" id="brackets-livereload-icon"></a>');
$lrIcon.click(function () {
CommandManager.execute(COMMAND_ID);
}).appendTo('#main-toolbar .buttons');
// Initializing lrDomain
initLrDomain(function (err) {
if (!err) {
// lr server can be running when brackets was refreshed
lrDomain.isServerRunning().done(function (running) {
isRunning = running;
if (isRunning) {
activateListeners();
}
}).fail(function (err) {
deactivateListeners(err);
});
}
});
});
});