mirror of
https://github.com/balena-io/etcher.git
synced 2025-07-22 18:56:31 +00:00
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:
parent
cf99a5d598
commit
dafa1f3ede
@ -126,8 +126,6 @@ exports.write = (image, drive, options) => {
|
|||||||
* }));
|
* }));
|
||||||
*/
|
*/
|
||||||
const bridgeRobotMessage = (message) => {
|
const bridgeRobotMessage = (message) => {
|
||||||
const ROBOT_COMMAND_LOG = 'log';
|
|
||||||
|
|
||||||
const parsedMessage = _.attempt(() => {
|
const parsedMessage = _.attempt(() => {
|
||||||
if (robot.isMessage(message)) {
|
if (robot.isMessage(message)) {
|
||||||
return robot.parseMessage(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
|
// Don't be so strict. If a message doesn't look like
|
||||||
// a robot message, then make the child writer log it
|
// a robot message, then make the child writer log it
|
||||||
// for debugging purposes.
|
// for debugging purposes.
|
||||||
return robot.parseMessage(robot.buildMessage(ROBOT_COMMAND_LOG, {
|
return robot.parseMessage(robot.buildMessage(robot.COMMAND.LOG, {
|
||||||
message
|
message
|
||||||
}));
|
}));
|
||||||
|
|
||||||
@ -157,9 +155,9 @@ exports.write = (image, drive, options) => {
|
|||||||
// The error object is decomposed by the CLI for serialisation
|
// The error object is decomposed by the CLI for serialisation
|
||||||
// purposes. We compose it back to an `Error` here in order
|
// purposes. We compose it back to an `Error` here in order
|
||||||
// to provide better encapsulation.
|
// to provide better encapsulation.
|
||||||
if (messageCommand === 'error') {
|
if (messageCommand === robot.COMMAND.ERROR) {
|
||||||
emitError(robot.recomposeErrorMessage(parsedMessage));
|
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
|
// If the message data is an object and it contains a
|
||||||
// message string then log the message string only.
|
// message string then log the message string only.
|
||||||
|
@ -19,6 +19,33 @@
|
|||||||
const _ = require('lodash');
|
const _ = require('lodash');
|
||||||
const errors = require('../errors');
|
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
|
* @summary Check whether we should emit parseable output
|
||||||
* @function
|
* @function
|
||||||
@ -154,7 +181,7 @@ exports.parseMessage = (string) => {
|
|||||||
* > 'foo'
|
* > 'foo'
|
||||||
*/
|
*/
|
||||||
exports.buildErrorMessage = (error) => {
|
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' });
|
* robot.log({ example: 'data' });
|
||||||
*/
|
*/
|
||||||
exports.log = (data) => {
|
exports.log = (data) => {
|
||||||
exports.printMessage('log', data);
|
exports.printMessage(exports.COMMAND.LOG, data);
|
||||||
};
|
};
|
||||||
|
@ -22,6 +22,23 @@ const robot = require('../../lib/shared/robot');
|
|||||||
|
|
||||||
describe('Shared: Robot', function() {
|
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() {
|
describe('.isEnabled()', function() {
|
||||||
|
|
||||||
it('should return false if ETCHER_CLI_ROBOT is not set', 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);
|
const message = robot.buildErrorMessage(error);
|
||||||
|
|
||||||
m.chai.expect(JSON.parse(message)).to.deep.equal({
|
m.chai.expect(JSON.parse(message)).to.deep.equal({
|
||||||
command: 'error',
|
command: robot.COMMAND.ERROR,
|
||||||
data: {
|
data: {
|
||||||
message: 'foo',
|
message: 'foo',
|
||||||
stack: error.stack
|
stack: error.stack
|
||||||
@ -207,7 +224,7 @@ describe('Shared: Robot', function() {
|
|||||||
const message = robot.buildErrorMessage(error);
|
const message = robot.buildErrorMessage(error);
|
||||||
|
|
||||||
m.chai.expect(JSON.parse(message)).to.deep.equal({
|
m.chai.expect(JSON.parse(message)).to.deep.equal({
|
||||||
command: 'error',
|
command: robot.COMMAND.ERROR,
|
||||||
data: {
|
data: {
|
||||||
message: 'foo',
|
message: 'foo',
|
||||||
description: 'error description',
|
description: 'error description',
|
||||||
@ -222,7 +239,7 @@ describe('Shared: Robot', function() {
|
|||||||
const message = robot.buildErrorMessage(error);
|
const message = robot.buildErrorMessage(error);
|
||||||
|
|
||||||
m.chai.expect(JSON.parse(message)).to.deep.equal({
|
m.chai.expect(JSON.parse(message)).to.deep.equal({
|
||||||
command: 'error',
|
command: robot.COMMAND.ERROR,
|
||||||
data: {
|
data: {
|
||||||
message: 'foo',
|
message: 'foo',
|
||||||
code: 'MYERROR',
|
code: 'MYERROR',
|
||||||
|
Loading…
x
Reference in New Issue
Block a user