"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var tslib_1 = require("tslib");
var common_1 = require("./common");
var util_server_1 = require("@tdb/util.server");
function copyResources() {
    return tslib_1.__awaiter(this, void 0, void 0, function () {
        var FROM, TO, error_1;
        return tslib_1.__generator(this, function (_a) {
            switch (_a.label) {
                case 0:
                    FROM = common_1.fsPath.join(common_1.constants.RESOURCES_PATH_SOURCE, 'external');
                    TO = common_1.constants.RESOURCES_PATH_HOST;
                    common_1.log.info.magenta("Copy " + common_1.log.cyan('editor.code') + " resources\n");
                    common_1.log.info.gray(" FROM: " + common_1.log.white(FROM));
                    common_1.log.info.gray(" TO:   " + common_1.log.white(TO));
                    common_1.log.info();
                    _a.label = 1;
                case 1:
                    _a.trys.push([1, 3, , 4]);
                    return [4, common_1.fs.copyAsync(FROM, TO)];
                case 2:
                    _a.sent();
                    return [2, { success: true }];
                case 3:
                    error_1 = _a.sent();
                    return [2, { success: false, error: error_1 }];
                case 4: return [2];
            }
        });
    });
}
exports.copyResources = copyResources;
exports.editorServer = function () {
    var router = util_server_1.express.router();
    var URL_BASE = common_1.constants.URL.BASE + "/*";
    var RESOURCES_PATH_LOCAL = common_1.constants.RESOURCES_PATH_LOCAL;
    common_1.log.info.gray("Initializing '" + common_1.log.magenta('editor.code') + "' routes:");
    common_1.log.info.gray(" - URL_BASE: " + common_1.log.cyan(URL_BASE));
    common_1.log.info.gray(" - RESOURCES_PATH_LOCAL: " + common_1.log.cyan(RESOURCES_PATH_LOCAL));
    router.get(URL_BASE, function (req, res) {
        var path = req.params[0];
        if (path.indexOf('..') > -1) {
            return res.status(404).send('Not Found');
        }
        var file = common_1.fsPath.join(RESOURCES_PATH_LOCAL, path);
        if (common_1.constants.IS_DEV) {
            common_1.log.info(common_1.log.magenta('GET'), common_1.log.cyan(req.url));
            common_1.log.info.gray("    " + file);
            common_1.log.info();
        }
        return res.sendFile(file);
    });
    return router;
};
//# sourceMappingURL=middleware.js.map