npm package: @pol-studios/core-sync
Description: CLI for syncing core schemas, functions, and triggers across projects
Latest version: 1.0.0

# @pol-studios/core-sync

CLI for syncing core schemas, Edge Functions, and Trigger.dev resources across projects.

## Usage

Run from anywhere within the POL-One monorepo:

```bash
pnpm exec core-sync <command>
```

## Commands

### Status

Check sync status across all resource types:

```bash
# Default project
pnpm exec core-sync status

# Specific project
pnpm exec core-sync status --project temple

# All projects
pnpm exec core-sync status --all-projects
```

### Sync Schemas

Generate a migration file using migra diff:

```bash
# Preview changes (dry-run)
pnpm exec core-sync sync schemas --dry-run

# Preview with error details
pnpm exec core-sync sync schemas --dry-run --verbose

# Generate migration
pnpm exec core-sync sync schemas

# Custom migration name
pnpm exec core-sync sync schemas --name add_new_feature

# Specific project
pnpm exec core-sync sync schemas --project temple
```

**Requires:** Local Supabase running in the target project

### Sync Functions

Copy Edge Functions from core to project:

```bash
# Preview changes
pnpm exec core-sync sync functions --dry-run

# Sync all functions
pnpm exec core-sync sync functions

# Sync specific function
pnpm exec core-sync sync functions --function query

# Specific project
pnpm exec core-sync sync functions --project temple
```

### Sync Triggers

Copy Trigger.dev resources from core to project:

```bash
# Preview changes
pnpm exec core-sync sync triggers --dry-run

# Sync all
pnpm exec core-sync sync triggers

# Specific project
pnpm exec core-sync sync triggers --project temple
```

### Sync All

Run all syncs at once:

```bash
pnpm exec core-sync sync all --dry-run
pnpm exec core-sync sync all --project temple
pnpm exec core-sync sync all --all-projects
```

### Init

Generate a config file template:

```bash
pnpm exec core-sync init
pnpm exec core-sync init --force  # Overwrite existing
```

## Configuration

Create `core-sync.config.ts` at the repo root:

```typescript
import { defineConfig } from '@pol-studios/core-sync';

export default defineConfig({
  // Path to the core folder
  corePath: 'apps/core',

  // Optional: override default paths within core
  core: {
    schemas: 'schemas',      // default
    functions: 'functions',  // default
    triggers: 'trigger',     // default
  },

  // Projects to sync to
  projects: {
    self: {
      // Default project (POL-One itself)
      target: {
        migrations: 'supabase/migrations',
        functions: 'supabase/functions',
        triggers: 'apps/trigger',
      },
    },
    temple: {
      // External project
      path: '/path/to/TempleV2',
      target: {
        migrations: 'supabase/migrations',
        functions: 'supabase/functions',
        triggers: 'apps/trigger',
      },
      database: {
        port: 54332,  // Custom Supabase port
      },
    },
  },

  // Optional: schema sync options
  options: {
    schemas: ['core'],  // Schemas to diff
    filteredExtensions: [
      'pg_cron', 'pg_net', 'pg_graphql',
      'supabase_vault', 'pgmq', 'wrappers',
    ],
  },
});
```

## How It Works

### Schema Sync

1. Creates two temporary PostgreSQL databases
2. Applies existing migrations to one database
3. Applies core schemas to the other database
4. Runs `migra` to generate a diff
5. Saves the diff as a new migration file
6. Cleans up temporary databases

### Function/Trigger Sync

1. Calculates MD5 hashes of source and target directories
2. Detects new, modified, and deleted files
3. Copies changed files to target
4. Removes deleted files from target

## Lock File

A `.core-sync.lock` file tracks sync state:

```json
{
  "version": 1,
  "lastSync": "2026-03-02T15:30:00Z",
  "schemas": {
    "hash": "abc123",
    "syncedAt": "2026-03-02T15:30:00Z",
    "migrationFile": "20260302_core_schema_sync.sql"
  },
  "functions": {
    "query": { "hash": "def456", "syncedAt": "..." }
  },
  "triggers": {
    "cli": { "hash": "ghi789", "syncedAt": "..." }
  }
}
```

## Development

```bash
# Build
pnpm --filter @pol-studios/core-sync build

# Watch mode
pnpm --filter @pol-studios/core-sync dev

# Type check
pnpm --filter @pol-studios/core-sync typecheck
```