How can I organize a project to share code?

Viewed 267

I'm writing some applications on top of a common core that I built, a sort of my-own-framework.

I would like to have this applications linked, so if I update a file in the common core, every application will be updated as well.

So I organized the code like this. I created a github project where in the main branch there is the common core files, and then every single branch is a different application. So if I update the common core, I can create a pull request from main to application branch and the common core will be updated.

Everything works fine like this, the only problem is when I have particular situations.

If I have to edit a common core file inside an application branch, because for example I need a different behaviour just for that application, I would like to have the opportunity to keep that single file of common core isolated and edited just inside the application branch, but the other files should be updated like before.

Instead, obviously in this situation I couldn't merge the pull request anymore, because the file inside the application branch is different from the one in the common core.

My projects are normally organized like this:

core/...
features/...
public/...
main.ts

core and public folders contain the common core files and normally these files are not changed. so I branch the common core repo, I add the features modules inside the folder and then I load all the modules (core and features) from the main.ts (it's a typescript web app). In public folder I have support files that should not change a lot. With this architecture, everything works fine, until I need to change a file in the public folder just for one application. If I change it, I wouldn't be able to merge the branch anymore and receive the updates from the common core files.

I tried before the approach with submodules, but I wasn't satisfied at all.

The goal is to be able to get continuous updates from the common core files, even though some of them are changed locally and should be "untracked" or something like that.

Do You have any idea on how to solve this problem or organize the code in a better way?

Thanks a lot

2 Answers

I think that your scenario would fit perfectly with a monorepo. The way you can do this is with workspaces. I've been using yarn workspaces for that (yarn1, yarn2), recently npm also introduced workspaces, but I haven't tested them recently. This is also possible using lerna.

The basic idea is that you can have multiple npm projects in the same repo, which you can directly link. It's similar to installing a dependency, with the only difference that this dependency is local. So you can have a structure like this:

  • apps/app1
  • apps/app2
  • libs/core
  • libs/features

Then inside of the apps' package.json file you can directly add a reference to the core/feature (or any other) libraries that you want to include.

The last versions of git has sparse-checkout feature.

You can look here for more details.

Related