Skip to content

Commit

Permalink
docs(repo): add monorepo documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
waldronmatt committed Oct 13, 2023
1 parent cccd6df commit f998335
Show file tree
Hide file tree
Showing 7 changed files with 153 additions and 83 deletions.
94 changes: 11 additions & 83 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ Install hooks:
pnpm prepare
```

## Getting Started
## Setup

Follow the directions [here](https://nx.dev/nx-cloud/intro/what-is-nx-cloud) to set up an nx account and access token.

Expand All @@ -32,7 +32,9 @@ Set up your `NPM_TOKEN` and `NX_CLOUD_ACCESS_TOKEN` via `Settings` -> `Secrets`

Enable read and write workflow permissions in your repo via `Settings` -> `Actions` -> `General` -> `Workflow permissions` -> `read and write permissions`.

## Commands
## Getting Started

**Note**: Append `--skip-nx-cache` at the end of cached commands to disable nx cloud caching

Run local development servers:

Expand All @@ -42,8 +44,6 @@ pnpm dev

Lint files:

**Note**: Since `vite-project` depends on linked packages, a linting error will occur if `pnpm build` has not run at least once first.

```bash
pnpm lint
```
Expand All @@ -60,19 +60,21 @@ Run tests in watch mode:
pnpm test:watch
```

Clean up bundle artifacts:
Compile files and build bundle:

**Note**: It is recommended to run `pnpm clean` before `pnpm build`

```bash
pnpm clean
pnpm build
```

Compile files and build bundle:
Clean up bundle artifacts:

```bash
pnpm build
pnpm clean
```

Starts local servers that serves the `build` outputs from their respective from `dist/` folders:
Starts local servers that serves the `build` outputs from their respective output folders:

**Note**: Run `pnpm build` first.

Expand All @@ -86,80 +88,6 @@ Stub `dist` for project linking without needing to watch and rebuild:
pnpm stub
```

### Workspace Commands

Install dependecies to the root of the workspace:

```bash
pnpm add -w [package-name]
```

Add a local package as a dependency for another local package:

```bash
pnpm add -D eslint-config-custom --filter vite-project --workspace
```

Remove a local package from another local package:

```bash
pnpm remove eslint-config-custom --filter vite-project
```

Install / run commands for a specific sub-package:

```bash
pnpm --filter vite-project dev
```

### Other Commands

Commit changes using conventional changelog:

```bash
pnpm commit
```

Delete workspace root `node_modules` and `pnpm-lock.yaml` files:

**Note**: Install `rimraf` globally and make sure it is not installed in the workspace root so errors aren't thrown.

```bash
pnpm delete
```

Lint root and `configs/` `js/cjs` files, check for secrets, lint dependency versions, validate published packages, and verify monorepo best practices:

```bash
pnpm lint:mr
```

Visualize the project structure/dependencies:

```bash
pnpm nx:graph
```

Format all files in the monorepo:

```bash
pnpm format
```

Preview the output result of the `version` command without actually executing it:

**Note**: Set your personal access token as an environment variable in your operating system as `GH_TOKEN` with the token unique identifier as the value.

```bash
pnpm preview:version
```

Preview the output result of the `publish` command without actually executing it:

```bash
pnpm preview:publish
```

## NX Distributed Caching

To enable nx distributed caching, refactor the `.github/workflows/release.yml` file via the directions [here](https://nx.dev/recipes/ci/monorepo-ci-github-actions#distributed-ci-with-nx-cloud).
Expand Down
36 changes: 36 additions & 0 deletions docs/repo/ARCHITECTURE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# Architecture

## Introduction

The repository setup and folder structure is heavily inspired by the turborepo [kitchen sink starter monorepo templates](https://github.com/vercel/turbo/tree/main/examples/kitchen-sink) and the [design system monorepo template](https://github.com/vercel/turbo/tree/main/examples/design-system).

## Folder Structure

```bash
apps/
configs/
docs/
packages/
...
package.json
pnpm-workspace.yaml
```

**`apps`** - Contains standalone applications / SPAs that can consume modules in `packages`

**`configs`** - Contains linters / configurations such as `eslint` / `tsconfig` shared across the monorepo

**`docs`** - Contains documentation for ui components and other documentation

**`packages`** - Contains utilities and tools published to `npm` that are used across the monorepo

## Turborepo Differences

- Tooling and configurations such as `eslint` and `tsconfig` are included in their own `config` folder. This is a personal preference to separate from the `packages` folder where reusable packages and utilities shared across projects are stored.
- Configurations such as `eslint` and `tsconfig` in the `config` folder do not reference other shareable configurations, for example, my own personal `eslint` config I use across projects. This is a personal preference so that configuration complexity and inheritence is kept to a minimum. This keeps configuration code more readable and self contained to the monorepo.
- Package naming for public packages published to `npm` have the `@` alias scoping while private packages do not have an alias. This is a personal preference to maintain the distinction between published and non-published packages more easily.
- The `apps` and `docs` folders remain separate and both are top-level folders. This is a personal preference to make the distinction between SPA and UI component documentation code / other documentation. I opt for more top-level folders.

## Turborepo Similarities

- Under `docs` is where `storybook` component code is stored, however it is common to house this along with the UI component for simplicity. If kept together, you would need to update the project to ignore `storybook` files when building and publishing the UI components to `npm`.
47 changes: 47 additions & 0 deletions docs/repo/COMMANDS.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
# Additional Commands

Commit changes using conventional changelog:

```bash
pnpm commit
```

Delete workspace root `node_modules` and `pnpm-lock.yaml` files:

**Note**: Install `rimraf` globally and make sure it is not installed in the workspace root so errors aren't thrown.

```bash
pnpm delete
```

Lint root and `configs/` `js/cjs` files, check for secrets, lint dependency versions, validate published packages, and verify monorepo best practices:

```bash
pnpm lint:mr
```

Visualize the project structure/dependencies:

```bash
pnpm nx:graph
```

Format all files in the monorepo:

```bash
pnpm format
```

Preview the output result of the `version` command without actually executing it:

**Note**: Set your personal access token as an environment variable in your operating system as `GH_TOKEN` with the token unique identifier as the value.

```bash
pnpm preview:version
```

Preview the output result of the `publish` command without actually executing it:

```bash
pnpm preview:publish
```
7 changes: 7 additions & 0 deletions docs/repo/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
Copyright 2023 Matthew Waldron

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
7 changes: 7 additions & 0 deletions docs/repo/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# Monorepo Documentation

[Architecture](ARCHTIECTURE.md)

[Additional Commands](COMMANDS.md)

[Workspace Commands](WORKSPACE.md)
39 changes: 39 additions & 0 deletions docs/repo/WORKSPACE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# Workspace Commands

pnpm workspace commands

## Add Packages

Install dependecies to the root of the workspace:

```bash
pnpm add -w [package-name]
```

Add a package as a dependency for another local package:

```bash
pnpm add react --filter vite-project --workspace
```

Add a **local** package as a development dependency for another local package:

```bash
pnpm add -D eslint-config-custom --filter vite-project --workspace
```

## Remove Packages

Remove a local package from another local package:

```bash
pnpm remove eslint-config-custom --filter vite-project
```

## Run Commands

Install / run commands for a specific sub-package:

```bash
pnpm dev --filter vite-project
```
6 changes: 6 additions & 0 deletions docs/repo/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"name": "monorepo-docs",
"version": "1.0.0",
"private": true,
"license": "MIT"
}

0 comments on commit f998335

Please sign in to comment.