"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var chalk_1 = require("chalk");
var file_utility_1 = require("../common/file-utility");
var glob = require("glob");
var os_1 = require("os");
var config_1 = require("../common/config");
/**
 * Concatenate all SQL files into a single file.
 */
var Cat = /** @class */ (function () {
    function Cat(name, options) {
        this.name = name;
        this.options = options;
    }
    Cat.prototype.invoke = function () {
        var start = process.hrtime();
        var config = new config_1.default(this.options.config);
        var conn = config.getConnection(this.name);
        var output = '';
        // order is important
        var directories = [
            config.output.schemas,
            config.output.tables,
            config.output.views,
            config.output['scalar-valued'],
            config.output['table-valued'],
            config.output.views,
            config.output.procs,
            config.output.triggers
        ];
        var fs = new file_utility_1.default(config, conn);
        var rootx = '';
        if (config.output.root > '') {
            rootx = config.output.root;
        }
        else {
            rootx = conn.name;
        }
        for (var _i = 0, directories_1 = directories; _i < directories_1.length; _i++) {
            var dir = directories_1[_i];
            var files = glob.sync(rootx + "/" + dir + "/**/*.sql");
            for (var _a = 0, files_1 = files; _a < files_1.length; _a++) {
                var file = files_1[_a];
                var content = fs.readFileSync(file).toString();
                var end = content.substr(-2).toLowerCase();
                output += content;
                output += os_1.EOL;
                output += (end !== 'go' ? 'go' : '');
                output += os_1.EOL + os_1.EOL;
            }
        }
        fs.write("" + config.output.root, conn.name + "_concatenate.sql", output);
        var time = process.hrtime(start);
        console.log(chalk_1.default.green("Finished after " + time[0] + "s!"));
    };
    return Cat;
}());
exports.default = Cat;
//# sourceMappingURL=cat.js.map