maests
Version:
An executable compiler for creating Maestro's yaml-flows with typescript.
61 lines (60 loc) • 2.09 kB
JavaScript
import fs, { writeFileSync } from "fs";
import path, { join } from "path";
import dotenv from "dotenv";
import { consola } from "consola";
import { rewriteCode } from "./rewrite-code.mjs";
import { defineCommand, runMain } from "citty";
import { createYamlOutPath, jiti } from "./utils.mjs";
import { execSync } from "child_process";
const main = defineCommand({
args: {
path: {
type: "positional",
required: !0
},
device: {
type: "string",
alias: "d",
description: "Device to run the test on"
}
},
async run({ args }) {
loadEnv();
const cwd = process.cwd(), fullFlowPath = args.path.startsWith("/") ? args.path : join(cwd, args.path), code = await rewriteCode(fullFlowPath), tempFilePath = fullFlowPath.replace(".ts", ".temp.ts");
writeFileSync(tempFilePath, code);
const yamlOutPath = createYamlOutPath(fullFlowPath);
try {
await jiti.import(tempFilePath), consola.success(`Created Yaml to ${yamlOutPath} \u2714`);
} catch (e) {
console.error(e), fs.unlinkSync(tempFilePath), process.exit(1);
}
fs.unlinkSync(tempFilePath);
const command = `maestro ${args.device ? `--device ${args.device}` : ""} test ${yamlOutPath}`;
try {
execSync(command, {
stdio: "inherit",
env: process.env
}), console.log("Test passed");
} catch (e) {
"status" in e && e.status === 1 ? consola.error({
message: `Test failed: ${fullFlowPath}`
}) : consola.error({
message: `Failed to start test: ${fullFlowPath}`,
additional: `You can check actual yaml file at ${yamlOutPath}`
}), process.exit(1);
}
}
});
runMain(main);
function loadEnv() {
let currentPath = process.cwd(), dotEnvPath = "";
for (; currentPath !== "/"; ) {
if (fs.readdirSync(currentPath).includes(".env")) {
dotEnvPath = path.join(currentPath, ".env");
break;
}
currentPath = path.join(currentPath, "../");
}
dotEnvPath && (consola.info(`Found .env file at ${dotEnvPath}`), dotenv.config({ path: dotEnvPath }));
}