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

Add root-level package.json and set up workspaces. #37

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

datu925
Copy link
Owner

@datu925 datu925 commented Mar 29, 2024

We can go further if we need to, but this gives us the whole-project capabilities that I was looking for. It uses npm workspaces which are documented here: https://docs.npmjs.com/cli/v7/using-npm/workspaces. I also found this blog post helpful.

In order for this to work, I added a package.json at the project root, and then I had to delete all package-lock.json files from the rest of the project. But you are allowed to keep using your own package.json for better modularity. In general, install dependencies in your own package.json files and not the root one. The only dependency I included at root was ts-node to make it easy to run scripts from root since the other commands require you to be in root as well.

To compile all sub-projects: npm exec --ws -- tsc
To run tests (or other scripts) for all sub-projects: npm run test --workspaces --if-present

@cdcyang I made some changes to your tsconfig file to write the compiled JS to a build/ directory – not sure how you were compiling/running things before, but this is what RA does and it keeps things tidy. I also removed the dummy test script since it was causing errors (as intended, but it was starting to get annoying).

@datu925 datu925 requested a review from cdcyang March 29, 2024 05:44
Copy link

vercel bot commented Mar 29, 2024

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
electric-machines ✅ Ready (Inspect) Visit Preview 💬 Add feedback Apr 1, 2024 4:16am
electric-machines-h6x1 ✅ Ready (Inspect) Visit Preview 💬 Add feedback Apr 1, 2024 4:16am

@datu925 datu925 requested review from rickycheah and removed request for cdcyang and rickycheah March 29, 2024 05:44
.gitignore Outdated Show resolved Hide resolved
@cdcyang
Copy link
Collaborator

cdcyang commented Mar 29, 2024

We can go further if we need to, but this gives us the whole-project capabilities that I was looking for. It uses npm workspaces which are documented here: https://docs.npmjs.com/cli/v7/using-npm/workspaces. I also found this blog post helpful.

In order for this to work, I added a package.json at the project root, and then I had to delete all package-lock.json files from the rest of the project. But you are allowed to keep using your own package.json for better modularity. In general, install dependencies in your own package.json files and not the root one. The only dependency I included at root was ts-node to make it easy to run scripts from root since the other commands require you to be in root as well.

To compile all sub-projects: npm exec --ws -- tsc To run tests (or other scripts) for all sub-projects: npm run test --workspaces --if-present

@cdcyang I made some changes to your tsconfig file to write the compiled JS to a build/ directory – not sure how you were compiling/running things before, but this is what RA does and it keeps things tidy. I also removed the dummy test script since it was causing errors (as intended, but it was starting to get annoying).

As vercel only permits serverless functions on the backend (https://vercel.com/templates/other/fastify-serverless-function), I don't build the backend project locally, but instead deploy to a dev instance of vercel using the vercel dev command to verify that the project works as expected.

I have checked that the backend still function the same as before and I am happy for this change to merge as long as the front-end deployment is fixed :)

@datu925
Copy link
Owner Author

datu925 commented Mar 29, 2024

We can go further if we need to, but this gives us the whole-project capabilities that I was looking for. It uses npm workspaces which are documented here: https://docs.npmjs.com/cli/v7/using-npm/workspaces. I also found this blog post helpful.
In order for this to work, I added a package.json at the project root, and then I had to delete all package-lock.json files from the rest of the project. But you are allowed to keep using your own package.json for better modularity. In general, install dependencies in your own package.json files and not the root one. The only dependency I included at root was ts-node to make it easy to run scripts from root since the other commands require you to be in root as well.
To compile all sub-projects: npm exec --ws -- tsc To run tests (or other scripts) for all sub-projects: npm run test --workspaces --if-present
@cdcyang I made some changes to your tsconfig file to write the compiled JS to a build/ directory – not sure how you were compiling/running things before, but this is what RA does and it keeps things tidy. I also removed the dummy test script since it was causing errors (as intended, but it was starting to get annoying).

As vercel only permits serverless functions on the backend (https://vercel.com/templates/other/fastify-serverless-function), I don't build the backend project locally, but instead deploy to a dev instance of vercel using the vercel dev command to verify that the project works as expected.

I have checked that the backend still function the same as before and I am happy for this change to merge as long as the front-end deployment is fixed :)

I think we ran into this. https://stackoverflow.com/questions/70117752/unable-to-deploy-a-next-js-monorepo-using-workspaces-to-vercel. I'll try it with yarn and request review again when it's done.

@datu925
Copy link
Owner Author

datu925 commented Apr 1, 2024

@cdcyang @rickycheah this change is ready for review now.

You can run commands from root for your backend like this:
yarn workspace backend <name of script>

To do things across all workspaces, you run yarn workspaces foreach -A (the -A flag is for --all):
yarn workspaces foreach -A run tsc
yarn workspaces foreach -A run test

This change includes switching over to Yarn 2 (aka Yarn modern) from npm. Migration should be relatively straightforward with https://yarnpkg.com/migration/guide. It's just the first few steps. But I want to make sure this is okay before springing it on you.

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 this pull request may close these issues.

2 participants