"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const path_1 = __importDefault(require("path"));
const koa_1 = __importDefault(require("koa"));
const koa_static_1 = __importDefault(require("koa-static"));
const koa_router_1 = __importDefault(require("koa-router"));
const detect_port_1 = __importDefault(require("detect-port"));
class Server {
    constructor() {
        this.app = new koa_1.default();
        this.router = new koa_router_1.default();
        this.port = 3000;
    }
    launch() {
        return __awaiter(this, void 0, void 0, function* () {
            const publicPath = path_1.default.resolve(__dirname, '../assets');
            this.app.use(this.router.routes()).use((0, koa_static_1.default)(publicPath));
            return new Promise((resolve, reject) => {
                (0, detect_port_1.default)(this.port)
                    .then((_port) => {
                    if (this.port !== _port) {
                        console.log(`端口: ${this.port} 已被占用, 尝试新端口: ${_port}`);
                        this.port = _port;
                    }
                    this.app.listen(this.port, () => {
                        resolve(true);
                    });
                })
                    .catch((err) => {
                    console.log(err);
                    reject();
                });
            });
        });
    }
}
exports.default = Server;
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiYXBwLmpzIiwic291cmNlUm9vdCI6IiIsInNvdXJjZXMiOlsiLi4vLi4vc2VydmVyL2FwcC50cyJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiOzs7Ozs7Ozs7Ozs7OztBQUFBLGdEQUF1QjtBQUN2Qiw4Q0FBcUI7QUFDckIsNERBQWtDO0FBQ2xDLDRE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