link-solana-version.mjs 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. #!/usr/bin/env zx
  2. import 'zx/globals';
  3. import { getInstalledSolanaVersion, getSolanaVersion } from './utils.mjs';
  4. const expectedVersion = getSolanaVersion();
  5. const installedVersion = await getInstalledSolanaVersion();
  6. const installPath = path.join(
  7. os.homedir(),
  8. '.local',
  9. 'share',
  10. 'solana',
  11. 'install'
  12. );
  13. const releasePath = path.join(
  14. installPath,
  15. 'releases',
  16. expectedVersion,
  17. 'solana-release'
  18. );
  19. const activeReleasePath = path.join(installPath, 'active_release');
  20. const hasRelease = await fs.exists(releasePath);
  21. if (!installedVersion) {
  22. echo(
  23. chalk.red('[ ERROR ]'),
  24. `No Solana installation found. Solana ${expectedVersion} is required for this project.`
  25. );
  26. await askToInstallSolana(expectedVersion);
  27. } else if (installedVersion === expectedVersion) {
  28. echo(
  29. chalk.green('[ SUCCESS ]'),
  30. `The expected Solana version ${expectedVersion} is installed.`
  31. );
  32. } else if (hasRelease) {
  33. await $`rm -f "${activeReleasePath}"`;
  34. await $`ln -s "${releasePath}" "${activeReleasePath}"`;
  35. echo(
  36. chalk.green('[ SUCCESS ]'),
  37. `Successfully switched from Solana version ${installedVersion} to ${expectedVersion} to match the project's requirements.`
  38. );
  39. } else {
  40. echo(
  41. chalk.yellow('[ WARNING ]'),
  42. `Cannot switch from Solana version ${installedVersion} to ${expectedVersion} because it is not installed.`
  43. );
  44. await askToInstallSolana(expectedVersion);
  45. }
  46. async function askToInstallSolana(version) {
  47. const installRelease = await question('Should we install it now? [y/N] ');
  48. if (installRelease === 'y') {
  49. await installSolana(version);
  50. echo(
  51. chalk.green('[ SUCCESS ]'),
  52. `Successfully installed Solana version ${version}.`
  53. );
  54. } else {
  55. process.exit(1);
  56. }
  57. }
  58. async function installSolana(version) {
  59. echo(`Installing Solana ${version}...`);
  60. const cutoff = '1.18.19';
  61. const isBeforeCutoff =
  62. (await $`[[ "$(printf '%s\n' "${cutoff}" "${version}" | sort -V | head -n1)" = "${version}" ]] && [[ "${cutoff}" != "${version}" ]]`.quiet()
  63. .exitCode) == 0;
  64. if (isBeforeCutoff) {
  65. await $`sh -c "$(curl -sSfL https://release.solana.com/v${version}/install)"`;
  66. } else {
  67. await $`sh -c "$(curl -sSfL https://release.anza.xyz/v${version}/install)"`;
  68. }
  69. }