// ../../adapters/shell/src/shell-prebuild.ts
import { spawn } from "child_process";
var defaultSpawner = (command, { cwd, env }) => new Promise((resolve, reject) => {
  const child = spawn(command, { cwd, env, shell: true, stdio: "inherit" });
  child.once("error", reject);
  child.once("close", (code) => {
    resolve(code);
  });
});
var ShellPreBuildHook = class {
  constructor(config) {
    this.config = config;
    this.spawnFn = config.spawn ?? defaultSpawner;
  }
  config;
  spawnFn;
  async run() {
    const code = await this.spawnFn(this.config.command, {
      cwd: this.config.cwd,
      env: this.config.env ?? process.env
    });
    if (code !== 0) {
      throw new Error(
        `prebuild "${this.config.command}" exited with code ${code ?? "null (signal)"}`
      );
    }
  }
};

// ../../adapters/shell/src/site-config.ts
import { readFile } from "fs/promises";
import { join } from "path";
async function readPrebuildCommand(siteDir) {
  let raw;
  try {
    raw = await readFile(join(siteDir, "package.json"), "utf8");
  } catch {
    return void 0;
  }
  const text = raw.charCodeAt(0) === 65279 ? raw.slice(1) : raw;
  let pkg;
  try {
    pkg = JSON.parse(text);
  } catch {
    return void 0;
  }
  if (typeof pkg !== "object" || pkg === null) return void 0;
  const nano = pkg["nanoesis"];
  if (typeof nano !== "object" || nano === null) return void 0;
  const cmd = nano["prebuild"];
  return typeof cmd === "string" && cmd.trim() !== "" ? cmd : void 0;
}
export {
  ShellPreBuildHook,
  readPrebuildCommand
};
