{"version":3,"file":"configuration-providers.mjs","names":[],"sources":["../../src/lib/configuration-providers.ts"],"sourcesContent":["import { ConfigurationFiles } from '@cli-forge/parser';\n\nlet md: typeof import('markdown-factory') | undefined;\ntry {\n  // markdown-factory is an optional peer dependency\n  md = require('markdown-factory');\n} catch {\n  // not available\n}\n\n/**\n * A collection of built-in configuration provider factories. These should be invoked and passed to\n * {@link CLI.config} to load configuration from various sources. For custom configuration providers, see\n * https://craigory.dev/cli-forge/api/parser/namespaces/ConfigurationFiles/type-aliases/ConfigurationProvider\n *\n * @example\n * ```typescript\n * import { cli, ConfigurationProviders } from 'cli-forge';\n *\n * cli(...).config(ConfigurationProviders.PackageJson('myConfig'));\n * ```\n */\nexport const ConfigurationProviders = {\n  /**\n   * Load configuration from a package.json file.\n   *\n   * @param key The key in the package.json file to load as configuration.\n   */\n  PackageJson<T>(key: string) {\n    return ConfigurationFiles.getPackageJsonConfigurationLoader<T>(key);\n  },\n\n  /**\n   * Load configuration from a JSON file (or files).\n   *\n   * @param filename The filename (or array of possible filenames) of the JSON file to load.\n   *   When an array is provided, each filename gets its own provider wrapped in an aggregate.\n   * @param key The key in the JSON file to load as configuration. By default, the entire JSON object is loaded.\n   */\n  JsonFile<T>(\n    filename: string | string[],\n    key?: string\n  ): ConfigurationFiles.AnyConfigProvider<T> {\n    const transform = key ? (json: any) => json[key] : undefined;\n    const writeTransform = key\n      ? (json: any, config: T) => ({ ...json, [key]: config })\n      : undefined;\n\n    if (Array.isArray(filename)) {\n      const aggregate = ConfigurationFiles.getJsonFileConfigLoader<T>(\n        filename,\n        transform,\n        writeTransform\n      );\n      if (key) {\n        for (let i = 0; i < aggregate.providers.length; i++) {\n          const provider = aggregate.providers[i];\n          if (!ConfigurationFiles.isAggregateConfigProvider(provider)) {\n            applyKeyDescribeConfig(provider, filename[i], key);\n          }\n        }\n      }\n      return aggregate;\n    }\n\n    const loader = ConfigurationFiles.getJsonFileConfigLoader<T>(\n      filename,\n      transform,\n      writeTransform\n    );\n    if (key) {\n      applyKeyDescribeConfig(loader, filename, key);\n    }\n    return loader;\n  },\n};\n\nfunction applyKeyDescribeConfig<T>(\n  loader: ConfigurationFiles.ConfigurationProvider<T>,\n  filename: string,\n  key: string\n) {\n  loader.describeConfig = () => {\n    const heading = `JSON File: ${filename} (key: \"${key}\")`;\n    if (md) {\n      return {\n        heading,\n        body: md.lines(\n          `Searches for ${md.code(filename)}`,\n          'Resolution walks up the directory tree from the working directory, using the nearest match.',\n          `Reads the ${md.code(`\"${key}\"`)} key from the JSON file.`,\n          `Supports ${md.code('\"extends\"')} for configuration inheritance.`,\n          '',\n          md.bold('Example:'),\n          md.codeBlock(\n            JSON.stringify({ [key]: { option: 'value' } }, null, 2),\n            'json'\n          )\n        ),\n      };\n    }\n    return {\n      heading,\n      body: [\n        `Searches for ${filename}`,\n        'Resolution walks up the directory tree from the working directory, using the nearest match.',\n        `Reads the \"${key}\" key from the JSON file.`,\n        'Supports \"extends\" for configuration inheritance.',\n      ].join('\\n\\n'),\n    };\n  };\n}\n"],"mappings":";;;AAEA,IAAI;AACJ,IAAI;AAEF,MAAA,UAAa,mBAAmB;QAC1B;;;;;;;;;;;;;AAgBR,MAAa,yBAAyB;CAMpC,YAAe,KAAa;AAC1B,SAAO,mBAAmB,kCAAqC,IAAI;;CAUrE,SACE,UACA,KACyC;EACzC,MAAM,YAAY,OAAO,SAAc,KAAK,OAAO,KAAA;EACnD,MAAM,iBAAiB,OAClB,MAAW,YAAe;GAAE,GAAG;IAAO,MAAM;GAAQ,IACrD,KAAA;AAEJ,MAAI,MAAM,QAAQ,SAAS,EAAE;GAC3B,MAAM,YAAY,mBAAmB,wBACnC,UACA,WACA,eACD;AACD,OAAI,IACF,MAAK,IAAI,IAAI,GAAG,IAAI,UAAU,UAAU,QAAQ,KAAK;IACnD,MAAM,WAAW,UAAU,UAAU;AACrC,QAAI,CAAC,mBAAmB,0BAA0B,SAAS,CACzD,wBAAuB,UAAU,SAAS,IAAI,IAAI;;AAIxD,U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