generator icon indicating copy to clipboard operation
generator copied to clipboard

feat: support `noOverwriteGlobs` for templates based on react

Open lmgyuan opened this issue 2 months ago • 6 comments

Description

  • add noOverwriteGlobs parameters to methods saveContentToFile and saveRenderedReactContent
  • pass this.noOverwriteGlobs to saveContentToFile
  • check the name of file to be written against the noOverwriteGlobs to determine whether it should be actually written to the target directory
  • add additional comments to relevant codes in the codebase so they are easier for future maintainers and contributors to understand.

Related issue(s)

Fixes #1128

lmgyuan avatar Apr 16 '24 16:04 lmgyuan

@derberg Thanks for your comments and guidance on adding a test to the test/integration.test.js! I agree that we could add a test so we can check whether noOverwriteGlobs is working when we use React Engine. However, I am a bit confused about how to use the template-for-generator-templates. You seem to suggest that I could use https://github.com/asyncapi/template-for-generator-templates react-based template as a base for test, so do you mean something like the following:

const generator = new Generator('https://github.com/asyncapi/template-for-generator-templates.git#328', outputDir, {
      forceWrite: true,
      noOverwriteGlobs: ['**/schema.js', '**/anotherFile.js'] // Specify files to ignore
    });

There is not a specific reason for picking "#328" as the commit that this test is based on. I just picked one out of all the commits there are. I can see from the codes that the template generates a template that will generate some files, but may I ask how I could give it parameters so its output contains instructions about specific files that I want it to generate? For example, if I want the output generator template to have something like

<File name={`noOverwrite_test.html`}>
        <SchemaFile schemaName={schemaName} schema={schema} />
      </File>

so after I use the generator template to generate files, I can check that it does not overwrite noOverwrite_test.html (with --noOverwriteGlobs nOverwrite_test as a command line argument). But how do I use the template-for-generator-templates to generate the output template with instructions on file name in the first place?

Currently, instead of using template-for-generator-template, I am thinking maybe the following codes could work as a test for noOverwriteGlobs feature as well:

it('should ignore specified files with noOverwriteGlobs', async () => {
    const outputDir = generateFolderName();
    // Manually create a file to test if it's not overwritten
    const testFilePath = path.join(outputDir, 'index.html');
    // eslint-disable-next-line sonarjs/no-duplicate-string
    writeFileSync(testFilePath, '<script>const initialContent = "This should not change";</script>');

    // based on the html-template documentation, the default output is index.html
    const generator = new Generator('@asyncapi/[email protected]', outputDir, {
      forceWrite: true,
      outFilename: 'index.html',
      noOverwriteGlobs: ['**/index.html']
    });

    generator.generateFromFile(dummySpecPath);
    
    // Read the file to confirm it was not overwritten
    const fileContent = readFileSync(testFilePath, 'utf8');
    expect(fileContent).toBe('<script>const initialContent = "This should not change";</script>');
  });

Let me know what you think! : )

lmgyuan avatar Apr 17 '24 17:04 lmgyuan

You seem to suggest that I could use https://github.com/asyncapi/template-for-generator-templates react-based template as a base for test, so do you mean something like the following:

yup exactly, just use last commit from that repo, like this

https://github.com/asyncapi/template-for-generator-templates.git#f8353637ca6bc2b4eaa5514ceb3bd06fe145e9c6

but @asyncapi/[email protected] is good too, especially that we use it in other test cases. I like your test suggestion 💯 especially that even though 1 file is ignored, with '**/index.html' globs are still tested

go ahead with implementation please

derberg avatar Apr 18 '24 08:04 derberg

@derberg just update the implementation!

lmgyuan avatar Apr 18 '24 14:04 lmgyuan

@lmgyuan you'll need to solve conflicts first before you amend your integration tests

derberg avatar Apr 29 '24 08:04 derberg

@lmgyuan you'll need to solve conflicts first before you amend your integration tests

Thanks! I will come back and take a closer look at this after my exams and projects are over : )

lmgyuan avatar Apr 29 '24 22:04 lmgyuan