development-project.ts 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. /** Prepare the disposable npm-project view used by an unpackaged Electron shell. */
  2. import {
  3. existsSync,
  4. lstatSync,
  5. mkdirSync,
  6. readFileSync,
  7. readdirSync,
  8. realpathSync,
  9. rmSync,
  10. symlinkSync,
  11. unlinkSync,
  12. writeFileSync,
  13. } from 'node:fs'
  14. import { dirname, join } from 'node:path'
  15. import { createDevelopmentProjectMetadata } from '../src/project-manager.ts'
  16. import type { DesktopRelease } from '../src/release.ts'
  17. import { DESKTOP_RUNTIME_FILE, type DesktopRuntimeDescriptor } from '../src/runtime-tree.ts'
  18. interface PackageManifest {
  19. readonly name?: string
  20. readonly version?: string
  21. }
  22. /** Inputs whose locations differ between the launcher and isolated tests. */
  23. export interface DevelopmentProjectOptions {
  24. /** Directory replaced with the generated development project. */
  25. readonly projectDir: string
  26. /** Current workspace's `apps/cli` package directory. */
  27. readonly cliDir: string
  28. /** Current workspace's private Desktop Host application directory. */
  29. readonly hostDir: string
  30. /** pnpm's workspace-wide virtual-hoist directory. */
  31. readonly dependencyDir: string
  32. /** Release identity written into the disposable project metadata. */
  33. readonly release: DesktopRelease
  34. }
  35. function readManifest(path: string): PackageManifest {
  36. return JSON.parse(readFileSync(path, 'utf8')) as PackageManifest
  37. }
  38. function removeOwnedPath(path: string): void {
  39. let stat: ReturnType<typeof lstatSync>
  40. try {
  41. stat = lstatSync(path)
  42. } catch (error) {
  43. if ((error as NodeJS.ErrnoException).code === 'ENOENT') return
  44. throw error
  45. }
  46. if (stat.isSymbolicLink()) {
  47. unlinkSync(path)
  48. return
  49. }
  50. if (stat.isDirectory()) {
  51. rmSync(path, { recursive: true })
  52. return
  53. }
  54. unlinkSync(path)
  55. }
  56. function linkDirectory(source: string, destination: string): void {
  57. mkdirSync(dirname(destination), { recursive: true })
  58. symlinkSync(realpathSync(source), destination, process.platform === 'win32' ? 'junction' : 'dir')
  59. }
  60. function mirrorDependencyLinks(sourceRoot: string, destinationRoot: string): string[] {
  61. const names: string[] = []
  62. for (const entry of readdirSync(sourceRoot, { withFileTypes: true })) {
  63. if (entry.name === '.bin') continue
  64. const source = join(sourceRoot, entry.name)
  65. if (entry.name.startsWith('@') && (entry.isDirectory() || entry.isSymbolicLink())) {
  66. mkdirSync(join(destinationRoot, entry.name), { recursive: true })
  67. for (const scoped of readdirSync(source, { withFileTypes: true })) {
  68. if (!scoped.isDirectory() && !scoped.isSymbolicLink()) continue
  69. linkDirectory(join(source, scoped.name), join(destinationRoot, entry.name, scoped.name))
  70. names.push(`${entry.name}/${scoped.name}`)
  71. }
  72. continue
  73. }
  74. if (entry.isDirectory() || entry.isSymbolicLink()) {
  75. linkDirectory(source, join(destinationRoot, entry.name))
  76. names.push(entry.name)
  77. }
  78. }
  79. return names
  80. }
  81. /**
  82. * Replace one disposable project with links to the current built workspace.
  83. * @param options - Project destination, CLI package, and release identity.
  84. * @returns the absolute project directory supplied by the caller.
  85. */
  86. export function prepareDevelopmentProject(options: DevelopmentProjectOptions): string {
  87. const cliManifest = readManifest(join(options.cliDir, 'package.json'))
  88. if (cliManifest.name !== '@deepseek-ai/dsh' || cliManifest.version !== options.release.version) {
  89. throw new Error(
  90. `desktop development: apps/cli must be @deepseek-ai/dsh@${options.release.version}, found `
  91. + `${String(cliManifest.name)}@${String(cliManifest.version)}`,
  92. )
  93. }
  94. if (!existsSync(options.dependencyDir)) {
  95. throw new Error('desktop development: workspace dependency links are missing; run pnpm install')
  96. }
  97. const hostManifest = readManifest(join(options.hostDir, 'package.json'))
  98. if (hostManifest.name !== '@deepseek-ai/dsh-desktop-host' || hostManifest.version !== options.release.version) {
  99. throw new Error(
  100. `desktop development: apps/desktop-host must be @deepseek-ai/dsh-desktop-host@${options.release.version}, found `
  101. + `${String(hostManifest.name)}@${String(hostManifest.version)}`,
  102. )
  103. }
  104. if (!existsSync(join(options.hostDir, 'lib', 'index.js'))) {
  105. throw new Error('desktop development: apps/desktop-host/lib/index.js is missing; run pnpm run build')
  106. }
  107. removeOwnedPath(options.projectDir)
  108. createDevelopmentProjectMetadata(options.projectDir, options.release)
  109. const destinationModules = join(options.projectDir, 'node_modules')
  110. mkdirSync(destinationModules, { recursive: true })
  111. const names = mirrorDependencyLinks(options.dependencyDir, destinationModules)
  112. const dshLink = join(destinationModules, '@deepseek-ai', 'dsh')
  113. removeOwnedPath(dshLink)
  114. linkDirectory(options.cliDir, dshLink)
  115. const hostLink = join(destinationModules, '@deepseek-ai', 'dsh-desktop-host')
  116. removeOwnedPath(hostLink)
  117. linkDirectory(options.hostDir, hostLink)
  118. const sharedPackages = [...new Set([...names, '@deepseek-ai/dsh', '@deepseek-ai/dsh-desktop-host'])].flatMap((name) => {
  119. const manifest = readManifest(join(destinationModules, name, 'package.json'))
  120. return typeof manifest.version === 'string' ? [{ name, version: manifest.version, path: `node_modules/${name}` }] : []
  121. })
  122. const runtime: DesktopRuntimeDescriptor = { schemaVersion: 1, release: options.release,
  123. platform: process.platform, arch: process.arch, sharedPackages, files: [] }
  124. writeFileSync(join(options.projectDir, DESKTOP_RUNTIME_FILE), `${JSON.stringify(runtime, undefined, 2)}\n`)
  125. return options.projectDir
  126. }