dump-config.ts 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. /**
  2. * Config-dump entry for `dsh --profile <name> --dump-config`: compose the
  3. * profile's patch layers through the include plugin's patch algorithm without
  4. * booting or evaluating `!!js`, with one source layer per bundle, the
  5. * profile's own patch file, and each `--patch` overlay.
  6. * @module @deepseek-ai/dsh/dump-config
  7. */
  8. import { existsSync } from 'node:fs'
  9. import { join, resolve } from 'node:path'
  10. import {
  11. composeProfileStack,
  12. formatRowConflict,
  13. loadOptionalPatches,
  14. loadOverlayPatches,
  15. renderConfigDump,
  16. type ConfigDumpLayer,
  17. type StackUserLayer,
  18. } from '@deepseek-ai/dsh-app-boot'
  19. import { homePatchPath, prepareProfile, PROFILE_ROOT_FILENAME } from './profile-boot.ts'
  20. const NAME = 'dsh'
  21. /* v8 ignore start -- built-bin acceptance drives this boot-free dispatch */
  22. /**
  23. * Print a profile composition with comments naming each source file and patch layer.
  24. * @param profile - the profile name.
  25. * @param defaultOnly - omit the profile's user layer and `--patch` overlays
  26. * (the recovery diagnostic for a broken `cordis.patch.yml`, which is then
  27. * never parsed).
  28. * @param patches - `--patch` overlay paths, in argv order.
  29. * @param fromDefaultProfile - shipped template used once to initialize a missing profile.
  30. */
  31. export function runDumpConfig(
  32. profile: string,
  33. defaultOnly: boolean,
  34. patches: readonly string[],
  35. fromDefaultProfile?: string,
  36. ): void {
  37. const loaded = prepareProfile(profile, !defaultOnly, fromDefaultProfile)
  38. const userLayers: StackUserLayer[] = []
  39. if (!defaultOnly) {
  40. if (existsSync(loaded.patchPath)) {
  41. userLayers.push({ label: loaded.patchPath, patches: loaded.patches })
  42. }
  43. const homePatchFile = homePatchPath()
  44. const homePatches = loadOptionalPatches(NAME, homePatchFile)
  45. if (homePatches !== undefined) {
  46. userLayers.push({ label: homePatchFile, patches: homePatches })
  47. }
  48. for (const file of patches) {
  49. const absolute = resolve(file)
  50. userLayers.push({ label: absolute, patches: loadOverlayPatches(NAME, absolute) })
  51. }
  52. }
  53. // The same composition boot mounts: external bundles as contained groups,
  54. // a bundle or row left out by an id conflict reported, not shown.
  55. const stack = composeProfileStack(NAME, loaded.layers, userLayers)
  56. for (const conflict of stack.conflicts) process.stderr.write(`${NAME}: ${formatRowConflict(conflict)}\n`)
  57. const layers: ConfigDumpLayer[] = stack.layers.map(layer => ({ label: layer.label, patches: layer.patches }))
  58. // The dump anchors on the same empty root file the boot includes.
  59. process.stdout.write(renderConfigDump(NAME, join(loaded.dir, PROFILE_ROOT_FILENAME), layers))
  60. }
  61. /* v8 ignore stop */