"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const tslib_1 = require("tslib");
const routing_controllers_1 = require("@gem-mine/routing-controllers");
const path = require("path");
const fs = require("fs-extra");
const terminalExec_1 = require("../util/terminalExec");
const db_1 = require("../db");
let default_1 = class default_1 {
    getList({ component_id, }) {
        return tslib_1.__awaiter(this, void 0, void 0, function* () {
            return {
                data: [
                    yield db_1.db.get('component')
                        .find({
                        component_id
                    }).value()
                ].filter(Boolean)
            };
        });
    }
    createOne(modal) {
        return tslib_1.__awaiter(this, void 0, void 0, function* () {
            const data = {
                component_id: modal.component_id,
                token: modal.token
            };
            yield db_1.db.get('component')
                .updateOrAdd(data, 'component_id')
                .write();
            return {
                data
            };
        });
    }
    openExplorer(_projectPath, context) {
        return tslib_1.__awaiter(this, void 0, void 0, function* () {
            const projectPath = path.resolve(_projectPath);
            if (!fs.existsSync(projectPath)) {
                context.throw(400, '指定路径不存在');
            }
            const projectPkgPath = path.resolve(projectPath, './package.json');
            if (!fs.existsSync(projectPkgPath)) {
                context.throw(400, '指定路径不是NPM项目');
            }
            const pkgInfo = yield fs.readJson(projectPkgPath, {
                throws: false
            });
            return pkgInfo;
        });
    }
    terminalExec(_projectPath, command, context) {
        return tslib_1.__awaiter(this, void 0, void 0, function* () {
            const projectPath = path.resolve(_projectPath);
            if (!fs.existsSync(projectPath)) {
                context.throw(400, '指定路径不存在');
            }
            yield terminalExec_1.default(command, {
                windowsHide: false,
                cwd: projectPath
            });
            return {};
        });
    }
};
tslib_1.__decorate([
    routing_controllers_1.Get('/'),
    tslib_1.__param(0, routing_controllers_1.QueryParams()),
    tslib_1.__metadata("design:type", Function),
    tslib_1.__metadata("design:paramtypes", [Object]),
    tslib_1.__metadata("design:returntype", Promise)
], default_1.prototype, "getList", null);
tslib_1.__decorate([
    routing_controllers_1.Post('/'),
    tslib_1.__param(0, routing_controllers_1.Body()),
    tslib_1.__metadata("design:type", Function),
    tslib_1.__metadata("design:paramtypes", [Object]),
    tslib_1.__metadata("design:returntype", Promise)
], default_1.prototype, "createOne", null);
tslib_1.__decorate([
    routing_controllers_1.Get('/getPkgInfo'),
    tslib_1.__param(0, routing_controllers_1.QueryParam('path')),
    tslib_1.__param(1, routing_controllers_1.Ctx()),
    tslib_1.__metadata("design:type", Function),
    tslib_1.__metadata("design:paramtypes", [String, Object]),
    tslib_1.__metadata("design:returntype", Promise)
], default_1.prototype, "openExplorer", null);
tslib_1.__decorate([
    routing_controllers_1.Get('/terminalExec'),
    tslib_1.__param(0, routing_controllers_1.QueryParam('path')),
    tslib_1.__param(1, routing_controllers_1.QueryParam('command')),
    tslib_1.__param(2, routing_controllers_1.Ctx()),
    tslib_1.__metadata("design:type", Function),
    tslib_1.__metadata("design:paramtypes", [String, String, Object]),
    tslib_1.__metadata("design:returntype", Promise)
], default_1.prototype, "terminalExec", null);
default_1 = tslib_1.__decorate([
    routing_controllers_1.Controller('/component')
], default_1);
exports.default = default_1;
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiY29tcG9uZW50LmpzIiwic291cmNlUm9vdCI6IiIsInNvdXJjZXMiOlsiLi4vLi4vc3JjL2NvbnRyb2xsZXIvY29tcG9uZW50LnRzIl0sIm5hbWVzIjpbXSwibWFwcGluZ3MiOiI7OztBQUNBLHVFQUF5RztBQUV6Ryw2QkFBNEI7QUFDNUIsK0JBQThCO0FBQzlCLHVEQUErQztBQUMvQyw4QkFBMEI7QUFHMUIsZ0JBQUE7SUFFUSxPQUFPLENBQWdCLEVBQzNCLFlBQVksR0FHYjs7WUFDQyxPQUFPO2dCQUNMLElBQUksRUFBRTtvQkFDSixNQUFNLE9BQUUsQ0FBQyxHQUFHLENBQUMsV0FBVyxDQUFDO3lCQUN0QixJQUFJLENBQUM7d0JBQ0osWUFBWTtxQkFDYixDQUFDLENBQUMsS0FBSyxFQUFFO2lCQUNiLENBQUMsTUFBTSxDQUFDLE9BQU8sQ0FBQzthQUNsQixDQUFBO1FBQ0gsQ0FBQztLQU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