Leo Jiang
Leo Jiang

Reputation: 26253

Is there a way to share framework code across multiple projects?

I have a set of build tools, UI components, boilerplate code, etc that I want to share across projects. Within this framework, there's an src folder that's specific to each project. E.g.

Project 1:

scripts/
src/
package.json

Project 2:

scripts/ <- shared with project 1
src/ <- specific to project 2
package.json <- shared with project 1

These 2 projects would be in separate repos. I thought Git submodules would be the solution, but it seems like this would make the project a dependency of the framework. I want them to either be independent or have the framework be a dependency of the project.

I put the project inside the framework instead of the other way around because there are certain files that are usually in the root directory, e.g. Node's package.json.

Is there a way to make this easy to work with?

Upvotes: 1

Views: 607

Answers (1)

slebetman
slebetman

Reputation: 114034

The NPM way

The way I prefer to do this is to structure the framework as a module. Then in both project1 and project2 you can do:

npm install git://github.com/leojiang/framework.git

There are lots of advantages for doing it this way:

  1. People familiar with node.js already know how this works so you don't need to teach new developers how to install the framework.

  2. It makes maintaining the framework separate from maintaining projects. This not only lead to cleaner code but allows all projects to pull bug fixes by simply doing an npm install or npm update.

  3. It moves all your framework files into node_modules thus allowing you to use require() without specifying paths.

  4. It moves all your framework files into node_modules thus basically hiding them from projects allowing each project to focus on only project-specific stuff.

The Git way

Sometimes what you have isn't really a collection of modules like Express or Nest.js. Sometimes it is a specific way to structure projects. For example what collection of modules are used, where is the config file, where are the templates, etc. This is often called boilerplate (or project boilerplate to differentiate it from boilerplate code in a single file).

If you want to save a project structure the best way is to create an example project (a kind of simple Hello World) then push that project to a git repo. Then both project1 and project2 can fork from the boilerplate repo. If you are using something like github or gitlab you can use their built-in fork functionality. If you have your own remote repo like gitosys then you can just clone the repo or pull the repo into a new empty git project:

mkdir project1
git init
git pull path/to/boilerplate/repo

Boilerplate repositories are very popular in the hackathon community as it allows teams to quickly start a project for the 24 or 48 hours of coding they have.

The Ruby-on-Rails way

I call this the RoR way but maybe other frameworks did this first. RoR certainly popularized this method. In javascript-land you can see this method being used by Next.js and React.js.

The idea is simple. Write a script that creates the correct project structure. Then just execute that script. Let the script do all the work of creating folders and installing modules.

This is of course the most involved way of doing this and takes the most amount of work. However it is also the most flexible.

Upvotes: 3

Related Questions