azu / monorepo-utils

A collection of utilities for monorepo/lerna. Tools for TypeScript project references etc..
164 stars 10 forks source link

Allow root tsconfig.json paths generation #43

Closed kevinwolfcr closed 3 years ago

kevinwolfcr commented 3 years ago

Thanks for making this tool! I think some users may want to also add the references to the root tsconfig.json file. In that way, they can run tsc --build from the root directory to build all referenced packages.

azu commented 3 years ago

Yes, I've seen this use-case in the following articles.

However, I have never used this approach, we need to investigate suitable commands.

kevinwolfcr commented 3 years ago

Yeah, in my case, I like having a root tsconfig.json so if I run tsc --build, it just builds all my packages.


I think what we can do is just add a --generateRoot argument that when specified, gathers all dependencies with a tsconfig.json and adds it to the root tsconfig.json, so it ends up being something like this:

{
    "extends": "./tsconfig.base.json",
    "files": [],
    "references": [{ "path": "packages/a" }, { "path": "packages/b" }]
}
azu commented 3 years ago

OK. Probably, it is good that the user can pass the target file path.

$ workspaces-to-typescript-project-references --generateRoot <filepath> 📝 <filepath> is <root>/tsconfig.json by default.

Which is suitable name, --generateRoot or --updateRoot?

However, I think we have a plan. Would you be willing to submit a PR that makes these changes?

kevinwolfcr commented 3 years ago

Sure, I can take a look at it today. Also, I think --includeRoot makes more sense when used with --check.

mxro commented 3 years ago

👍 for this feature!

I've created a little hacky script as a temporary workaround (works only with Yarn 2 workspaces) if that's of use to anyone: updateReferences.js

azu commented 3 years ago

I've implemented --includesRoot and released it as v2.7.0.

Can you try it?

mxro commented 3 years ago

Thank you @azu - tested and works very well!

Quick question, would the utility support workspaces that are nested more than one level deep?

azu commented 3 years ago

nested more than one level deep

Is it means next structure?

- root ← workspace root (workspace of workspaces)
    - project-a ← workspace root
        - packages
            - a
            - b
    - project-b ← workspace root
        - packages
            - c
            - d

Currently, this tools does not nest workspace.

This tool aim to have same behavior of npm/yarn workspaces mechanism. https://github.com/azu/monorepo-utils/blob/345ea4b6d5d4105b4bb84a02d8e752e18e15456e/packages/%40monorepo-utils/package-utils/src/get-packages.ts#L43-L78

mxro commented 3 years ago

Yes, correct, structure like you propose.

Yarn does support multiple levels of workspaces:

Finally, a worktree is the name given to workspaces that list their own child workspaces. A project contains one or more worktrees, which may themselves contain any number of workspaces. Any project contains at least one workspace: the root one.

https://yarnpkg.com/features/workspaces

I have been using this and it works very well!

That's how I get all the packages/workspaces (including the nested ones) from Yarn (and only works in Yarn 2+):

const cmdRes = execSync('yarn workspaces list --json').toString();

https://github.com/goldstack/goldstack/blob/master/workspaces/templates-lib/packages/utils-typescript-references/src/updatePackageProjectReferences.ts#L7

But would be very happy to deprecate that in favour of this library!