Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Command Line Parameters causing bad option: --plopfile=plopfile.ts #456

Open
awhitford opened this issue Oct 15, 2024 · 2 comments
Open

Comments

@awhitford
Copy link

Summary

I am trying to pass command-line options to my plop script, but it instead provides an error message:

/usr/local/Cellar/node/22.9.0_1/bin/node: bad option: --plopfile=plopfile.ts
 ELIFECYCLE  Command failed with exit code 9.

I am using a TypeScript plopfile, inspired by #423 and documented here.

How to Reproduce

Create a package.json

{
  "name": "plop-cmd-bug",
  "type": "module",
  "scripts": {
    "plop": "cross-env NODE_OPTIONS='--import tsx' plop --plopfile=plopfile.ts"
  },
  "dependencies": {
    "plop": "^4.0.1",
    "typescript": "^5.6.3"
  },
  "devDependencies": {
    "cross-env": "^7.0.3",
    "tsx": "^4.19.1"
  },
  "packageManager": "[email protected]+sha512.e5a7e52a4183a02d5931057f7a0dbff9d5e9ce3161e33fa68ae392125b79282a8a8a470a51dfc8a0ed86221442eb2fb57019b0990ed24fab519bf0e1bc5ccfc4"
}

Create a plopfile.ts

import type { NodePlopAPI } from "plop";

export default function (plop: NodePlopAPI) {
  plop.setGenerator("sample", {
    description: "Sample Generator to show bug with command line arguments.",
    prompts: [
      {
        type: "input",
        name: "name",
        message: "Project Name:",
      },
      {
        type: "input",
        name: "title",
        message: "Project Title:",
      },
    ],
    actions: ["No operation necessary"],
  });
}

Install dependencies

pnpm install

Validate that Prompts Work Fine

Run the plop script and answer the prompts. It will look something like this:

$ pnpm plop sample

> plop-cmd-bug@ plop /Users/anthony/github/awhitford/plop-cmd-bug
> cross-env NODE_OPTIONS='--import tsx' plop --plopfile=plopfile.ts "sample"

? Project Name: my-name
? Project Title: My Title
ℹ No operation necessary

Use Command Line Parameters to Bypass Prompts 🐛

Run the plop script with answers to bypass the prompts. It will look something like this:

$ pnpm plop sample -- --name my-name --title "My Title"

> plop-cmd-bug@ plop /Users/anthony/github/awhitford/plop-cmd-bug
> cross-env NODE_OPTIONS='--import tsx' plop --plopfile=plopfile.ts "sample" "--" "--name" "my-name" "--title" "My Title"

/usr/local/Cellar/node/22.9.0_1/bin/node: bad option: --plopfile=plopfile.ts
 ELIFECYCLE  Command failed with exit code 9.

Interesting Note 🤔

If you only supply name and not title, then it works fine:

$ pnpm plop sample -- --name my-name

> plop-cmd-bug@ plop /Users/anthony/github/awhitford/plop-cmd-bug
> cross-env NODE_OPTIONS='--import tsx' plop --plopfile=plopfile.ts "sample" "--" "--name" "my-name"

? Project Title: My Title
ℹ No operation necessary

Also, if you remove the title from the prompts and simply have the one name prompt left, then that will work fine too:

$ pnpm plop sample -- --name my-name

> plop-cmd-bug@ plop /Users/anthony/github/awhitford/plop-cmd-bug
> cross-env NODE_OPTIONS='--import tsx' plop --plopfile=plopfile.ts "sample" "--" "--name" "my-name"

ℹ No operation necessary

Please help 🛟

@awhitford
Copy link
Author

This bug doesn't seem limited to TypeScript because I tried a basic JavaScript version too...

Create a plopfile.js like:

export default function (plop) {
  plop.setGenerator("sample", {
    description: "Sample Generator to show bug with command line arguments.",
    prompts: [
      {
        type: "input",
        name: "name",
        message: "Project Name:",
      },
      {
        type: "input",
        name: "title",
        message: "Project Title:",
      },
    ],
    actions: ["No operation necessary"],
  });
}

Next, tweak the script command in package.json to be plain:

  "scripts": {
    "plop": "plop",
  },

Then, try specifying both parameters:

$ pnpm plop plopfile  -- --name my-name --title "My Title"

> plop-cmd-bug@ plopjs /Users/anthony/github/awhitford/plop-cmd-bug
> plop "plopfile" "--" "--name" "my-name" "--title" "My Title"

⚠️ You will notice that there is no action output. 🐛

This is different than if you don't specify title, like:

$ pnpm plop plopfile  -- --name my-name

> plop-cmd-bug@ plop /Users/anthony/github/awhitford/plop-cmd-bug
> plop "plopfile" "--" "--name" "my-name"

? Project Title: My Title
ℹ No operation necessary

@awhitford
Copy link
Author

I traced this problem to the internals of Liftoff, which uses flagged-respawn.

I noticed that the process.argv from line 310 was inconsistent with the argv from line line 312. For example:

>>> Liftoff.prototype.execute: process.argv [
  '/usr/local/Cellar/node/22.9.0_1/bin/node',
  '/Users/anthony/github/awhitford/plop-cmd-bug/node_modules/plop/bin/plop.js',
  'sample',
  '--',
  '--title',
  'My Title',
  '--name',
  'My Nom'
]
>>> Liftoff.prototype.execute: argv [
  '/usr/local/Cellar/node/22.9.0_1/bin/node',
  '--title',
  '/Users/anthony/github/awhitford/plop-cmd-bug/node_modules/plop/bin/plop.js',
  'sample',
  '--',
  'My Title',
  '--name',
  'My Nom',
  '--no-respawning'
]

It appears that the flagged-respawn package is determining that --title should be a node parameter, so it is adjusting the argv. (Where is this documented?)

As a result, I need to change my prompt name to be something else like projectTitle (not title -- because then --title conflicts with node's --title parameter).

Now that I understand what is going on, I will work around this, but this strikes me as unexpected behavior. I wouldn't expect the parameters after -- to be interpreted as potential node parameters. If this can't be fixed, then this nuance should be documented.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant