uetchy / create-create-app

⚡️ Create your own `create-something` app.
MIT License
96 stars 24 forks source link

Expose package manager variable in AfterHookOptions #35

Closed alexanderl19 closed 2 years ago

alexanderl19 commented 2 years ago

Quality of life feature for displaying commands in caveat text. Would support use cases such as https://github.com/vercel/next.js/blob/0cb2037a41c27d4ced57b5479e27edf96f748083/packages/create-next-app/create-app.ts#L308-L326.

packageManager variable in AfterHookOptions:

{
  // variables
  packageManager: string,
  ...
}

or with a helper function

{
  ...
  // helper functions
  npmCommand: (command: string) => string; // returns npm command text. uses package manager specified by --node-pm CLI param (default: npm)
}
uetchy commented 2 years ago

I love your idea. Would you open a pull request for this? Not directly related to this issue though, I'm also thinking about having an interactive prompt to allow users to choose which package manager to use (kind of busy to get around to it right now)

alexanderl19 commented 2 years ago

Yeah, I'd be more than happy to open a PR.

I'm not actually quite sure what's the most optimal way to implement this feature though. A few options that come to mind are:

  1. Use the --node-pm CLI param
  2. Potentially determine what package manager was used to run the command (Not sure if this is possible?)

If the second option is possible, the package manager prompt is probably pretty redundant. For example, I don't see why someone would run the cli with yarn and then want packages installed via npm.

uetchy commented 2 years ago

Thanks for the input! Combining your idea, the flow is going to look like this (right?):

  1. If --node-pm is specified, use that as a package manager for npmCommand
  2. Otherwise, try to guess the package manager that the user has invoked with and use it
  3. Otherwise, use npm
alexanderl19 commented 2 years ago

I did some digging and turns out VS Code also has a package manager detection feature.

Code 2022-05-18 at 20 21 15@2x

I believe they're using which-pm under the hood. https://github.com/microsoft/vscode/blob/ba2dd825803b5f95d5dcce927afc3a4882ed0f84/extensions/npm/package.json#L27

If you're okay with adding an additional dependency, which-pm-runs seems to be a good fit for what we're trying to accomplish.

Here's an updated list of how the package manager would be determined, in order from highest priority to lowest.

  1. --node-pm CLI param
  2. Package manager prompt (defaults to --node-pm, user defined default, or package manager that executed the process, in that order)
  3. If for some reason, which-pm-runs can't determine the package manager, default to npm

In terms of the package manager selection, I think these options would work:

uetchy commented 2 years ago

@alexanderl19 Nice finds! Just skimmed through which-pm-runs and realized that there were just a few lines of code, meaning nothing affects package size.

Note: create-something -> the another create- app generated by create-create-app

  1. Let users of create-something choose a template through either CLI flag (--template) or an interactive prompt. If there are no multiple templates available, just skip 1.
  2. If there's package.json in the root of the selected template directory, go to 3. Otherwise, do nothing.
  3. If --node-pm CLI flag is specified, just use that. Otherwise, detect running package manager and use it. If failed to detect, use npm.

We won't expose any config for Node.js package manager on create function because it's just redundant. Bear in mind that in the future I might reconsider the design based on further feedback.

alexanderl19 commented 2 years ago

I think we're on the same page now, so I'll work up a PR when I'm able to find some time. Thanks for all the input!

uetchy commented 2 years ago

Fixed in v7.3.0