Skip to content

Commit

Permalink
refactor: split up next templates into separate generator
Browse files Browse the repository at this point in the history
  • Loading branch information
beeman committed Nov 22, 2023
1 parent 8334ef7 commit 81ad479
Show file tree
Hide file tree
Showing 31 changed files with 572 additions and 69 deletions.
14 changes: 10 additions & 4 deletions packages/preset-next/generators.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
{
"generators": {
"application": {
"factory": "./src/generators/application/application-next-generator",
"schema": "./src/generators/application/application-next-schema.json",
"next-application": {
"factory": "./src/generators/next-application/next-application-generator",
"schema": "./src/generators/next-application/next-application-schema.json",
"description": "application generator",
"aliases": ["preset"]
"aliases": ["application", "preset"]
},
"next-template": {
"factory": "./src/generators/next-template/next-template-generator",
"schema": "./src/generators/next-template/next-template-schema.json",
"description": "next-template generator",
"aliases": ["template"]
}
}
}
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
import { getProjects, readProjectConfiguration, Tree } from '@nx/devkit'
import { createTreeWithEmptyWorkspace } from '@nx/devkit/testing'
import { getRecursiveFileContents } from '@solana-developers/preset-common'
import { ApplicationNextUi, normalizeApplicationNextSchema, NormalizedApplicationNextSchema } from '../../utils'
import { applicationNextGenerator } from './application-next-generator'
import { ApplicationNextSchema } from './application-next-schema'
import { NextApplicationUi, normalizeNextApplicationSchema, NormalizedNextApplicationSchema } from '../../utils'
import { nextApplicationGenerator } from './next-application-generator'
import { NextApplicationSchema } from './next-application-schema'

describe('application generator', () => {
let tree: Tree
const rawOptions: ApplicationNextSchema = { name: 'test-app', anchor: 'counter' }
const options: NormalizedApplicationNextSchema = normalizeApplicationNextSchema(rawOptions)
const rawOptions: NextApplicationSchema = { name: 'test-app', anchor: 'counter' }
const options: NormalizedNextApplicationSchema = normalizeNextApplicationSchema(rawOptions)

beforeEach(() => {
tree = createTreeWithEmptyWorkspace()
})

describe('default apps', () => {
it.each([['none'], ['tailwind']])('should generate default app with "%s" ui', async (ui) => {
await applicationNextGenerator(tree, { ...rawOptions, ui: ui as ApplicationNextUi })
await nextApplicationGenerator(tree, { ...rawOptions, ui: ui as NextApplicationUi })

const appConfig = readProjectConfiguration(tree, options.webName)
const anchorConfig = readProjectConfiguration(tree, options.anchorName)
Expand All @@ -30,10 +30,10 @@ describe('application generator', () => {

describe('custom apps', () => {
it('should generate 4 Next apps and 2 Anchor apps', async () => {
await applicationNextGenerator(tree, { ...rawOptions, ui: 'none' })
await applicationNextGenerator(tree, { ...rawOptions, name: 'app-1', ui: 'none' })
await applicationNextGenerator(tree, { ...rawOptions, name: 'app-2', ui: 'none' })
await applicationNextGenerator(tree, { ...rawOptions, name: 'app-3', anchorName: 'anchor-1', ui: 'none' })
await nextApplicationGenerator(tree, { ...rawOptions, ui: 'none' })
await nextApplicationGenerator(tree, { ...rawOptions, name: 'app-1', ui: 'none' })
await nextApplicationGenerator(tree, { ...rawOptions, name: 'app-2', ui: 'none' })
await nextApplicationGenerator(tree, { ...rawOptions, name: 'app-3', anchorName: 'anchor-1', ui: 'none' })

const app0 = readProjectConfiguration(tree, options.webName)
const app1 = readProjectConfiguration(tree, 'app-1')
Expand All @@ -53,7 +53,7 @@ describe('application generator', () => {
})

it('should generate app without anchor', async () => {
await applicationNextGenerator(tree, { ...rawOptions, ui: 'none', anchor: 'none' })
await nextApplicationGenerator(tree, { ...rawOptions, ui: 'none', anchor: 'none' })
const projects = getProjects(tree)
const appProject = projects.has(options.webName)
const anchorProject = projects.has(options.anchorName)
Expand All @@ -68,10 +68,10 @@ describe('application generator', () => {
})

it.each([['none'], ['tailwind']])('should generate app with custom name and "%s" ui', async (ui) => {
await applicationNextGenerator(tree, {
await nextApplicationGenerator(tree, {
...rawOptions,
anchor: 'none',
ui: ui as ApplicationNextUi,
ui: ui as NextApplicationUi,
webName: 'web-app',
})
const projects = getProjects(tree)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,4 @@
import {
addDependenciesToPackageJson,
formatFiles,
generateFiles,
getProjects,
installPackagesTask,
Tree,
} from '@nx/devkit'
import { addDependenciesToPackageJson, formatFiles, getProjects, installPackagesTask, Tree } from '@nx/devkit'
import { getNpmScope } from '@nx/js/src/utils/package-json/get-npm-scope'
import { anchorApplicationGenerator } from '@solana-developers/preset-anchor'
import { applicationCleanup, packageVersion } from '@solana-developers/preset-common'
Expand All @@ -15,16 +8,12 @@ import {
walletAdapterDependencies,
} from '@solana-developers/preset-react'
import { join } from 'path'
import {
applicationSubstitutions,
generateNextApplication,
normalizeApplicationNextSchema,
NormalizedApplicationNextSchema,
} from '../../utils'
import { ApplicationNextSchema } from './application-next-schema'
import { generateNextApplication, NormalizedNextApplicationSchema, normalizeNextApplicationSchema } from '../../utils'
import nextTemplateGenerator from '../next-template/next-template-generator'
import { NextApplicationSchema } from './next-application-schema'

export async function applicationNextGenerator(tree: Tree, rawOptions: ApplicationNextSchema) {
const options: NormalizedApplicationNextSchema = normalizeApplicationNextSchema(rawOptions)
export async function nextApplicationGenerator(tree: Tree, rawOptions: NextApplicationSchema) {
const options: NormalizedNextApplicationSchema = normalizeNextApplicationSchema(rawOptions)
const project = await generateNextApplication(tree, options)
const npmScope = getNpmScope(tree)

Expand All @@ -39,19 +28,28 @@ export async function applicationNextGenerator(tree: Tree, rawOptions: Applicati
]
applicationCleanup(tree, join(project.sourceRoot, 'app'), cleanup)

const substitutions = applicationSubstitutions({
// Generate the base files from the templates.
await nextTemplateGenerator(tree, {
name: options.webName,
npmScope,
template: 'base',
anchor: options.anchor,
anchorName: options.anchorName,
webName: options.webName,
directory: project.sourceRoot,
})

// Generate the ui files from the templates.
await nextTemplateGenerator(tree, {
name: options.webName,
npmScope,
template: options.ui,
anchor: options.anchor,
anchorName: options.anchorName,
webName: options.webName,
directory: project.sourceRoot,
})

// 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.ui), project.root, substitutions)

// Add the dependencies for the base application.
reactApplicationDependencies(tree, options)

Expand Down Expand Up @@ -84,4 +82,4 @@ export async function applicationNextGenerator(tree: Tree, rawOptions: Applicati
}
}

export default applicationNextGenerator
export default nextApplicationGenerator
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* and run json-schema-to-typescript to regenerate this file.
*/

export interface ApplicationNextSchema {
export interface NextApplicationSchema {
/**
* Name of the application
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"$schema": "http://json-schema.org/schema",
"$id": "ApplicationNextSchema",
"$id": "NextApplicationSchema",
"title": "",
"type": "object",
"properties": {
Expand Down
Loading

0 comments on commit 81ad479

Please sign in to comment.