diff --git a/libs/plugin/package.json b/libs/plugin/package.json index 3b2b299..3beb4d1 100644 --- a/libs/plugin/package.json +++ b/libs/plugin/package.json @@ -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" } } diff --git a/libs/plugin/src/lib/generatorPlugin.ts b/libs/plugin/src/lib/generatorPlugin.ts index d63e0ac..8753c35 100644 --- a/libs/plugin/src/lib/generatorPlugin.ts +++ b/libs/plugin/src/lib/generatorPlugin.ts @@ -1,4 +1,3 @@ -import NodeCache from "node-cache" import { GeneratorSchema } from "@leancodepl/contractsgenerator-typescript-schema" import { GeneratorPluginInstance } from "./generatorPluginInstance" @@ -14,7 +13,6 @@ export interface GeneratorInput { export type GeneratorSessionContext = { getSchema(input: GeneratorInput): Promise - cache: NodeCache metadata: any } diff --git a/package-lock.json b/package-lock.json index 45cbb73..72a8a26 100644 --- a/package-lock.json +++ b/package-lock.json @@ -17,7 +17,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", @@ -63,8 +62,7 @@ "version": "1.1.3", "license": "Apache-2.0", "dependencies": { - "@leancodepl/contractsgenerator-typescript-schema": "1.1.3", - "node-cache": "^5.0.0" + "@leancodepl/contractsgenerator-typescript-schema": "1.1.3" } }, "libs/schema": { @@ -10393,15 +10391,6 @@ "url": "https://github.com/chalk/wrap-ansi?sponsor=1" } }, - "node_modules/clone": { - "version": "2.1.2", - "resolved": "https://registry.npmjs.org/clone/-/clone-2.1.2.tgz", - "integrity": "sha512-3Pe/CF1Nn94hyhIYpjtiLhdCoEoz0DqQ+988E9gmeEdQZlojxnOb74wctFyuwWQHzqyf9X7C7MG8juUpqBJT8w==", - "license": "MIT", - "engines": { - "node": ">=0.8" - } - }, "node_modules/clone-deep": { "version": "4.0.1", "resolved": "https://registry.npmjs.org/clone-deep/-/clone-deep-4.0.1.tgz", @@ -23654,18 +23643,6 @@ "dev": true, "license": "MIT" }, - "node_modules/node-cache": { - "version": "5.1.2", - "resolved": "https://registry.npmjs.org/node-cache/-/node-cache-5.1.2.tgz", - "integrity": "sha512-t1QzWwnk4sjLWaQAS8CHgOJ+RAfmHpxFWmc36IWTiWHQfs0w5JDMBS1b1ZxQteo0vVVuWJvIUKHDkkeK7vIGCg==", - "license": "MIT", - "dependencies": { - "clone": "2.x" - }, - "engines": { - "node": ">= 8.0.0" - } - }, "node_modules/node-fetch": { "version": "2.7.0", "resolved": "https://registry.npmjs.org/node-fetch/-/node-fetch-2.7.0.tgz", @@ -29781,7 +29758,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", diff --git a/package.json b/package.json index 9f72717..944c4ca 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/packages/generator/package.json b/packages/generator/package.json index d4e8e37..3a0fe31 100644 --- a/packages/generator/package.json +++ b/packages/generator/package.json @@ -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", diff --git a/packages/generator/src/cache.ts b/packages/generator/src/cache.ts deleted file mode 100644 index 370d2b7..0000000 --- a/packages/generator/src/cache.ts +++ /dev/null @@ -1,3 +0,0 @@ -import NodeCache from "node-cache" - -export const cache = new NodeCache({ checkperiod: 0, useClones: false }) diff --git a/packages/generator/src/generate.ts b/packages/generator/src/generate.ts index 5ba4a5e..663d367 100644 --- a/packages/generator/src/generate.ts +++ b/packages/generator/src/generate.ts @@ -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" @@ -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 = {} - 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 } diff --git a/packages/generator/src/generateFile.ts b/packages/generator/src/generateFile.ts index 78b8367..ef1b0bd 100644 --- a/packages/generator/src/generateFile.ts +++ b/packages/generator/src/generateFile.ts @@ -1,3 +1,4 @@ +import { concat } from "lodash" import { GeneratorFileContext, GeneratorPlugin, @@ -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("") } diff --git a/packages/generator/src/getSchemaCached.ts b/packages/generator/src/getSchemaCached.ts index 10e38d1..921486d 100644 --- a/packages/generator/src/getSchemaCached.ts +++ b/packages/generator/src/getSchemaCached.ts @@ -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>() + + return (input: GeneratorInput) => { const inputHash = hash(input) - const cachedSchema = cache.get(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 } }