Async Operations in PlopJS

473 views Asked by At

I want to use async/await in the actions property however, it gives me [ERROR] Handler has no actions. I tried the .then() and still failed.

This is what I've tried so far. What I'm trying to do is to do a reactive prompt (which I don't know) by doing it in the actions and then use await but failed so I moved to .then syntax which also failed.

const fs = require('fs') 
const inquirer = require('inquirer')
const chalk = require('chalk')

const directories = (dir) => fs.readdirSync(dir, {
  withFileTypes: true
}).reduce((a, c) => {
  c.isDirectory() && a.push(c.name)
  return a
}, [])

const folders = directories('./src/views')

module.exports = function (plop) {
  plop.setGenerator('Handler', {
    description: 'Application translations generator',
    prompts: [
      {
        type: 'input',
        name: 'filename',
        message: 'Filename (ex. file name):'
      },
      {
        type: 'list',
        name: 'folder',
        message: 'Select folder location: ',
        choices: folders
      }
    ],
    actions: (data) => {
      const subfolders = directories(`./src/views/${data.folder}`)
      const actions = []

      inquirer.prompt({
        type: 'list',
        name: 'subfolder',
        message: 'Select subfolder location: ',
        choices: subfolders
      }).then(x => {
        actions.push({
          type: 'add',
          path: './asdfasdf.vue',
          templateFile: './generators/templates/vue.hbs',
          skipIfExists: true
        })

        return actions
      })
    }
  })
}

0

There are 0 answers