Monorepo

From tinh2/skills-hub-registry · View interactive version

Monorepo is an AI agent skill from the GitHub repository tinh2/skills-hub-registry. Set up or migrate to a monorepo with Turborepo, Nx, or pnpm workspaces. Triggers: splitting a project into packages, merging multiple repos, adding workspace-aware builds.

How to use this skill

Skill file

You are in AUTONOMOUS MODE. Do NOT ask questions. Do NOT pause for confirmation.

Execute every phase below in sequence, making decisions based on what you find.

============================================================

PHASE 0 — INPUT

============================================================

$ARGUMENTS may contain:

If no arguments, detect existing setup and optimize it, or scaffold a new monorepo if none exists.

============================================================

PHASE 1 — DETECT CURRENT STATE

============================================================

Determine if the project is already a monorepo, a single package, or multi-repo:

Monorepo Indicators:

Single Package Indicators:

Detect Existing Structure:

Record: current state (monorepo/single/multi), tool (if any), packages found, language.

============================================================

PHASE 2 — SELECT MONOREPO TOOL

============================================================

If no tool is specified, select based on detected stack:

Turborepo (recommended for most Node.js/TypeScript projects):

Nx (recommended for large/enterprise projects):

pnpm workspaces (recommended for lightweight needs):

Cargo workspaces (for Rust):

Go workspaces (for Go):

============================================================

PHASE 3 — SCAFFOLD OR MIGRATE

============================================================

3.1 — If starting fresh (no existing monorepo):

Create the directory structure:

.
├── apps/
│   ├── web/          # Frontend application
│   └── api/          # Backend application
├── packages/
│   ├── shared/       # Shared types, utils, constants
│   ├── ui/           # Shared UI components (if frontend)
│   ├── config/       # Shared configs (eslint, tsconfig, tailwind)
│   └── db/           # Database client and migrations (if applicable)
├── turbo.json        # or nx.json
├── package.json      # Root workspace config
├── pnpm-workspace.yaml  # if using pnpm
└── tsconfig.json     # Root tsconfig with project references

Adjust based on --packages if provided.

3.2 — If migrating from single package (--migrate):

  1. Create apps/ and packages/ directories
  2. Move the existing app into apps/{name}/
  3. Extract shared code into packages/shared/:
  1. Update all import paths
  2. Create workspace config at root
  3. Update CI workflows to use workspace commands

3.3 — If migrating from multi-repo (--from=REPOS):

  1. For each repo in the --from list:
  1. Deduplicate shared dependencies → move to root package.json
  2. Extract common code into packages/shared/
  3. Update all cross-repo imports to workspace references
  4. Remove duplicated configs (eslint, prettier, tsconfig) → use shared configs from packages/config/

============================================================

PHASE 4 — CONFIGURE WORKSPACE

============================================================

4.1 — Package Manager Workspace Config:

For pnpm (create pnpm-workspace.yaml):

packages:
  - 'apps/*'
  - 'packages/*'

For npm/yarn (add to root package.json):

{
  "workspaces": ["apps/*", "packages/*"]
}

4.2 — Shared Package Setup:

For each package in packages/:

For apps referencing shared packages:

4.3 — Root TypeScript Config (if TypeScript):

Create root tsconfig.json:

{
  "compilerOptions": {
    "composite": true,
    "declaration": true,
    "declarationMap": true,
    "incremental": true
  },
  "references": [
    { "path": "apps/web" },
    { "path": "apps/api" },
    { "path": "packages/shared" }
  ]
}

============================================================

PHASE 5 — CONFIGURE BUILD PIPELINE

============================================================

5.1 — Turborepo Config (if selected):

Create turbo.json:

{
  "$schema": "https://turbo.build/schema.json",
  "globalDependencies": ["**/.env.*local"],
  "tasks": {
    "build": {
      "dependsOn": ["^build"],
      "outputs": ["dist/**", ".next/**", "build/**"]
    },
    "lint": {
      "dependsOn": ["^build"]
    },
    "typecheck": {
      "dependsOn": ["^build"]
    },
    "test": {
      "dependsOn": ["^build"]
    },
    "dev": {
      "cache": false,
      "persistent": true
    }
  }
}

Add scripts to root package.json:

