development-project.ts 4.6 KB

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