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

Selecting workspaces in a monorepo #395

Closed
nareshbhatia opened this issue Apr 26, 2022 · 3 comments
Closed

Selecting workspaces in a monorepo #395

nareshbhatia opened this issue Apr 26, 2022 · 3 comments

Comments

@nareshbhatia
Copy link

I am writing a generator for a monorepo that uses npm Workspaces. The workspaces are defined in package.json like this:

"workspaces": [
  "apps/*",
  "packages/*"
],

The actual monorepo layout looks like this. Note that there is 1 app and 3 packages.

CleanShot 2022-04-26 at 01 22 15

What's the best way to prompt the user with a select which populates the choices dynamically with values ['movie-magic', 'config', 'tsconfig', 'ui-lib']?

The approach could be something like this:

  1. Read the workspaces list from package.json. In the above example, it will be ["apps/*", "packages/*"].
  2. Expand each entry to get a list of actual workspaces in the repo. This should result in ['movie-magic', 'config', 'tsconfig', 'ui-lib'].

Has anyone attempted something like this before? Any pointers?

@jamir0
Copy link

jamir0 commented May 27, 2022

Hi, i would like to use the tool for the same purposes:

  • ask user where to create new package by select specific folder
  • generate new component inside selected folder (each folder has different templates to generate package)

example:
I have such folder structure:

/packages/
  - libs
     - /lib1
     - /lib2
  - components
     - /component1
     - /component2

so I have workspaces

"workspaces": [
  "packages/libs/*",
  "packages/components/*"
],

at the root folder I would like to run command:
hyden component new component3

then ask user to select the workspace where they would like to create a new component and generate a folder for it based on the selected workspace.

Probably someone has a receipt or example for this use case?

@jamlen
Copy link

jamlen commented Dec 2, 2022

I'm also in a monorepo with workspaces, but I'm not actually parsing the paths from the package.json, although maybe I should be! But this is what I'm doing which works nicely

Note that the folder structure I'm working with is:

.
└── src
    ├── apps
    │   ├── customer
    │   ├── notification
    │   ├── order
    │   ├── payment
    │   └── product
    ├── clients
    │   └── frontend
    └── packages
       ├── payment-clients
       ├── test-data
       └── test-utils
const fs = require('fs');

const exclude = ['things-to-ignore', '.DS_Store'];
const dirs = fs.readdirSync('./src/apps');
const apps = dirs.reduce((acc, d) => {
  if (!exclude.includes(d)) acc.push({ name: d, value: d });
  return acc;
}, []);

module.exports = [
  {
    type: 'input',
    name: 'name',
    message: 'Package name',
  },
  {
    type: 'multiselect',
    name: 'apps',
    message: 'Which App to add to?',
    limit: 5,
    choices: apps,
  },
];

For your use case @nareshbhatia you could use a glob to get the directory list for both the apps and packages

@nareshbhatia
Copy link
Author

Thank you for your suggestion, @jamlen. Since I posted this issue, I wrote my own code generation framework that is built from ground up to be monorepo friendly. Would love to see your feedback: https://www.code-shaper.dev/

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

3 participants