|
| 1 | +#!/usr/bin/env bash |
| 2 | +# |
| 3 | +# Regenerates the pnpm patch for @y/y (yjs) from a local build. |
| 4 | +# |
| 5 | +# Usage: |
| 6 | +# ./scripts/patch-yjs.sh [path-to-yjs] |
| 7 | +# |
| 8 | +# Defaults to ../yjs relative to this repo root. |
| 9 | + |
| 10 | +set -euo pipefail |
| 11 | + |
| 12 | +# Version that is actually installed in this repo (pnpm patches the installed |
| 13 | +# version). The local ../yjs checkout may be a newer rc; we still pin to this. |
| 14 | +YJS_PKG="@y/y" |
| 15 | +YJS_VERSION="14.0.0-rc.16" |
| 16 | + |
| 17 | +# pnpm keeps the scope path for the temp patch dir (e.g. .pnpm_patches/@y/y@VER) |
| 18 | +# but escapes "/" to "__" for the committed patch file name. |
| 19 | +YJS_PATCH_DIR_NAME="$YJS_PKG@$YJS_VERSION" |
| 20 | +YJS_PATCH_FILE_NAME="@y__y@$YJS_VERSION.patch" |
| 21 | + |
| 22 | +SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)" |
| 23 | +BLOCKNOTE_ROOT="$(cd "$SCRIPT_DIR/.." && pwd)" |
| 24 | +LOCAL_YJS="${1:-$(cd "$BLOCKNOTE_ROOT/../yjs" && pwd)}" |
| 25 | + |
| 26 | +if [[ ! -d "$LOCAL_YJS/src" ]]; then |
| 27 | + echo "ERROR: Cannot find yjs at $LOCAL_YJS" |
| 28 | + echo "Pass the path as an argument: $0 /path/to/yjs" |
| 29 | + exit 1 |
| 30 | +fi |
| 31 | + |
| 32 | +echo "==> Using local yjs at: $LOCAL_YJS" |
| 33 | +echo "==> BlockNote root: $BLOCKNOTE_ROOT" |
| 34 | + |
| 35 | +# 0. Build yjs so dist/ is up to date |
| 36 | +echo "==> Building yjs (npm run dist) ..." |
| 37 | +(cd "$LOCAL_YJS" && npm run dist) |
| 38 | + |
| 39 | +PATCH_DIR="$BLOCKNOTE_ROOT/node_modules/.pnpm_patches/$YJS_PATCH_DIR_NAME" |
| 40 | + |
| 41 | +# 1. Clean up any leftover patch dir, then start fresh |
| 42 | +if [[ -d "$PATCH_DIR" ]]; then |
| 43 | + echo "==> Cleaning up old patch dir ..." |
| 44 | + rm -rf "$PATCH_DIR" |
| 45 | +fi |
| 46 | + |
| 47 | +echo "==> Running pnpm patch $YJS_PKG@$YJS_VERSION ..." |
| 48 | +cd "$BLOCKNOTE_ROOT" |
| 49 | +pnpm patch "$YJS_PKG@$YJS_VERSION" |
| 50 | + |
| 51 | +echo "==> Patch temp dir: $PATCH_DIR" |
| 52 | + |
| 53 | +# 2. Replace src/ with local build |
| 54 | +echo "==> Replacing src/ ..." |
| 55 | +rm -rf "$PATCH_DIR/src" |
| 56 | +cp -R "$LOCAL_YJS/src" "$PATCH_DIR/src" |
| 57 | + |
| 58 | +# 3. Replace dist/ with local build (.d.ts files) |
| 59 | +echo "==> Replacing dist/ ..." |
| 60 | +rm -rf "$PATCH_DIR/dist" |
| 61 | +cp -R "$LOCAL_YJS/dist" "$PATCH_DIR/dist" |
| 62 | + |
| 63 | +# 4. Replace tests/ (testHelper is part of the published exports) |
| 64 | +if [[ -d "$LOCAL_YJS/tests" ]]; then |
| 65 | + echo "==> Replacing tests/ ..." |
| 66 | + rm -rf "$PATCH_DIR/tests" |
| 67 | + cp -R "$LOCAL_YJS/tests" "$PATCH_DIR/tests" |
| 68 | +fi |
| 69 | + |
| 70 | +# 5. Copy top-level type decls referenced by the package (e.g. global.d.ts) |
| 71 | +if [[ -f "$LOCAL_YJS/global.d.ts" ]]; then |
| 72 | + echo "==> Copying global.d.ts ..." |
| 73 | + cp "$LOCAL_YJS/global.d.ts" "$PATCH_DIR/global.d.ts" |
| 74 | +fi |
| 75 | + |
| 76 | +# 6. Update package.json in the patch dir |
| 77 | +echo "==> Updating package.json ..." |
| 78 | +node -e " |
| 79 | +const fs = require('fs'); |
| 80 | +const orig = JSON.parse(fs.readFileSync('$PATCH_DIR/package.json', 'utf8')); |
| 81 | +const local = JSON.parse(fs.readFileSync('$LOCAL_YJS/package.json', 'utf8')); |
| 82 | +
|
| 83 | +// Keep the original (installed) version so pnpm doesn't try to fetch a |
| 84 | +// different version from the registry. |
| 85 | +orig.version = '$YJS_VERSION'; |
| 86 | +
|
| 87 | +// Update exports (this package is exports-based, no main/module) |
| 88 | +if (local.exports) orig.exports = local.exports; |
| 89 | +
|
| 90 | +// Update files list |
| 91 | +if (local.files) orig.files = local.files; |
| 92 | +
|
| 93 | +// Update type/sideEffects if present |
| 94 | +if (local.type) orig.type = local.type; |
| 95 | +if ('sideEffects' in local) orig.sideEffects = local.sideEffects; |
| 96 | +
|
| 97 | +// Update bin if present |
| 98 | +if (local.bin) orig.bin = local.bin; |
| 99 | +
|
| 100 | +fs.writeFileSync('$PATCH_DIR/package.json', JSON.stringify(orig, null, 2) + '\n'); |
| 101 | +console.log(' package.json updated'); |
| 102 | +" |
| 103 | + |
| 104 | +# 7. Commit the patch |
| 105 | +echo "" |
| 106 | +echo "==> Running pnpm patch-commit ..." |
| 107 | +pnpm patch-commit "$PATCH_DIR" |
| 108 | + |
| 109 | +echo "" |
| 110 | +echo "==> Done! Patch regenerated at patches/$YJS_PATCH_FILE_NAME" |
0 commit comments