Skip to content

Commit

Permalink
✨ [api]: generate script #146
Browse files Browse the repository at this point in the history
  • Loading branch information
apolkingg8 authored and solomondefi-dev committed Nov 17, 2021
1 parent 981376a commit 0146241
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 6 deletions.
16 changes: 16 additions & 0 deletions .idea/jsonSchemas.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,9 @@ import {
validateSync,
} from 'class-validator'
import { IsEthAddress } from '../util/IsEthAddress'
import {PaymentCreatedEvent as IPaymentCreatedEvent} from '@solomon/shared/util-event'

export class PaymentCreatedEvent {
export class PaymentCreatedEvent implements IPaymentCreatedEvent {
@IsUUID()
@IsString()
id: string = v4()
Expand Down Expand Up @@ -47,8 +48,8 @@ export class PaymentCreatedEvent {
discount: number = 0

@Min(0)
@IsNumber()
ethPaid: number = 0
@IsString()
ethPaid: string = ''

isValid() {
let errArr = validateSync(this)
Expand Down
32 changes: 29 additions & 3 deletions libs/shared/util-event/src/generateEventInterface.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,34 @@
import { compile, compileFromFile } from 'json-schema-to-typescript'
import { write, writeFile } from "fs-extra";
import { readFile, write, writeFile } from "fs-extra";
import path from "path";
import { readdir } from "fs-extra";

export const generateEventInterface = async ()=> {
let res = await compileFromFile(path.resolve(__dirname, '..', '_sample.schema.json'))
await writeFile(path.resolve(__dirname, 'test.schema.ts'), res, 'utf-8')
const interfaceFolderPath = path.resolve(__dirname, '..', 'interface')
const schemaFolderPath = path.resolve(__dirname, '..', 'schema')
const schemaFileNames = await readdir(schemaFolderPath)
let indexStr = ``

for(let schemaFileName of schemaFileNames) {
try {
const schemaFilePath = path.resolve(schemaFolderPath, schemaFileName)
const interfaceFileName = schemaFileName.replace('.schema.json', '.schema.ts')
const interfaceFilePath = path.resolve(interfaceFolderPath, interfaceFileName)

let interfaceStr = await compileFromFile(schemaFilePath, {
unreachableDefinitions: true,
})

interfaceStr = interfaceStr.replace(/\[k: string]: unknown;/g, ``)

await writeFile(interfaceFilePath, interfaceStr, 'utf-8')
indexStr += `export * from '../interface/${interfaceFileName.replace('.ts', '')}'\n`
console.log(`${interfaceFilePath} generated.`)
} catch (err) {
console.error(err)
console.log(`${schemaFileName} generate fail.`)
}
}

await writeFile(path.resolve(__dirname, 'index.ts'), indexStr)
}
1 change: 1 addition & 0 deletions libs/shared/util-event/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from '../interface/_sample.schema'

0 comments on commit 0146241

Please sign in to comment.