renderContext.ts 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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 anchorVersion = resolveAnchorVersion(anchorVersionDetected);
  57. const solanaVersion = resolveSolanaVersion(
  58. language,
  59. inputs.solanaVersion,
  60. solanaVersionDetected
  61. );
  62. const rustVersion = resolveRustVersion(
  63. language,
  64. solanaVersion,
  65. inputs.rustVersion,
  66. rustVersionDetected
  67. );
  68. // Directories.
  69. const templateDirectory = path.resolve(__dirname, 'template');
  70. const currentDirectory = process.cwd();
  71. const targetDirectory = path.join(
  72. currentDirectory,
  73. inputs.targetDirectoryName
  74. );
  75. const programDirectory = path.join(targetDirectory, 'program');
  76. const clientDirectory = path.join(targetDirectory, 'client');
  77. return {
  78. ...inputs,
  79. anchorVersion,
  80. clientDirectory,
  81. clients,
  82. currentDirectory,
  83. getNpmCommand,
  84. language,
  85. packageManager,
  86. programAddress,
  87. programDirectory,
  88. repositoryUrl,
  89. rustVersion,
  90. solanaVersion,
  91. targetDirectory,
  92. templateDirectory,
  93. toolchain: rustVersion.full,
  94. };
  95. }