Skip to content

Commit

Permalink
feat: add support for solana-actions template
Browse files Browse the repository at this point in the history
  • Loading branch information
beeman committed Jul 1, 2024
1 parent 505996b commit c79611c
Show file tree
Hide file tree
Showing 11 changed files with 2,745 additions and 1,357 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,11 @@ exports[`get-args expected args should get args with minimal parameters 1`] = `
"anchorName": "anchor",
"anchorProgram": "my-app",
"dryRun": false,
"isTemplate": false,
"name": "my-app",
"package": "@solana-developers/[email protected]",
"packageManager": "npm",
"path": "",
"pnpm": false,
"preset": "next",
"ui": "tailwind",
Expand All @@ -26,9 +28,11 @@ exports[`get-args expected args should get args with minimal parameters and pack
"anchorName": "anchor",
"anchorProgram": "my-app",
"dryRun": false,
"isTemplate": false,
"name": "my-app",
"package": "@solana-developers/[email protected]",
"packageManager": "npm",
"path": "",
"pnpm": false,
"preset": "next",
"ui": "tailwind",
Expand All @@ -45,9 +49,11 @@ exports[`get-args expected args should get args with minimal parameters and pack
"anchorName": "anchor",
"anchorProgram": "my-app",
"dryRun": false,
"isTemplate": false,
"name": "my-app",
"package": "@solana-developers/[email protected]",
"packageManager": "pnpm",
"path": "",
"pnpm": false,
"preset": "next",
"ui": "tailwind",
Expand All @@ -64,9 +70,11 @@ exports[`get-args expected args should get args with minimal parameters and pack
"anchorName": "anchor",
"anchorProgram": "my-app",
"dryRun": false,
"isTemplate": false,
"name": "my-app",
"package": "@solana-developers/[email protected]",
"packageManager": "yarn",
"path": "",
"pnpm": false,
"preset": "next",
"ui": "tailwind",
Expand All @@ -83,9 +91,11 @@ exports[`get-args expected args should get args with preset: --preset next --ui
"anchorName": "anchor",
"anchorProgram": "my-app",
"dryRun": false,
"isTemplate": false,
"name": "my-app",
"package": "@solana-developers/[email protected]",
"packageManager": "npm",
"path": "",
"pnpm": false,
"preset": "next",
"ui": "tailwind",
Expand All @@ -102,9 +112,11 @@ exports[`get-args expected args should get args with preset: --preset react --an
"anchorName": "program",
"anchorProgram": "some-counter",
"dryRun": false,
"isTemplate": false,
"name": "my-app",
"package": "@solana-developers/[email protected]",
"packageManager": "npm",
"path": "",
"pnpm": false,
"preset": "react",
"ui": "tailwind",
Expand All @@ -121,9 +133,11 @@ exports[`get-args expected args should get args with preset: --preset react --an
"anchorName": "program",
"anchorProgram": "my-third-counter",
"dryRun": false,
"isTemplate": false,
"name": "my-app",
"package": "@solana-developers/[email protected]",
"packageManager": "pnpm",
"path": "",
"pnpm": true,
"preset": "react",
"ui": "none",
Expand All @@ -140,9 +154,11 @@ exports[`get-args expected args should get args with preset: --preset react --an
"anchorName": "program",
"anchorProgram": "my-third-counter",
"dryRun": false,
"isTemplate": false,
"name": "my-app",
"package": "@solana-developers/[email protected]",
"packageManager": "npm",
"path": "",
"pnpm": false,
"preset": "react",
"ui": "none",
Expand All @@ -159,9 +175,11 @@ exports[`get-args expected args should get args with preset: --preset react --an
"anchorName": "program",
"anchorProgram": "my-third-counter",
"dryRun": false,
"isTemplate": false,
"name": "my-app",
"package": "@solana-developers/[email protected]",
"packageManager": "yarn",
"path": "",
"pnpm": false,
"preset": "react",
"ui": "none",
Expand All @@ -178,9 +196,11 @@ exports[`get-args expected args should get args with preset: --preset react --an
"anchorName": "anchor",
"anchorProgram": "my-app",
"dryRun": false,
"isTemplate": false,
"name": "my-app",
"package": "@solana-developers/[email protected]",
"packageManager": "npm",
"path": "",
"pnpm": false,
"preset": "react",
"ui": "none",
Expand All @@ -197,9 +217,11 @@ exports[`get-args expected args should get args with preset: --preset react --ui
"anchorName": "anchor",
"anchorProgram": "my-app",
"dryRun": false,
"isTemplate": false,
"name": "my-app",
"package": "@solana-developers/[email protected]",
"packageManager": "npm",
"path": "",
"pnpm": false,
"preset": "react",
"ui": "tailwind",
Expand All @@ -216,9 +238,11 @@ exports[`get-args expected args should get args with preset: --preset=react --an
"anchorName": "program",
"anchorProgram": "my-third-counter",
"dryRun": false,
"isTemplate": false,
"name": "my-app",
"package": "@solana-developers/[email protected]",
"packageManager": "npm",
"path": "",
"pnpm": false,
"preset": "react",
"ui": "tailwind",
Expand All @@ -235,9 +259,11 @@ exports[`get-args expected args should get args with preset: --preset=react --an
"anchorName": "program",
"anchorProgram": "another-counter",
"dryRun": false,
"isTemplate": false,
"name": "my-app",
"package": "@solana-developers/[email protected]",
"packageManager": "npm",
"path": "",
"pnpm": false,
"preset": "react",
"ui": "tailwind",
Expand Down
37 changes: 27 additions & 10 deletions packages/create-solana-dapp/lib/create-project-from-template.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,23 @@ import { spinner } from '@clack/prompts'
import { existsSync } from 'fs'
import { ensureGitRepo, gitClone } from './git-helpers'
import { installPackages, PackageManager } from './nx-helpers'
import { tmpdir } from 'node:os'
import { join } from 'path'
import { copy } from 'nx/src/native'
import { initializeGitRepo } from 'create-nx-workspace/src/utils/git/git'

export interface CreateProjectFromTemplateArgs {
directory: string
target: string
path: string
packageManager: PackageManager
url: string
}

export async function createProjectFromTemplate({ directory, packageManager, url }: CreateProjectFromTemplateArgs) {
// Make sure the directory doesn't already exist
const exists = existsSync(directory)
export async function createProjectFromTemplate({ target, packageManager, path, url }: CreateProjectFromTemplateArgs) {
// Make sure the target doesn't already exist
const exists = existsSync(target)
if (exists) {
throw new Error(`Directory already exists: ${directory}`)
throw new Error(`Directory already exists: ${target}`)
}
// Make sure we get the git repo info
const info = await ensureGitRepo(url)
Expand All @@ -23,12 +28,24 @@ export async function createProjectFromTemplate({ directory, packageManager, url
const branch = info.HEAD?.replace('refs/heads/', '')

const s = spinner()
s.start(`Cloning template from ${url} to ${directory}`)
await gitClone({ branch, directory, url })
s.stop(`Cloned template from ${url} to ${directory}`)
s.start(`Cloning template from ${url} to ${target}`)

if (path.length) {
const tempDir = tmpdir()
await gitClone({ branch, target: tempDir, url })
copy(join(tempDir, path), target)
} else {
await gitClone({ branch, target, url })
}
s.stop(`Cloned template from ${url} to ${target}`)

s.start(`Installing dependencies with ${packageManager}`)
await installPackages(directory, packageManager)
await installPackages(target, packageManager)
s.stop(`Installed dependencies with ${packageManager}`)
return { directory }

await initializeGitRepo(target, {
defaultBase: 'main',
commit: { name: '', email: '', message: 'chore: initial commit' },
})
return { directory: target }
}
1 change: 1 addition & 0 deletions packages/create-solana-dapp/lib/get-args-result.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ export interface GetArgsResult {
name: string | undefined
package: string
packageManager: PackageManager
path: string
pnpm: boolean
preset: string | undefined
ui: string | undefined
Expand Down
6 changes: 6 additions & 0 deletions packages/create-solana-dapp/lib/get-args.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ Examples:
name: name ?? '',
package: '',
packageManager,
path: '',
pnpm: result.pnpm ? result.pnpm : false,
preset: result.preset,
ui: result.ui,
Expand Down Expand Up @@ -149,6 +150,11 @@ Examples:
options.package = `${packageName}@${packageVersion}`
}

const preset = presets.find((preset) => preset.value === options.preset)
if (preset.path) {
options.path = preset.path
}

return options
}

Expand Down
9 changes: 9 additions & 0 deletions packages/create-solana-dapp/lib/get-presets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ export interface Preset {
label: string
value: string
package: string
path?: string
type: PresetType
version: string
}
Expand All @@ -31,6 +32,14 @@ export function getPresets(version: string): {
type: PresetType.react,
version,
},
{
label: 'Solana Actions Template',
value: 'solana-developers/solana-actions',
package: '',
type: PresetType.template,
version: '',
path: '/examples/next-js',
},
{
label: 'Solana Mobile Expo Template',
value: 'solana-mobile/solana-mobile-expo-template',
Expand Down
4 changes: 2 additions & 2 deletions packages/create-solana-dapp/lib/git-helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@ export async function ensureGitRepo(url: string): Promise<GetRemoteInfoResult> {
}
}

export function gitClone({ branch, directory, url }: { branch: string; directory: string; url: string }) {
export function gitClone({ branch, target, url }: { branch: string; target: string; url: string }) {
return git.clone({
depth: 1,
dir: directory,
dir: target,
fs,
http,
ref: branch,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@ import { CreateProjectFromTemplateArgs } from './create-project-from-template'
import { GetArgsResult } from './get-args-result'

export function normalizeProjectFromTemplateArgs({
name: directory,
name: target,
preset: url,
path,
packageManager,
}: GetArgsResult): CreateProjectFromTemplateArgs {
return {
directory,
target,
path,
packageManager,
url: url.startsWith('http') ? url : `https://github.com/${url}`,
}
Expand Down
1 change: 1 addition & 0 deletions packages/create-solana-dapp/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
"@solana-developers/preset-common": "*",
"commander": "^11.1.0",
"create-nx-workspace": "19.0.0",
"isomorphic-git": "^1.25.10",
"nx": "19.0.0",
"tmp": "0.2.1",
"tslib": "^2.3.0"
Expand Down
Loading

0 comments on commit c79611c

Please sign in to comment.