14 |
15 |
16 |
17 |
18 | This is a monorepo boilerplate to quickly start nestjs projects, this start includes an api, client and shared libraries.
19 |
20 | - [Overview](#overview)
21 | - [What's inside](#whats-inside)
22 | - [Tweaking for your project](#tweaking-for-your-project)
23 | - [Referencing packages from other packages/apps](#referencing-packages-from-other-packagesapps)
24 | - [Installing](#installing)
25 | - [Development](#development)
26 | - [Package Management](#package-management)
27 | - [Installing a module from Yarn](#installing-a-module-from-yarn)
28 | - [Uninstalling a module from a package](#uninstalling-a-module-from-a-package)
29 | - [Package Versioning and TS Paths](#package-versioning-and-ts-paths)
30 | - [Docker image](#docker-image)
31 | - [Running the tests](#running-the-tests)
32 | - [Inspiration](#inspiration)
33 |
34 | ## Overview
35 |
36 | The repository is powered by [Lerna](https://github.com/lerna/lerna) and [Yarn](https://yarnpkg.com/en/).
37 | Lerna is responsible for bootstrapping, installing, symlinking all of the packages/apps together.
38 |
39 | ### What's inside
40 |
41 | This repo includes multiple packages and applications for a hypothetical project called `nest-starter`. Here's a rundown of the components:
42 |
43 | - `shared`: Shared utilities between server and client (TypeScript)
44 | - `core`: Shared utilities between servers
45 | - `client`: Angular app (depends on `shared`)
46 | - `api`: NestJS Api server (depends on `shared` + `core`)
47 | - `landing`: A static landing page
48 |
49 | ## Tweaking for your project
50 |
51 | You should run a search and replace on the word `nest-starter` and replace with your project name.
52 |
53 | ## TypeScript
54 |
55 | ### Referencing packages from other packages/apps
56 |
57 | Each package can be referenced within other packages/app files by importing from `@/` (kind of like an npm scoped package).
58 |
59 | ```tsx
60 | import * as React from 'react';
61 | import { Button } from '@nest-starter/shared';
62 |
63 | class App extends React.Component {
64 | render() {
65 | return (
66 |
67 |
68 |
Welcome to React
69 |
70 |
71 |
72 | To get started, edit src/App.tsx and save to reload.
73 |
74 |
75 | );
76 | }
77 | }
78 |
79 | export default App;
80 | ```
81 |
82 | **IMPORTANT: YOU DO NOT NEED TO CREATE/OWN THE NPM ORGANIZATION OF YOUR PROJECT NAME BECAUSE NOTHING IS EVER PUBLISHED TO NPM.**
83 |
84 | For more info, see the section on [package versioning](#package-versioning-and-ts-paths)
85 |
86 | ### Installing
87 |
88 | Install lerna globally.
89 |
90 | ```
91 | npm i -g lerna
92 | ```
93 |
94 | ```
95 | git clone git@github.com:scopsy/nestjs-monorepo-starter.git
96 | cd typescript-monorepo-starter
97 | rm -rf .git
98 | lerna bootstrap
99 | ```
100 |
101 | ### Development
102 |
103 | Lerna allows some pretty nifty development stuff. Here's a quick rundown of stuff you can do:
104 |
105 | - _`yarn start`_: Run's the `yarn start` command in every project and pipe output to the console. This will do the following:
106 | - api: Starts the api in dev mode on port 3000
107 | - client: Starts the app in dev mode on port 4200
108 | - shared: Starts TypeScript watch task
109 | - core: Starts TypeScript watch task
110 | - _`yarn build`_: Build all projects
111 | - _`lerna clean`_: Clean up all node_modules
112 | - _`lerna bootstrap`_: Rerun lerna's bootstrap command
113 |
114 | ### Package Management
115 |
116 | \*\*IF you run `yarn add ` or `npm install ` from inside a project folder, you will break your symlinks.\*\* To manage package modules, please refer to the following instructions:
117 |
118 | #### Installing a module from Yarn
119 |
120 | To add a new npm module to ALL packages, run
121 |
122 | ```bash
123 | lerna add
124 | ```
125 |
126 | To add a new npm module(s) to just one package
127 |
128 | ```bash
129 | lerna add --scope=
130 |
131 | # Examples (if your project name was `nest-starter`)
132 | lerna add classnames --scope=@nest-starter/api
133 | lerna add @types/classnames @types/jest --scope=@nest-starter/api --dev
134 | ```
135 |
136 | #### Uninstalling a module from a package
137 |
138 | Unfortunately, there is no `lerna remove` or `lerna uninstall` command. Instead, you should remove the target module from the relevant package or packages' `package.json` and then run `lerna bootstrap` again.
139 | Reference issue: https://github.com/lerna/lerna/issues/1229#issuecomment-359508643
140 |
141 | ### Package Versioning and TS Paths
142 |
143 | None of the packages in this setup are _ever_ published to NPM. Instead, each shared packages' (like `shared` and `core`) have build steps (which are run via `yarn prepare`) and get built locally and then symlinked. This symlinking solves some problems often faced with monorepos:
144 |
145 | - All projects/apps are always on the latest version of other packages in the monorepo
146 | - You don't actually need to version things (unless you actually want to publish to NPM)
147 | - You don't need to do special stuff in the CI or Cloud to work with private NPM packages
148 |
149 | This project works great with WebStorm when opened from the root, this means all imports, Go to should work correctly when navigating between packages.
150 | You are welcome.
151 |
152 | ### Docker image
153 |
154 | Build the docker image:
155 |
156 | ```shell
157 | docker build -t api -f Docker.api .
158 | ```
159 |
160 | Run the Docker Container:
161 |
162 | ```shell
163 | docker run -p 3000:3000 api
164 | ```
165 |
166 | ## Inspiration
167 | A LOT of this has been shameless taken from [palmerhq/monorepo-starter](https://github.com/palmerhq/monorepo-starter).
168 |
--------------------------------------------------------------------------------