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

Modify action for json files #33

Closed
adrianmroz opened this issue Jul 26, 2016 · 8 comments
Closed

Modify action for json files #33

adrianmroz opened this issue Jul 26, 2016 · 8 comments

Comments

@adrianmroz
Copy link

My proposition is to add a modify action for JSON files, where you will provide callback instead of pattern and template. In callback you would get JSON.parsed object and you should also return POJsO which will be JSON.stringifyd and written to file.

@wbobeirne
Copy link

wbobeirne commented Sep 9, 2016

I went ahead and made a custom action for this, but I definitely think this could get rolled in as a flag on modify action types or a new action type altogether. Here's the code for alterJsonAction.js: https://gist.github.com/wbobeirne/63e1b8c3a16ffce6dac5c7d81afd5cec?ts=2

Example usage from my use case is:

const alterJsonAction = require("alterJsonAction");

actions: [alterJsonAction({
    path: "../webpack/entries.json",
    alter: (json, data) => {
        const name = snakeCase(data.name);
        json[data.platform][name] = `pages/${name}`;
        return json;
    },
})],

I also threw in an indentation arg there that takes an int or "\t", if 2 spaces ain't your game.

@amwmedia
Copy link
Member

sorry for the late response, I agree that this would be pretty useful in some cases. I'll have to give some more thought to the best implementation for this feature. Thanks!

@amwmedia
Copy link
Member

amwmedia commented Jun 1, 2017

hey, with plop 1.8.0 you can now create custom action types that you can share with other projects :-)

@mritzco
Copy link

mritzco commented Mar 8, 2018

I created a custom action type to add data to my config.json file.
Tried to follow the plop style to keep it customizable.

https://gist.github.com/mritzco/88306b56c0184e66e33374d6b6ed2751

@amwmedia
Copy link
Member

amwmedia commented Mar 8, 2018

nice! You could publish this out as an NPM module that others can use via plop.load(). If you want to do that and need help, let me know.

@mritzco
Copy link

mritzco commented Mar 15, 2018

I'll happily do! Just publish to npm or any specific requirements?

@amwmedia
Copy link
Member

Sorry for the delay on this response. You can look at this repo as a guide to how this is done. I'm going to try to get more instructional content together soon.

@mritzco
Copy link

mritzco commented May 22, 2018

@amwmedia
Ok, that took a bit longer than expected:

Could you please take a look at this repo and let me know if that would do.

Original method gets the config on the method call, while the load method passes the config on the require function. It's working with the testing file but I'm not sure it will work properly in all cases.

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

4 participants