forked from joewalker/gcli
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgcli.js
More file actions
189 lines (166 loc) · 6.35 KB
/
gcli.js
File metadata and controls
189 lines (166 loc) · 6.35 KB
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
#!/usr/bin/env node
/*
* Copyright 2012, Mozilla Foundation and contributors
*
* 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.
*/
'use strict';
exports.gcliHome = __dirname;
/**
* There are 2 options for loading GCLI CommonJS modules:
* 1. Use Require's r.js
* 2. Convert the modules to CommonJS format on the fly.
*
* The former feels less hacky, the latter allows us to use 'cover' test
* coverage. Neither are complex so we've left them both in so they can fight
* it out.
*/
exports.useUnamd = false;
// Setup the exports.require function to use either:
// - requirejs (through r.js) or
// - node's require (via unamd)
if (exports.useUnamd) {
var unamd = require('./lib/server/unamd');
[ 'gcli', 'gclitest', 'test' ].forEach(function(packageName) {
var srcDir = exports.gcliHome + '/lib/' + packageName;
var destDir = exports.gcliHome + '/node_modules/' + packageName;
unamd.unamdize(srcDir, destDir);
});
exports.require = require;
}
else {
// It's tempting to use RequireJS from NPM, however that would break
// running GCLI in Firefox just by opening index.html
var requirejs = require('./scripts/r.js');
requirejs.config({
nodeRequire: require,
paths: { 'text': 'scripts/text', 'i18n': 'scripts/i18n' },
packagePaths: {
'lib': [
{ name: 'gcli', main: 'index', lib: '.' },
{ name: 'test', main: 'index', lib: '.' },
{ name: 'gclitest', main: 'index', lib: '.' },
{ name: 'demo', main: 'index', lib: '.' },
{ name: 'server', main: 'index', lib: '.' },
{ name: 'util', main: 'index', lib: '.' }
]
}
});
exports.require = requirejs;
// The Firefox build has an override directory to enable custom code, but in
// NodeJS it's more hacky - we inject into require
var serverOverride = function(requirePath, nodePath) {
var host = require(nodePath);
requirejs.define(requirePath, function(require, exports, module) {
Object.keys(host).forEach(function(key) {
exports[key] = host[key];
});
});
};
serverOverride('util/host', './lib/server/util/host');
serverOverride('util/filesystem', './lib/server/util/filesystem');
serverOverride('gcli/types/fileparser', './lib/server/gcli/types/fileparser');
var fs = require('fs');
var helpManHtml = fs.readFileSync(exports.gcliHome + '/lib/server/gcli/commands/help_man.html', 'utf8');
var helpListHtml = fs.readFileSync(exports.gcliHome + '/lib/server/gcli/commands/help_list.html', 'utf8');
requirejs.define('text!gcli/commands/help_man.html', helpManHtml);
requirejs.define('text!gcli/commands/help_list.html', helpListHtml);
}
exports.require('gcli/index');
var gcli = exports.require('gcli/api').getApi();
// gcli.addItems(exports.require('gcli/commands/connect').items);
gcli.addItems(exports.require('gcli/commands/context').items);
gcli.addItems(exports.require('gcli/commands/exec').items);
gcli.addItems(exports.require('gcli/commands/help').items);
gcli.addItems(exports.require('gcli/commands/intro').items);
gcli.addItems(exports.require('gcli/commands/pref_list').items);
gcli.addItems(exports.require('gcli/commands/pref').items);
gcli.addItems(exports.require('demo/commands/alert').items);
// gcli.addItems(exports.require('demo/commands/bugs').items);
// gcli.addItems(exports.require('demo/commands/demo').items);
gcli.addItems(exports.require('demo/commands/echo').items);
// gcli.addItems(exports.require('demo/commands/edit').items);
// gcli.addItems(exports.require('demo/commands/git').items);
// gcli.addItems(exports.require('demo/commands/hg').items);
gcli.addItems(exports.require('demo/commands/sleep').items);
// Commands using the Node API
gcli.addItems(require('./lib/server/commands/exit').items);
gcli.addItems(require('./lib/server/commands/firefox').items);
gcli.addItems(require('./lib/server/commands/server').items);
gcli.addItems(require('./lib/server/commands/standard').items);
gcli.addItems(require('./lib/server/commands/test').items);
gcli.addItems(require('./lib/server/commands/unamd').items);
var Requisition = exports.require('gcli/cli').Requisition;
var Status = exports.require('gcli/types').Status;
var jsdom = require('jsdom').jsdom;
var doc = jsdom('<html><head></head><body></body></html>');
var environment = {
document: doc,
window: doc.defaultView
};
var requisition = new Requisition(environment, doc);
var command, extraActions;
if (process.argv.length < 3) {
// No command passed in. Serve GCLI over http and start a local REPL
command = 'server start';
extraActions = function(output) {
if (!output.error) {
startRepl();
}
};
}
else {
// Command passed in. No server/REPL and a process.exit(1) on failure to
// propagate test errors
command = process.argv.slice(2).join(' ');
extraActions = function(output) {
if (output.error) {
process.exit(1);
}
};
}
/**
* Convert an Output object to a string, and then log that to stdout/stderr
* depending on the error status
*/
function logResults(output) {
var context = requisition.conversionContext;
return output.convert('string', context).then(function(message) {
if (output.error) {
console.error(message);
}
else {
console.log(message);
}
return output;
});
}
requisition.updateExec(command).then(logResults).then(extraActions);
/**
* Start a NodeJS REPL to execute commands
*/
function startRepl() {
var repl = require('repl');
var gcliEval = function(command, scope, file, callback) {
// Why does node wrap the command in '(...)\n'?
command = command.replace(/^\((.*)\n\)$/, function(all, part) {
return part;
});
if (command.length !== 0) {
requisition.updateExec(command).then(logResults).then(function() { callback(); });
}
};
console.log('This is also a limited GCLI REPL. ' +
'Type \'help\' for a list of commands, CTRL+C 3 times to exit:');
repl.start('\u00bb ', process, gcliEval, false, true);
}