renderContext.ts 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. import * as path from 'node:path';
  2. import { Client, Inputs, allClients } from './inputs';
  3. import { Language } from './localization';
  4. import {
  5. PackageManager,
  6. getPackageManager,
  7. getPackageManagerCommand,
  8. } from './packageManager';
  9. import { ResolvedVersion, Version } from './version-core';
  10. import { resolveRustVersion } from './version-rust';
  11. import { resolveAnchorVersion } from './version-anchor';
  12. import { resolveSolanaVersion } from './version-solana';
  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. rustVersion: ResolvedVersion;
  27. solanaVersion: ResolvedVersion;
  28. targetDirectory: string;
  29. templateDirectory: string;
  30. toolchain: string;
  31. };
  32. export function getRenderContext({
  33. inputs,
  34. language,
  35. programAddress,
  36. solanaVersionDetected,
  37. rustVersionDetected,
  38. anchorVersionDetected,
  39. }: {
  40. inputs: Inputs;
  41. language: Language;
  42. programAddress: string;
  43. solanaVersionDetected: Version;
  44. rustVersionDetected?: Version;
  45. anchorVersionDetected?: Version;
  46. }): RenderContext {
  47. const packageManager = getPackageManager();
  48. const clients = allClients.flatMap((client) =>
  49. inputs[`${client}Client`] ? [client] : []
  50. );
  51. const getNpmCommand: RenderContext['getNpmCommand'] = (...args) =>
  52. getPackageManagerCommand(packageManager, ...args);
  53. // Versions.
  54. const anchorVersion = resolveAnchorVersion(anchorVersionDetected);
  55. const solanaVersion = resolveSolanaVersion(
  56. language,
  57. inputs.solanaVersion,
  58. solanaVersionDetected
  59. );
  60. const rustVersion = resolveRustVersion(
  61. language,
  62. solanaVersion,
  63. inputs.rustVersion,
  64. rustVersionDetected
  65. );
  66. // Directories.
  67. const templateDirectory = path.resolve(__dirname, 'template');
  68. const currentDirectory = process.cwd();
  69. const targetDirectory = path.join(
  70. currentDirectory,
  71. inputs.targetDirectoryName
  72. );
  73. const programDirectory = path.join(targetDirectory, 'program');
  74. const clientDirectory = path.join(targetDirectory, 'client');
  75. return {
  76. ...inputs,
  77. anchorVersion,
  78. clientDirectory,
  79. clients,
  80. currentDirectory,
  81. getNpmCommand,
  82. language,
  83. packageManager,
  84. programAddress,
  85. programDirectory,
  86. rustVersion,
  87. solanaVersion,
  88. targetDirectory,
  89. templateDirectory,
  90. toolchain: rustVersion.full,
  91. };
  92. }