practica icon indicating copy to clipboard operation
practica copied to clipboard

Code the code generator

Open goldbergyoni opened this issue 3 years ago • 3 comments

🎯 Goal: - Currently our code generator does almost nothing but copying the code-templates folder to the destination as-is. We need to perform some logic to support future cases (e.g. remove some code blocks). We should code a small module that performs this logic during code generation

For a start, we should support:

  • Not copying specific file/folder (e.g., GitHub workflow file)
  • Removing the 'Best Practice' comments from our code (the user might flag not to have those comments inside her code). Example here
  • These 👆 will serve as an example for future modifications and logic

🤔 Things to consider:

  • This logic exists inside the generation-service
  • We tried a code generator that is based on templating engine, but then the repo code is not executable (due to the mustache characters) and it becomes much harder to modify code, test it and sync with the repo. Our solution should be based on custom code/regex that does the manipulation. In other words, the source files in code-templates should remain valid .ts files that can be executed
  • There is already a test file with 1-2 example tests in the same folder

🏭 Proposed design: - Our code generator should act on .ts files, apply its logic based on custom code and identify areas of change using regex/comments

  • The main function generate-service.generateApp should recursively load file from the source code-template folder. Fs.walk is a great library for a recursive reading of all files
  • It should load an array of modifiers functions and call each one of them for every file. A modifier is a function that can apply some logic to the copied file, tweak the file content or instruct not to copy it
  • The function modifier.modifyFileEntry should response with: {fileContent: string, copyItem: boolean, newFileName: string}
  • For example, we can have 'bestPracticeCommentRemovalModifier' that will get files, if one has 'Best practice comment' (example) It will remove it. We may use Regex for this (the user might flag not to have those comments inside her code)
  • For every file, the modifier response will guide the main function (generate-service.generateApp) whether to copy it at all, and if yes it will copy the content that was returned by the modifier
  • This way, anytime we wish to introduce a new logic - It demands nothing but introducing a new function (modifier)

goldbergyoni avatar Jun 30 '22 14:06 goldbergyoni

Why not use tools like yeoman or plopjs?

mikicho avatar Jul 03 '22 17:07 mikicho

Because we want to have full control. I've tried plopjs, it's great for simple and quick skeleton. We're different - A robust generator that might end with some complex logic. Consider replacing the express layer with fastify layer. Also, plopjs is based on templating - the issue explains why templating won't work for us

Yeoman is the front DX which the developer uses, we want to have our own UI/DX/CLI. You can tweak it but it never built to serve as the back layer.

Does it make sense?

goldbergyoni avatar Jul 05 '22 18:07 goldbergyoni

Yes. Must say that it sounds like a package by itself, and we may want separate it to another repo

mikicho avatar Jul 05 '22 20:07 mikicho