Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions libs/plugin/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
"license": "Apache-2.0",
"repository": "https://github.com/leancodepl/contractsgenerator-typescript",
"dependencies": {
"@leancodepl/contractsgenerator-typescript-schema": "1.1.3",
"node-cache": "^5.0.0"
"@leancodepl/contractsgenerator-typescript-schema": "1.1.3"
}
}
2 changes: 0 additions & 2 deletions libs/plugin/src/lib/generatorPlugin.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import NodeCache from "node-cache"
import { GeneratorSchema } from "@leancodepl/contractsgenerator-typescript-schema"
import { GeneratorPluginInstance } from "./generatorPluginInstance"

Expand All @@ -14,7 +13,6 @@ export interface GeneratorInput {

export type GeneratorSessionContext = {
getSchema(input: GeneratorInput): Promise<GeneratorSchema>
cache: NodeCache
metadata: any
}

Expand Down
26 changes: 1 addition & 25 deletions package-lock.json

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

1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
"lilconfig": "3.1.3",
"lodash": "^4.0.0",
"long": "5.3.2",
"node-cache": "5.1.2",
"object-hash": "3.0.0",
"tslib": "^2.3.0",
"yaml": "2.8.0",
Expand Down
1 change: 0 additions & 1 deletion packages/generator/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
"@leancodepl/utils": ">=7.2.3",
"lilconfig": "3.1.3",
"lodash": "^4.0.0",
"node-cache": "5.1.2",
"object-hash": "3.0.0",
"yaml": "2.8.0",
"yargs": "18.0.0",
Expand Down
3 changes: 0 additions & 3 deletions packages/generator/src/cache.ts

This file was deleted.

15 changes: 7 additions & 8 deletions packages/generator/src/generate.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { z } from "zod/v4"
import { GeneratorSessionContext } from "@leancodepl/contractsgenerator-typescript-plugin"
import { cache } from "./cache"
import { generateFile } from "./generateFile"
import { getSchemaCached } from "./getSchemaCached"

Expand All @@ -26,19 +25,19 @@ const contractsGeneratorConfigurationSchema = z.object({
export async function generate(unsafeConfig: unknown) {
const config = contractsGeneratorConfigurationSchema.parse(unsafeConfig)

const sessionContext: GeneratorSessionContext = { getSchema: getSchemaCached(cache), metadata: {}, cache }
const sessionContext: GeneratorSessionContext = { getSchema: getSchemaCached(), metadata: {} }

const configl1 = config.config

const outputs: Record<string, string> = {}

for (const file in config.generates) {
const configuration = config.generates[file]
await Promise.all(
Object.entries(config.generates).map(async ([file, configuration]) => {
const configl2 = configuration.config ?? {}

const configl2 = configuration.config ?? {}

outputs[file] = await generateFile({ ...configl1, ...configl2 }, configuration.plugins, sessionContext)
}
outputs[file] = await generateFile({ ...configl1, ...configl2 }, configuration.plugins, sessionContext)
}),
)

return outputs
}
72 changes: 40 additions & 32 deletions packages/generator/src/generateFile.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { concat } from "lodash"
import {
GeneratorFileContext,
GeneratorPlugin,
Expand All @@ -14,36 +15,43 @@ export async function generateFile(
metadata: {},
}

let prepend = ""
let fileOutput = ""
let append = ""

for (const pluginConfiguration of plugins) {
let configl3, pluginName

if (typeof pluginConfiguration === "string") {
pluginName = pluginConfiguration
configl3 = {}
} else {
pluginName = Object.keys(pluginConfiguration)[0]
configl3 = pluginConfiguration[pluginName]
}

const plugin: GeneratorPlugin = await import(`@leancodepl/contractsgenerator-typescript-plugin-${pluginName}`).then(
plugin => plugin.default ?? plugin,
)

const pluginInstance = plugin.instance(
{ ...configl2, ...configl3 },
{ session: sessionContext, file: fileContext, plugin: fileContext },
)

prepend += (await pluginInstance.beforeAll?.()) ?? ""
fileOutput += (await pluginInstance.before?.()) ?? ""
fileOutput += (await pluginInstance.generate?.()) ?? ""
fileOutput += (await pluginInstance.after?.()) ?? ""
append += (await pluginInstance.afterAll?.()) ?? ""
}

return prepend + fileOutput + append
const prepend: string[] = []
const output: string[] = []
const append: string[] = []

await Promise.all(
plugins.map(async (pluginConfiguration, i) => {
let configl3, pluginName

if (typeof pluginConfiguration === "string") {
pluginName = pluginConfiguration
configl3 = {}
} else {
pluginName = Object.keys(pluginConfiguration)[0]
configl3 = pluginConfiguration[pluginName]
}

const plugin: GeneratorPlugin = await import(
`@leancodepl/contractsgenerator-typescript-plugin-${pluginName}`
).then(plugin => plugin.default ?? plugin)

const pluginInstance = plugin.instance(
{ ...configl2, ...configl3 },
{ session: sessionContext, file: fileContext, plugin: fileContext },
)

const [beforeAll = "", before = "", generate = "", after = "", afterAll = ""] = await Promise.all([
pluginInstance.beforeAll?.(),
pluginInstance.before?.(),
pluginInstance.generate?.(),
pluginInstance.after?.(),
pluginInstance.afterAll?.(),
])
prepend[i] = beforeAll
output[i] = before + generate + after
append[i] = afterAll
}),
)

return concat(prepend, output, append).join("")
}
20 changes: 12 additions & 8 deletions packages/generator/src/getSchemaCached.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,25 @@
import NodeCache from "node-cache"
import hash from "object-hash"
import { GeneratorInput } from "@leancodepl/contractsgenerator-typescript-plugin"
import { GeneratorSchema } from "@leancodepl/contractsgenerator-typescript-schema"
import { getSchema } from "./getSchema"

export function getSchemaCached(cache: NodeCache) {
return async (input: GeneratorInput) => {
export function getSchemaCached() {
const promises = new Map<string, Promise<GeneratorSchema>>()

return (input: GeneratorInput) => {
const inputHash = hash(input)

const cachedSchema = cache.get<GeneratorSchema>(inputHash)
const existingPromise = promises.get(inputHash)

if (cachedSchema) return cachedSchema
if (existingPromise) return existingPromise

const schema = await getSchema(input)
const promise = getSchema(input).catch(error => {
promises.delete(inputHash)
throw error
})

cache.set(inputHash, schema)
promises.set(inputHash, promise)

return schema
return promise
}
}