From 9162ffd2fcf1f9e758bb83959f4128f3e6810941 Mon Sep 17 00:00:00 2001 From: Light13008 Date: Thu, 19 Dec 2024 21:50:35 +0530 Subject: [PATCH 1/2] Todo from apps/generator/test/integration.test.js line 153 is completed --- apps/generator/lib/generator.js | 27 ++++++++++++++++--- apps/generator/lib/logMessages.js | 4 +-- apps/generator/lib/renderer/react.js | 14 +++++++++- apps/generator/test/integration.test.js | 11 ++++++++ .../__transpiled/test-file.md.js | 4 +-- package-lock.json | 2 +- 6 files changed, 53 insertions(+), 9 deletions(-) diff --git a/apps/generator/lib/generator.js b/apps/generator/lib/generator.js index 077634e1c..9a8bf0848 100644 --- a/apps/generator/lib/generator.js +++ b/apps/generator/lib/generator.js @@ -91,6 +91,23 @@ class Generator { * @param {String} [options.registry.token] Optional parameter to pass npm registry auth token that you can grab from .npmrc file */ + /** + * Checks if a given file should be skipped based on the noOverwriteGlobs option. + * + * @private + * @param {string} filePath Path to the file to check against a list of glob patterns. + * @return {boolean} + */ + skipOverwrite(filePath) { + if (!Array.isArray(this.noOverwriteGlobs)) return false; + const shouldSkip = this.noOverwriteGlobs.some(globExp => minimatch(filePath, globExp)); + if (shouldSkip) { + console.debug(`Skipping overwrite for: ${filePath}`); + } + return shouldSkip; + } + + constructor(templateName, targetDir, { templateParams = {}, entrypoint, noOverwriteGlobs, disabledHooks, output = 'fs', forceWrite = false, install = false, debug = false, mapBaseUrlToFolder = {}, registry = {}, compile = true } = {}) { const options = arguments[arguments.length - 1]; this.verifyoptions(options); @@ -871,10 +888,14 @@ class Generator { const relativeTargetFile = path.relative(this.targetDir, targetFile); if (shouldIgnoreFile(relativeSourceFile)) return; - + if (this.skipOverwrite(relativeTargetFile)) { + return; + } const shouldOverwriteFile = await this.shouldOverwriteFile(relativeTargetFile); - if (!shouldOverwriteFile) return; - + if (!shouldOverwriteFile) { + log.debug(logMessage.skipOverwrite(sourceFile)); + return; + } if (this.templateConfig.conditionalFiles?.[relativeSourceFile]) { const server = this.templateParams.server && asyncapiDocument.servers().get(this.templateParams.server); const source = jmespath.search({ diff --git a/apps/generator/lib/logMessages.js b/apps/generator/lib/logMessages.js index a02f3f1f1..8a775865e 100644 --- a/apps/generator/lib/logMessages.js +++ b/apps/generator/lib/logMessages.js @@ -38,7 +38,7 @@ function relativeSourceFileNotGenerated(relativeSourceFile , subject) { return `${relativeSourceFile} was not generated because ${subject} specified in template configuration in conditionalFiles was not found in provided AsyncAPI specification file.`; } -function skipOverwrite(testFilePath) { +function write(testFilePath) { return `Skipping overwrite for: ${testFilePath}`; } @@ -64,5 +64,5 @@ module.exports = { relativeSourceFileNotGenerated, conditionalFilesMatched, compileEnabled, - skipOverwrite + write }; diff --git a/apps/generator/lib/renderer/react.js b/apps/generator/lib/renderer/react.js index 5a8171b8b..69ee31ce2 100644 --- a/apps/generator/lib/renderer/react.js +++ b/apps/generator/lib/renderer/react.js @@ -56,6 +56,17 @@ reactExport.renderReact = async (asyncapiDocument, filePath, extraTemplateData, ); }; +const skipOverwrite = (filePath, noOverwriteGlobs) => { + if (!Array.isArray(noOverwriteGlobs)) return false; + const shouldSkip = noOverwriteGlobs.some(globExp => minimatch(filePath, globExp)); + if (shouldSkip) { + console.debug(`Skipping overwrite for: ${filePath}`); + } + return shouldSkip; +}; + + + /** * Save the single rendered react content based on the meta data available. * @@ -86,9 +97,10 @@ const saveContentToFile = async (renderedContent, outputPath, noOverwriteGlobs = // get the final file name of the file const finalFileName = path.basename(filePath); // check whether the filename should be ignored based on user's inputs - const shouldOverwrite = !noOverwriteGlobs.some(globExp => minimatch(finalFileName, globExp)); + const shouldOverwrite = !skipOverwrite(filePath, noOverwriteGlobs); // Write the file only if it should not be skipped + if (shouldOverwrite) { await writeFile(filePath, content, { mode: permissions diff --git a/apps/generator/test/integration.test.js b/apps/generator/test/integration.test.js index c2df3fac5..21428b682 100644 --- a/apps/generator/test/integration.test.js +++ b/apps/generator/test/integration.test.js @@ -135,6 +135,10 @@ describe('Integration testing generateFromFile() to make sure the result of the const testFilePath = path.normalize(path.resolve(outputDir, testOutputFile)); await writeFile(testFilePath, testContent); + // Mock the console.debug method + const debugMock = jest.spyOn(console, 'debug').mockImplementation(() => {}); + + // Manually create an output first, before generation, with additional custom file to validate if later it is still there, not overwritten const generator = new Generator(cleanReactTemplate, outputDir, { forceWrite: true, @@ -142,6 +146,9 @@ describe('Integration testing generateFromFile() to make sure the result of the debug: true, }); + + console.log('Generator options:', generator.options); + await generator.generateFromFile(dummySpecPath); // Read the file to confirm it was not overwritten @@ -152,5 +159,9 @@ describe('Integration testing generateFromFile() to make sure the result of the /*TODO: Include log message test in the future to ensure that the log.debug for skipping overwrite is called */ + + expect(debugMock).toHaveBeenCalledWith(expect.stringContaining('Skipping overwrite for')); + // Restore the original console.debug method + debugMock.mockRestore(); }); }); diff --git a/apps/generator/test/test-templates/react-template/__transpiled/test-file.md.js b/apps/generator/test/test-templates/react-template/__transpiled/test-file.md.js index 93c5b0245..4c46bafdf 100644 --- a/apps/generator/test/test-templates/react-template/__transpiled/test-file.md.js +++ b/apps/generator/test/test-templates/react-template/__transpiled/test-file.md.js @@ -1,8 +1,8 @@ 'use strict'; require('source-map-support/register'); -const generatorReactSdk = require('@asyncapi/generator-react-sdk'); -const jsxRuntime = require('react/cjs/react-jsx-runtime.production.min'); +var generatorReactSdk = require('@asyncapi/generator-react-sdk'); +var jsxRuntime = require('/media/sarvesh-patil/all-Set-Up/AsyncApi/generator/apps/generator/node_modules/react/cjs/react-jsx-runtime.production.min.js'); function testFile_md ({ asyncapi, diff --git a/package-lock.json b/package-lock.json index d543520d0..32df19e15 100644 --- a/package-lock.json +++ b/package-lock.json @@ -21,7 +21,7 @@ }, "apps/generator": { "name": "@asyncapi/generator", - "version": "2.4.1", + "version": "2.5.0", "license": "Apache-2.0", "dependencies": { "@asyncapi/generator-hooks": "*", From 2f9eaf514ef2eb3ff1e3a5974a17c9cdd84773cb Mon Sep 17 00:00:00 2001 From: Light13008 Date: Thu, 19 Dec 2024 22:23:19 +0530 Subject: [PATCH 2/2] logMessage sipOverwrite function --- apps/generator/lib/logMessages.js | 4 +- .../react-template/package-lock.json | 63 ++++++++++++------- 2 files changed, 42 insertions(+), 25 deletions(-) diff --git a/apps/generator/lib/logMessages.js b/apps/generator/lib/logMessages.js index 8a775865e..a02f3f1f1 100644 --- a/apps/generator/lib/logMessages.js +++ b/apps/generator/lib/logMessages.js @@ -38,7 +38,7 @@ function relativeSourceFileNotGenerated(relativeSourceFile , subject) { return `${relativeSourceFile} was not generated because ${subject} specified in template configuration in conditionalFiles was not found in provided AsyncAPI specification file.`; } -function write(testFilePath) { +function skipOverwrite(testFilePath) { return `Skipping overwrite for: ${testFilePath}`; } @@ -64,5 +64,5 @@ module.exports = { relativeSourceFileNotGenerated, conditionalFilesMatched, compileEnabled, - write + skipOverwrite }; diff --git a/apps/generator/test/test-templates/react-template/package-lock.json b/apps/generator/test/test-templates/react-template/package-lock.json index 28b8e6d70..c557ac5a5 100644 --- a/apps/generator/test/test-templates/react-template/package-lock.json +++ b/apps/generator/test/test-templates/react-template/package-lock.json @@ -1850,9 +1850,9 @@ } }, "node_modules/@jsep-plugin/ternary": { - "version": "1.1.4", - "resolved": "https://registry.npmjs.org/@jsep-plugin/ternary/-/ternary-1.1.4.tgz", - "integrity": "sha512-ck5wiqIbqdMX6WRQztBL7ASDty9YLgJ3sSAK5ZpBzXeySvFGCzIvM6UiAI4hTZ22fEcYQVV/zhUbNscggW+Ukg==", + "version": "1.1.3", + "resolved": "https://registry.npmjs.org/@jsep-plugin/ternary/-/ternary-1.1.3.tgz", + "integrity": "sha512-qtLGzCNzPVJ3kdH6/zoLWDPjauHIKiLSBAR71Wa0+PWvGA8wODUQvRgxtpUA5YqAYL3CQ8S4qXhd/9WuWTZirg==", "engines": { "node": ">= 10.16.0" }, @@ -1991,34 +1991,34 @@ } }, "node_modules/@stoplight/spectral-core": { - "version": "1.19.2", - "resolved": "https://registry.npmjs.org/@stoplight/spectral-core/-/spectral-core-1.19.2.tgz", - "integrity": "sha512-Yx1j7d0VGEbsOCimPgl+L8w7ZuuOaxqGvXSUXgm9weoGR5idLQjPaTuHLdfdziR1gjqQdVTCEk/dN0cFfUKhow==", + "version": "1.18.3", + "resolved": "https://registry.npmjs.org/@stoplight/spectral-core/-/spectral-core-1.18.3.tgz", + "integrity": "sha512-YY8x7X2SWJIhGTLPol+eFiQpWPz0D0mJdkK2i4A0QJG68KkNhypP6+JBC7/Kz3XWjqr0L/RqAd+N5cQLPOKZGQ==", "dependencies": { "@stoplight/better-ajv-errors": "1.0.3", "@stoplight/json": "~3.21.0", "@stoplight/path": "1.3.2", "@stoplight/spectral-parsers": "^1.0.0", - "@stoplight/spectral-ref-resolver": "^1.0.4", + "@stoplight/spectral-ref-resolver": "^1.0.0", "@stoplight/spectral-runtime": "^1.0.0", "@stoplight/types": "~13.6.0", "@types/es-aggregate-error": "^1.0.2", "@types/json-schema": "^7.0.11", - "ajv": "^8.17.1", + "ajv": "^8.6.0", "ajv-errors": "~3.0.0", "ajv-formats": "~2.1.0", "es-aggregate-error": "^1.0.7", - "jsonpath-plus": "10.1.0", + "jsonpath-plus": "7.1.0", "lodash": "~4.17.21", "lodash.topath": "^4.5.2", "minimatch": "3.1.2", - "nimma": "0.2.3", + "nimma": "0.2.2", "pony-cause": "^1.0.0", - "simple-eval": "1.0.1", + "simple-eval": "1.0.0", "tslib": "^2.3.0" }, "engines": { - "node": "^16.20 || ^18.18 || >= 20.17" + "node": "^12.20 || >= 14.13" } }, "node_modules/@stoplight/spectral-core/node_modules/@stoplight/types": { @@ -2033,6 +2033,14 @@ "node": "^12.20 || >=14.13" } }, + "node_modules/@stoplight/spectral-core/node_modules/jsonpath-plus": { + "version": "7.1.0", + "resolved": "https://registry.npmjs.org/jsonpath-plus/-/jsonpath-plus-7.1.0.tgz", + "integrity": "sha512-gTaNRsPWO/K2KY6MrqaUFClF9kmuM6MFH5Dhg1VYDODgFbByw1yb7xu3hrViE/sz+dGOeMWgCzwUwQtAnCTE9g==", + "engines": { + "node": ">=12.0.0" + } + }, "node_modules/@stoplight/spectral-core/node_modules/tslib": { "version": "2.6.3", "resolved": "https://registry.npmjs.org/tslib/-/tslib-2.6.3.tgz", @@ -2363,9 +2371,9 @@ } }, "node_modules/astring": { - "version": "1.9.0", - "resolved": "https://registry.npmjs.org/astring/-/astring-1.9.0.tgz", - "integrity": "sha512-LElXdjswlqjWrPpJFg1Fx4wpkOCxj1TDHlSV4PlaRxHGWko024xICaa97ZkMfs6DRKlCguiAI+rbXv5GWwXIkg==", + "version": "1.8.6", + "resolved": "https://registry.npmjs.org/astring/-/astring-1.8.6.tgz", + "integrity": "sha512-ISvCdHdlTDlH5IpxQJIex7BWBywFWgjJSVdwst+/iQCoEYnyOaQ95+X1JGshuBjGp6nxKUy1jMgE3zPqN7fQdg==", "bin": { "astring": "bin/astring" } @@ -3441,9 +3449,9 @@ "integrity": "sha512-sGkPx+VjMtmA6MX27oA4FBFELFCZZ4S4XqeGOXCv68tT+jb3vk/RyaKWP0PTKyWtmLSM0b+adUTEvbs1PEaH2w==" }, "node_modules/nimma": { - "version": "0.2.3", - "resolved": "https://registry.npmjs.org/nimma/-/nimma-0.2.3.tgz", - "integrity": "sha512-1ZOI8J+1PKKGceo/5CT5GfQOG6H8I2BencSK06YarZ2wXwH37BSSUWldqJmMJYA5JfqDqffxDXynt6f11AyKcA==", + "version": "0.2.2", + "resolved": "https://registry.npmjs.org/nimma/-/nimma-0.2.2.tgz", + "integrity": "sha512-V52MLl7BU+tH2Np9tDrIXK8bql3MVUadnMIl/0/oZSGC9keuro0O9UUv9QKp0aMvtN8HRew4G7byY7H4eWsxaQ==", "dependencies": { "@jsep-plugin/regex": "^1.0.1", "@jsep-plugin/ternary": "^1.0.2", @@ -3454,10 +3462,19 @@ "node": "^12.20 || >=14.13" }, "optionalDependencies": { - "jsonpath-plus": "^6.0.1 || ^10.1.0", + "jsonpath-plus": "^6.0.1", "lodash.topath": "^4.5.2" } }, + "node_modules/nimma/node_modules/jsonpath-plus": { + "version": "6.0.1", + "resolved": "https://registry.npmjs.org/jsonpath-plus/-/jsonpath-plus-6.0.1.tgz", + "integrity": "sha512-EvGovdvau6FyLexFH2OeXfIITlgIbgZoAZe3usiySeaIDm5QS+A10DKNpaPBBqqRSZr2HN6HVNXxtwUAr2apEw==", + "optional": true, + "engines": { + "node": ">=10.0.0" + } + }, "node_modules/node-fetch": { "version": "2.6.7", "resolved": "https://registry.npmjs.org/node-fetch/-/node-fetch-2.6.7.tgz", @@ -3803,11 +3820,11 @@ } }, "node_modules/simple-eval": { - "version": "1.0.1", - "resolved": "https://registry.npmjs.org/simple-eval/-/simple-eval-1.0.1.tgz", - "integrity": "sha512-LH7FpTAkeD+y5xQC4fzS+tFtaNlvt3Ib1zKzvhjv/Y+cioV4zIuw4IZr2yhRLu67CWL7FR9/6KXKnjRoZTvGGQ==", + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/simple-eval/-/simple-eval-1.0.0.tgz", + "integrity": "sha512-kpKJR+bqTscgC0xuAl2xHN6bB12lHjC2DCUfqjAx19bQyO3R2EVLOurm3H9AUltv/uFVcSCVNc6faegR+8NYLw==", "dependencies": { - "jsep": "^1.3.6" + "jsep": "^1.1.2" }, "engines": { "node": ">=12"