"use strict";
/// <reference path="../node_modules/immutable/dist/immutable.d.ts" />
var task_resolve_1 = require("./task.resolve");
var path_1 = require("path");
var command_init_1 = require("./command.init");
var _ = require('../lodash.custom');
(function (SignalTypes) {
    SignalTypes[SignalTypes["Exit"] = 'Exit'] = "Exit";
    SignalTypes[SignalTypes["FileWrite"] = 'FileWrite'] = "FileWrite";
})(exports.SignalTypes || (exports.SignalTypes = {}));
var SignalTypes = exports.SignalTypes;
/**
 * @type {{cwd: *, runMode: string, resumeOnError: boolean, summary: string, strict: boolean}}
 */
var defaults = {
    /**
     * The current working directory, we never advise changing this
     */
    cwd: process.cwd(),
    /**
     * By default, tasks will wait in line and run when the previous
     * one completes. You can set this to 'parallel' instead
     * if you wish for your code to run as fast as possible
     */
    runMode: task_resolve_1.TaskRunModes.series,
    resumeOnError: false,
    parallel: false,
    input: [],
    /**
     * Dump json to disk for debugging
     */
    dump: false,
    debug: false,
    dryRun: false,
    dryRunDuration: 1000,
    force: false,
    /**
     * How much task information should be output
     * following task completion/setup
     */
    verbose: 2,
    /**
     * How should task summaries be output
     */
    reporter: 'default',
    /**
     * Will eliminate any crossbow output.
     *
     */
    outputOnly: false,
    /**
     * What to do with child process output
     */
    suppressOutput: false,
    /**
     * Log when tasks start/end
     */
    progress: false,
    /**
     * should the resolved workload be handed off
     * to the caller?
     */
    handoff: false,
    /**
     * Show the prompt for selecting tasks?
     */
    interactive: false,
    /**
     *
     */
    nodeModulesPaths: ['node_modules'],
    /**
     *
     * CI mode - will exit if any shell/npm scripts
     * return a non-zero exit code
     *
     * Should a failing task be allowed to quit the process?
     */
    fail: true,
    /**
     * Crossbow will add all options to your environment vars
     * and will be path-based + prefixed
     * eg:
     *  options: {
     *      docker: {
     *          port: 8000
     *      }
     *  }
     *
     *  ->
     *      CB_DOCKER_PORT=8000
     */
    envPrefix: 'cb',
    /**
     * Global ENV vars
     */
    env: {},
    /**
     * Tasks that should be run before any watchers begin
     */
    before: [],
    /**
     * Global block flag for watcher tasks
     */
    block: false,
    /**
     * Any tasks that should be skipped
     */
    skip: [],
    /**
     *
     */
    type: command_init_1.InitConfigFileTypes.yaml,
    reporters: [],
    tasksDir: ['tasks']
};
/**
 * Allow single char flags such as
 *    $ crossbow run task1 task2 -p
 *
 * @type {{p: flagTransforms.p}}
 */
var flagTransforms = {
    /**
     * Take any -e flags and set them
     * on the config.env vars.
     *
     * eg: crossbow run task.js -e PET=kittie
     */
    e: function (opts) {
        opts.e.forEach(function (string) {
            var split = string.split('=').map(function (x) { return x.trim(); }).filter(Boolean);
            if (split.length === 2) {
                opts.env[split[0]] = split[1];
            }
        });
        return opts;
    },
    /**
     * If parallel run mode has been set, update the
     * corresponding runMode options too
     */
    parallel: function (opts) {
        if (opts.parallel === true) {
            opts.runMode = task_resolve_1.TaskRunModes.parallel;
            return opts;
        }
        opts.runMode = task_resolve_1.TaskRunModes.series;
        return opts;
    },
    cwd: function (opts) {
        opts.cwd = path_1.resolve(opts.cwd);
        return opts;
    },
    input: function (opts) {
        if (opts.input && !Array.isArray(opts.input)) {
            opts.input = [opts.input];
        }
        return opts;
    }
};
/**
 * Merge default with incoming opts.
 * Also deal with single char flag
 * @returns {*}
 */
function merge(opts) {
    var newOpts = _.assign({}, defaults, opts);
    return Object.keys(flagTransforms)
        .reduce(function (opts, x) {
        if (opts[x] !== undefined) {
            return flagTransforms[x].call(null, opts);
        }
        return opts;
    }, newOpts);
}
exports.merge = merge;
//# sourceMappingURL=config.js.map