renderContext.ts 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. import * as path from 'node:path';
  2. import { Client, Inputs, allClients } from './inputCore';
  3. import { Language } from './localization';
  4. import {
  5. PackageManager,
  6. getPackageManager,
  7. getPackageManagerCommand,
  8. } from './packageManager';
  9. import { ResolvedVersion, Version } from './versionCore';
  10. import { resolveRustVersion } from './versionRust';
  11. import { resolveAnchorVersion } from './versionAnchor';
  12. import { resolveSolanaVersion } from './versionSolana';
  13. export type RenderContext = Omit<
  14. Inputs,
  15. 'programAddress' | 'rustVersion' | 'solanaVersion'
  16. > & {
  17. anchorVersion?: ResolvedVersion;
  18. clientDirectory: string;
  19. clients: Client[];
  20. currentDirectory: string;
  21. getNpmCommand: (scriptName: string, args?: string) => string;
  22. language: Language;
  23. programAddress: string;
  24. programDirectory: string;
  25. packageManager: PackageManager;
  26. repositoryUrl: string;
  27. rustVersion: ResolvedVersion;
  28. solanaVersion: ResolvedVersion;
  29. targetDirectory: string;
  30. templateDirectory: string;
  31. toolchain: string;
  32. };
  33. export function getRenderContext({
  34. inputs,
  35. language,
  36. programAddress,
  37. solanaVersionDetected,
  38. rustVersionDetected,
  39. anchorVersionDetected,
  40. }: {
  41. inputs: Inputs;
  42. language: Language;
  43. programAddress: string;
  44. solanaVersionDetected: Version;
  45. rustVersionDetected?: Version;
  46. anchorVersionDetected?: Version;
  47. }): RenderContext {
  48. const packageManager = getPackageManager();
  49. const clients = allClients.flatMap((client) =>
  50. inputs[`${client}Client`] ? [client] : []
  51. );
  52. const getNpmCommand: RenderContext['getNpmCommand'] = (...args) =>
  53. getPackageManagerCommand(packageManager, ...args);
  54. const repositoryUrl = `https://github.com/${inputs.organizationName}/${inputs.targetDirectoryName}`;
  55. // Versions.
  56. const solanaVersion = resolveSolanaVersion(
  57. language,
  58. inputs.solanaVersion,
  59. solanaVersionDetected
  60. );
  61. const anchorVersion = resolveAnchorVersion(
  62. anchorVersionDetected,
  63. solanaVersion
  64. );
  65. const rustVersion = resolveRustVersion(
  66. language,
  67. solanaVersion,
  68. inputs.rustVersion,
  69. rustVersionDetected
  70. );
  71. // Directories.
  72. const templateDirectory = path.resolve(__dirname, 'template');
  73. const currentDirectory = process.cwd();
  74. const targetDirectory = path.join(
  75. currentDirectory,
  76. inputs.targetDirectoryName
  77. );
  78. const programDirectory = path.join(targetDirectory, 'program');
  79. const clientDirectory = path.join(targetDirectory, 'client');
  80. return {
  81. ...inputs,
  82. anchorVersion,
  83. clientDirectory,
  84. clients,
  85. currentDirectory,
  86. getNpmCommand,
  87. language,
  88. packageManager,
  89. programAddress,
  90. programDirectory,
  91. repositoryUrl,
  92. rustVersion,
  93. solanaVersion,
  94. targetDirectory,
  95. templateDirectory,
  96. toolchain: rustVersion.full,
  97. };
  98. }