hygen icon indicating copy to clipboard operation
hygen copied to clipboard

Add async helpers

Open jondot opened this issue 6 years ago • 5 comments

Context: https://github.com/jondot/hygen/issues/95

jondot avatar Feb 06 '19 08:02 jondot

It looks like ejs should have async support if you add async: true to the render options. What that looks like I'm not quite clear.

In any case, I'm already finding times when I would like to be able to utilized helpers that get data from sources that may be asynchronous in nature, so this would be a great feature to have.

csdswarm avatar Jan 24 '20 18:01 csdswarm

Okay, in case anyone else is looking for this, I found an excellent workaround that already exists in hygen depending on what you need to do.

Create a generator and add an index.js to it.

expose a params method as an async function and wait for the results.

looks something like this:

const { promisify } = require('util');
const exec = promisify(require('child_process').exec);

module.exports = {
  params: async () => {
    const { stdout } = await exec('git rev-parse --abbrev-ref HEAD');
    const branch = (`${stdout}`).trim().toUpperCase();

    return {  branch };
  }
};

Then just use <%= branch %> in your template.

csdswarm avatar Jan 24 '20 20:01 csdswarm

@csdswarm thank you. it worked. I couldn't get this and interactive prompting both work in same generator. Have you tried it? if so did u get it to work?

rameshjanjyam avatar May 26 '21 21:05 rameshjanjyam

@rameshjanjyam TBH, I've moved on to a different scaffolding tool. I'm currently using plop mostly because that uses handlebars syntax, which is already a syntax being used in the project I'm on. Even then, I've had a hard time getting around to building scaffolds in it.

EDIT: You may want to see if spawn works better than exec. spawn doesn't generate a separate shell to work from. spawn does require a bit of different syntax.

csdswarm avatar May 27 '21 11:05 csdswarm

Thank you @csdswarm

rameshjanjyam avatar May 27 '21 17:05 rameshjanjyam