jondot / hygen

The simple, fast, and scalable code generator that lives in your project.
http://www.hygen.io
MIT License
5.69k stars 256 forks source link

Run result of generator through jsonrepair [add a post process command for text ops] #298

Open fcisio opened 3 years ago

fcisio commented 3 years ago

Hey! I'm generating some package.json and the end result is pretty dirty.

I'm able to run prettier as a command with the sh option of the front-matter. I would also like to run jsonrepair, which would allow not to write logic just for the commas in json.

jsonrepair doesn't have a CLI and I can't seem to find an alternative.

So I'm thinking it would be nice to be able to do something like:

const jsonrepair = require('jsonrepair')

jsonrepair(generatorOutput)

This would also allow to clean up pretty much anything with prettier.


As an alternative, I tried creating an helper, but there is no way to preserve ejs syntax, passing it in a function.

Possibly related to #296

airtonix commented 3 years ago

looks like your request got solved:

4_finally_repair.ejs.t

---
sh: jsonrepair package.json > package.json
---
fcisio commented 3 years ago

@airtonix Yes! I created a feature request which was deved pretty quickly 👍

stropho commented 3 years ago

a workaround for me was to create my own node executable. Something like

#!/usr/bin/env node

const foo = require('my-favourite-lib')
// get something from `process.argv`

// do any operations you like

and then in the hygen template just

---
sh: path/to/my/executable.js
---

and if you are struggling with having the correct path to the executable, you can setup a hygen helper to always use the absolute path :crossed_fingers:

jondot commented 2 years ago

I think it is reasonable to add a postprocess stage which takes a command, and the end of generation is passed to this custom command, for each text-manipulating operation Scheduling for v7