Skip to content
This repository was archived by the owner on Jun 28, 2022. It is now read-only.

Commit 2f57fa9

Browse files
committed
runat-1128988: Update gcli/index API to not include system
Now systems are created explicitly with createSystem, and the startup sequence is less magic. Signed-off-by: Joe Walker <jwalker@mozilla.com>
1 parent f525d59 commit 2f57fa9

7 files changed

Lines changed: 45 additions & 66 deletions

File tree

index.html

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -27,17 +27,15 @@
2727

2828
var modules = [ 'gcli/index', 'gcli/demo', 'gcli/test/index' ];
2929
require(modules, function(gcli, demo, test) {
30-
// Add demo commands. You'll probably want to replace this with
31-
// your own set of commands
32-
gcli.addItems(demo.items);
30+
// Add the commands/types/converters as required
31+
var system = gcli.createSystem();
32+
system.addItems(gcli.items); // The basic set that most people need
33+
system.addItems(demo.items); // Demo commands
3334

34-
// To run commands remotely,
35-
// Hook GCLI into this page, using gcli-root above
36-
var options = {};
37-
gcli.createTerminal(options).then(function() {
38-
// Run the unit test at each startup.
39-
test.run(options);
40-
}).then(null, console.error);
35+
gcli.createTerminal(system).then(function(terminal) {
36+
terminal.language.showIntro(); // Intro text
37+
test.run(terminal); // Run the unit test at each startup
38+
}).then(null, console.error.bind(console));
4139
});
4240
</script>
4341

lib/gcli/connectors/direct.js

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616

1717
'use strict';
1818

19-
var system = require('../api').createSystem();
19+
var createSystem = require('../api').createSystem;
2020
var Commands = require('../commands/commands').Commands;
2121
var Types = require('../types/types').Types;
2222
var Requisition = require('../cli').Requisition;
@@ -63,10 +63,6 @@ var items = [
6363

6464
].reduce(function(prev, curr) { return prev.concat(curr); }, []);
6565

