Files
upstream-tldraw/skills/tldraw-migrate/detect-versions.mjs
upstream-mirror a774ac673a
Some checks failed
Deploy analytics worker / Deploy analytics worker to ${{ (github.ref == 'refs/heads/production' && 'production') || (github.ref == 'refs/heads/main' && 'staging') || 'preview' }} (push) Failing after 1s
i18n upload strings / Upload latest strings (push) Failing after 1s
Deploy bemo / Deploy bemo to ${{ (github.ref == 'refs/heads/bemo-production' && 'production') || (github.ref == 'refs/heads/main' && 'staging') || 'preview' }} (push) Failing after 1s
Deploy grafana resources / ${{ github.event_name == 'push' && 'Deploy grafana resources' || 'Validate grafana resources (dry run)' }} (push) Failing after 0s
End to end tests (dotcom) / Check for relevant changes (push) Failing after 0s
End to end tests (examples) / Check for relevant changes (push) Failing after 0s
End to end tests (mermaid readability) / Check for relevant changes (push) Failing after 1s
End to end tests (perf) / Check for relevant changes (push) Failing after 0s
Publish editor extensions / Publish editor extensions (push) Failing after 1s
Publish SDK packages / Remove the publish-packages label (push) Has been skipped
Checks / Tests & checks (push) Has been cancelled
Checks / Build all projects (push) Has been cancelled
Deploy templates / Deploy templates (push) Has been cancelled
End to end tests (dotcom) / End to end tests (dotcom) (push) Has been cancelled
End to end tests (mermaid readability) / End to end tests (mermaid readability) (push) Has been cancelled
End to end tests (perf) / End to end tests (perf) (push) Has been cancelled
Deploy .com / Deploy dotcom to ${{ (github.ref == 'refs/heads/production' && 'production') || (github.ref == 'refs/heads/main' && 'staging') || 'preview' }} (push) Has been cancelled
End to end tests (examples) / End to end tests (examples) (push) Has been cancelled
Publish SDK packages / Publish SDK packages (push) Has been cancelled
Publish SDK packages / Tell user released version (push) Has been cancelled
Publish SDK packages / Publishes code templates to separate repositories (push) Has been cancelled
Publish SDK packages / Deploys the template demos with the new SDK version (push) Has been cancelled
snapshot of https://github.com/tldraw/tldraw.git (main, 2026-09-23; blobs > 131072B omitted)
2026-09-23 16:12:52 +05:00

108 lines
3.0 KiB
JavaScript

#!/usr/bin/env node
/**
* Detects the previous tldraw version and writes it to stdout.
*
* Resolution order:
* 1. An explicit version passed as argv[2] (e.g., "/tldraw-migrate 4.0.0").
* 2. The version of any tldraw package on the main/master branch — but only
* if it differs from the working tree (otherwise the user is migrating
* directly on main and main already reflects the new version).
* 3. The version in HEAD~1.
* 4. The version in the current working tree (last resort: assumes the user
* hasn't bumped yet, so the working tree IS the previous version).
*
* Outputs just the version string (no trailing newline) so the SKILL.md can
* interpolate it into other commands.
*/
import { execSync } from 'child_process'
import { readFileSync } from 'fs'
function exec(cmd) {
try {
return execSync(cmd, { encoding: 'utf8', stdio: ['pipe', 'pipe', 'pipe'] }).trim()
} catch {
return null
}
}
const TLDRAW_PACKAGES = [
'tldraw',
'@tldraw/editor',
'@tldraw/tldraw',
'@tldraw/store',
'@tldraw/tlschema',
'@tldraw/state',
'@tldraw/state-react',
'@tldraw/sync',
'@tldraw/sync-core',
'@tldraw/utils',
'@tldraw/validate',
]
function extractTldrawVersion(packageJsonStr) {
try {
const pkg = JSON.parse(packageJsonStr)
const deps = { ...(pkg.dependencies || {}), ...(pkg.devDependencies || {}) }
for (const name of TLDRAW_PACKAGES) {
const raw = deps[name]
if (!raw) continue
const cleaned = String(raw).replace(/[\^~>=<\s]/g, '')
// Preserve pre-release suffixes (e.g., 4.7.0-canary.abc) so the
// release-notes filter can compare them correctly against stable
// versions.
const match = cleaned.match(/(\d+\.\d+\.\d+(?:-[0-9A-Za-z.-]+)?)/)
if (match) return match[1]
}
return null
} catch {
return null
}
}
function readWorkingTreeVersion() {
try {
return extractTldrawVersion(readFileSync('package.json', 'utf8'))
} catch {
return null
}
}
function emit(version) {
process.stdout.write(version)
process.exit(0)
}
const explicit = process.argv[2]?.trim()
if (explicit) {
const cleaned = explicit.replace(/^v/, '')
const match = cleaned.match(/^(\d+\.\d+\.\d+(?:-[0-9A-Za-z.-]+)?)/)
if (match) emit(match[1])
}
const workingTreeVersion = readWorkingTreeVersion()
for (const branch of ['main', 'master']) {
const content = exec(`git show ${branch}:package.json`)
if (!content) continue
const ver = extractTldrawVersion(content)
if (!ver) continue
// If the branch version matches the working tree, the user likely bumped
// directly on this branch — main isn't a useful "before" reference.
if (workingTreeVersion && ver === workingTreeVersion) continue
emit(ver)
}
const parent = exec('git show HEAD~1:package.json')
if (parent) {
const ver = extractTldrawVersion(parent)
if (ver && ver !== workingTreeVersion) emit(ver)
}
if (workingTreeVersion) emit(workingTreeVersion)
console.error(
'Could not detect previous tldraw version. Pass it as an argument: /tldraw-migrate 4.0.0'
)
process.exit(1)