runConcurrentTasks().catch((error) => {
	console.error("Error running tasks:", error);
	Deno.exit(1);
});

async function runConcurrentTasks() {
	const tasks = [
		runCommand("deno task build:watch"),
		runCommand("deno task serve:watch"),
	];

	const results = await Promise.all(tasks);
	if (results.includes(false)) {
		console.error("One or more tasks failed.");
		Deno.exit(1);
	} else {
		console.log("All tasks completed successfully.");
	}
}

async function runCommand(fullCommand: string) {
	const [command, ...args] = fullCommand.split(" ");

	const cmd = new Deno.Command(command!, {
		args,
		stdout: "piped",
	});
	const process = cmd.spawn();

	const status = await process.status;
	if (status.code !== 0) {
		console.error(`Command failed: ${command}`);
		return false;
	}

	return true;
}
