Skip to content

Commit d4dbfa1

Browse files
committed
Improve Solana version link script
1 parent 84d8aec commit d4dbfa1

File tree

4 files changed

+57
-30
lines changed

4 files changed

+57
-30
lines changed

.changeset/plenty-tools-invite.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
"create-solana-program": patch
3+
---
4+
5+
Improve Solana version link script
6+
7+
`pnpm solana:link` now also asks you to download the required Solana version if it's not already installed. Additionally, if the required Solana version is equal to or greater than `1.18.19`, the install URL will use `release.anza.xyz` instead of `release.solana.com`.

template/base/scripts/check-solana-version.mjs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,13 @@ import { getInstalledSolanaVersion, getSolanaVersion } from './utils.mjs';
55
const expectedVersion = getSolanaVersion();
66
const installedVersion = await getInstalledSolanaVersion();
77

8-
if (installedVersion !== expectedVersion) {
8+
if (!installedVersion) {
9+
echo(
10+
chalk.red('[ ERROR ]'),
11+
`No Solana installation found. Please install Solana ${expectedVersion} before proceeding.`
12+
);
13+
process.exit(1);
14+
} else if (installedVersion !== expectedVersion) {
915
echo(
1016
chalk.yellow('[ WARNING ]'),
1117
`The installed Solana version ${installedVersion} does not match the expected version ${expectedVersion}.`

template/base/scripts/link-solana-version.mjs

Lines changed: 42 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,8 @@
22
import 'zx/globals';
33
import { getInstalledSolanaVersion, getSolanaVersion } from './utils.mjs';
44

5-
const installedVersion = await getInstalledSolanaVersion();
65
const expectedVersion = getSolanaVersion();
7-
8-
if (installedVersion === expectedVersion) {
9-
echo(
10-
chalk.green('[ SUCCESS ]'),
11-
`The expected Solana version ${expectedVersion} is installed.`
12-
);
13-
process.exit(0);
14-
}
6+
const installedVersion = await getInstalledSolanaVersion();
157

168
const installPath = path.join(
179
os.homedir(),
@@ -29,28 +21,54 @@ const releasePath = path.join(
2921
const activeReleasePath = path.join(installPath, 'active_release');
3022
const hasRelease = await fs.exists(releasePath);
3123

32-
if (hasRelease) {
24+
if (!installedVersion) {
25+
echo(
26+
chalk.red('[ ERROR ]'),
27+
`No Solana installation found. Solana ${expectedVersion} is required for this project.`
28+
);
29+
await askToInstallSolana(expectedVersion);
30+
} else if (installedVersion === expectedVersion) {
31+
echo(
32+
chalk.green('[ SUCCESS ]'),
33+
`The expected Solana version ${expectedVersion} is installed.`
34+
);
35+
} else if (hasRelease) {
3336
await $`rm -f "${activeReleasePath}"`;
3437
await $`ln -s "${releasePath}" "${activeReleasePath}"`;
3538
echo(
3639
chalk.green('[ SUCCESS ]'),
3740
`Successfully switched from Solana version ${installedVersion} to ${expectedVersion} to match the project's requirements.`
3841
);
39-
process.exit(0);
42+
} else {
43+
echo(
44+
chalk.yellow('[ WARNING ]'),
45+
`Cannot switch from Solana version ${installedVersion} to ${expectedVersion} because it is not installed.`
46+
);
47+
await askToInstallSolana(expectedVersion);
4048
}
4149

42-
echo(
43-
chalk.yellow('[ WARNING ]'),
44-
`Cannot switch from Solana version ${installedVersion} to ${expectedVersion} because it is not installed.`
45-
);
46-
47-
const installRelease = await question('Should we install it now? [y/N] ');
48-
if (installRelease === 'y') {
49-
echo(`Installing Solana ${expectedVersion}...`);
50-
await $`sh -c "$(curl -sSfL https://release.solana.com/v${expectedVersion}/install)"`;
50+
async function askToInstallSolana(version) {
51+
const installRelease = await question('Should we install it now? [y/N] ');
52+
if (installRelease === 'y') {
53+
await installSolana(version);
54+
echo(
55+
chalk.green('[ SUCCESS ]'),
56+
`Successfully installed Solana version ${version}.`
57+
);
58+
} else {
59+
process.exit(1);
60+
}
5161
}
5262

53-
echo(
54-
chalk.green('[ SUCCESS ]'),
55-
`Successfully switched from Solana version ${installedVersion} to ${expectedVersion} to match the project's requirements.`
56-
);
63+
async function installSolana(version) {
64+
echo(`Installing Solana ${version}...`);
65+
const cutoff = '1.18.19';
66+
const isBeforeCutoff =
67+
(await $`[[ "$(printf '%s\n' "${cutoff}" "${version}" | sort -V | head -n1)" = "${version}" ]] && [[ "${cutoff}" != "${version}" ]]`.quiet()
68+
.exitCode) == 0;
69+
if (isBeforeCutoff) {
70+
await $`sh -c "$(curl -sSfL https://release.solana.com/v${version}/install)"`;
71+
} else {
72+
await $`sh -c "$(curl -sSfL https://release.anza.xyz/v${version}/install)"`;
73+
}
74+
}

template/base/scripts/utils.mjs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -121,10 +121,6 @@ export async function getInstalledSolanaVersion() {
121121
const { stdout } = await $`solana --version`.quiet();
122122
return stdout.match(/(\d+\.\d+\.\d+)/)?.[1];
123123
} catch (error) {
124-
echo(
125-
chalk.red('[ ERROR ]'),
126-
`No Solana installation found. Please install Solana ${getSolanaVersion()} before proceeding.`
127-
);
128-
process.exit(1);
124+
return '';
129125
}
130126
}

0 commit comments

Comments
 (0)