This repository has been archived by the owner on Dec 21, 2017. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
extensionManagerClient.js
164 lines (140 loc) · 5.38 KB
/
extensionManagerClient.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
/*
* Copyright (c) 2012 Adobe Systems Incorporated. All rights reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*
*/
/*jslint vars: true, plusplus: true, devel: true, nomen: true, regexp: true, indent: 4, maxerr: 50 */
/*global define, brackets, $, window */
define(function (require, exports, module) {
'use strict';
// Brackets modules
var ExtensionLoader = brackets.getModule("utils/ExtensionLoader");
// Our modules
var client = require("client");
// The extensions folder
var extensionDir = ExtensionLoader.getUserExtensionPath() + "/";
var moduleName = "extension-manager";
// Monkey-patch the extension loader
ExtensionLoader.unloadExtension = function (name, config, entryPoint) {
console.log("[Extension] Unloading " + name + " (in " + config.baseUrl + ")");
var result = new $.Deferred();
// Hack: Get the loaded module
var module = brackets.libRequire.s.contexts[name].defined.main;
function moduleUnloaded(err) {
// Hack: Make require.js forget it ever loaded this extension
delete brackets.libRequire.s.contexts[name];
if (err) {
console.log("[Extension] Error while unloading " + name + ": " + err.message);
result.reject(err);
} else {
console.log("[Extension] Successfully unloaded " + name);
result.resolve();
}
}
if (module && module.unload) {
console.log("[Extension] Unloading " + name + " with its module's unload method");
try {
if (module.unload.length > 0) {
console.log("[Extension] Waiting for callback to be called");
module.unload(moduleUnloaded);
} else {
console.log("[Extension] Unloading sequentially");
module.unload();
moduleUnloaded();
}
} catch (err) {
moduleUnloaded(err);
}
}
else {
moduleUnloaded(new Error("Module did not provide an unload() method"));
}
return result.promise();
};
// load an extension
function _load(name) {
ExtensionLoader.loadExtension(name, { baseUrl: extensionDir + name }, "main");
}
// unload an extension
function _unload(name) {
return ExtensionLoader.unloadExtension(name, { baseUrl: extensionDir + name }, "unload");
}
// list extensions
function list(callback) {
client.send(moduleName, "list", callback);
}
// install an extension
function install(name, callback) {
client.send(moduleName, "install", name, function (res) {
_load(name);
if (callback) { callback(); }
});
}
// uninstall an extension
function uninstall(name, callback) {
client.send(moduleName, "uninstall", name, function (res) {
if (callback) { callback(); }
});
}
// enable an extension
function enable(name, callback) {
client.send(moduleName, "enable", name, function (res) {
_load(name);
if (callback) { callback(); }
});
}
// disable an extension
function disable(name, callback) {
var fn = function () {
client.send(moduleName, "disable", name, callback);
};
// Wait for unload to complete or fail before disabling
_unload(name).done(fn).fail(fn);
}
// update an extension
function update(name, callback) {
client.send(moduleName, "update", name, function (res) {
if (callback) { callback(); }
});
}
// update all extensions
function updateAll(callback) {
client.send(moduleName, "updateAll", function (res) {
if (callback) { callback(); }
});
}
// open a URL
function openUrl(url) {
client.send(moduleName, "openUrl", url);
}
// init the extension client
function init(callback) {
client.connect(callback);
}
exports.list = list;
exports.install = install;
exports.uninstall = uninstall;
exports.enable = enable;
exports.disable = disable;
exports.update = update;
exports.updateAll = updateAll;
exports.openUrl = openUrl;
exports.init = init;
});