mde / ejs

Embedded JavaScript templates -- http://ejs.co
Apache License 2.0
7.7k stars 841 forks source link

Parse required variables from file #683

Closed willwill96 closed 2 years ago

willwill96 commented 2 years ago

Apologies if this has already been asked or if this is the wrong place to put this question.

It's typical for me to create a CLI command along with my ejs templates. It would be nice if I could automate some of the argument and/or type definitions.

What I'm looking for is a utility to scaffold typescript types based on my ejs template. I am not looking for anything too complicated (I'm not looking for type inference).

At a minimum, just getting a list of all required variable names would be good enough for my purposes.

It feels like this is something that may already be available somewhere in .ejs, but maybe it's not part of the public API, or my google skills are just rusty :laughing:

mde commented 2 years ago

I'm not sure what you're asking for here. EJS already ships with a CLI itself. And of course, as it's EJS (not ETS), we don't do types. Could you flesh this request out a bit more?

vmorkunas commented 2 years ago

We want to know is it possible to fetch all variables used in template before processing it. In some automation we would ask user to enter values to these unknown vars so having the list of vars before processing would allow to pass data to the template which is required.

For example we have a template:

<% for (var i = 0; i < aws_account_region.length; i++) { %> cleanup-<%= aws_account_name %>-<%= aws_account_region[i] %>-envs-js: stage: cleanup-envs-js tags:

so the output we want is to list all variables in this template which would be: aws_account_name, aws_account_region

mde commented 2 years ago

Yes, if you pass the option of _with: false to rendering, passed vars are put in a locals variable, rather than being top-level variables. This should allow you to access all of them cleanly.