Skip to content

Commit

Permalink
feat: add sync-schema script to sync generator schema.json to d.ts
Browse files Browse the repository at this point in the history
  • Loading branch information
beeman committed Nov 9, 2023
1 parent 832470f commit 78e1e09
Show file tree
Hide file tree
Showing 29 changed files with 463 additions and 80 deletions.
1 change: 1 addition & 0 deletions .husky/pre-commit
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@
. "$(dirname "$0")/_/husky.sh"

yarn verify-yarn-lock
yarn sync-schemas --check
yarn lint-staged
2 changes: 2 additions & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,5 @@ tmp
/.yarn

.nx/cache
# Ignore generated schema files in packages
/packages/*/src/generators/**/*-schema.d.ts
6 changes: 4 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@
"local-registry": "nx run @solana-developers/source:local-registry",
"local-publish": "nx run-many --targets publish --tag local --ver ",
"test": "yarn nx run-many --target=test --all",
"verify-package-versions": "ts-node ./verify-package-versions.ts",
"verify-yarn-lock": "ts-node ./verify-yarn-lock.ts"
"sync-schemas": "ts-node ./tools/scripts/sync-schemas.ts",
"verify-package-versions": "ts-node tools/scripts/verify-package-versions.ts",
"verify-yarn-lock": "ts-node tools/scripts/verify-yarn-lock.ts"
},
"private": true,
"dependencies": {
Expand All @@ -20,6 +21,7 @@
"@swc/helpers": "~0.5.2",
"commander": "^11.1.0",
"create-nx-workspace": "17.0.1",
"json-schema-to-typescript": "^13.1.1",
"tslib": "^2.3.0",
"yargs": "^17.7.2"
},
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
import { readProjectConfiguration, Tree } from '@nx/devkit'
import { createTreeWithEmptyWorkspace } from '@nx/devkit/testing'
import { getRecursiveFileContents } from '@solana-developers/preset-common'

import {
ApplicationAnchorTemplate,
normalizeApplicationAnchorSchema,
NormalizedApplicationAnchorSchema,
} from '../../utils'
import { applicationAnchorGenerator } from './application-anchor-generator'
import { ApplicationAnchorSchema, ApplicationAnchorTemplate } from './application-anchor-schema'
import { ApplicationAnchorSchema } from './application-anchor-schema'

