'use strict';

const _ = require('lodash');

const Shipment = require('./Shipment');

class ShipmentWorker {

    constructor(exportPath) {
        if (!_.isString(exportPath))
            throw new Error('Expected a string');
        this.shipment = require(exportPath);
        if (!(this.shipment instanceof Shipment))
            throw new Error(`The module at ${exportPath} did not provide an instance of Shipment -- aborting`);
    }

    /**
     * Call the given action on the internal shipment instance
     * @param {String} action
     * @param {Object} args
     * @returns {Promise}
     */
    call({action, args}) {
        let a = _.find(this.shipment.actions, a => a.getName() === action);

        if (!a) throw new Error(`No action with name ${action} found in Shipment instance`);

        return a.executeApi(args);
    }

    /**
     * Process an imcoming message (from the forker process)
     * @param {String} exportPath
     * @param {String} action
     * @param {String} verifyKey Optional key that can be used to verify the origin of shipment log lines
     * @param {Object} args
     * @returns {Promise.<T>}
     */
    static processMessage({exportPath, action, args, verifyKey = ''}) {
        let prefix = `SHIPMENT${verifyKey ? `-${verifyKey}` : ''}: `;
        console.log(`${prefix}start: ${action}`);
        const worker = new ShipmentWorker(exportPath);
        return worker.call({action, args})
            .then(() => {
                console.log(`${prefix}ok`);
                process.exit();
            })
            .catch(e => {
                console.error(`${prefix}error: ${e.message}`);
                process.exit(1);
            });
    }
}

process.on('message', message => ShipmentWorker.processMessage(message));

module.exports = ShipmentWorker;
@@ -202,27 +202,60 @@
 ror(
-'Expected a string'
+%60Exportpath should be a string, $%7BexportPath%7D given%60
 );%0A 
@@ -406,18 +406,19 @@
 tPath%7D d
-id
+oes
  not pro
@@ -449,20 +449,8 @@
 ment
- -- aborting
 %60);%0A

update error messages in ShipmentWorker