"use strict";
var _npm_1 = require('./@npm');
var task_resolve_1 = require("../task.resolve");
var task_utils_1 = require("../task.utils");
var file = require("../file.utils");
var merge = require('../../lodash.custom').merge;
var debug = require('debug')('cb:@shell');
module.exports = function (task, trigger) {
    return function (opts, ctx, done) {
        var args = (function () {
            if (task.origin === task_resolve_1.TaskOriginTypes.FileSystem) {
                try {
                    var contentFromDisk = file.readFileContent(task.externalTasks[0]);
                    return _npm_1.getArgs(contentFromDisk);
                }
                catch (e) {
                    return {
                        errors: [e]
                    };
                }
            }
            return _npm_1.getArgs(task.command);
        })();
        if (args.errors.length) {
            done(args.errors[0]);
            return;
        }
        var stdio = _npm_1.getStdio(trigger);
        var cbEnv = task_utils_1.getCBEnv(trigger);
        var env = merge({}, cbEnv, task.env, process.env, trigger.config.env);
        debug("running %s", args.cmd);
        var emitter = _npm_1.runCommand(args.cmd, {
            cwd: trigger.config.cwd,
            env: env,
            stdio: stdio
        });
        _npm_1.handleExit(emitter, done);
        return function tearDownShellAdaptor() {
            _npm_1.teardown(emitter, task);
        };
    };
};
//# sourceMappingURL=@shell.js.map