{"version":3,"file":"init-C2l3jnfq.mjs","names":["path","fs"],"sources":["../src/init.ts"],"sourcesContent":["import * as fs from 'fs-extra'\nimport * as path from 'path'\nimport { CONFIG_FILENAMES } from '~/constants/index.js'\nimport { logger } from './utils/logger.js'\n\n\ninterface InitOptions {\n  force?: boolean\n}\n\n// cosmiconfig's TypeScript loader needs the classic compiler API, which was\n// removed in TypeScript 7.\nconst MAX_TYPESCRIPT_CLASSIC_MAJOR = 6\n\nconst TS_CONFIG_TEMPLATE = `import { defineConfig, FileNamingStyle } from '@keq-request/cli'\n\nexport default defineConfig({\n  // Output directory for generated files\n  outdir: './apis',\n\n  // Rendering options for code generation\n  rendering: {\n    // File naming style for generated files\n    fileNamingStyle: FileNamingStyle.raw,\n  },\n\n  // OpenAPI/Swagger document sources\n  modules: {\n    // Example: petstore: 'https://petstore3.swagger.io/api/v3/openapi.json'\n  },\n\n  // Plugins to extend code generation functionality\n  plugins: [],\n})\n`\n\nconst JS_CONFIG_TEMPLATE = `const { defineConfig } = require('@keq-request/cli')\n\nmodule.exports = defineConfig({\n  // Output directory for generated files\n  outdir: './apis',\n\n  // Rendering options for code generation\n  rendering: {\n    // File naming style for generated files\n    // Options: 'raw', 'camelCase', 'capitalCase', 'constantCase', 'dotCase', 'headerCase',\n    //          'noCase', 'paramCase', 'pascalCase', 'pathCase', 'sentenceCase',\n    //          'snakeCase', 'kebabCase'\n    fileNamingStyle: 'raw',\n  },\n\n  // OpenAPI/Swagger document sources\n  modules: {\n    // Example: petstore: 'https://petstore3.swagger.io/api/v3/openapi.json'\n  },\n\n  // Plugins to extend code generation functionality\n  plugins: [],\n})\n`\n\nconst MJS_CONFIG_TEMPLATE = `import { defineConfig, FileNamingStyle } from '@keq-request/cli'\n\nexport default defineConfig({\n  // Output directory for generated files\n  outdir: './apis',\n\n  // Rendering options for code generation\n  rendering: {\n    // File naming style for generated files\n    fileNamingStyle: FileNamingStyle.raw,\n  },\n\n  // OpenAPI/Swagger document sources\n  modules: {\n    // Example: petstore: 'https://petstore3.swagger.io/api/v3/openapi.json'\n  },\n\n  // Plugins to extend code generation functionality\n  plugins: [],\n})\n`\n\n/**\n * Check if a file exists in the current directory or any parent directory\n */\nasync function findFileInParents(filename: string, startDir: string = process.cwd()): Promise<string | null> {\n  let currentDir = startDir\n\n  while (true) {\n    const filePath = path.join(currentDir, filename)\n    if (await fs.pathExists(filePath)) {\n      return filePath\n    }\n\n    const parentDir = path.dirname(currentDir)\n    // Reached the root directory\n    if (parentDir === currentDir) {\n      return null\n    }\n    currentDir = parentDir\n  }\n}\n\n/**\n * Detect if the project can load a TypeScript config file.\n *\n * A `.keqrc.ts` is loaded through cosmiconfig's TypeScript loader, which relies\n * on the classic compiler API. That API still exists in TypeScript 5.x and 6.x,\n * but was removed in TypeScript 7, and the loader also fails when `typescript`\n * cannot be resolved from the project at all.\n */\nasync function detectTypeScript(cwd: string): Promise<boolean> {\n  const tsconfigPath = await findFileInParents('tsconfig.json', cwd)\n  if (tsconfigPath === null) {\n    return false\n  }\n\n  const typescriptPackageJsonPath = await findFileInParents('node_modules/typescript/package.json', cwd)\n  if (typescriptPackageJsonPath === null) {\n    return false\n  }\n\n  try {\n    const { version } = await fs.readJson(typescriptPackageJsonPath)\n    if (typeof version !== 'string') {\n      return false\n    }\n\n    return Number.parseInt(version, 10) <= MAX_TYPESCRIPT_CLASSIC_MAJOR\n  } catch {\n    return false\n  }\n}\n\n/**\n * Detect project module system from package.json\n */\nasync function detectModuleSystem(cwd: string): Promise<'esm' | 'cjs'> {\n  const packageJsonPath = await findFileInParents('package.json', cwd)\n\n  if (packageJsonPath) {\n    try {\n      const packageJson = await fs.readJson(packageJsonPath)\n      if (packageJson.type === 'module') {\n        return 'esm'\n      }\n    } catch (error) {\n      // Ignore JSON parse errors\n    }\n  }\n\n  return 'cjs'\n}\n\n/**\n * Get the appropriate config filename and content based on project setup\n */\nexport async function getConfigFileInfo(cwd: string = process.cwd()): Promise<{ filename: string; content: string }> {\n  const hasTypeScript = await detectTypeScript(cwd)\n  const moduleSystem = await detectModuleSystem(cwd)\n\n  if (hasTypeScript) {\n    return {\n      filename: '.keqrc.ts',\n      content: TS_CONFIG_TEMPLATE,\n    }\n  }\n\n  if (moduleSystem === 'esm') {\n    return {\n      filename: '.keqrc.mjs',\n      content: MJS_CONFIG_TEMPLATE,\n    }\n  }\n\n  return {\n    filename: '.keqrc.js',\n    content: JS_CONFIG_TEMPLATE,\n  }\n}\n\n/**\n * Check if any config file already exists\n */\nasync function findExistingConfigFile(): Promise<string | null> {\n  for (const filename of CONFIG_FILENAMES) {\n    const filePath = path.join(process.cwd(), filename)\n    if (await fs.pathExists(filePath)) {\n      return filename\n    }\n  }\n\n  return null\n}\n\nexport async function initConfig(options: InitOptions): Promise<void> {\n  try {\n    // Check for existing config file\n    const existingConfig = await findExistingConfigFile()\n\n    if (existingConfig && !options.force) {\n      logger.error(`Config file already exists: ${existingConfig}`)\n      logger.error('Use --force to overwrite')\n      process.exit(1)\n    }\n\n    // Get config file info based on project setup\n    const { filename, content } = await getConfigFileInfo()\n    const filePath = path.join(process.cwd(), filename)\n\n    // Write config file\n    await fs.writeFile(filePath, content, 'utf8')\n\n    logger.log(`Created config file: ${filename}`)\n\n    // Print helpful next steps\n    console.log('\\nNext steps:')\n    console.log('1. Edit the config file to add your OpenAPI/Swagger document URLs')\n    console.log('2. Run \"keq build\" to generate API client code')\n  } catch (error) {\n    logger.error(error instanceof Error ? error.message : String(error))\n    process.exit(1)\n  }\n}\n"],"mappings":";;;;;AAYA,MAAM,+BAA+B;AAErC,MAAM,qBAAqB;;;;;;;;;;;;;;;;;;;;;AAsB3B,MAAM,qBAAqB;;;;;;;;;;;;;;;;;;;;;;;;AAyB3B,MAAM,sBAAsB;;;;;;;;;;;;;;;;;;;;;;;;AAyB5B,eAAe,kBAAkB,UAAkB,WAAmB,QAAQ,KAAK,EAA0B;CAC3G,IAAI,aAAa;AAEjB,QAAO,MAAM;EACX,MAAM,WAAWA,OAAK,KAAK,YAAY,S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