-
Notifications
You must be signed in to change notification settings - Fork 116
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
31 changed files
with
1,150 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,10 @@ | ||
import { Tree } from '@nx/devkit' | ||
import { join } from 'path' | ||
|
||
export function applicationCleanup(tree: Tree, path: string) { | ||
tree.children(path).forEach((file) => tree.delete(join(path, file))) | ||
export function applicationCleanup(tree: Tree, path: string, files: string[] = []) { | ||
tree.children(path).forEach((file) => { | ||
if (files.includes(join(path, file)) || !files.length) { | ||
tree.delete(join(path, file)) | ||
} | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,4 +19,5 @@ export const packageVersion = { | |
}, | ||
bs58: '5.0.0', | ||
daisyui: '3.9.3', | ||
encoding: '0.1.13', | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
{ | ||
"generators": { | ||
"application": { | ||
"factory": "./src/generators/application/application-next-generator", | ||
"schema": "./src/generators/application/application-next-schema.json", | ||
"description": "application generator", | ||
"aliases": ["preset"] | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
581 changes: 581 additions & 0 deletions
581
...set-next/src/generators/application/__snapshots__/application-next-generator.spec.ts.snap
Large diffs are not rendered by default.
Oops, something went wrong.
69 changes: 69 additions & 0 deletions
69
packages/preset-next/src/generators/application/application-next-generator.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
import { getProjects, readProjectConfiguration, Tree } from '@nx/devkit' | ||
import { createTreeWithEmptyWorkspace } from '@nx/devkit/testing' | ||
import { getRecursiveFileContents } from '@solana-developers/preset-common' | ||
import { ApplicationReactUiLibrary } from '@solana-developers/preset-react' | ||
|
||
import { normalizeApplicationNextSchema } from '../../utils' | ||
import { applicationNextGenerator } from './application-next-generator' | ||
import { ApplicationNextSchema, NormalizedApplicationNextSchema } from './application-next-schema' | ||
|
||
describe('application generator', () => { | ||
let tree: Tree | ||
const rawOptions: ApplicationNextSchema = { name: 'test-app' } | ||
const options: NormalizedApplicationNextSchema = normalizeApplicationNextSchema(rawOptions) | ||
|
||
beforeEach(() => { | ||
tree = createTreeWithEmptyWorkspace() | ||
}) | ||
|
||
describe('default apps', () => { | ||
it.each([['none'], ['tailwind']])('should generate default app with "%s" ui', async (uiLibrary) => { | ||
await applicationNextGenerator(tree, { ...rawOptions, uiLibrary: uiLibrary as ApplicationReactUiLibrary }) | ||
|
||
const appConfig = readProjectConfiguration(tree, options.name) | ||
const anchorConfig = readProjectConfiguration(tree, options.anchorName) | ||
expect(appConfig).toBeDefined() | ||
expect(anchorConfig).toBeDefined() | ||
|
||
const contents = getRecursiveFileContents(tree, '.') | ||
const stringified = JSON.stringify(contents, null, 2) | ||
expect(stringified).toMatchSnapshot() | ||
}) | ||
}) | ||
|
||
describe('custom apps', () => { | ||
it('should generate 4 Next apps and 2 Anchor apps', async () => { | ||
await applicationNextGenerator(tree, { ...rawOptions, uiLibrary: 'none' }) | ||
await applicationNextGenerator(tree, { ...rawOptions, name: 'app-1', uiLibrary: 'none' }) | ||
await applicationNextGenerator(tree, { ...rawOptions, name: 'app-2', uiLibrary: 'none' }) | ||
await applicationNextGenerator(tree, { ...rawOptions, name: 'app-3', anchorName: 'anchor-1', uiLibrary: 'none' }) | ||
|
||
const app0 = readProjectConfiguration(tree, options.name) | ||
const app1 = readProjectConfiguration(tree, 'app-1') | ||
const app2 = readProjectConfiguration(tree, 'app-2') | ||
const app3 = readProjectConfiguration(tree, 'app-3') | ||
const anchor0Config = readProjectConfiguration(tree, options.anchorName) | ||
const anchor1Config = readProjectConfiguration(tree, 'anchor-1') | ||
const projects = getProjects(tree) | ||
|
||
expect(app0).toBeDefined() | ||
expect(app1).toBeDefined() | ||
expect(app2).toBeDefined() | ||
expect(app3).toBeDefined() | ||
expect(anchor0Config).toBeDefined() | ||
expect(anchor1Config).toBeDefined() | ||
expect(projects.size).toEqual(6) | ||
}) | ||
|
||
it('should generate app without anchor', async () => { | ||
await applicationNextGenerator(tree, { ...rawOptions, uiLibrary: 'none', withAnchor: false }) | ||
const projects = getProjects(tree) | ||
const appProject = projects.has(options.name) | ||
const anchorProject = projects.has(options.anchorName) | ||
|
||
expect(projects.size).toEqual(1) | ||
expect(appProject).toBeDefined() | ||
expect(anchorProject).toBeFalsy() | ||
}) | ||
}) | ||
}) |
81 changes: 81 additions & 0 deletions
81
packages/preset-next/src/generators/application/application-next-generator.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
import { | ||
addDependenciesToPackageJson, | ||
formatFiles, | ||
generateFiles, | ||
getProjects, | ||
installPackagesTask, | ||
Tree, | ||
} from '@nx/devkit' | ||
import { getNpmScope } from '@nx/js/src/utils/package-json/get-npm-scope' | ||
import { applicationAnchorGenerator } from '@solana-developers/preset-anchor' | ||
import { applicationCleanup, packageVersion } from '@solana-developers/preset-common' | ||
import { | ||
applicationReactDependencies, | ||
applicationTailwindConfig, | ||
walletAdapterDependencies, | ||
} from '@solana-developers/preset-react' | ||
import { join } from 'path' | ||
import { applicationSubstitutions, generateNextApplication, normalizeApplicationNextSchema } from '../../utils' | ||
import { ApplicationNextSchema } from './application-next-schema' | ||
|
||
export async function applicationNextGenerator(tree: Tree, rawOptions: ApplicationNextSchema) { | ||
const options = normalizeApplicationNextSchema(rawOptions) | ||
const project = await generateNextApplication(tree, options) | ||
const npmScope = getNpmScope(tree) | ||
|
||
// Clean up the default project files. | ||
const cleanup = [ | ||
'/app/global.css', | ||
'/app/page.module.css', | ||
'/app/layout.tsx', | ||
'/app/page.tsx', | ||
'/public/favicon.ico', | ||
'/public/.gitkeep', | ||
] | ||
applicationCleanup(tree, join(project.sourceRoot, 'app'), cleanup) | ||
|
||
const substitutions = applicationSubstitutions({ | ||
name: options.name, | ||
npmScope, | ||
}) | ||
|
||
// Generate the common files. | ||
generateFiles(tree, join(__dirname, 'files/common'), project.root, substitutions) | ||
|
||
// Generate the files from the templates. | ||
generateFiles(tree, join(__dirname, 'files/ui', options.uiLibrary), project.root, substitutions) | ||
|
||
// Add the dependencies for the base application. | ||
applicationReactDependencies(tree, options) | ||
|
||
addDependenciesToPackageJson(tree, { encoding: packageVersion.encoding }, {}) | ||
|
||
// Add the dependencies for the wallet adapter. | ||
walletAdapterDependencies(tree) | ||
|
||
if (options.uiLibrary === 'tailwind') { | ||
// Add the tailwind config. | ||
await applicationTailwindConfig(tree, options.name) | ||
} | ||
|
||
if (options.withAnchor && !getProjects(tree).has(options.anchorName)) { | ||
// Add the anchor application. | ||
await applicationAnchorGenerator(tree, { | ||
name: options.anchorName, | ||
programName: options.anchorProgramName, | ||
skipFormat: true, | ||
}) | ||
} | ||
|
||
// Format the files. | ||
if (!options.skipFormat) { | ||
await formatFiles(tree) | ||
} | ||
|
||
// Install the packages on exit. | ||
return () => { | ||
installPackagesTask(tree, true) | ||
} | ||
} | ||
|
||
export default applicationNextGenerator |
12 changes: 12 additions & 0 deletions
12
packages/preset-next/src/generators/application/application-next-schema.d.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import { ApplicationReactUiLibrary } from '@solana-developers/preset-react' | ||
|
||
export interface ApplicationNextSchema { | ||
anchorName?: string | ||
anchorProgramName?: string | ||
name: string | ||
skipFormat?: boolean | ||
uiLibrary?: ApplicationReactUiLibrary | ||
withAnchor?: boolean | ||
} | ||
|
||
export type NormalizedApplicationNextSchema = Required<ApplicationNextSchema> |
18 changes: 18 additions & 0 deletions
18
packages/preset-next/src/generators/application/application-next-schema.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
{ | ||
"$schema": "http://json-schema.org/schema", | ||
"$id": "ApplicationNextSchema", | ||
"title": "", | ||
"type": "object", | ||
"properties": { | ||
"name": { | ||
"type": "string", | ||
"description": "", | ||
"$default": { | ||
"$source": "argv", | ||
"index": 0 | ||
}, | ||
"x-prompt": "What name would you like to use?" | ||
} | ||
}, | ||
"required": ["name"] | ||
} |
Binary file added
BIN
+16.6 KB
packages/preset-next/src/generators/application/files/common/public/favicon.ico
Binary file not shown.
Binary file added
BIN
+46.8 KB
...ages/preset-next/src/generators/application/files/common/public/solana-logo.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
48 changes: 48 additions & 0 deletions
48
packages/preset-next/src/generators/application/files/ui/none/app/app-layout.tsx.template
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
"use client" | ||
import { ReactNode } from 'react' | ||
import Link from 'next/link' | ||
import dynamic from "next/dynamic"; | ||
|
||
const WalletMultiButton = dynamic( | ||
async () => | ||
(await import("@solana/wallet-adapter-react-ui")).WalletMultiButton, | ||
{ ssr: false } | ||
) | ||
export function AppLayout({ children }: { children: ReactNode }) { | ||
const { pathname } = { pathname: '' } | ||
const pages = [ | ||
{ label: 'Airdrop', path: '/airdrop' }, | ||
{ label: 'Page 1', path: '/page-1' }, | ||
{ label: 'Page 2', path: '/page-2' }, | ||
] | ||
|
||
return ( | ||
<div> | ||
<div> | ||
<div> | ||
<Link href="/" > | ||
<%= packageName %> | ||
</Link> | ||
<ul > | ||
{pages.map(({ label, path }) => ( | ||
<li key={path}> | ||
<Link href={path}> | ||
{label} | ||
</Link> | ||
</li> | ||
))} | ||
</ul> | ||
</div> | ||
<div> | ||
<WalletMultiButton /> | ||
</div> | ||
</div> | ||
<div>{children}</div> | ||
<footer> | ||
<aside> | ||
<p>Generated with <%= packageName %>@<%= packageVersion %></p> | ||
</aside> | ||
</footer> | ||
</div> | ||
) | ||
} |
4 changes: 4 additions & 0 deletions
4
packages/preset-next/src/generators/application/files/ui/none/app/global.css.template
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
html, | ||
body { | ||
height: 100%; | ||
} |
22 changes: 22 additions & 0 deletions
22
packages/preset-next/src/generators/application/files/ui/none/app/layout.tsx.template
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import './global.css' | ||
import { AppLayout } from './app-layout' | ||
import { SolanaProvider } from './solana-provider' | ||
|
||
export const metadata = { | ||
title: 'Welcome to <%= name %>', | ||
description: 'Generated by <%= packageName %>@<%= packageVersion %>', | ||
} | ||
|
||
export default function RootLayout({ children }: { children: React.ReactNode }) { | ||
return ( | ||
<html lang="en"> | ||
<body> | ||
<SolanaProvider> | ||
<AppLayout> | ||
{children} | ||
</AppLayout> | ||
</SolanaProvider> | ||
</body> | ||
</html> | ||
) | ||
} |
9 changes: 9 additions & 0 deletions
9
packages/preset-next/src/generators/application/files/ui/none/app/page.tsx.template
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import { IDL } from '@<%= npmScope %>/anchor'; | ||
|
||
export default async function Index() { | ||
return ( | ||
<div> | ||
<pre>{JSON.stringify(IDL, null, 2)}</pre> | ||
</div> | ||
) | ||
} |
Oops, something went wrong.