hygen icon indicating copy to clipboard operation
hygen copied to clipboard

[feature] Replace a line during inject

Open Whoaa512 opened this issue 6 years ago • 6 comments

Amazing piece of software you've got here. Has saved my team countless hours.

I'd like the ability to be able to run an arbitrary function on the matching line and return a modified version of that line.

Whoaa512 avatar Dec 04 '18 22:12 Whoaa512

Thanks! Interesting.. what can help is if you drop here a sample for a template with API you would expect, and then i'll do the hard work of implementing that. so just pencil in how you would see the YAML frontmatter for such an ability

jondot avatar Dec 06 '18 11:12 jondot

So I've thought about this a bit and I keep coming back to 2 API proposals.

Both share the common boolean attribute replace to trigger the replacement code.

The tricky part comes in specifying the replacement function. My initial thought is to specify a replacer attribute which contains the path to a js file with a single export or maybe a specifically named export. Alternatively we could try allowing the replacement function to be defined within the body of the template.

Defining the function in the template body is convenient for simple replacements, but we are then forced to eval the body which doesn't seem desirable.

Here's an example:

---
replace: true
to: src/apis/serverless.yml
at_line: 145
skip_if: "<%= h.inflection.classify(name) %>,"
eof_last: false
replacer: ./replacers/my-replacement-func.js
---
// Some body text here
// Or maybe a replacement function

What do you think @jondot?

Whoaa512 avatar Dec 11 '18 01:12 Whoaa512

Interesting. And this kind of replacer function - is this specific to this template (in this instance, the serverless template) in term of logic that's "attached" to this template or is it a generic kind of function that can be used by other templates as well?

jondot avatar Dec 11 '18 10:12 jondot

Not sure I guess it could be specific but it doesn't have to be. Why do you ask?

Also forgot to mention my proposed function signature

// @flow
type ReplacerFunc = (matchingLine: string, lineIndex: number, content: string) => string

Maybe the front-matter attributes could be in there too.

Whoaa512 avatar Dec 11 '18 19:12 Whoaa512

Hi, trying to pick up the pace again -- not sure under what scenario this is? is it for injecting into existing file? Maybe best if you can show an example repo so I can 'get into your head' in that sense

jondot avatar Jan 05 '19 11:01 jondot

for replacing feature not released yet?

d-string avatar Feb 03 '22 11:02 d-string