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

Loaded plopfiles do not respect the --force flag #392

Open
moltar opened this issue Sep 4, 2023 · 6 comments · May be fixed by #394
Open

Loaded plopfiles do not respect the --force flag #392

moltar opened this issue Sep 4, 2023 · 6 comments · May be fixed by #394

Comments

@moltar
Copy link

moltar commented Sep 4, 2023

I know it worked on previous versions, and I suspect it might be my local setup, but want to raise an issue to see if there are any other leads, as I've spent over an hour trying to debug this, and so far no leads.

Running

## for TS support
export NODE_OPTIONS="--loader ts-node/esm --no-warnings"

plop --force --plopfile=.plop/plopfile.ts        

Output:

? What is your name? asd
✖  ++ File already exists
 -> /../project/.plop/foo-bar

.plop/plopfile.ts contents:

import type { NodePlopAPI } from 'plop';

module.exports = function (plop: NodePlopAPI) {
  plop.setGenerator('test', {
      description: 'This is loaded.',
      prompts: [{
        name: 'name',
        message: 'What is your name?',
        type: 'input',
      }],
      actions: [
        {
          type: 'add',
          template: 'foo {{name}}',
          path: 'foo-bar',
        }
      ]
  });
};

Version

❯ pnpm ls plop

devDependencies:
plop 3.1.2

Btw, if I use force: true on the action, then it works fine.

@moltar
Copy link
Author

moltar commented Sep 4, 2023

Alright, as soon as I opened an issue, had an idea... (of course!) 💡 😁


I was actually using a loaded plop file from the main file, and that is what triggered this bug!

So it looks like force is not being applied to loaded plopfiles.

plopfile.ts

import type { NodePlopAPI } from 'plop';

module.exports = async function (plop: NodePlopAPI) {
  await plop.load('./name.ts')
};

name.ts

import type { NodePlopAPI } from 'plop';

module.exports = function (plop: NodePlopAPI) {
  plop.setGenerator('test', {
      description: 'This is loaded.',
      prompts: [{
        name: 'name',
        message: 'What is your name?',
        type: 'input',
      }],
      actions: [
        {
          type: 'add',
          template: 'foo {{name}}',
          path: 'foo-bar',
        }
      ]
  });
};

@moltar moltar changed the title Somehow cannot get --force to work. Loaded plopfiles do not respect the --force flag Sep 4, 2023
@crutchcorn
Copy link
Member

That clarification is extremely helpful. Lemme see if I can take a stab at finding the root cause

@crutchcorn
Copy link
Member

I think this is because we don't inherit any of the parent config props from our load method:

https://github.com/plopjs/plop/blob/main/packages/node-plop/src/node-plop.js#L102-L107

I could be wrong, but I think if we simply changed the const config lines to:

    const config = Object.assign(
      plopCfg,
      {
        destBasePath: getDestBasePath(),
      },
      loadCfg
    );

It would fix this bug.

Any chance you wanna pull it down, test my theory, maybe write a test to validate this fix, and make a PR? 😊

@moltar
Copy link
Author

moltar commented Sep 4, 2023

Your fix does indeed work! :)

@moltar
Copy link
Author

moltar commented Sep 4, 2023

FYI I am working on a PR.

moltar added a commit to moltar/plop that referenced this issue Sep 4, 2023
@moltar moltar linked a pull request Sep 4, 2023 that will close this issue
@crutchcorn
Copy link
Member

BTW @moltar I deeply appreciate you having opened a PR. I'm working on TS support given our botched 4.0 release right now, but I will address your fix soon there after :)

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

Successfully merging a pull request may close this issue.

2 participants