66-
system.addItems(items);
67-
68-
var requisition = new Requisition(system);
69-
7066
exports.items = [
7167
{
7268
// Communicate with a 'remote' server that isn't remote at all
@@ -81,6 +77,12 @@ exports.items = [
8177

8278
function DirectConnection() {
8379
this._emit = this._emit.bind(this);
80+
81+
// This is the 'server'
82+
var system = createSystem();
83+
system.addItems(items);
84+
var requisition = new Requisition(system);
85+
8486
this.remoter = new Remoter(requisition);
8587
this.remoter.addListener(this._emit);
8688
}

lib/gcli/connectors/index.js

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616

1717
'use strict';
1818

19-
var api = require('../api');
19+
var createSystem = require('../api').createSystem;
2020
var Commands = require('../commands/commands').Commands;
2121
var Types = require('../types/types').Types;
2222

@@ -81,7 +81,8 @@ var items = [
8181

8282
/**
8383
* These are the commands stored on the remote side that have converters which
84-
* we'll need to present the data
84+
* we'll need to present the data. Ideally connection.specs would transfer
85+
* these, that doesn't happen yet so we add them manually
8586
*/
8687
var requiredConverters = [
8788
require('../cli').items,
@@ -107,21 +108,20 @@ var requiredConverters = [
107108
exports.connect = function(options) {
108109
options = options || {};
109110

110-
var system = api.createSystem();
111-
111+
var system = createSystem();
112112
system.addItems(items);
113113
system.addItems(requiredConverters);
114114

115115
var connector = system.connectors.get(options.connector);
116116
return connector.connect(options.url).then(function(connection) {
117-
options.connection = connection;
117+
system.connection = connection;
118118
connection.on('commandsChanged', function(specs) {
119119
exports.addItems(system, specs, connection);
120120
});
121121

122122
return connection.call('specs').then(function(specs) {
123123
exports.addItems(system, specs, connection);
124-
return connection;
124+
return system;
125125
});
126126
});
127127
};
@@ -144,8 +144,11 @@ exports.addLocalFunctions = function(specs, connection) {
144144
// in removeRemoteItems() below
145145
commandSpec.connection = connection;
146146

147+
// TODO: removeRemoteItems() doesn't remove types, so do we need this?
147148
commandSpec.params.forEach(function(param) {
148-
param.type.connection = connection;
149+
if (typeof param.type !== 'string') {
150+
param.type.connection = connection;
151+
}
149152
});
150153

151154
if (!commandSpec.isParent) {

lib/gcli/index.js

Lines changed: 3 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616

1717
'use strict';
1818

19-
var api = require('./api');
19+
var createSystem = require('./api').createSystem;
2020
var Terminal = require('./ui/terminal').Terminal;
2121

2222
// Patch-up old browsers
@@ -76,32 +76,6 @@ exports.items = [
7676
// No commands in the basic set
7777
].reduce(function(prev, curr) { return prev.concat(curr); }, []);
7878

79-
var system = api.createSystem();
79+
exports.createSystem = createSystem;
8080

81-
// Export the system API by adding it to our exports
82-
Object.keys(system).forEach(function(key) {
83-
exports[key] = system[key];
84-
});
85-
86-
system.addItems(exports.items);
87-
88-
/**
89-
* createTerminal() calls 'Terminal.create()' but returns an object which
90-
* exposes a much restricted set of functions rather than all those exposed
91-
* by Terminal.
92-
* This allows for robust testing without exposing too many internals.
93-
* @param options See Terminal.create() for a description of the available
94-
* options.
95-
*/
96-
exports.createTerminal = function(options) {
97-
options = options || {};
98-
if (options.settings != null) {
99-
system.settings.setDefaults(options.settings);
100-
}
101-
102-
return Terminal.create(system, options).then(function(terminal) {
103-
options.terminal = terminal;
104-
terminal.language.showIntro();
105-
return terminal;
106-
});
107-
};
81+
exports.createTerminal = Terminal.create.bind(Terminal);

lib/gcli/test/index.js

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -69,17 +69,18 @@ var addDebugAids = exports.addDebugAids = function(options) {
6969
* - Runs the unit tests automatically on startup
7070
* - Registers a 'test' command to re-run the unit tests
7171
*/
72-
exports.run = function(options) {
73-
var requisition = options.terminal.language.requisition;
74-
75-
options.window = window;
76-
options.automator = createTerminalAutomator(options.terminal);
77-
options.requisition = requisition;
78-
options.isNode = false;
79-
options.isFirefox = false;
80-
options.isPhantomjs = (window.navigator.userAgent.indexOf('hantom') !== -1);
81-
options.isRemote = (options.connection != null);
82-
options.hideExec = true;
72+
exports.run = function(terminal) {
73+
var options = {
74+
terminal: terminal,
75+
window: window,
76+
automator: createTerminalAutomator(terminal),
77+
requisition: terminal.language.requisition,
78+
isNode: false,
79+
isFirefox: false,
80+
isPhantomjs: (window.navigator.userAgent.indexOf('hantom') !== -1),
81+
isRemote: (terminal.system.connection != null),
82+
hideExec: true
83+
};
8384

8485
addDebugAids(options);
8586

@@ -92,7 +93,7 @@ exports.run = function(options) {
9293

9394
// PhantomJS may tell us to tell the server to shutdown
9495
if (window.location.href.indexOf('shutdown=true') > 0) {
95-
shutdownServer(requisition).then(closeIfPhantomJs, closeIfPhantomJs);
96+
shutdownServer(terminal.language.requisition).then(closeIfPhantomJs, closeIfPhantomJs);
9697
return;
9798
}
9899

lib/gcli/ui/terminal.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ function Terminal() {
4747
* - document
4848
*/
4949
Terminal.create = function(system, options) {
50+
options = options || {};
5051
if (resourcesPromise == null) {
5152
resourcesPromise = Promise.all([
5253
host.staticRequire(module, './terminal.css'),

remote.html

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,9 @@
2828

2929
require([ 'gcli/index', 'gcli/test/index', 'gcli/connectors/index' ], function(gcli, test, cnx) {
3030
var options = { connector: 'websocket' };
31-
cnx.connect(options).then(function() {
32-
gcli.createTerminal(options).then(function() {
33-
test.run(options);
31+
cnx.connect(options).then(function(system) {
32+
gcli.createTerminal(system).then(function(terminal) {
33+
test.run(terminal);
3434
});
3535
}).then(null, console.error.bind(console));
3636
});

0 commit comments

Comments
 (0)