dump-config.ts 2.4 KB

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