"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    var desc = Object.getOwnPropertyDescriptor(m, k);
    if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
      desc = { enumerable: true, get: function() { return m[k]; } };
    }
    Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
    if (mod && mod.__esModule) return mod;
    var result = {};
    if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
    __setModuleDefault(result, mod);
    return result;
};
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 fs_extra_1 = require("fs-extra");
const command_1 = require("@oclif/command");
const chalk_1 = __importDefault(require("chalk"));
const portfinder = __importStar(require("portfinder"));
const analyzer_1 = require("../hooks/analyzer");
const logger_1 = require("../utils/logger");
const webpack_1 = require("../utils/webpack");
const server_1 = require("../../server");
class Dev extends command_1.Command {
    static description = 'Start your Sales App in development mode';
    static examples = [
        '$ sales-app dev {account}',
    ];
    static args = [
        {
            name: 'account',
            description: 'the account for which the dev environment should run',
            required: true,
        },
        {
            name: 'path',
            description: 'the path where the Sales App is located',
            default: 'sales-app',
            required: false,
        },
        {
            name: 'port',
            description: 'the first port number allocated to Sales App',
            default: '5413',
            required: false,
        },
    ];
    static flags = {
        'show-placeholders': command_1.flags.boolean({
            description: 'Show placeholders for extension points',
            default: false,
        }),
    };
    async run() {
        // eslint-disable-next-line @typescript-eslint/no-shadow
        const { args, flags } = this.parse(Dev);
        const projectDir = path_1.default.resolve(args.path);
        const projectSrcDir = path_1.default.join(projectDir, 'src');
        if (!(0, fs_extra_1.existsSync)(projectSrcDir)) {
            logger_1.logger.error(`${chalk_1.default.red('Error')} - Sales App project not created. Please run "sales-app create" first`);
            return;
        }
        this.log(`${chalk_1.default.blue('Info')} - Running fsp-analyzer on ${projectDir}...`);
        try {
            await (0, analyzer_1.Analyzer)(projectDir);
            this.log(`${chalk_1.default.green('Success')} - Static analysis passed.`);
        }
        catch {
            logger_1.logger.error(`${chalk_1.default.red('Error')} - Static analysis found issues.`);
        }
        this.log(`${chalk_1.default.green('Success')} - Project started successfully`);
        this.log(`${chalk_1.default.blue('Info')} - Watching for changes in ${projectSrcDir}`);
        const liveReloadPort = await portfinder.getPortPromise({
            port: +args.port + 1,
        });
        await (0, webpack_1.runWebpackDevServer)(projectDir, liveReloadPort);
        (0, server_1.startServer)({
            account: args.account,
            proxyPort: +args.port,
            liveReloadPort,
            showPlaceholders: flags['show-placeholders'],
        });
        process.on('SIGINT', () => this.terminateProcesses());
        process.on('SIGTERM', () => this.terminateProcesses());
    }
    terminateProcesses() {
        logger_1.logger.log(`${chalk_1.default.yellow('Warning')} - Received termination signal. Stopping...`);
        process.exit();
    }
}
exports.default = Dev;
//# sourceMappingURL=dev.js.map