development-project.ts 4.0 KB

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