hygen icon indicating copy to clipboard operation
hygen copied to clipboard

Manually set variables inside prompt

Open worldspawn opened this issue 5 years ago • 0 comments

Like what @rhberro was requesting here -> https://github.com/jondot/hygen/issues/80

In my case I'm trying to write some templates that use prompts to then query some info from a sql database. I save the results of those prompts and then on subsequent runs i want to check i have the value and only prompt if i dont. However there doesn't seem to be a way to pass a value to the template other than to use the prompter. Adding values to args does nothing and if I use params the prompts are not executed.

The example does allude to something here:

module.exports = {
  prompt: ({ prompter, args }) => {
    if (args.age > 18) {
      return Promise.resolve({ allow: true })
    }
    return prompter.prompt({
      type: 'input',
      name: 'age',
      message: 'whats your age?'
    })
  }
}

Where it return {allow:true} which (i'm assuming) is a variable that can be read by the template. However this doesn't seem to be adaptable to what I need.

Some code for context:

let configPath;
        let name = __dirname;
        let nextName = null;
        let config = null;

        while (true) {            
            configPath = path.resolve(name, '.syngen.config.json');
            if (fs.existsSync(configPath)) {
                config = require(configPath);
                break;
            }
            nextName = path.resolve(name, '..');
            if (nextName === name) {
                break;
            }
            name = nextName;
        }

        if (!config) {
            config = {};
        }

        let prompts = [];

        if (args.server) {
            config.server = args.server;
        }
        else if (!config.server) {            
            prompts.push({
                type: 'input',
                name: 'server',
                message: 'what is the sql server address?'
            });
        }
        if (args.username) {
            config.username = args.username
        }
        else if (!config.username) {
            prompts.push({
                type: 'input',
                name: 'username',
                message: 'what is the sql username?'
            });            
        }
        if (args.password) {
            config.password = args.password
        }
        else if (!config.password) {
            prompts.push({
                type: 'input',
                name: 'password',
                message: 'what is the sql password?'
            });            
        }
        if (args.database) {
            config.database = args.database;
        }
        else if (!config.database) {
            prompts.push({
                type: 'input',
                name: 'database',
                message: 'what is the name of the database?'
            });
        }

        return prompts.reduce((promiseChain, currentTask) => {
            return promiseChain.then(chainResults =>
                prompter.prompt(currentTask).then(currentResult =>
                    [ ...chainResults, currentResult ]
                )
            );
        }, Promise.resolve([])).then(async arrayOfResults => {
            arrayOfResults.forEach(r => {
                Object.keys(r).forEach(k => {
                    config[k]= r[k];
                });
            })

            if (args.refresh || !config.tables) {
                config.tables = {};

                // what happens here isn't important to my issue

                pool.close();                

                //console.log(config.tables);
            }            

worldspawn avatar Mar 23 '19 09:03 worldspawn