superplate icon indicating copy to clipboard operation
superplate copied to clipboard

Support for creating files dynamically

Open buremba opened this issue 3 years ago • 3 comments

I'm working on a Superplate plugin that will automatically generate the CRUD pages based on the value the user enters for a config defined in prompt.js. In order to be able to create the CRUD pages, I'm looking for a way to create Typescript files/directories in a dynamic way and I couldn't figure out how I can do that. Is it something on the roadmap?

buremba avatar Nov 17 '22 13:11 buremba

Hey @buremba, that's a good idea really, and can inspire for more ideas around that. Unfortunately this is not planned in our roadmap due to limitations on SAO. We use SAO for compiling the templates in the files and moving etc and it does not support such dynamic structure. (I guess it won't be because its archived already by the owner of the project 😅)

Still, I think we can do a workaround here, we already have extend.js for the plugins which allows them to pass conditional values and ignore files conditionally.

We can add some field like files with type Array<{ path: string; filename: string; content: string; encoding: string; }> which can be created dynamically by the answers to the prompts.

Then, we should create those files inside the plugin file (already stored in a temporary directory after cloning). This way we can trick SAO to handle those files like the regular plugin files (compiling and moving to the target).

As the result, we can create files dynamically. 🎉 🎉

While doing so, we might need to check and update our code for handling prompts, for dynamic files we might want to support wider range of prompt types 🤔

Currently, this is not planned but hypothetically it looks like something achievable 😅 If you want to give it a try, We'll be happy to help 🙏

aliemir avatar Nov 17 '22 14:11 aliemir

Thanks for the quick answer @aliemir! I understand the limitation and I feel like this is not Refine/Superplate's focus and the solution that you suggested makes sense.

What would you think about implementing something more generic such as a post_hook in ArrayPromptOptions? It can be a function with the following signature:

{ choice: Choice; path: string; }

Superplate can call this callback function/hook when the user selects a choice and the template files are generated automatically. That way, the post_hook can generate files or do something else as it's just a function. My assumption is that this is easier to implement and also generic enough to cover different use cases. WDYT?

buremba avatar Nov 17 '22 15:11 buremba

Hey @buremba sorry for the late response 🙏 post_hook is a better generic to handle 🚀 I think there's no limitation on the current codebase to conflict with this implementation 🤔 We might need to provide a richer context to post_hook to enable wider range of use cases maybe? (Just thinking out loud 😅)

Thanks again for the issue, I'll definitely try to spare some time for this. This can provide a great flexibility to plugins 💪

aliemir avatar Nov 23 '22 09:11 aliemir