{"version":3,"file":"index.mjs","names":[],"sources":["../../../src/initConfig/index.ts"],"sourcesContent":["import { writeFile } from 'node:fs/promises';\nimport { join, relative } from 'node:path';\nimport { readAsset } from 'utils:asset';\nimport { colorizePath, logger, v } from '@intlayer/config/logger';\nimport {\n  type configurationFilesCandidates,\n  searchConfigurationFile,\n} from '@intlayer/config/node';\n\ntype ConfigFormat = 'ts' | 'cjs' | 'mjs' | 'js' | 'json';\n\nconst getTemplatePath = (format: ConfigFormat) => {\n  switch (format) {\n    case 'ts':\n      return './templates/ts.txt';\n    case 'cjs':\n      return './templates/cjs.txt';\n    case 'mjs':\n      return './templates/mjs.txt';\n    case 'js':\n      return './templates/mjs.txt';\n    case 'json':\n      return './templates/json.txt';\n    default:\n      return './templates/ts.txt';\n  }\n};\n\n/** Values injected into the configuration file template. */\nexport type InitConfigOptions = {\n  /** Locales detected in the project, inserted into `internationalization.locales`. */\n  locales?: string[];\n  /**\n   * Dev-server URL of the detected framework, inserted as\n   * `editor.applicationURL` (the template defaults to `http://localhost:3000`).\n   */\n  applicationURL?: string;\n};\n\n/**\n * Initialize the Intlayer configuration file. No-ops when a configuration file\n * already exists at `baseDir`.\n */\nexport const initConfig = async (\n  format: (typeof configurationFilesCandidates)[number],\n  baseDir: string,\n  options?: InitConfigOptions\n) => {\n  //   Search for configuration file\n  const { configurationFilePath } = searchConfigurationFile(baseDir);\n\n  //   return if the configuration file is found\n  if (configurationFilePath) {\n    const relativePath = relative(baseDir, configurationFilePath);\n    logger(`${v} ${colorizePath(relativePath)} already exists`);\n    return;\n  }\n\n  // Extract the format from the filename (e.g. 'intlayer.config.ts' -> 'ts')\n  const extension = format.split('.').pop() as ConfigFormat;\n\n  const templatePath = getTemplatePath(extension);\n  let configContent = readAsset(templatePath);\n\n  const { locales, applicationURL } = options ?? {};\n\n  if (locales && locales.length > 0) {\n    if (extension === 'json') {\n      const localesString = JSON.stringify(locales);\n      configContent = configContent.replace('[\"en\"]', localesString);\n    } else {\n      const localesString = `[${locales.map((locale) => `'${locale}'`).join(', ')}]`;\n      configContent = configContent.replace('[Locales.ENGLISH]', localesString);\n    }\n  }\n\n  if (applicationURL) {\n    configContent = configContent.replace(\n      'http://localhost:3000',\n      applicationURL\n    );\n  }\n\n  await writeFile(join(baseDir, format), configContent, 'utf8');\n  logger(`${v} Created ${colorizePath(format)}`);\n};\n"],"mappings":";;;;;;;AAWA,MAAM,mBAAmB,WAAyB;CAChD,QAAQ,QAAR;EACE,KAAK,MACH,OAAO;EACT,KAAK,OACH,OAAO;EACT,KAAK,OACH,OAAO;EACT,KAAK,MACH,OAAO;EACT,KAAK,QACH,OAAO;EACT,SACE,OAAO;CACX;AACF;;;;;AAiBA,MAAa,aAAa,OACxB,QACA,SACA,YACG;CAEH,MAAM,EAAE,0BAA0B,wBAAwB,OAAO;CAGjE,IAAI,uBAAuB;EACzB,MAAM,eAAe,SAAS,SAAS,qBAAqB;EAC5D,OAAO,GAAG,EAAE,GAAG,aAAa,YAAY,EAAE,gBAAgB;EAC1D;CACF;CAGA,MAAM,YAAY,OAAO,MAAM,GAAG,CAAC,CAAC,IAAI;CAExC,MAAM,eAAe,gBAAgB,SAAS;CAC9C,IAAI,gBAAgB,UAAU,YAAY;CAE1C,MAAM,EAAE,SAAS,mBAAmB,WAAW,CAAC;CAEhD,IAAI,WAAW,QAAQ,SAAS,GAAG;EACjC,IAAI,cAAc,QAAQ;GACxB,MAAM,gBAAgB,KAAK,UAAU,OAAO;GAC5C,gBAAgB,cAAc,QAAQ,YAAU,aAAa;EAC/D,OAAO;GACL,MAAM,gBAAgB,IAAI,QAAQ,KAAK,WAAW,IAAI,OAAO,EAAE,CAAC,CAAC,KAAK,IAAI,EAAE;GAC5E,gBAAgB,cAAc,QAAQ,qBAAqB,aAAa;EAC1E;CACF;CAEA,IAAI,gBACF,gBAAgB,cAAc,QAC5B,yBACA,cACF;CAGF,MAAM,UAAU,KAAK,SAAS,MAAM,GAAG,eAAe,MAAM;CAC5D,OAAO,GAAG,EAAE,WAAW,aAAa,MAAM,GAAG;AAC/C"}