|
|
|
|
@ -195,6 +195,13 @@ router.post('/apps/:app/activate', async c => {
|
|
|
|
|
rmSync(dirPath, { recursive: true, force: true })
|
|
|
|
|
console.log(`Cleaned up old version: ${dir}`)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Remove sync checkpoint - new deployment is now source of truth
|
|
|
|
|
const checkpointPath = join(appDir, '.sync-checkpoint')
|
|
|
|
|
if (existsSync(checkpointPath)) {
|
|
|
|
|
rmSync(checkpointPath, { recursive: true, force: true })
|
|
|
|
|
console.log(`Removed sync checkpoint after successful deployment`)
|
|
|
|
|
}
|
|
|
|
|
} catch (e) {
|
|
|
|
|
// Log but don't fail activation if cleanup fails
|
|
|
|
|
console.error(`Failed to clean up old versions: ${e}`)
|
|
|
|
|
@ -216,6 +223,65 @@ router.post('/apps/:app/activate', async c => {
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
router.post('/apps/:app/sync/rollback', async c => {
|
|
|
|
|
const appName = c.req.param('app')
|
|
|
|
|
if (!appName) return c.json({ error: 'App name required' }, 400)
|
|
|
|
|
|
|
|
|
|
const appDir = join(APPS_DIR, appName)
|
|
|
|
|
const checkpointPath = join(appDir, '.sync-checkpoint')
|
|
|
|
|
const currentLink = join(appDir, 'current')
|
|
|
|
|
|
|
|
|
|
if (!existsSync(checkpointPath)) {
|
|
|
|
|
return c.json({ error: 'No sync checkpoint found' }, 404)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
// Get current version name for cleanup
|
|
|
|
|
const currentReal = existsSync(currentLink) ? realpathSync(currentLink) : null
|
|
|
|
|
const currentVersion = currentReal ? currentReal.split('/').pop() : null
|
|
|
|
|
|
|
|
|
|
// Generate timestamp for rollback version
|
|
|
|
|
const now = new Date()
|
|
|
|
|
const pad = (n: number) => String(n).padStart(2, '0')
|
|
|
|
|
const timestamp = `${now.getFullYear()}${pad(now.getMonth() + 1)}${pad(now.getDate())}-${pad(now.getHours())}${pad(now.getMinutes())}${pad(now.getSeconds())}`
|
|
|
|
|
|
|
|
|
|
// Copy checkpoint to new timestamped version
|
|
|
|
|
const newVersion = join(appDir, timestamp)
|
|
|
|
|
cpSync(checkpointPath, newVersion, { recursive: true })
|
|
|
|
|
|
|
|
|
|
// Atomic symlink update
|
|
|
|
|
const tempLink = join(appDir, '.current.tmp')
|
|
|
|
|
if (existsSync(tempLink)) {
|
|
|
|
|
unlinkSync(tempLink)
|
|
|
|
|
}
|
|
|
|
|
symlinkSync(timestamp, tempLink, 'dir')
|
|
|
|
|
renameSync(tempLink, currentLink)
|
|
|
|
|
|
|
|
|
|
// Clean up the broken version if it's a timestamp dir (not named 'current')
|
|
|
|
|
if (currentVersion && /^\d{8}-\d{6}$/.test(currentVersion)) {
|
|
|
|
|
const brokenVersion = join(appDir, currentVersion)
|
|
|
|
|
if (existsSync(brokenVersion)) {
|
|
|
|
|
rmSync(brokenVersion, { recursive: true, force: true })
|
|
|
|
|
console.log(`Removed broken version: ${currentVersion}`)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Restart app with rolled-back version
|
|
|
|
|
const app = allApps().find(a => a.name === appName)
|
|
|
|
|
if (app?.state === 'running') {
|
|
|
|
|
try {
|
|
|
|
|
await restartApp(appName)
|
|
|
|
|
} catch (e) {
|
|
|
|
|
return c.json({ error: `Rolled back but failed to restart: ${e instanceof Error ? e.message : String(e)}` }, 500)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return c.json({ ok: true, version: timestamp })
|
|
|
|
|
} catch (e) {
|
|
|
|
|
return c.json({ error: `Failed to rollback: ${e instanceof Error ? e.message : String(e)}` }, 500)
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
router.sse('/apps/:app/watch', (send, c) => {
|
|
|
|
|
const appName = c.req.param('app')
|
|
|
|
|
|
|
|
|
|
@ -224,17 +290,32 @@ router.sse('/apps/:app/watch', (send, c) => {
|
|
|
|
|
const safeAppPath = safePath(APPS_DIR, appName)
|
|
|
|
|
if (!safeAppPath || !existsSync(appPath)) return
|
|
|
|
|
|
|
|
|
|
// Resolve to canonical path for consistent watch events
|
|
|
|
|
const canonicalPath = realpathSync(appPath)
|
|
|
|
|
const appDir = join(APPS_DIR, appName)
|
|
|
|
|
const checkpointPath = join(appDir, '.sync-checkpoint')
|
|
|
|
|
const currentReal = realpathSync(appPath)
|
|
|
|
|
|
|
|
|
|
const gitignore = loadGitignore(canonicalPath)
|
|
|
|
|
// Create checkpoint snapshot for rollback
|
|
|
|
|
try {
|
|
|
|
|
// Remove old checkpoint if exists
|
|
|
|
|
if (existsSync(checkpointPath)) {
|
|
|
|
|
rmSync(checkpointPath, { recursive: true, force: true })
|
|
|
|
|
}
|
|
|
|
|
// Copy current version to checkpoint
|
|
|
|
|
cpSync(currentReal, checkpointPath, { recursive: true })
|
|
|
|
|
console.log(`Created sync checkpoint for ${appName}`)
|
|
|
|
|
} catch (e) {
|
|
|
|
|
console.error(`Failed to create sync checkpoint: ${e}`)
|
|
|
|
|
// Continue anyway - checkpoint is optional safety feature
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const gitignore = loadGitignore(currentReal)
|
|
|
|
|
let debounceTimer: Timer | null = null
|
|
|
|
|
const pendingChanges = new Map<string, 'change' | 'delete'>()
|
|
|
|
|
|
|
|
|
|
const watcher = watch(canonicalPath, { recursive: true }, (_event, filename) => {
|
|
|
|
|
const watcher = watch(appPath, { recursive: true }, (_event, filename) => {
|
|
|
|
|
if (!filename || gitignore.shouldExclude(filename)) return
|
|
|
|
|
|
|
|
|
|
const fullPath = join(canonicalPath, filename)
|
|
|
|
|
const fullPath = join(appPath, filename)
|
|
|
|
|
const type = existsSync(fullPath) ? 'change' : 'delete'
|
|
|
|
|
pendingChanges.set(filename, type)
|
|
|
|
|
|
|
|
|
|
@ -244,7 +325,7 @@ router.sse('/apps/:app/watch', (send, c) => {
|
|
|
|
|
const evt: FileChangeEvent = { type: changeType, path }
|
|
|
|
|
if (changeType === 'change') {
|
|
|
|
|
try {
|
|
|
|
|
const content = readFileSync(join(canonicalPath, path))
|
|
|
|
|
const content = readFileSync(join(appPath, path))
|
|
|
|
|
evt.hash = computeHash(content)
|
|
|
|
|
} catch {
|
|
|
|
|
continue // File was deleted between check and read
|
|
|
|
|
|