-
Notifications
You must be signed in to change notification settings - Fork 5
/
main.js
173 lines (157 loc) · 6.32 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
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
/* global Promise:true */
define(function (require, exports, module) {
"use strict";
/* beautify preserve:start *//* eslint-disable no-multi-spaces */
var CommandManager = brackets.getModule("command/CommandManager");
var Menus = brackets.getModule("command/Menus");
var FileSystem = brackets.getModule("filesystem/FileSystem");
var ProjectManager = brackets.getModule("project/ProjectManager");
var AppInit = brackets.getModule("utils/AppInit");
var ExtensionUtils = brackets.getModule("utils/ExtensionUtils");
var NodeDomain = brackets.getModule("utils/NodeDomain");
var MainViewManager = brackets.getModule("view/MainViewManager");
var Mustache = brackets.getModule("thirdparty/mustache/mustache");
var Dialog = require("src/Dialog");
var Strings = require("strings");
var ungitViewerTemplate = require("text!templates/ungit.html");
if (typeof Promise !== "function") {
Promise = require("thirdparty/Promise");
}
var BASE_URL = "http://localhost:8448/#/repository?path=";
var STATUS_ERROR = 0;
var STATUS_INITIAL = 1;
var STATUS_INSTALLING = 2;
var STATUS_INSTALLED = 3;
var STATUS_RUNNING = 4;
var STATUS_OPEN = 5;
/* eslint-enable no-multi-spaces *//* beautify preserve:end */
var nodeDomain = new NodeDomain("hirseUngit", ExtensionUtils.getModulePath(module, "domain"));
var npmDomain = new NodeDomain("hirseNpm", ExtensionUtils.getModulePath(module, "npmDomain"));
var currentPath;
var status = STATUS_INITIAL;
var $viewer;
var $toolbarButton;
function installUngit() {
status = STATUS_INSTALLING;
$toolbarButton.removeClass();
$toolbarButton.addClass("installing");
$toolbarButton.attr("title", Strings.TOOLBAR_ICON_TOOLTIP_INSTALLING);
npmDomain.exec("install");
npmDomain.on("out", function (event, message) {
Dialog.appendLog(message);
});
npmDomain.on("installComplete", function (event, code) {
$toolbarButton.removeClass();
if (code === 0) {
Dialog.openInstallationCompleteDialog();
$toolbarButton.attr("title", Strings.TOOLBAR_ICON_TOOLTIP);
status = STATUS_INSTALLED;
} else {
Dialog.openInstallationErrorDialog();
$toolbarButton.addClass("error");
status = STATUS_ERROR;
}
});
}
function startUngit() {
if (status === STATUS_RUNNING) {
return Promise.resolve();
}
return new Promise(function (resolve, reject) {
$toolbarButton.addClass("starting");
nodeDomain.exec("start");
nodeDomain.on("error", function () {
$toolbarButton.removeClass();
$toolbarButton.addClass("error");
status = STATUS_INSTALLED;
reject();
});
nodeDomain.on("out", function (event, message) {
var started = message.indexOf("## Ungit started ##") !== -1 ||
message.indexOf("Ungit server already running") !== -1 ||
message.indexOf("Error: listen EADDRINUSE 127.0.0.1:8448") !== -1;
if (started) {
$toolbarButton.removeClass();
$toolbarButton.addClass("enabled");
status = STATUS_RUNNING;
resolve();
}
});
});
}
function openUngit() {
startUngit().then(function () {
var projectPath = ProjectManager.getProjectRoot().fullPath;
if (currentPath !== projectPath) {
currentPath = projectPath;
$viewer.find("iframe").attr("src", BASE_URL + currentPath);
}
$viewer.fadeIn("fast");
status = STATUS_OPEN;
});
}
function closeUngit() {
$viewer.fadeOut("fast");
status = STATUS_RUNNING;
CommandManager.execute("brackets-git.refreshAll");
}
function killUngit() {
if (status === STATUS_OPEN) {
closeUngit();
}
nodeDomain.exec("kill");
nodeDomain.on("close", function () {
$toolbarButton.removeClass();
status = STATUS_INSTALLED;
$viewer.find("iframe").attr("src", "");
currentPath = "";
});
}
$viewer = $(Mustache.render(ungitViewerTemplate, {
Strings: Strings
}));
$viewer.find("button#hirse-ungit-close").click(closeUngit);
$viewer.appendTo(".main-view .content");
ExtensionUtils.loadStyleSheet(module, "styles/styles.css");
MainViewManager.on("currentFileChange", function () {
if (status === STATUS_OPEN) {
closeUngit();
}
});
CommandManager.register(Strings.OPEN_UNGIT_CMD, "hirse.ungit.open", openUngit);
CommandManager.register(Strings.KILL_UNGIT_CMD, "hirse.ungit.kill", killUngit);
var menu = Menus.getMenu(Menus.AppMenuBar.NAVIGATE_MENU);
menu.addMenuDivider();
menu.addMenuItem("hirse.ungit.open", "Ctrl-Alt-U");
menu.addMenuItem("hirse.ungit.kill", "Ctrl-Alt-K");
$toolbarButton = $(document.createElement("a"))
.attr("id", "hirse-ungit-toolbar-icon")
.addClass("warning")
.attr("href", "#")
.on("click", function () {
if (status === STATUS_ERROR) {
Dialog.openInstallationErrorDialog();
} else if (status === STATUS_INSTALLING) {
Dialog.openInstallDialog();
} else if (status >= STATUS_INSTALLED) {
if (status === STATUS_OPEN) {
closeUngit();
} else {
openUngit();
}
}
})
.appendTo($("#main-toolbar .buttons"));
AppInit.appReady(function () {
var nodeModules = FileSystem.getDirectoryForPath(ExtensionUtils.getModulePath(module, "node_modules"));
nodeModules.exists(function (error, exists) {
if (exists) {
$toolbarButton.removeClass();
$toolbarButton.attr("title", Strings.TOOLBAR_ICON_TOOLTIP);
status = STATUS_INSTALLED;
} else {
installUngit();
}
});
});
});