"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const fs_1 = __importDefault(require("fs"));
const path_1 = __importDefault(require("path"));
const mri_1 = __importDefault(require("mri"));
const modules_1 = require("./modules");
const server_1 = require("./server");
const argv = (0, mri_1.default)(process.argv.slice(2));
const { serverPort, workspaceDir, extensionCandidate, isDev, extHostPath, watchServerPort } = argv;
let serverAppOpts = {
    modules: modules_1.modules,
};
let clientAppOpts = {};
/**
 * sumi-dev.config.js 用于在插件开发时自定义一些 opensumi client 及 server 端的默认配置
 * 当传入多个 extensionDir 时，优先取第一个插件目录下的 sumi-dev.config.js
 */
const extensions = strToArray(extensionCandidate);
const opensumiDevConfigPath = path_1.default.resolve(extensions[0], 'sumi-dev.config.js');
// read `sumi-dev.config.js`
if (fs_1.default.existsSync(opensumiDevConfigPath)) {
    const opensumiDevConfig = require(opensumiDevConfigPath);
    serverAppOpts = {
        ...serverAppOpts,
        ...opensumiDevConfig.serverAppOpts,
    };
    clientAppOpts = { ...opensumiDevConfig.clientAppOpts };
}
(0, server_1.startServer)({
    port: Number(serverPort),
    isDev: !!isDev,
    workspaceDir: workspaceDir,
    extensionCandidate: extensionCandidate ? strToArray(extensionCandidate) : undefined,
    extHostPath: extHostPath,
    watchServerPort,
}, {
    serverAppOpts,
    clientAppOpts,
});
function strToArray(item) {
    return Array.isArray(item) ? item : [item];
}