describe('application generator', () => {
let tree: Tree
const options: ApplicationAnchorSchema = { name: 'anchor-app' }
const rawOptions: ApplicationAnchorSchema = { name: 'anchor-app' }
const options: NormalizedApplicationAnchorSchema = normalizeApplicationAnchorSchema(rawOptions)

beforeEach(() => {
tree = createTreeWithEmptyWorkspace()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,16 @@ import { libraryGenerator } from '@nx/js'
import { applicationCleanup, runCommand } from '@solana-developers/preset-common'
import * as path from 'path'
import { join } from 'path'
import { addAnchorIgnoreFields, applicationAnchorDependencies } from '../../utils'
import { normalizeApplicationAnchorSchema } from '../../utils/normalize-application-anchor-schema'
import {
addAnchorIgnoreFields,
applicationAnchorDependencies,
normalizeApplicationAnchorSchema,
NormalizedApplicationAnchorSchema,
} from '../../utils'
import { ApplicationAnchorSchema } from './application-anchor-schema'

export async function applicationAnchorGenerator(tree: Tree, rawOptions: ApplicationAnchorSchema) {
const options = normalizeApplicationAnchorSchema(rawOptions)
const options: NormalizedApplicationAnchorSchema = normalizeApplicationAnchorSchema(rawOptions)
await libraryGenerator(tree, {
name: options.name,
bundler: 'rollup',
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,22 @@
export type ApplicationAnchorTemplate = 'counter' | 'hello-world'
/* eslint-disable */
/**
* This file was automatically generated by json-schema-to-typescript.
* DO NOT MODIFY IT BY HAND. Instead, modify the source JSONSchema file,
* and run json-schema-to-typescript to regenerate this file.
*/

export interface ApplicationAnchorSchema {
name: string
skipFormat?: boolean
template?: ApplicationAnchorTemplate
/**
* Name of the application
*/
name: string;
/**
* Skip formatting files
*/
skipFormat?: boolean;
/**
* The template to use
*/
template?: "counter" | "hello-world";
[k: string]: unknown;
}

export type NormalizedApplicationAnchorSchema = Required<ApplicationAnchorSchema>
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
{
"$schema": "http://json-schema.org/schema",
"$id": "Application",
"$id": "ApplicationAnchorSchema",
"title": "",
"type": "object",
"properties": {
"name": {
"type": "string",
"description": "",
"description": "Name of the application",
"$default": {
"$source": "argv",
"index": 0
Expand Down
1 change: 1 addition & 0 deletions packages/preset-anchor/src/utils/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export * from './application-anchor-ignore-files'
export * from './application-anchor-dependencies'
export * from './normalize-application-anchor-schema'
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
import {
ApplicationAnchorSchema,
NormalizedApplicationAnchorSchema,
} from '../generators/application/application-anchor-schema'
import { ApplicationAnchorSchema } from '../'

export function normalizeApplicationAnchorSchema(options: ApplicationAnchorSchema): NormalizedApplicationAnchorSchema {
return {
Expand All @@ -10,3 +7,6 @@ export function normalizeApplicationAnchorSchema(options: ApplicationAnchorSchem
template: options.template ?? 'counter',
}
}

export type NormalizedApplicationAnchorSchema = Required<ApplicationAnchorSchema>
export type ApplicationAnchorTemplate = ApplicationAnchorSchema['template']
3 changes: 1 addition & 2 deletions packages/preset-common/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,5 @@
},
"type": "commonjs",
"main": "./src/index.js",
"typings": "./src/index.d.ts",
"generators": "./generators.json"
"typings": "./src/index.d.ts"
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ import { createTreeWithEmptyWorkspace } from '@nx/devkit/testing'
import { getRecursiveFileContents } from '@solana-developers/preset-common'
import { applicationReactGenerator, ApplicationReactUiLibrary } from '@solana-developers/preset-react'

import { normalizeApplicationNextSchema } from '../../utils'
import { normalizeApplicationNextSchema, NormalizedApplicationNextSchema } from '../../utils'
import { applicationNextGenerator } from './application-next-generator'
import { ApplicationNextSchema, NormalizedApplicationNextSchema } from './application-next-schema'
import { ApplicationNextSchema } from './application-next-schema'

describe('application generator', () => {
let tree: Tree
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,16 @@ import {
walletAdapterDependencies,
} from '@solana-developers/preset-react'
import { join } from 'path'
import { applicationSubstitutions, generateNextApplication, normalizeApplicationNextSchema } from '../../utils'
import {
applicationSubstitutions,
generateNextApplication,
normalizeApplicationNextSchema,
NormalizedApplicationNextSchema,
} from '../../utils'
import { ApplicationNextSchema } from './application-next-schema'

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

Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,42 @@
import { ApplicationAnchorTemplate } from '@solana-developers/preset-anchor'
import { ApplicationReactUiLibrary } from '@solana-developers/preset-react'
/* eslint-disable */
/**
* This file was automatically generated by json-schema-to-typescript.
* DO NOT MODIFY IT BY HAND. Instead, modify the source JSONSchema file,
* and run json-schema-to-typescript to regenerate this file.
*/

export interface ApplicationNextSchema {
appName?: string
name: string
port?: number
skipFormat?: boolean
uiLibrary?: ApplicationReactUiLibrary
withAnchor?: boolean
anchorName?: string
anchorTemplate?: ApplicationAnchorTemplate
/**
* Name of the application
*/
name: string;
/**
* Name of the application (overrides name)
*/
appName?: string;
/**
* Port to run the application on
*/
port?: number;
/**
* Skip formatting files
*/
skipFormat?: boolean;
/**
* The UI library to use
*/
uiLibrary?: "none" | "tailwind";
/**
* Create an anchor program
*/
withAnchor?: boolean;
/**
* Anchor project name
*/
anchorName?: string;
/**
* The template to use
*/
anchorTemplate?: "counter" | "hello-world";
[k: string]: unknown;
}

export type NormalizedApplicationNextSchema = Required<ApplicationNextSchema>
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"properties": {
"name": {
"type": "string",
"description": "",
"description": "Name of the application",
"$default": {
"$source": "argv",
"index": 0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { getProjects, Tree, updateJson } from '@nx/devkit'
import { Linter } from '@nx/linter'
import { applicationGenerator as reactApplicationGenerator } from '@nx/next/src/generators/application/application'
import { join } from 'path'
import { NormalizedApplicationNextSchema } from '../generators/application/application-next-schema'
import { NormalizedApplicationNextSchema } from './normalize-application-next-schema'

export async function generateNextApplication(tree: Tree, options: NormalizedApplicationNextSchema) {
await reactApplicationGenerator(tree, {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import {
ApplicationNextSchema,
NormalizedApplicationNextSchema,
} from '../generators/application/application-next-schema'
import { ApplicationNextSchema } from '../generators/application/application-next-schema'

export type NormalizedApplicationNextSchema = Required<ApplicationNextSchema>
export function normalizeApplicationNextSchema(options: ApplicationNextSchema): NormalizedApplicationNextSchema {
return {
...options,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import { getProjects, readProjectConfiguration, Tree } from '@nx/devkit'
import { createTreeWithEmptyWorkspace } from '@nx/devkit/testing'
import { getRecursiveFileContents } from '@solana-developers/preset-common'
import { normalizeApplicationReactSchema } from '../../utils/normalize-application-react-schema'

import { applicationReactGenerator } from './application-react-generator'
import {
ApplicationReactSchema,
ApplicationReactUiLibrary,
normalizeApplicationReactSchema,
NormalizedApplicationReactSchema,
} from './application-react-schema'
} from '../../utils'

import { applicationReactGenerator } from './application-react-generator'
import { ApplicationReactSchema } from './application-react-schema'

describe('application react generator', () => {
let tree: Tree
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,11 @@ import {
applicationSubstitutions,
applicationTailwindConfig,
generateReactApplication,
normalizeApplicationReactSchema,
NormalizedApplicationReactSchema,
walletAdapterDependencies,
} from '../../utils'
import { normalizeApplicationReactSchema } from '../../utils/normalize-application-react-schema'
import { ApplicationReactSchema, NormalizedApplicationReactSchema } from './application-react-schema'
import { ApplicationReactSchema } from './application-react-schema'

export async function applicationReactGenerator(tree: Tree, rawOptions: ApplicationReactSchema) {
const options: NormalizedApplicationReactSchema = normalizeApplicationReactSchema(rawOptions)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,38 @@
import { ApplicationAnchorTemplate } from '@solana-developers/preset-anchor'
export type ApplicationReactUiLibrary = 'none' | 'tailwind'
/* eslint-disable */
/**
* This file was automatically generated by json-schema-to-typescript.
* DO NOT MODIFY IT BY HAND. Instead, modify the source JSONSchema file,
* and run json-schema-to-typescript to regenerate this file.
*/

export interface ApplicationReactSchema {
anchorName?: string
anchorTemplate?: ApplicationAnchorTemplate
appName?: string
name: string
skipFormat?: boolean
uiLibrary?: ApplicationReactUiLibrary
withAnchor?: boolean
/**
* Name of the application
*/
name: string;
/**
* Name of the application (overrides name)
*/
appName?: string;
/**
* Skip formatting files
*/
skipFormat?: boolean;
/**
* The UI library to use
*/
uiLibrary?: "none" | "tailwind";
/**
* Create an anchor program
*/
withAnchor?: boolean;
/**
* Anchor project name
*/
anchorName?: string;
/**
* The template to use
*/
anchorTemplate?: "counter" | "hello-world";
[k: string]: unknown;
}

export type NormalizedApplicationReactSchema = Required<ApplicationReactSchema>
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"properties": {
"name": {
"type": "string",
"description": "",
"description": "Name of the application",
"$default": {
"$source": "argv",
"index": 0
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { addDependenciesToPackageJson, Tree } from '@nx/devkit'
import { NormalizedApplicationReactSchema } from '../generators/application/application-react-schema'
import { getUiDependencies } from './get-ui-dependencies'
import { NormalizedApplicationReactSchema } from './normalize-application-react-schema'

export function applicationReactDependencies(tree: Tree, options: NormalizedApplicationReactSchema) {
const { dependencies, devDependencies } = getUiDependencies(options.uiLibrary)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { getProjects, Tree } from '@nx/devkit'
import { Linter } from '@nx/linter'
import { applicationGenerator as reactApplicationGenerator } from '@nx/react/src/generators/application/application'
import { NormalizedApplicationReactSchema } from '../generators/application/application-react-schema'
import { NormalizedApplicationReactSchema } from './normalize-application-react-schema'

export async function generateReactApplication(tree: Tree, options: NormalizedApplicationReactSchema) {
await reactApplicationGenerator(tree, {
Expand Down
2 changes: 1 addition & 1 deletion packages/preset-react/src/utils/get-ui-dependencies.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { packageVersion } from '@solana-developers/preset-common'
import { ApplicationReactUiLibrary } from '../generators/application/application-react-schema'
import { ApplicationReactUiLibrary } from './normalize-application-react-schema'

export function getUiDependencies(type: ApplicationReactUiLibrary): {
dependencies?: Record<string, string>
Expand Down
1 change: 1 addition & 0 deletions packages/preset-react/src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@ export * from './application-react-dependencies'
export * from './application-substitutions'
export * from './application-tailwind-config'
export * from './generate-react-application'
export * from './normalize-application-react-schema'
export * from './get-ui-dependencies'
export * from './wallet-adapter-dependencies'
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import {
ApplicationReactSchema,
NormalizedApplicationReactSchema,
} from '../generators/application/application-react-schema'
import { ApplicationReactSchema } from '../generators/application/application-react-schema'

export type NormalizedApplicationReactSchema = Required<ApplicationReactSchema>

export type ApplicationReactUiLibrary = ApplicationReactSchema['uiLibrary']

export function normalizeApplicationReactSchema(options: ApplicationReactSchema): NormalizedApplicationReactSchema {
return {
Expand Down
Loading

0 comments on commit 78e1e09

Please sign in to comment.