"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
}) : (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.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
    __setModuleDefault(result, mod);
    return result;
};
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 });
exports.initCode = void 0;
const child_process_1 = require("child_process");
const path = __importStar(require("path"));
const fs = __importStar(require("fs"));
const open_1 = __importDefault(require("open"));
const render_engine_1 = require("@ui-studio/render-engine");
const options_1 = require("./options");
exports.initCode = () => __awaiter(void 0, void 0, void 0, function* () {
    const { GENERATED_CODE_PATH, FUNCTIONS_PATH, PREVIEW_SERVER_PORT, PREVIEW_CLIENT_PORT, SERVER_PORT, } = yield options_1.getOptions();
    const clientPath = path.join(GENERATED_CODE_PATH, 'client');
    const serverPath = path.join(GENERATED_CODE_PATH, 'server');
    yield render_engine_1.run(FUNCTIONS_PATH, true);
    const logError = (error, stdout, stderr) => {
        if (error) {
            console.error(`exec error: ${error}`);
            return;
        }
        if (stdout)
            console.log(`stdout: ${stdout}`);
        if (stderr)
            console.error(`stderr: ${stderr}`);
    };
    const startPreviewServer = () => {
        console.log(`Starting server at ${FUNCTIONS_PATH}...`);
        child_process_1.exec(`nodemon -w ${path.join(FUNCTIONS_PATH, 'build')} -x "env PORT=${PREVIEW_SERVER_PORT} yarn dev"`, {
            cwd: serverPath,
        });
    };
    const startPreviewClient = () => {
        console.log(`Starting client as ${clientPath}...`);
        child_process_1.exec(`env BROWSER=none PORT=${PREVIEW_CLIENT_PORT} yarn start`, { cwd: clientPath }, logError);
    };
    const buildFunctions = () => {
        console.log(`Building functions at ${FUNCTIONS_PATH}...`);
        child_process_1.execSync('yarn build', { cwd: FUNCTIONS_PATH, stdio: 'inherit' });
    };
    const installPackages = () => {
        console.log(`Installing client packages as ${clientPath}...`);
        child_process_1.execSync('yarn --force --prefer-offline', { cwd: clientPath, stdio: 'inherit' });
        console.log(`Installing server packages at ${serverPath}...`);
        child_process_1.execSync('yarn --force --prefer-offline', { cwd: serverPath, stdio: 'inherit' });
    };
    installPackages();
    buildFunctions();
    startPreviewServer();
    startPreviewClient();
    open_1.default(`http://localhost:${SERVER_PORT}`);
    fs.watch(path.join(FUNCTIONS_PATH, 'package.json'), { recursive: true }, () => {
        installPackages();
        buildFunctions();
    });
    fs.watch(path.join(FUNCTIONS_PATH, 'src'), { recursive: true }, buildFunctions);
});
//# sourceMappingURL=preview.js.map