"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const tslib_1 = require("tslib");
const routing_controllers_1 = require("@gem-mine/routing-controllers");
const launch = require("launch-code-editor");
const project_1 = require("../service/project");
const model_1 = require("../util/model");
const util = require('util');
const path = require('path');
const fs = require('fs-extra');
const exec = util.promisify(require('child_process').exec);
let default_1 = class default_1 {
    getList(path = '', projectId, offset = 0, limit = model_1.LIMIT) {
        return tslib_1.__awaiter(this, void 0, void 0, function* () {
            const query = yield project_1.default.getList({ path, projectId, offset, limit });
            const result = model_1.pagination({ query, offset, limit });
            return result;
        });
    }
    createOne(modal) {
        return tslib_1.__awaiter(this, void 0, void 0, function* () {
            const data = yield project_1.default.createOne(modal);
            return data;
        });
    }
    getOne(id) {
        return tslib_1.__awaiter(this, void 0, void 0, function* () {
            const data = yield project_1.default.getOne(id);
            return { data };
        });
    }
    updateOne(id, { name, env, version, star }) {
        return tslib_1.__awaiter(this, void 0, void 0, function* () {
            const data = yield project_1.default.updateOne(id, { name, env, version, star });
            return { data };
        });
    }
    deleteOne(id) {
        return tslib_1.__awaiter(this, void 0, void 0, function* () {
            const resp = yield project_1.default.getOne(id);
            const path = resp.path;
            if (fs.existsSync(path)) {
                fs.renameSync(path, `${path}_delete`);
            }
            const data = yield project_1.default.deleteOne(id);
            return data;
        });
    }
    generate(modal) {
        return tslib_1.__awaiter(this, void 0, void 0, function* () {
            const targetPath = modal.path;
            fs.ensureDirSync(targetPath);
            const data = yield project_1.default.createOne(modal);
            yield exec(`gem-mine-cli generate -c --name ${modal.name} -d`, {
                cwd: path.join(targetPath),
                windowsHide: true
            });
            return {
                data
            };
        });
    }
    upgrade(modal) {
        return tslib_1.__awaiter(this, void 0, void 0, function* () {
            const targetPath = modal.path;
            const exist = fs.existsSync(targetPath);
            if (exist) {
                yield exec(`gem-mine-cli generate -d`, {
                    cwd: path.join(targetPath),
                    windowsHide: true
                });
                return {
                    success: true
                };
            }
            else {
                return {
                    exists: false
                };
            }
        });
    }
    open(modal) {
        return tslib_1.__awaiter(this, void 0, void 0, function* () {
            const { path, editor } = modal;
            if (!fs.existsSync(path)) {
                return {
                    success: false,
                    message: '文件打开失败，请检查本地文件路径是否正确'
                };
            }
            try {
                yield launch(`${path}`, editor);
                return {
                    success: true
                };
            }
            catch (error) {
                // 硬编码 绕过 launch-code-editor 异常
                if (error.message.indexOf('Cannot destructure property') > -1) {
                    return { success: true };
                }
                return {
                    success: false,
                    message: '文件打开失败，请检查本地文件路径是否正确；编辑器是否已安装！'
                };
            }
        });
    }
};
tslib_1.__decorate([
    routing_controllers_1.Get('/'),
    tslib_1.__param(0, routing_controllers_1.QueryParam('path')),
    tslib_1.__param(1, routing_controllers_1.QueryParam('project_id')),
    tslib_1.__param(2, routing_controllers_1.QueryParam('offset')),
    tslib_1.__param(3, routing_controllers_1.QueryParam('limit')),
    tslib_1.__metadata("design:type", Function),
    tslib_1.__metadata("design:paramtypes", [String, String, Number, Number]),
    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('/:id'),
    tslib_1.__param(0, routing_controllers_1.Param('id')),
    tslib_1.__metadata("design:type", Function),
    tslib_1.__metadata("design:paramtypes", [String]),
    tslib_1.__metadata("design:returntype", Promise)
], default_1.prototype, "getOne", null);
tslib_1.__decorate([
    routing_controllers_1.Put('/:id'),
    tslib_1.__param(0, routing_controllers_1.Param('id')), tslib_1.__param(1, routing_controllers_1.Body()),
    tslib_1.__metadata("design:type", Function),
    tslib_1.__metadata("design:paramtypes", [String, Object]),
    tslib_1.__metadata("design:returntype", Promise)
], default_1.prototype, "updateOne", null);
tslib_1.__decorate([
    routing_controllers_1.Delete('/:id'),
    tslib_1.__param(0, routing_controllers_1.Param('id')),
    tslib_1.__metadata("design:type", Function),
    tslib_1.__metadata("design:paramtypes", [String]),
    tslib_1.__metadata("design:returntype", Promise)
], default_1.prototype, "deleteOne", null);
tslib_1.__decorate([
    routing_controllers_1.Post('/generate'),
    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, "generate", null);
tslib_1.__decorate([
    routing_controllers_1.Post('/upgrade'),
    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, "upgrade", null);
tslib_1.__decorate([
    routing_controllers_1.Post('/open'),
    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, "open", null);
default_1 = tslib_1.__decorate([
    routing_controllers_1.Controller('/project')
], default_1);
exports.default = default_1;
//# sourceMappingURL=data:application/json;base64, 