diff --git a/lib/dev/gcli.js b/lib/dev/gcli.js new file mode 100644 index 000000000..3d65c6384 --- /dev/null +++ b/lib/dev/gcli.js @@ -0,0 +1,55 @@ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ + +"use strict"; + +const { Cu } = require("chrome"); +const { CommandUtils } = require("resource:///modules/devtools/DeveloperToolbar.jsm"); +const { devtools } = require("resource://gre/modules/devtools/Loader.jsm"); +const { gcli } = require("resource://gre/modules/devtools/gcli.jsm"); + +const { getActiveTab } = require("sdk/tabs/utils"); +const { getMostRecentBrowserWindow } = require("sdk/window/utils"); +const { Conversion, Status } = devtools.require("gcli/types/types"); + +const targetFor = ({target, tab, window}) => + target ? target : + tab ? devtools.TargetFactory.forTab(tab) : + window ? devtools.TargetFactory.forTab(getActiveTab(window)) : + devtools.TargetFactory.forTab(getActiveTab(getMostRecentBrowserWindow())) +exports.targetFor = targetFor + +const environmentFor = options => + options.environment ? options.environment : + CommandUtils.createEnvironment({target: targetFor(options)}) +exports.environmentFor = environmentFor + + +const run = (command, options={}) => { + const environment = environmentFor(options) + return CommandUtils.createRequisition(environment).then(requisition => { + return requisition.updateExec(command).then(({error, data}) => { + if (error) { + throw data + } + return data + }) + }) +} +exports.run = run + +const isUpperCase = text => text.toUpperCase() === text + +const install = (...items) => { + gcli.addItems(items.map(item => isUpperCase(item.name.charAt(0)) ? + Object.assign(item, {item: "type"}) : + item)) +} +exports.install = install + +const uninstall = (...items) => gcli.removeItems(items) +exports.uninstall = uninstall + +exports.Conversion = Conversion +exports.Status = Status diff --git a/test/test-gcli.js b/test/test-gcli.js new file mode 100644 index 000000000..cf982ccd3 --- /dev/null +++ b/test/test-gcli.js @@ -0,0 +1,68 @@ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ +"use strict"; + +const {run, install, uninstall, Conversion, Status} = require("dev/gcli"); + +exports["test gcli run"] = function*(assert) { + const result = yield run("help"); + assert.ok(Array.isArray(result.commands), + "help returns array of commands"); +}; + +exports["test gcli failed run"] = function*(assert) { + try { + const result = yield run("hlep"); + assert.fail("should have rejected promise"); + } catch (error) { + assert.ok(error); + assert.equal(error.message, "Invalid Command"); + } +}; + +exports["test install / uninstall command"] = function*(assert) { + const command = { + name: "echo", + description: "Returns same text back", + params: [{name: "text", + type: "string", + description: "text to echo"}], + exec({text}) { + return text; + } + }; + + install(command); + + const r1 = yield run("help echo"); + + assert.ok(r1.command, "returns command"); + assert.equal(r1.command.description, command.description); + assert.equal(r1.command.name, command.name); + assert.equal(r1.command.params.length, command.params.length); + assert.equal(r1.command.params[0].name, command.params[0].name); + assert.equal(r1.command.params[0].description, command.params[0].description); + + try { + const r2 = yield run("echo"); + assert.fail("should have errored"); + } catch(error) { + assert.equal(error.message, "Value required for 'text'."); + } + + const r3 = yield run("echo Hi"); + + assert.equal(r3, "Hi") + + uninstall(command); + + try { + const r4 = yield run("echo Bye"); + assert.fail("should have errored"); + } catch(error) { + assert.equal(error.message, "Invalid Command"); + } +}; + +require("sdk/test").run(exports);