getRenderContext.ts 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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(solanaVersion);
  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(solanaVersion: string): string {
  85. const map: Record<string, string> = {
  86. '1.17': '1.75.0',
  87. '1.18': '1.75.0',
  88. '2.0': '1.75.0',
  89. };
  90. return map[solanaVersion] ?? '1.75.0';
  91. }
  92. function resolveSolanaVersion(
  93. language: Language,
  94. inputVersion: string | undefined,
  95. detectedVersion: string
  96. ): string {
  97. if (!inputVersion) {
  98. return detectedVersion;
  99. }
  100. if (!inputVersion.match(/^\d+\.\d+(\.\d+)?$/)) {
  101. throw new Error(
  102. language.errors.invalidSolanaVersion.replace('$version', inputVersion)
  103. );
  104. }
  105. const versionSegments = inputVersion.split('.');
  106. if (versionSegments.length === 3) {
  107. return inputVersion;
  108. }
  109. const map: Record<string, string> = {
  110. '1.17': '1.17.34',
  111. '1.18': '1.18.18',
  112. };
  113. return map[inputVersion] ?? `${inputVersion}.0`;
  114. }
  115. function resolveAnchorVersion(detectedVersion: string | undefined): string {
  116. return detectedVersion ?? '';
  117. }