Skip to content

Commit

Permalink
Revert "fixed logging issue"
Browse files Browse the repository at this point in the history
This reverts commit 3e7afe7.
  • Loading branch information
lmgyuan committed May 24, 2024
1 parent fbd41c8 commit 09e7882
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 24 deletions.
1 change: 0 additions & 1 deletion jest.config.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
module.exports = {
clearMocks: true,
modulePathIgnorePatterns: ["./__mocks__(?!/loglevel.js)"],
moduleNameMapper: {
'^nimma/legacy$': '<rootDir>/node_modules/nimma/dist/legacy/cjs/index.js',
'^nimma/(.*)': '<rootDir>/node_modules/nimma/dist/cjs/$1',
Expand Down
8 changes: 4 additions & 4 deletions lib/generator.js
Original file line number Diff line number Diff line change
Expand Up @@ -145,8 +145,8 @@ class Generator {
}
});
});
// // PR 1162: set log level during construction to avoid asynchronous issues
// this.setLogLevel();
// PR 1162: set log level during construction to avoid asynchronous issues
this.setLogLevel();
}

/**
Expand Down Expand Up @@ -200,7 +200,7 @@ class Generator {
this.validateAsyncAPIDocument(asyncapiDocument);
await this.setupOutput();
// PR 1162: always set log level during construction to avoid asynchronous issues
this.setLogLevel();
// this.setLogLevel();

await this.installAndSetupTemplate();
await this.configureTemplateWorkflow(parseOptions);
Expand Down Expand Up @@ -856,7 +856,7 @@ class Generator {
if (renderContent === undefined) {
return;
} else if (isReactTemplate(this.templateConfig)) {
await saveRenderedReactContent(renderContent, outputPath, this.noOverwriteGlobs, this);
await saveRenderedReactContent(renderContent, outputPath, this.noOverwriteGlobs);
} else {
await writeFile(outputPath, renderContent);
}
Expand Down
7 changes: 1 addition & 6 deletions lib/logMessages.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,6 @@ function conditionalFilesMatched(relativeSourceFile) {
return `${relativeSourceFile} was not generated because condition specified for this file in template configuration in conditionalFiles matched.`;
}

function skipOverwriting(filePath) {
return `Skipping overwrite for: ${filePath}`;
}

module.exports = {
TEMPLATE_INSTALL_FLAG_MSG,
TEMPLATE_INSTALL_DISK_MSG,
Expand All @@ -63,7 +59,6 @@ module.exports = {
templateSuccessfullyInstalled,
relativeSourceFileNotGenerated,
conditionalFilesMatched,
fileNotOverwritten,
skipOverwriting
fileNotOverwritten

};
15 changes: 9 additions & 6 deletions lib/renderer/react.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ const path = require('path');
const AsyncReactSDK = require('@asyncapi/generator-react-sdk');
const minimatch = require('minimatch');
const log = require('loglevel');
const logMessages = require('../logMessages');
const {
writeFile
} = require('../utils');
Expand Down Expand Up @@ -62,7 +61,7 @@ reactExport.renderReact = async (asyncapiDocument, filePath, extraTemplateData,
* @param {String} outputPath Path to the file being rendered.
* @param {String[]} noOverwriteGlobs globs to check for files that should not be overwritten.
*/
const saveContentToFile = async (renderedContent, outputPath, noOverwriteGlobs = [], generator) => {
const saveContentToFile = async (renderedContent, outputPath, noOverwriteGlobs = []) => {
let filePath = outputPath;
// Might be the same as in the `fs` package, but is an active choice for our default file permission for any rendered files.
let permissions = 0o666;
Expand All @@ -87,8 +86,12 @@ 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));
// reuse methods from the generator.js to check if the file should be overwritten
const shouldOverwrite = await generator.shouldOverwriteFile(filePath);
const shouldOverwrite = await reactExport.shouldOverwriteFile(filePath, noOverwriteGlobs);

// Write the file only if it should not be skipped
if (shouldOverwrite) {
Expand All @@ -97,7 +100,7 @@ const saveContentToFile = async (renderedContent, outputPath, noOverwriteGlobs =
});
} else {
log.debug(`noOverwriteGlobs matched`);
log.debug(logMessages.skipOverwriting(filePath));
log.debug(`Skipping overwrite for: ${filePath}`);
}
};

Expand All @@ -109,11 +112,11 @@ const saveContentToFile = async (renderedContent, outputPath, noOverwriteGlobs =
* @param {String} outputPath Path to the file being rendered.
* @param {String[]} noOverwriteGlobs globs to check for files that should not be overwritten.
*/
reactExport.saveRenderedReactContent = async (renderedContent, outputPath, noOverwriteGlobs = [], generator) => {
reactExport.saveRenderedReactContent = async (renderedContent, outputPath, noOverwriteGlobs = []) => {
// If the rendered content is an array, we need to save each file individually
if (Array.isArray(renderedContent)) {
return Promise.all(renderedContent.map(content => saveContentToFile(content, outputPath, noOverwriteGlobs)));
}
// Otherwise, we can save the single file
return saveContentToFile(renderedContent, outputPath, noOverwriteGlobs, generator);
return saveContentToFile(renderedContent, outputPath, noOverwriteGlobs);
};
19 changes: 12 additions & 7 deletions test/integration.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ const {exists, writeFile} = require('../lib/utils');
const mainTestResultPath = 'test/temp/integrationTestResult';
const reactTemplate = 'test/test-templates/react-template';
const nunjucksTemplate = 'test/test-templates/nunjucks-template';
const logMessage = require('../lib/logMessages');
const log = require('loglevel');

describe('Integration testing generateFromFile() to make sure the result of the generation is not changend comparing to snapshot', () => {
Expand Down Expand Up @@ -68,12 +67,12 @@ describe('Integration testing generateFromFile() to make sure the result of the
});

it('should ignore specified files with noOverwriteGlobs', async () => {
const logSpyDebug = jest.spyOn(log, 'debug').mockImplementation(() => {});
// log.debug = jest.fn();
// const logSpyDebug = jest.spyOn(log, 'debug').mockImplementation(() => {});
log.debug = jest.fn();

const outputDir = generateFolderName();
// Manually create a file to test if it's not overwritten
if (!existsSync(outputDir)) {
if (!await exists(outputDir)) {
mkdirSync(outputDir, { recursive: true });
}
// Create a variable to store the file content
Expand All @@ -96,9 +95,15 @@ describe('Integration testing generateFromFile() to make sure the result of the
// Check if the files have been overwritten
await expect(fileContent).toBe(testContent);
// Check if the log debug message was printed
// await expect(logSpyDebug).toHaveBeenCalledWith(`noOverwriteGlobs matched`);
await expect(logSpyDebug).toHaveBeenCalledWith(logMessage.skipOverwriting(testFilePath));
await expect(log.debug).toHaveBeenCalledWith(`noOverwriteGlobs matched`);
await expect(log.debug).toHaveBeenCalledWith(`Skipping overwrite for: ${testFilePath}`);
// await console.log('All console.log calls:');

logSpyDebug.mockRestore();
// // Print all console.log calls
// await log.mock.calls.forEach((call, index) => {
// console.log(`${index + 1}:`, call);
// });

log.mockRestore();
});
});

0 comments on commit 09e7882

Please sign in to comment.