plopjs / plop

Consistency Made Simple
http://plopjs.com
MIT License
7.06k stars 277 forks source link

Action type that trigger shell command #417

Open michal-bella opened 7 months ago

michal-bella commented 7 months ago

Hello, can you add action type that trigger shell command? For example I want to achive during generating my code I want to trigger npm install "some-package". It would be nice to have this type of action.

{ type: 'command', command: 'npm install @some-package' // or any other command }

JulSeb42 commented 2 months ago

Hey, have you found a solution for this?

fabiobiondi commented 2 months ago

I use child_process:


const { exec } = require('child_process');

// ...

plop.setActionType('runCommand', function (answers, config, plop) {
    return new Promise((resolve, reject) => {
      exec(config.command, (error, stdout, stderr) => {
        if (error) {
          console.error(`Errore: ${error.message}`);
          reject(error);
        }
        if (stderr) {
          console.error(`Stderr: ${stderr}`);
          reject(new Error(stderr));
        }
        console.log(`Risultato: ${stdout}`);
        resolve(stdout);
      });
    });
  });

  actions: [
    // ...
     {
        type: 'runCommand',
        command: 'npm run [COMMAND]',
      }
  ]