hygen
hygen copied to clipboard
[feature] Replace a line during inject
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.
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
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?
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?
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.
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
for replacing feature not released yet?