Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Inject multiple node dependencies within one template, each with dedicated skip_if's #422

Open
kaplanan opened this issue Mar 3, 2023 · 1 comment

Comments

@kaplanan
Copy link
Contributor

kaplanan commented Mar 3, 2023

I want to know if it is possible, and if not, feasible, to create one template for injecting multiple (let's say node) dependencies to the package.json with one template, but each of them may be coupled to a different skip_if. For instance:

// Project setup (1.0)
_templates/
  proxy/
    controller/
      controller.ejs.t
      dependencies.ejs.t
package.json

Whereas the controller template scaffolds something like follows:

// controller.ejs.t
---
to: app/controller/<%= name %>.controller.ts
---
import { Body, Controller, Post, Get } from '@nestjs/common'
<% if(locals.healthcheck) {-%>import { ApiResponse, ApiExcludeEndpoint } from '@nestjs/swagger'<% } -%>

@Controller()
export class Controller {
    constructor() {}

# THIS IS ONLY AVAILABLE IF healthcheck GETS PASSED AS PROPERTY
<% if (locals.healthcheck) { -%>
    @ApiExcludeEndpoint()
    @Get(`${BASE_URL}${HEALTH_CHECK}`)
    @ApiResponse({ status: 200, description: 'Proxy service is up and running.' })
    async health(): Promise<void> {}
<% } -%>

    @Post()
    async proxyRequest(@Body() request: ProxyRequestDto) {
        // no op
    }
}

We can see in the controller example above, that some dependencies will only be necessary to import and therefore maintained in the package.json if the optional code block gets included in the file creation.

Currently, the only possibility, at least as it seems to me, would be to create one template for each dependency:

// dependencies.ejs.t, mandatory dependencies
---
inject: true
to: package.json
after: devDependencies
skip_if: nestjs/common
sh: cd <%= cwd %> && yarn
---
    "@nestjs/common":"*",

// dependencies.health.ejs.t, optional dependencies (only if healthcheck is included)
---
inject: true
to: package.json
after: devDependencies
skip_if: nestjs/swagger
sh: cd <%= cwd %> && yarn
---
    <% if(locals.healthcheck) {-%>"@nestjs/swagger":"*",<% } -%>

Which leads us to the following template setup

// Project setup (2.0)
_templates/
  proxy/
    controller/
      controller.ejs.t
      dependencies.ejs.t
      dependencies.health.ejs.t
package.json

This would be fine for this scenario, however, it would scale linear with increasing complexity of your templates.
It would be nice to have something like aggregations of Frontmatter templates like this:

// dependencies.ejs.t
---
inject: true
to: package.json
after: devDependencies
skip_if: nestjs/common
sh: cd <%= cwd %> && yarn
---
    "@nestjs/common":"*",

---
inject: true
to: package.json
after: devDependencies
skip_if: nestjs/swagger
sh: cd <%= cwd %> && yarn
---
    <% if(locals.healthcheck) {-%>"@nestjs/swagger":"*",<% } -%>

As far as I know, nothing comparable is possible currently. Can you help me out with this?

Kind regards!

@svallory
Copy link

That would require especial parsing and allowing frontmatter in the middle may be complicated.

How about adding a modify or update action that gets the existing file content and returns the updated content?

That could be achieved in at least two ways, by having a variable contents in the context and a normal ejs body, or by having the template export a function transform(contents: string, context: {}): string as a named or as the default export

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants