jondot / hygen

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

Use node code in hygen #444

Closed Raiper34 closed 6 months ago

Raiper34 commented 6 months ago

Hi, is possible to use node code in hygen? I am new to hygen and have not found answers. I use my helper script for generating asset map, but I want to replace it with hygen. My script first get structure of given folder and based that folder i generate asset map. Can I run node code in hygen to get folder structure like this?

const assets = fs.readdirSync(BASE_ASSETS_FOLDER).filter(folder => !folder.includes('.'));

Thanks!

Raiper34 commented 6 months ago

For future, you can use prompt.js with params like

const fs = require('fs');
const BASE_ASSETS_FOLDER = './raw';
module.exports =
  {
    params: ({args}) => {
      // use fs (or similar) to read data
      // process data
      // add to args

      // anything you add to args available in templates
      args.assets = fs.readdirSync(BASE_ASSETS_FOLDER).filter(folder => !folder.includes('.'))
      return args
    }
  }

and then you can use assets variable in template :)