Skip to content

Commit

Permalink
TypeBox-Codegen
Browse files Browse the repository at this point in the history
  • Loading branch information
sinclairzx81 committed Jul 31, 2023
1 parent 6d7eab6 commit 70a4198
Show file tree
Hide file tree
Showing 38 changed files with 1,641 additions and 565 deletions.
6 changes: 3 additions & 3 deletions .git-hooks/pre-commit
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
#!/bin/sh
#
# Runs prettier on all files before comitting them.
# This does only work correctly if prettier is the only formatting tool (which
# should be the case).
# Runs prettier on all files before comitting them. This does only work correctly if
# prettier is the only formatting tool (which should be the case).
#
# src: https://prettier.io/docs/en/precommit.html

FILES=$(git diff --cached --name-only --diff-filter=ACMR | sed 's| |\\ |g')
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ jobs:
node: [18.x]
os: [ubuntu-latest, windows-latest, macOS-latest]
steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v3
- name: Install Node
uses: actions/setup-node@v1
with:
Expand Down
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
node_modules
target
target
dist
Binary file removed arch.png
Binary file not shown.
Binary file added codegen.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
7 changes: 0 additions & 7 deletions example/index.html

This file was deleted.

53 changes: 22 additions & 31 deletions example/index.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import { Type, Static } from '@sinclair/typebox'
import * as Codegen from '@typebox/codegen'
import * as Codegen from '@sinclair/typebox-codegen'
import * as util from 'node:util'

function Print(transform: string, code: any) {
const data = typeof code === 'string' ? code : util.inspect(code, false, 100)
const data = typeof code === 'string' ? Codegen.Formatter.Format(code) : util.inspect(code, false, 100)
const length = 72
console.log('┌' + '─'.repeat(length) + '┐')
console.log('│', transform.padEnd(length - 1) + '│')
Expand All @@ -12,48 +11,40 @@ function Print(transform: string, code: any) {
console.log(data)
console.log('')
}

const Code = `
/**
* @description 'A union type'
*/
export type T = string | number
/**
* @description 'A vector type'
*/
export interface Vector {
/**
* @default 1
*/
x: number
/**
* @default 2
*/
y: number
/**
* @default 3
*/
z: number
export type A = {
x: number,
y: string,
z: boolean
}
export type B = {
a: number,
b: string,
c: boolean
}
export type T = A & B
type M = {[K in keyof T]: 1 }
`
// ----------------------------------------------------------------------------
// Typescript Base
// ----------------------------------------------------------------------------
Print('Typescript code (base)', Code)

Print('Typescript', Code)
// ----------------------------------------------------------------------------
// Immediate Transform
// TypeBox Transform
// ----------------------------------------------------------------------------
Print('TypeScript To TypeBox', Codegen.TypeScriptToTypeBox.Generate(Code))

// ----------------------------------------------------------------------------
// Model Transform
// ----------------------------------------------------------------------------
const model = Codegen.TypeScriptToModel.Generate(Code)
Print('TypeScript To Model', model)
Print('Model To JsonSchema', Codegen.ModelToJsonSchema.Generate(model))
Print('TypeScript To Inline Model', model)
Print('Model To JsonSchema Inline', Codegen.ModelToJsonSchema.Generate(model))
Print('Model To JavaScript', Codegen.ModelToJavaScript.Generate(model))
Print('Model To TypeScript', Codegen.ModelToTypeScript.Generate(model))
Print('Model To Valibot', Codegen.ModelToValibot.Generate(model))
Print('Model To Value', Codegen.ModelToValue.Generate(model))
Print('Model To Yup', Codegen.ModelToYup.Generate(model))
Print('Model To Zod', Codegen.ModelToZod.Generate(model))
Print('Model To ArkType', Codegen.ModelToArkType.Generate(model))
54 changes: 36 additions & 18 deletions hammer.mjs
Original file line number Diff line number Diff line change
@@ -1,38 +1,56 @@
// -------------------------------------------------------------------------------
import { readFileSync } from 'node:fs'

// -------------------------------------------------------------
// Clean
// -------------------------------------------------------------------------------
// -------------------------------------------------------------
export async function clean() {
await folder('target').delete()
}
// -------------------------------------------------------------------------------
// -------------------------------------------------------------
// Format
// -------------------------------------------------------------------------------
// -------------------------------------------------------------
export async function format() {
await shell('prettier --write src test example')
}
// -------------------------------------------------------------------------------
// -------------------------------------------------------------
// Test
// -------------------------------------------------------------------------------
// -------------------------------------------------------------
export async function test(testReporter = 'spec', filter = '') {
const pattern = filter.length > 0 ? `"--test-name-pattern=${filter}.*"` : ''
await shell('hammer build test/index.ts --dist target/test --platform node')
await shell(`node --test-reporter ${testReporter} --test ${pattern} target/test/index.js`)
}
// -------------------------------------------------------------------------------
// Serve
// -------------------------------------------------------------------------------
export async function serve() {
await shell('hammer serve example/index.html --dist target/example')
}
// -------------------------------------------------------------------------------
// -------------------------------------------------------------
// Start
// -------------------------------------------------------------------------------
// -------------------------------------------------------------
export async function start() {
await shell('hammer run example/index.ts --dist target/example')
}
// -------------------------------------------------------------------------------
// -------------------------------------------------------------
// Build
// -------------------------------------------------------------------------------
export async function build() {
await shell('tsc -p src/tsconfig.json --outDir target/build --declaration')
// -------------------------------------------------------------
export async function build(target = 'target/build') {
await shell(`tsc -p src/tsconfig.json --outDir ${target} --declaration`)
await folder(target).add('package.json')
await folder(target).add('license')
await folder(target).add('readme.md')
await shell(`cd ${target} && npm pack`)
}
// -------------------------------------------------------------
// Publish
// -------------------------------------------------------------
export async function publish(otp, target = 'target/build') {
const { version } = JSON.parse(readFileSync(`${target}/package.json`, 'utf8'))
if(version.includes('-dev')) throw Error(`package version should not include -dev specifier`)
await shell(`cd ${target} && npm publish sinclair-typebox-codegen-${version}.tgz --access=public --otp ${otp}`)
await shell(`git tag ${version}`)
await shell(`git push origin ${version}`)
}
// -------------------------------------------------------------
// Publish-Dev
// -------------------------------------------------------------
export async function publish_dev(otp, target = 'target/build') {
const { version } = JSON.parse(readFileSync(`${target}/package.json`, 'utf8'))
if(!version.includes('-dev')) throw Error(`development package version should include -dev specifier`)
await shell(`cd ${target} && npm publish sinclair-typebox-codegen-${version}.tgz --access=public --otp ${otp} --tag dev`)
}
21 changes: 21 additions & 0 deletions license
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
@sinclair/typebox-codegen

The MIT License (MIT)

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
Loading

0 comments on commit 70a4198

Please sign in to comment.