"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 });
exports.testRealizeProject = testRealizeProject;
const filesystem_1 = require("@autobe/filesystem");
const crypto_1 = __importDefault(require("crypto"));
const fs_1 = __importDefault(require("fs"));
const os_1 = __importDefault(require("os"));
const tgrid_1 = require("tgrid");
const ProcessUtil_1 = require("../utils/ProcessUtil");
function testRealizeProject(props, listener) {
    return __awaiter(this, void 0, void 0, function* () {
        const cwd = `${os_1.default.tmpdir()}/autobe-realize-${crypto_1.default.randomUUID()}`;
        yield fs_1.default.promises.mkdtemp(cwd);
        try {
            yield setup(props.files, cwd);
            return yield test(props, listener, cwd);
        }
        catch (error) {
            throw error;
        }
        finally {
            yield fs_1.default.promises.rm(cwd, {
                recursive: true,
                force: true,
            });
        }
    });
}
function setup(files, cwd) {
    return __awaiter(this, void 0, void 0, function* () {
        yield filesystem_1.FileSystemIterator.save({
            root: cwd,
            files,
        });
        const exec = (s) => __awaiter(this, void 0, void 0, function* () {
            return ProcessUtil_1.ProcessUtil.exec(s, {
                cwd,
            });
        });
        yield exec("pnpm install");
        yield exec("pnpm run build:prisma");
        yield exec("pnpm run build:test");
    });
}
function test(props, listener, cwd) {
    return __awaiter(this, void 0, void 0, function* () {
        const worker = new tgrid_1.WorkerConnector(null, listener, "thread");
        yield worker.connect(`${cwd}/bin/test/servant.js`, {
            stdio: "ignore",
            cwd,
        });
        const service = worker.getDriver();
        try {
            const result = yield service.execute({
                reset: props.reset,
                simultaneous: props.simultaneous,
            });
            return result;
        }
        catch (error) {
            throw error;
        }
        finally {
            yield worker.close();
        }
    });
}
//# sourceMappingURL=testRealizeProject.js.map