import { isBundledSEA } from './chunk-KPFLSGGP.js';
import { fmt } from './chunk-NEWKASA7.js';
import { define } from './chunk-WIL4LMMA.js';
import { CliExitError } from './chunk-7M6WZ65V.js';
import { require_semver, ansis_default, downloadPluginToCache } from './chunk-LTYLEVHC.js';
import { __toESM, __name } from './chunk-IBAHCH6O.js';

// src/cli/commands/install-plugin.command.ts
var import_semver = __toESM(require_semver(), 1);
var commandSpec = define({
  name: "install-plugin",
  description: "Download and cache a plugin from npm for use with the standalone binary",
  args: {
    plugin: {
      type: "positional",
      description: "Plugin to install, in the format name@version (e.g. my-plugin@1.2.3)"
    }
  },
  examples: `
Pre-downloads a plugin into the local varlock plugin cache so it is available without
needing an interactive confirmation prompt. This is useful in CI environments or any
other non-interactive workflow where the standalone binary is used.

The plugin must be specified with an exact version number.

Examples:
  varlock install-plugin my-plugin@1.2.3
  varlock install-plugin @my-scope/my-plugin@2.0.0
`.trim()
});
var commandFn = /* @__PURE__ */ __name(async (ctx) => {
  if (!isBundledSEA()) {
    throw new CliExitError("This command is only available when using the standalone varlock binary", {
      suggestion: `In a JS project, install plugins as regular dependencies using your package manager.
For example: ${fmt.command("add my-plugin", { jsPackageManager: true })}`
    });
  }
  const pluginDescriptor = ctx.values.plugin;
  if (!pluginDescriptor) {
    throw new CliExitError("No plugin specified", {
      suggestion: "Usage: varlock install-plugin <name@version>  (e.g. my-plugin@1.2.3)"
    });
  }
  const atLocation = pluginDescriptor.lastIndexOf("@");
  if (atLocation === -1) {
    throw new CliExitError(`Missing version in "${pluginDescriptor}"`, {
      suggestion: `Specify an exact version, e.g. \`varlock install-plugin ${pluginDescriptor}@1.2.3\``
    });
  }
  const moduleName = pluginDescriptor.slice(0, atLocation);
  const versionDescriptor = pluginDescriptor.slice(atLocation + 1);
  if (!versionDescriptor) {
    throw new CliExitError(`Missing version in "${pluginDescriptor}"`, {
      suggestion: `Specify an exact version, e.g. \`varlock install-plugin ${moduleName}@1.2.3\``
    });
  }
  if (!import_semver.default.valid(versionDescriptor)) {
    throw new CliExitError(`"${versionDescriptor}" is not an exact version`, {
      suggestion: `Use a fixed version number (e.g. 1.2.3), not a range. Example: \`varlock install-plugin ${moduleName}@1.2.3\``
    });
  }
  console.log(`
\u{1F4E6} Installing plugin ${ansis_default.bold(`${moduleName}@${versionDescriptor}`)} into local cache...
`);
  try {
    const cachedPath = await downloadPluginToCache(moduleName, versionDescriptor);
    console.log(`\u2705 Plugin ${ansis_default.bold(`${moduleName}@${versionDescriptor}`)} installed successfully`);
    console.log(ansis_default.dim(`   Cached at: ${cachedPath}
`));
  } catch (err) {
    throw new CliExitError(`Failed to install plugin "${moduleName}@${versionDescriptor}"`, {
      details: err.message
    });
  }
}, "commandFn");

export { commandFn, commandSpec };
//# sourceMappingURL=chunk-JE6B7XUX.js.map
//# sourceMappingURL=chunk-JE6B7XUX.js.map