getRenderContext.ts 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. import * as path from 'node:path';
  2. import { Client, Inputs, allClients } from './getInputs';
  3. import { Language } from './getLanguage';
  4. import {
  5. PackageManager,
  6. getPackageManager,
  7. getPackageManagerCommand,
  8. } from './getPackageManager';
  9. import { toMinorSolanaVersion } from './solanaCli';
  10. export type RenderContext = Omit<Inputs, 'programAddress' | 'solanaVersion'> & {
  11. anchorVersion: string;
  12. clientDirectory: string;
  13. clients: Client[];
  14. currentDirectory: string;
  15. getNpmCommand: (scriptName: string, args?: string) => string;
  16. language: Language;
  17. programAddress: string;
  18. programDirectory: string;
  19. packageManager: PackageManager;
  20. solanaVersion: string;
  21. solanaVersionDetected: string;
  22. solanaVersionWithoutPatch: string;
  23. targetDirectory: string;
  24. templateDirectory: string;
  25. toolchain: string;
  26. };
  27. export function getRenderContext({
  28. inputs,
  29. language,
  30. programAddress,
  31. solanaVersionDetected,
  32. anchorVersionDetected,
  33. }: {
  34. inputs: Inputs;
  35. language: Language;
  36. programAddress: string;
  37. solanaVersionDetected: string;
  38. anchorVersionDetected?: string;
  39. }): RenderContext {
  40. const packageManager = getPackageManager();
  41. const clients = allClients.flatMap((client) =>
  42. inputs[`${client}Client`] ? [client] : []
  43. );
  44. const getNpmCommand: RenderContext['getNpmCommand'] = (...args) =>
  45. getPackageManagerCommand(packageManager, ...args);
  46. const solanaVersion = resolveSolanaVersion(
  47. language,
  48. inputs.solanaVersion,
  49. solanaVersionDetected
  50. );
  51. const solanaVersionWithoutPatch = toMinorSolanaVersion(
  52. language,
  53. solanaVersion
  54. );
  55. const toolchain = getToolchainFromSolanaVersion(solanaVersionWithoutPatch);
  56. // Directories.
  57. const templateDirectory = path.resolve(__dirname, 'template');
  58. const currentDirectory = process.cwd();
  59. const targetDirectory = path.join(
  60. currentDirectory,
  61. inputs.targetDirectoryName
  62. );
  63. const programDirectory = path.join(targetDirectory, 'program');
  64. const clientDirectory = path.join(targetDirectory, 'client');
  65. return {
  66. ...inputs,
  67. anchorVersion: resolveAnchorVersion(anchorVersionDetected),
  68. clientDirectory,
  69. clients,
  70. currentDirectory,
  71. getNpmCommand,
  72. language,
  73. packageManager,
  74. programAddress,
  75. programDirectory,
  76. solanaVersion,
  77. solanaVersionDetected,
  78. solanaVersionWithoutPatch,
  79. targetDirectory,
  80. templateDirectory,
  81. toolchain,
  82. };
  83. }
  84. function getToolchainFromSolanaVersion(
  85. solanaVersionWithoutPatch: string
  86. ): string {
  87. const map: Record<string, string> = {
  88. '1.17': '1.75.0',
  89. '1.18': '1.75.0',
  90. '2.0': '1.75.0',
  91. };
  92. return map[solanaVersionWithoutPatch] ?? '1.75.0';
  93. }
  94. function resolveSolanaVersion(
  95. language: Language,
  96. inputVersion: string | undefined,
  97. detectedVersion: string
  98. ): string {
  99. if (!inputVersion) {
  100. return detectedVersion;
  101. }
  102. if (!inputVersion.match(/^\d+\.\d+(\.\d+)?$/)) {
  103. throw new Error(
  104. language.errors.invalidSolanaVersion.replace('$version', inputVersion)
  105. );
  106. }
  107. const versionSegments = inputVersion.split('.');
  108. if (versionSegments.length === 3) {
  109. return inputVersion;
  110. }
  111. const map: Record<string, string> = {
  112. '1.17': '1.17.34',
  113. '1.18': '1.18.18',
  114. };
  115. return map[inputVersion] ?? `${inputVersion}.0`;
  116. }
  117. function resolveAnchorVersion(detectedVersion: string | undefined): string {
  118. return detectedVersion ?? '';
  119. }