dump-config.ts 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  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. loadOptionalPatches,
  12. loadOverlayPatches,
  13. renderConfigDump,
  14. type ConfigDumpLayer,
  15. } from '@deepseek-ai/dsh-app-boot'
  16. import { homePatchPath, prepareProfile, PROFILE_ROOT_FILENAME } from './profile-boot.ts'
  17. const NAME = 'dsh'
  18. /* v8 ignore start -- built-bin acceptance drives this boot-free dispatch */
  19. /**
  20. * Print a profile composition with comments naming each source file and patch layer.
  21. * @param profile - the profile name.
  22. * @param defaultOnly - omit the profile's user layer and `--patch` overlays
  23. * (the recovery diagnostic for a broken `cordis.patch.yml`, which is then
  24. * never parsed).
  25. * @param patches - `--patch` overlay paths, in argv order.
  26. * @param fromDefaultProfile - shipped template used once to initialize a missing profile.
  27. */
  28. export function runDumpConfig(
  29. profile: string,
  30. defaultOnly: boolean,
  31. patches: readonly string[],
  32. fromDefaultProfile?: string,
  33. ): void {
  34. const loaded = prepareProfile(profile, !defaultOnly, fromDefaultProfile)
  35. const layers: ConfigDumpLayer[] = loaded.layers.map(layer => ({
  36. label: layer.packageName,
  37. patches: layer.patches,
  38. }))
  39. if (!defaultOnly) {
  40. if (existsSync(loaded.patchPath)) {
  41. layers.push({ label: loaded.patchPath, patches: loaded.patches })
  42. }
  43. const homePatchFile = homePatchPath()
  44. const homePatches = loadOptionalPatches(NAME, homePatchFile)
  45. if (homePatches !== undefined) {
  46. layers.push({ label: homePatchFile, patches: homePatches })
  47. }
  48. for (const file of patches) {
  49. const absolute = resolve(file)
  50. layers.push({ label: absolute, patches: loadOverlayPatches(NAME, absolute) })
  51. }
  52. }
  53. // The dump anchors on the same empty root file the boot includes.
  54. process.stdout.write(renderConfigDump(NAME, join(loaded.dir, PROFILE_ROOT_FILENAME), layers))
  55. }
  56. /* v8 ignore stop */