{
  "scripts": {
    "build": "turbo run build",
    "dev": "turbo run dev",
    "lint": "turbo run lint",
    "test": "turbo run test",
    "typecheck": "turbo run typecheck"
  }
}

5.2 — Nx Config (if selected):

Create nx.json:

{
  "$schema": "https://raw.githubusercontent.com/nrwl/nx/master/packages/nx/schemas/nx-schema.json",
  "targetDefaults": {
    "build": {
      "dependsOn": ["^build"],
      "cache": true
    },
    "lint": { "cache": true },
    "test": { "cache": true }
  },
  "defaultBase": "main",
  "namedInputs": {
    "default": ["{projectRoot}/**/*", "sharedGlobals"],
    "sharedGlobals": ["{workspaceRoot}/tsconfig.base.json"],
    "production": ["default", "!{projectRoot}/**/*.spec.ts"]
  }
}

Create project.json in each package/app with targets.

5.3 — Caching:

Local caching is enabled by default for both Turborepo and Nx.

For remote caching (if --remote-cache):

============================================================

PHASE 6 — CONFIGURE CI

============================================================

Create or update .github/workflows/ci.yml for affected-only builds:

Turborepo CI:

name: CI
on:
  pull_request:
    branches: [main]
jobs:
  build:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
        with:
          fetch-depth: 2
      - uses: pnpm/action-setup@v4
      - uses: actions/setup-node@v4
        with:
          node-version: 20
          cache: pnpm
      - run: pnpm install --frozen-lockfile
      - run: pnpm turbo run lint typecheck test build --filter=...[HEAD~1]

Nx CI:

      - run: npx nx affected --target=lint --base=origin/main
      - run: npx nx affected --target=test --base=origin/main
      - run: npx nx affected --target=build --base=origin/main

============================================================

PHASE 7 — VERIFY SETUP

============================================================

  1. Install all dependencies from root: pnpm install (or npm/yarn equivalent)
  2. Run build: pnpm turbo run build (or npx nx run-many --target=build)
  3. Verify each package resolves workspace dependencies correctly
  4. Run lint across all packages
  5. Run tests across all packages
  6. Verify the task graph: pnpm turbo run build --dry or npx nx graph
  7. Check cache hits: run build twice and verify second run uses cache

Fix any issues found during verification.

============================================================

SELF-HEALING VALIDATION (max 2 iterations)

============================================================

After completing, validate the output was produced correctly:

  1. Verify generated files exist and are syntactically valid.
  2. Run any available validation (lint, type-check, dry-run).
  3. If the skill produces configuration, verify it parses without errors.

IF VALIDATION FAILS:

============================================================

OUTPUT

============================================================

Print a summary:

## Monorepo Setup Complete

### Tool: {Turborepo | Nx | pnpm workspaces}
### Package Manager: {pnpm | npm | yarn}

### Workspace Structure
- apps/web — {description}
- apps/api — {description}
- packages/shared — {description}
- packages/config — {description}

### Task Pipeline
- build: depends on ^build, cached, outputs: dist/**
- lint: cached
- test: cached
- dev: not cached, persistent

### Caching
- Local: enabled ({cache directory})
- Remote: {configured with Vercel/Nx Cloud | not configured}

### CI Configuration
- .github/workflows/ci.yml — affected-only builds on PRs

### Files Created/Modified
- {list of files}

============================================================

NEXT STEPS

============================================================

  1. Run pnpm dev to start all apps in development mode
  2. Add new packages: create directory in packages/, add package.json, run pnpm install
  3. Run /release --monorepo to set up versioning with changesets
  4. Run /linter to set up shared lint config in packages/config/
  5. Enable remote caching: run with --remote-cache flag

============================================================

SELF-EVOLUTION TELEMETRY

============================================================

After producing output, record execution metadata for the /evolve pipeline.

Check if a project memory directory exists:

Entry format:

### /monorepo — {{YYYY-MM-DD}}
- Outcome: {{SUCCESS | PARTIAL | FAILED}}
- Self-healed: {{yes — what was healed | no}}
- Iterations used: {{N}} / {{N max}}
- Bottleneck: {{phase that struggled or "none"}}
- Suggestion: {{one-line improvement idea for /evolve, or "none"}}

Only log if the memory directory exists. Skip silently if not found.

Keep entries concise — /evolve will parse these for skill improvement signals.

============================================================

DO NOT

============================================================

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.