-
Notifications
You must be signed in to change notification settings - Fork 267
Feature-Request: Allow inject to replace #106
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
Comments
I think writing code to change or replace lines in a file is going to be very specific to the task at hand. A generic solution is going to be kind of nightmarish to write. One way to approach it might be import foobarProps from './foobarProps'
const Foobar = () => {
/* lot's of code */
}; And then have your generator overwrite the foobarProps.js file. Another approach would be to use a |
I landed here, today, looking for the same kind of code-mod functionality, and wondered what the group might think about something like: replace:
from: "\n<%= componentName %>.propTypes = {\n"
to: "\n};\n" In essence, you define make a RegEx-y pair of matchers, and then inject the content between them. The replacement would only work if:
An alternative semantic might be to expand upon the The above would then become: after: "\n<%= componentName %>.propTypes = {\n"
before: "\n};\n" The supposition is that If there is no One limitation, here, is that these semantics don't allow you to replace/remove Perhaps both syntax additions would be nice, to give you more granular control over injection/replacement? If these suggestions sound reasonable, I would be happy to try and submit a PR for the features. |
I created a helper function that can remove lines from a file. While it's not an integrated option, I thought it might help others if I shared it. Template file: ---
to: filename.js
---
<%= h.removeLines("filename.js", 7, 20) %> .hygen.js file const fs = require('fs');
const removeLines = (data, to, from) => {
const lines = Array.from({length: ((from +1) - to)}, (_, i) => i + to - 1);
let d = data.split('\n')
for (let i = lines.length -1; i >= 0; i--) {
d.splice(lines[i],1);
}
return d.join('\n');
}
module.exports = {
helpers: {
removeLines: (fileName, t, f) => {
fs.readFile(fileName, 'utf8', (err, data) => {
if (err) throw err;
const content = removeLines(data, t, f);
fs.writeFile(fileName, content, err => {
if (err) throw err;
});
})
}
}
} |
I have a use-case in React Native recently for this. To configure an app from RN 0.68+ template to use the New Architecture, you have to change a few lines of code. For example, for the Android configuration we want to set I'd also love to use this for the ability to quickly change other configuration values in projects. |
From what I see, the proposal to just allow using
---
to: src/index.ts
inject: true
after: (?=\n<%= componentName %>.propTypes = {\n)
before: (?<=\n};\n)
---
…replacement… I'd like to pile on adding the This will also automatically allow deletion. |
I thought about something similar to #84, but I think it's a bit different.
In my case there's a part in my .js files, that should be regeneratered.
I have a simplified example:
And I want to be able to "update" the lines with meta and payload with a hygen generator.
Not really sure how we could detect the end of the propTypes part yet, but maybe let's discuss the replacing part first :-)
The text was updated successfully, but these errors were encountered: