solanaCli.ts 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. import { Language } from './localization';
  2. import { RenderContext } from './renderContext';
  3. import {
  4. hasCommand,
  5. readStdout,
  6. spawnCommand,
  7. waitForCommand,
  8. } from './commands';
  9. import { VersionWithoutPatch } from './versionCore';
  10. export async function patchSolanaDependencies(
  11. ctx: Pick<RenderContext, 'solanaVersion' | 'targetDirectory'>
  12. ): Promise<void> {
  13. const patchMap: Record<VersionWithoutPatch, string[]> = {
  14. '1.17': ['-p ahash@0.8.12 --precise 0.8.6'],
  15. };
  16. const patches = patchMap[ctx.solanaVersion.withoutPatch] ?? [];
  17. await Promise.all(
  18. patches.map(async (patch) =>
  19. waitForCommand(
  20. spawnCommand('cargo', ['update', ...patch.split(' ')], {
  21. cwd: ctx.targetDirectory,
  22. })
  23. )
  24. )
  25. );
  26. }
  27. export async function generateKeypair(
  28. language: Language,
  29. outfile: string
  30. ): Promise<string> {
  31. const hasSolanaKeygen = await hasCommand('solana-keygen');
  32. if (!hasSolanaKeygen) {
  33. throw new Error(
  34. language.errors.solanaCliNotFound.replace('$command', 'solana-keygen')
  35. );
  36. }
  37. // Run the solana-keygen command to generate a new keypair.
  38. const child = spawnCommand('solana-keygen', [
  39. 'new',
  40. '--no-bip39-passphrase',
  41. '--outfile',
  42. outfile,
  43. ]);
  44. // Wait for the command to finish and read the stdout.
  45. const [stdout] = await Promise.all([
  46. readStdout(child),
  47. waitForCommand(child),
  48. ]);
  49. // Update the render context with the generated address.
  50. const address = stdout.join('').match(/pubkey: (\w+)/)?.[1];
  51. if (!address) {
  52. throw new Error(language.errors.solanaKeygenFailed);
  53. }
  54. return address;
  55. }