forked from apache/cordova-plugman
-
Notifications
You must be signed in to change notification settings - Fork 0
/
plugman.js
206 lines (191 loc) · 7.25 KB
/
plugman.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
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
203
204
205
206
/*
*
* Copyright 2013 Anis Kadri
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*
*/
// copyright (c) 2013 Andrew Lunny, Adobe Systems
var emitter = require('./src/events');
var Q = require('q');
function addProperty(o, symbol, modulePath, doWrap) {
var val = null;
if (doWrap) {
o[symbol] = function() {
val = val || require(modulePath);
if (arguments.length && typeof arguments[arguments.length - 1] === 'function') {
// If args exist and the last one is a function, it's the callback.
var args = Array.prototype.slice.call(arguments);
var cb = args.pop();
val.apply(o, args).done(cb, cb);
} else {
val.apply(o, arguments).done(null, function(err){ throw err; });
}
};
} else {
// The top-level plugman.foo
Object.defineProperty(o, symbol, {
get : function() { return val = val || require(modulePath); },
set : function(v) { val = v; }
});
}
// The plugman.raw.foo
Object.defineProperty(o.raw, symbol, {
get : function() { return val = val || require(modulePath); },
set : function(v) { val = v; }
});
}
plugman = {
on: emitter.addListener,
off: emitter.removeListener,
removeAllListeners: emitter.removeAllListeners,
emit: emitter.emit,
raw: {}
};
addProperty(plugman, 'help', './src/help');
addProperty(plugman, 'install', './src/install', true);
addProperty(plugman, 'uninstall', './src/uninstall', true);
addProperty(plugman, 'fetch', './src/fetch', true);
addProperty(plugman, 'prepare', './src/prepare');
addProperty(plugman, 'config', './src/config', true);
addProperty(plugman, 'owner', './src/owner', true);
addProperty(plugman, 'adduser', './src/adduser', true);
addProperty(plugman, 'publish', './src/publish', true);
addProperty(plugman, 'unpublish', './src/unpublish', true);
addProperty(plugman, 'search', './src/search', true);
addProperty(plugman, 'info', './src/info', true);
addProperty(plugman, 'create', './src/create', true);
addProperty(plugman, 'platform', './src/platform_operation', true);
addProperty(plugman, 'config_changes', './src/util/config-changes');
plugman.commands = {
'config' : function(cli_opts) {
plugman.config(cli_opts.argv.remain, function(err) {
if (err) throw err;
else console.log('done');
});
},
'owner' : function(cli_opts) {
plugman.owner(cli_opts.argv.remain);
},
'install' : function(cli_opts) {
if(!cli_opts.platform || !cli_opts.project || !cli_opts.plugin) {
return console.log(plugman.help());
}
var cli_variables = {}
if (cli_opts.variable) {
cli_opts.variable.forEach(function (variable) {
var tokens = variable.split('=');
var key = tokens.shift().toUpperCase();
if (/^[\w-_]+$/.test(key)) cli_variables[key] = tokens.join('=');
});
}
var opts = {
subdir: '.',
cli_variables: cli_variables,
www_dir: cli_opts.www,
searchpath: cli_opts.searchpath
};
var p = Q();
cli_opts.plugin.forEach(function (pluginSrc) {
p = p.then(function () {
return plugman.raw.install(cli_opts.platform, cli_opts.project, pluginSrc, cli_opts.plugins_dir, opts);
})
});
return p;
},
'uninstall': function(cli_opts) {
if(!cli_opts.platform || !cli_opts.project || !cli_opts.plugin) {
return console.log(plugman.help());
}
var p = Q();
cli_opts.plugin.forEach(function (pluginSrc) {
p = p.then(function () {
return plugman.raw.uninstall(cli_opts.platform, cli_opts.project, pluginSrc, cli_opts.plugins_dir, { www_dir: cli_opts.www });
});
});
return p;
},
'adduser' : function(cli_opts) {
plugman.adduser(function(err) {
if (err) throw err;
else console.log('user added');
});
},
'search' : function(cli_opts) {
plugman.search(cli_opts.argv.remain, function(err, plugins) {
if (err) throw err;
else {
for(var plugin in plugins) {
console.log(plugins[plugin].name, '-', plugins[plugin].description || 'no description provided');
}
}
});
},
'info' : function(cli_opts) {
plugman.info(cli_opts.argv.remain, function(err, plugin_info) {
if (err) throw err;
else {
console.log('name:', plugin_info.name);
console.log('version:', plugin_info.version);
if (plugin_info.engines) {
for(var i = 0, j = plugin_info.engines.length ; i < j ; i++) {
console.log(plugin_info.engines[i].name, 'version:', plugin_info.engines[i].version);
}
}
}
});
},
'publish' : function(cli_opts) {
var plugin_path = cli_opts.argv.remain;
if(!plugin_path) {
return console.log(plugman.help());
}
plugman.publish(plugin_path, function(err) {
if (err) throw err;
else console.log('Plugin published');
});
},
'unpublish': function(cli_opts) {
var plugin = cli_opts.argv.remain;
if(!plugin) {
return console.log(plugman.help());
}
plugman.unpublish(plugin, function(err) {
if (err) throw err;
else console.log('Plugin unpublished');
});
},
'create': function(cli_opts) {
if( !cli_opts.name || !cli_opts.plugin_id || !cli_opts.plugin_version) {
return console.log( plugman.help() );
}
var cli_variables = {};
if (cli_opts.variable) {
cli_opts.variable.forEach(function (variable) {
var tokens = variable.split('=');
var key = tokens.shift().toUpperCase();
if (/^[\w-_]+$/.test(key)) cli_variables[key] = tokens.join('=');
});
}
plugman.create( cli_opts.name, cli_opts.plugin_id, cli_opts.plugin_version, cli_opts.path || ".", cli_variables );
},
'platform': function(cli_opts) {
var operation = cli_opts.argv.remain[ 0 ] || "";
if( ( operation !== 'add' && operation !== 'remove' ) || !cli_opts.platform_name ) {
return console.log( plugman.help() );
}
plugman.platform( { operation: operation, platform_name: cli_opts.platform_name } );
}
};
module.exports = plugman;