Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: detect Anchor version and add instructions to final note #63

Merged
merged 1 commit into from
May 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 20 additions & 1 deletion packages/create-solana-dapp/lib/final-note.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { bold, magentaBright } from 'chalk'
import { bold, magentaBright, greenBright, redBright, yellowBright } from 'chalk'
import { GetArgsResult } from './get-args-result'
import { validateAnchorVersion } from './validate-anchor-version'

export function finalNote(args: GetArgsResult & { target: string }): string {
const lines: string[] = [
Expand All @@ -12,6 +13,24 @@ export function finalNote(args: GetArgsResult & { target: string }): string {
if (args.anchor !== 'none') {
lines.push(...[`Run Anchor commands:`, cmd(args.packageManager, 'anchor build | test | localnet | deploy')])
lines.push(...[`Generate more features using the following command:`, cmd(args.packageManager, 'feature')])
const { requiredVersion, valid, version } = validateAnchorVersion()
if (!version) {
lines.push(
...[
bold(yellowBright(`Could not find Anchor version. Please install Anchor.`)),
'https://www.anchor-lang.com/docs/installation',
],
)
} else if (!valid) {
lines.push(
...[
yellowBright(`Found Anchor version ${version}. Please upgrade to Anchor ${requiredVersion}.`),
'https://www.anchor-lang.com/release-notes/0.30.0',
],
)
} else {
lines.push(greenBright(`Found Anchor version ${version}. Great!`))
}
}
return lines.join('\n\n')
}
Expand Down
16 changes: 16 additions & 0 deletions packages/create-solana-dapp/lib/parse-version.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { execSync } from 'node:child_process'

function execCommandOutput(command: string) {
return execSync(command, { stdio: ['ignore', 'pipe', 'ignore'] })
.toString()
.trim()
}

export function parseVersion(command: string, regex: RegExp) {
const version = execCommandOutput(command)
const match = version.match(regex)
if (!match) {
throw new Error(`Unable to parse version: ${version}`)
}
return match[1]
}
25 changes: 25 additions & 0 deletions packages/create-solana-dapp/lib/validate-anchor-version.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { parseVersion } from './parse-version'

const requiredVersion = '0.30.0'

function getAnchorVersion(): string | undefined {
try {
return parseVersion('anchor --version', /anchor-cli (.*)/)
} catch (error) {
return undefined
}
}

export function validateAnchorVersion(): {
requiredVersion: string
valid: boolean
version: string | undefined
} {
const version = getAnchorVersion()

return {
requiredVersion,
valid: version === requiredVersion,
version,
}
}
Loading