refactor(child-writer): add robot commands constant (#1332)

See: https://github.com/resin-io/etcher/pull/1295#discussion_r112463686
Signed-off-by: Juan Cruz Viotti <jviotti@openmailbox.org>
This commit is contained in:
Juan Cruz Viotti 2017-04-21 17:41:20 -04:00 committed by GitHub
parent cf99a5d598
commit dafa1f3ede
3 changed files with 52 additions and 10 deletions

View File

@ -126,8 +126,6 @@ exports.write = (image, drive, options) => {
* }));
*/
const bridgeRobotMessage = (message) => {
const ROBOT_COMMAND_LOG = 'log';
const parsedMessage = _.attempt(() => {
if (robot.isMessage(message)) {
return robot.parseMessage(message);
@ -136,7 +134,7 @@ exports.write = (image, drive, options) => {
// Don't be so strict. If a message doesn't look like
// a robot message, then make the child writer log it
// for debugging purposes.
return robot.parseMessage(robot.buildMessage(ROBOT_COMMAND_LOG, {
return robot.parseMessage(robot.buildMessage(robot.COMMAND.LOG, {
message
}));
@ -157,9 +155,9 @@ exports.write = (image, drive, options) => {
// The error object is decomposed by the CLI for serialisation
// purposes. We compose it back to an `Error` here in order
// to provide better encapsulation.
if (messageCommand === 'error') {
if (messageCommand === robot.COMMAND.ERROR) {
emitError(robot.recomposeErrorMessage(parsedMessage));
} else if (messageCommand === ROBOT_COMMAND_LOG) {
} else if (messageCommand === robot.COMMAND.LOG) {
// If the message data is an object and it contains a
// message string then log the message string only.

View File

@ -19,6 +19,33 @@
const _ = require('lodash');
const errors = require('../errors');
/**
* @summary Robot commands
* @namespace COMMAND
* @public
*/
exports.COMMAND = {
/**
* @property {String} ERROR
* @memberof COMMAND
*
* @description
* This command is used to represent an error message.
*/
ERROR: 'error',
/**
* @property {String} LOG
* @memberof COMMAND
*
* @description
* This command is used to represent a log message.
*/
LOG: 'log'
};
/**
* @summary Check whether we should emit parseable output
* @function
@ -154,7 +181,7 @@ exports.parseMessage = (string) => {
* > 'foo'
*/
exports.buildErrorMessage = (error) => {
return exports.buildMessage('error', errors.toJSON(error));
return exports.buildMessage(exports.COMMAND.ERROR, errors.toJSON(error));
};
/**
@ -263,5 +290,5 @@ exports.printMessage = (message, data) => {
* robot.log({ example: 'data' });
*/
exports.log = (data) => {
exports.printMessage('log', data);
exports.printMessage(exports.COMMAND.LOG, data);
};

View File

@ -22,6 +22,23 @@ const robot = require('../../lib/shared/robot');
describe('Shared: Robot', function() {
describe('.COMMAND', function() {
it('should be a plain object', function() {
m.chai.expect(_.isPlainObject(robot.COMMAND)).to.be.true;
});
it('should only contain string values', function() {
m.chai.expect(_.every(_.values(robot.COMMAND), _.isString)).to.be.true;
});
it('should contain only unique values', function() {
const numberOfKeys = _.size(_.keys(robot.COMMAND));
m.chai.expect(_.size(_.uniq(_.values(robot.COMMAND)))).to.equal(numberOfKeys);
});
});
describe('.isEnabled()', function() {
it('should return false if ETCHER_CLI_ROBOT is not set', function() {
@ -193,7 +210,7 @@ describe('Shared: Robot', function() {
const message = robot.buildErrorMessage(error);
m.chai.expect(JSON.parse(message)).to.deep.equal({
command: 'error',
command: robot.COMMAND.ERROR,
data: {
message: 'foo',
stack: error.stack
@ -207,7 +224,7 @@ describe('Shared: Robot', function() {
const message = robot.buildErrorMessage(error);
m.chai.expect(JSON.parse(message)).to.deep.equal({
command: 'error',
command: robot.COMMAND.ERROR,
data: {
message: 'foo',
description: 'error description',
@ -222,7 +239,7 @@ describe('Shared: Robot', function() {
const message = robot.buildErrorMessage(error);
m.chai.expect(JSON.parse(message)).to.deep.equal({
command: 'error',
command: robot.COMMAND.ERROR,
data: {
message: 'foo',
code: 'MYERROR',