plopjs / plop

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

node-plop and electron: navigator is not defined #424

Closed dave-cross closed 5 months ago

dave-cross commented 5 months ago

Hi,

I'm trying to create a UI that sits on top of Plop, but I've run into a snag. When I try to use node-plop (even just console.logging the import), it throughs errors.

This is in the main process of an electron app, created with electron-forge (Vite-typescript).

import nodePlop from 'node-plop';

// other code

async function getPrompts(directory, plopFileName){
  const plop = await nodePlop(`${directory}/${plopFileName}`)
  // the rest doesn't matter; this is all it takes to through errors
)

Error is ReferenceError: navigator is not defined. Shows up even when above function isn't being called. Seems electron is trying to precompile the code. Reviewing the build file shows that it's looking for navigator.userAgent, which seems to be part of Chalk's "supports-color" check, which is used by inquirer.

I've tried every flag or global variable I could find (ie. FORCE_COLOR=0 didn't work). I was able to test the above code with Node directly and it works, just something about electron and this navigator not defined issue.

My question is this: since I will display a UI for the prompts and send them directly to node-plop with runActions, is there a way to disable/bypass/circumvent this?

amwmedia commented 5 months ago

After a little looking, it seems like Electron does not allow access to navigator from within the main process. Have you tried?:

dave-cross commented 5 months ago

Interesting. I know they try to keep it locked down for security, but didn't realize it was that tight.

Tried mocking navigator when I first discovered the bug, but it wasn't working either. Maybe they don't allow attaching to a global object. More likely that I set up it up wrong.

I did try a child process this morning after my opening post. That works. Not the buttery smooth workflow I was hoping for, but that's not Plop's fault.

Closing as this solution works and the heart of the issue isn't with Plop. Thank you for the explanation.