#!/usr/bin/env node
'use strict';

var child_process = require('child_process');
var path = require('path');
var fs = require('fs');
var module$1 = require('module');

var _documentCurrentScript = typeof document !== 'undefined' ? document.currentScript : null;
function _interopDefault (e) { return e && e.__esModule ? e : { default: e }; }

var path__default = /*#__PURE__*/_interopDefault(path);

var __defProp = Object.defineProperty;
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
var __require = /* @__PURE__ */ ((x) => typeof require !== "undefined" ? require : typeof Proxy !== "undefined" ? new Proxy(x, {
  get: (a, b) => (typeof require !== "undefined" ? require : a)[b]
}) : x)(function(x) {
  if (typeof require !== "undefined")
    return require.apply(this, arguments);
  throw Error('Dynamic require of "' + x + '" is not supported');
});
var __defProp2 = Object.defineProperty;
var __name2 = /* @__PURE__ */ __name((target, value) => __defProp2(target, "name", { value, configurable: true }), "__name");
var __require2 = /* @__PURE__ */ ((x) => typeof __require !== "undefined" ? __require : typeof Proxy !== "undefined" ? new Proxy(x, {
  get: (a, b) => (typeof __require !== "undefined" ? __require : a)[b]
}) : x)(function(x) {
  if (typeof __require !== "undefined")
    return __require.apply(this, arguments);
  throw Error('Dynamic require of "' + x + '" is not supported');
});
function rootDir() {
  return process.cwd();
}
__name(rootDir, "rootDir");
__name2(rootDir, "rootDir");
function outputDirOverride() {
  const { CHROMATIC_ARCHIVE_LOCATION } = process.env;
  return CHROMATIC_ARCHIVE_LOCATION;
}
__name(outputDirOverride, "outputDirOverride");
__name2(outputDirOverride, "outputDirOverride");
function archivesDir(defaultOutputDir) {
  const outputDir = outputDirOverride() || defaultOutputDir;
  return path__default.default.resolve(rootDir(), outputDir, "chromatic-archives");
}
__name(archivesDir, "archivesDir");
__name2(archivesDir, "archivesDir");
function checkArchivesDirExists(defaultOutputDir) {
  const dir = archivesDir(defaultOutputDir);
  if (!fs.existsSync(dir)) {
    throw new Error(`Chromatic archives directory cannot be found: ${dir}

Please make sure that you have run your E2E tests, or have set the CHROMATIC_ARCHIVE_LOCATION env var if the output directory for the tests is not in the standard location.`);
  }
}
__name(checkArchivesDirExists, "checkArchivesDirExists");
__name2(checkArchivesDirExists, "checkArchivesDirExists");
var req = __require2.resolve ? __require2 : module$1.createRequire((typeof document === 'undefined' ? require('u' + 'rl').pathToFileURL(__filename).href : (_documentCurrentScript && _documentCurrentScript.tagName.toUpperCase() === 'SCRIPT' && _documentCurrentScript.src || new URL('out.js', document.baseURI).href)));
function archiveStorybook(processArgs, configDir2, defaultOutputDir) {
  checkArchivesDirExists(defaultOutputDir);
  child_process.execFileSync("node", [
    binPath(),
    "dev",
    ...processArgs,
    "-c",
    configDir2
  ], {
    stdio: "inherit"
  });
}
__name(archiveStorybook, "archiveStorybook");
__name2(archiveStorybook, "archiveStorybook");
function buildArchiveStorybook(processArgs, configDir2, defaultOutputDir) {
  checkArchivesDirExists(defaultOutputDir);
  child_process.execFileSync("node", [
    binPath(),
    "build",
    ...processArgs,
    "-c",
    configDir2
  ], {
    stdio: "inherit"
  });
}
__name(buildArchiveStorybook, "buildArchiveStorybook");
__name2(buildArchiveStorybook, "buildArchiveStorybook");
function binPath() {
  const filename = req.resolve("storybook/package.json");
  const packageJson = JSON.parse(fs.readFileSync(filename, "utf8"));
  return path.resolve(path.dirname(filename), packageJson.bin);
}
__name(binPath, "binPath");
__name2(binPath, "binPath");

// src/constants.ts
var DEFAULT_OUTPUT_DIR = "cypress/downloads";

// src/bin/archive-storybook.ts
var args = process.argv.slice(2);
var configDir = path__default.default.resolve(__dirname, "../storybook-config");
try {
  archiveStorybook(args, configDir, DEFAULT_OUTPUT_DIR);
} catch (err) {
  console.error(err.message);
  process.exitCode = 1;
}
