jondot / hygen

The simple, fast, and scalable code generator that lives in your project.

Home Page:http://www.hygen.io

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Selecting workspaces in a monorepo

nareshbhatia opened this issue · comments

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?

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?

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

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/