{"version":3,"file":"buildExamples-Bv-aYKDQ.mjs","names":[],"sources":["../src/lib/docgen/buildExamples.ts"],"sourcesContent":["import { name } from '../../constants.js';\n\nexport interface Example<Flags> {\n  /** A description of the example */\n  description: string;\n  /** The flag arguments to the command */\n  flags: Partial<Flags>;\n}\n\n/**\n * Builds a string of examples for the CLI\n *\n * @param commandPath - The path to the command to run, omitting the `transcend` command name, e.g., `['consent', 'upload-preferences']`\n * @param examples - The examples to build\n * @returns A string of examples for the CLI\n */\nexport function buildExamples<Flags = never>(\n  commandPath: string[],\n  examples: NoInfer<Example<Flags>>[],\n): string {\n  return examples\n    .map((example) => {\n      const exampleCommand = buildExampleCommand<Flags>(commandPath, example.flags);\n      return `**${example.description}**\\n\\n\\`\\`\\`sh\\n${exampleCommand}\\n\\`\\`\\``;\n    })\n    .join('\\n\\n');\n}\n\n/**\n * Builds a command string for an example\n *\n * @param commandPath - The path to the command to run, omitting the `transcend` command name, e.g., `['consent', 'upload-preferences']`\n * @param flags - The flags to build the command with\n * @param options - The options for the command\n * @returns A command string for the example\n */\nexport function buildExampleCommand<Flags = never>(\n  commandPath: string[],\n  flags: NoInfer<Partial<Flags>>,\n  options?: {\n    /** If true, the command will be forced to a single line */\n    forceSingleLine?: boolean;\n    /** If true, the command will be indented */\n    argsIndent?: number;\n  },\n): string {\n  const command = commandPath.join(' ');\n  const flagList = getFlagList(flags);\n  const { forceSingleLine = false, argsIndent = 2 } = options ?? {};\n\n  if (flagList.length === 0) {\n    return `${name} ${command}`;\n  }\n\n  // Break the command into multiple lines if it's too long\n  const exampleCommand =\n    `${name} ${command} ${flagList.join(' ')}`.length <= 117 && !forceSingleLine\n      ? `${command} ${flagList.join(' ')}`\n      : `${command} \\\\\\n${' '.repeat(argsIndent)}${flagList.join(\n          ` \\\\\\n${' '.repeat(argsIndent)}`,\n        )}`;\n\n  // Add `transcend` before command name\n  return `${name} ${exampleCommand}`;\n}\n\n/**\n * Formats a flag value to the bash string for an example command\n *\n * @param value - The value to format\n * @param depth - The depth of the recursion\n * @returns The formatted value\n */\nfunction formatFlagValue(value: unknown, depth = 0): string {\n  if (typeof value === 'boolean') {\n    return value ? 'true' : 'false';\n  }\n\n  if (typeof value === 'number') {\n    return value.toString();\n  }\n\n  if (value instanceof Date) {\n    return value.toISOString();\n  }\n\n  if (Array.isArray(value) && depth === 0) {\n    const values = value.map((v) => formatFlagValue(v, depth + 1));\n    if (values.every((x) => x.startsWith('$') || x.includes(' '))) {\n      return `\"${values.join(',')}\"`;\n    }\n    return values.join(',');\n  }\n\n  if (typeof value === 'string') {\n    // If we're operating on list elements\n    if (depth === 1) {\n      if (value.startsWith('$')) {\n        return `$\\{${value.slice(1)}}`;\n      }\n      return value;\n    }\n\n    // Escape strings that start with $ or contain spaces or special characters\n    return value.startsWith('$') || value.includes(' ') ? `\"${value}\"` : value;\n  }\n\n  throw new Error(`Unsupported value type: ${typeof value}`);\n}\n\n/**\n * Builds a list of flags formatted for an example command\n *\n * @param flags - The flags to build the command with\n * @param depth - The depth of the recursion\n * @returns A list of flags for the example command\n */\nexport function getFlagList<Flags = never>(flags: Partial<Flags>, depth = 0): string[] {\n  return Object.entries(flags).map(([flag, value]) => {\n    if (typeof value === 'boolean' && value) {\n      // For true booleans, just pass the flag alone\n      return `--${flag}`;\n    }\n\n    const formattedValue = formatFlagValue(value, depth);\n\n    return `--${flag}=${formattedValue}`;\n  });\n}\n"],"mappings":"6CAoCA,SAAgB,EACd,EACA,EACA,EAMQ,CACR,IAAM,EAAU,EAAY,KAAK,IAAI,CAC/B,EAAW,EAAY,EAAM,CAC7B,CAAE,kBAAkB,GAAO,aAAa,GAAM,GAAW,EAAE,CAejE,OAbI,EAAS,SAAW,EACf,GAAG,EAAK,GAAG,IAYb,GAAG,EAAK,GAPb,aAAW,EAAQ,GAAG,EAAS,KAAK,IAAI,GAAG,QAAU,KAAO,CAAC,EACzD,GAAG,EAAQ,GAAG,EAAS,KAAK,IAAI,GAChC,GAAG,EAAQ,OAAO,IAAI,O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