|
| 1 | +#!/usr/bin/env node |
| 2 | +/** |
| 3 | + * CLI tool to manage locked workspaces |
| 4 | + * |
| 5 | + * Usage: |
| 6 | + * npx ts-node src/cli/lock-workspace.ts list - List all workspaces and their lock status |
| 7 | + * npx ts-node src/cli/lock-workspace.ts lock <id|name> - Lock a workspace by ID or name |
| 8 | + * npx ts-node src/cli/lock-workspace.ts unlock <id|name> - Unlock a workspace by ID or name |
| 9 | + * npx ts-node src/cli/lock-workspace.ts show - Show current locked workspaces config |
| 10 | + */ |
| 11 | + |
| 12 | +import { existsSync, readFileSync, writeFileSync } from 'fs'; |
| 13 | +import { join } from 'path'; |
| 14 | +import initSqlJs from 'sql.js'; |
| 15 | + |
| 16 | +const CONFIG_FILE = join(process.cwd(), 'locked-workspaces.json'); |
| 17 | +const DATA_DIR = join(process.cwd(), 'data'); |
| 18 | +const MASTER_DB_PATH = join(DATA_DIR, '_master.db'); |
| 19 | + |
| 20 | +interface LockedWorkspacesConfig { |
| 21 | + lockedWorkspaceIds?: string[]; |
| 22 | + lockedWorkspaceNames?: string[]; |
| 23 | +} |
| 24 | + |
| 25 | +interface Workspace { |
| 26 | + id: string; |
| 27 | + name: string; |
| 28 | +} |
| 29 | + |
| 30 | +function loadConfig(): LockedWorkspacesConfig { |
| 31 | + if (!existsSync(CONFIG_FILE)) { |
| 32 | + return { lockedWorkspaceIds: [], lockedWorkspaceNames: [] }; |
| 33 | + } |
| 34 | + try { |
| 35 | + const content = readFileSync(CONFIG_FILE, 'utf-8'); |
| 36 | + const parsed = JSON.parse(content) as LockedWorkspacesConfig; |
| 37 | + return { |
| 38 | + lockedWorkspaceIds: parsed.lockedWorkspaceIds || [], |
| 39 | + lockedWorkspaceNames: parsed.lockedWorkspaceNames || [] |
| 40 | + }; |
| 41 | + } catch { |
| 42 | + return { lockedWorkspaceIds: [], lockedWorkspaceNames: [] }; |
| 43 | + } |
| 44 | +} |
| 45 | + |
| 46 | +function saveConfig(config: LockedWorkspacesConfig): void { |
| 47 | + writeFileSync(CONFIG_FILE, JSON.stringify(config, null, 2) + '\n'); |
| 48 | +} |
| 49 | + |
| 50 | +async function getWorkspaces(): Promise<Workspace[]> { |
| 51 | + if (!existsSync(MASTER_DB_PATH)) { |
| 52 | + console.error('Error: No database found. Run the server first to create workspaces.'); |
| 53 | + process.exit(1); |
| 54 | + } |
| 55 | + |
| 56 | + const SQL = await initSqlJs(); |
| 57 | + const buffer = readFileSync(MASTER_DB_PATH); |
| 58 | + const db = new SQL.Database(buffer); |
| 59 | + |
| 60 | + const stmt = db.prepare('SELECT id, name FROM workspaces ORDER BY name ASC'); |
| 61 | + const workspaces: Workspace[] = []; |
| 62 | + while (stmt.step()) { |
| 63 | + const row = stmt.getAsObject() as { id: string; name: string }; |
| 64 | + workspaces.push({ id: row.id, name: row.name }); |
| 65 | + } |
| 66 | + stmt.free(); |
| 67 | + db.close(); |
| 68 | + |
| 69 | + return workspaces; |
| 70 | +} |
| 71 | + |
| 72 | +function isLocked(config: LockedWorkspacesConfig, workspace: Workspace): boolean { |
| 73 | + return ( |
| 74 | + (config.lockedWorkspaceIds?.includes(workspace.id) || false) || |
| 75 | + (config.lockedWorkspaceNames?.includes(workspace.name) || false) |
| 76 | + ); |
| 77 | +} |
| 78 | + |
| 79 | +async function listWorkspaces(): Promise<void> { |
| 80 | + const workspaces = await getWorkspaces(); |
| 81 | + const config = loadConfig(); |
| 82 | + |
| 83 | + if (workspaces.length === 0) { |
| 84 | + console.log('No workspaces found.'); |
| 85 | + return; |
| 86 | + } |
| 87 | + |
| 88 | + console.log('\nWorkspaces:\n'); |
| 89 | + console.log(' Status ID Name'); |
| 90 | + console.log(' ------ -- ----'); |
| 91 | + |
| 92 | + for (const ws of workspaces) { |
| 93 | + const locked = isLocked(config, ws); |
| 94 | + const status = locked ? '🔒 LOCKED' : ' open '; |
| 95 | + console.log(` ${status} ${ws.id} ${ws.name}`); |
| 96 | + } |
| 97 | + console.log(''); |
| 98 | +} |
| 99 | + |
| 100 | +async function lockWorkspace(identifier: string): Promise<void> { |
| 101 | + const workspaces = await getWorkspaces(); |
| 102 | + const config = loadConfig(); |
| 103 | + |
| 104 | + // Find workspace by ID or name |
| 105 | + const workspace = workspaces.find(ws => ws.id === identifier || ws.name === identifier); |
| 106 | + |
| 107 | + if (!workspace) { |
| 108 | + console.error(`Error: Workspace not found: "${identifier}"`); |
| 109 | + console.log('\nAvailable workspaces:'); |
| 110 | + for (const ws of workspaces) { |
| 111 | + console.log(` - ${ws.name} (${ws.id})`); |
| 112 | + } |
| 113 | + process.exit(1); |
| 114 | + } |
| 115 | + |
| 116 | + if (isLocked(config, workspace)) { |
| 117 | + console.log(`Workspace "${workspace.name}" is already locked.`); |
| 118 | + return; |
| 119 | + } |
| 120 | + |
| 121 | + // Add to locked IDs (prefer ID over name for stability) |
| 122 | + if (!config.lockedWorkspaceIds) { |
| 123 | + config.lockedWorkspaceIds = []; |
| 124 | + } |
| 125 | + config.lockedWorkspaceIds.push(workspace.id); |
| 126 | + |
| 127 | + saveConfig(config); |
| 128 | + console.log(`✓ Locked workspace: "${workspace.name}" (${workspace.id})`); |
| 129 | +} |
| 130 | + |
| 131 | +async function unlockWorkspace(identifier: string): Promise<void> { |
| 132 | + const workspaces = await getWorkspaces(); |
| 133 | + const config = loadConfig(); |
| 134 | + |
| 135 | + // Find workspace by ID or name |
| 136 | + const workspace = workspaces.find(ws => ws.id === identifier || ws.name === identifier); |
| 137 | + |
| 138 | + if (!workspace) { |
| 139 | + console.error(`Error: Workspace not found: "${identifier}"`); |
| 140 | + process.exit(1); |
| 141 | + } |
| 142 | + |
| 143 | + if (!isLocked(config, workspace)) { |
| 144 | + console.log(`Workspace "${workspace.name}" is not locked.`); |
| 145 | + return; |
| 146 | + } |
| 147 | + |
| 148 | + // Remove from both ID and name lists |
| 149 | + if (config.lockedWorkspaceIds) { |
| 150 | + config.lockedWorkspaceIds = config.lockedWorkspaceIds.filter(id => id !== workspace.id); |
| 151 | + } |
| 152 | + if (config.lockedWorkspaceNames) { |
| 153 | + config.lockedWorkspaceNames = config.lockedWorkspaceNames.filter(name => name !== workspace.name); |
| 154 | + } |
| 155 | + |
| 156 | + saveConfig(config); |
| 157 | + console.log(`✓ Unlocked workspace: "${workspace.name}" (${workspace.id})`); |
| 158 | +} |
| 159 | + |
| 160 | +function showConfig(): void { |
| 161 | + const config = loadConfig(); |
| 162 | + console.log('\nCurrent locked workspaces config:\n'); |
| 163 | + console.log(JSON.stringify(config, null, 2)); |
| 164 | + console.log(''); |
| 165 | +} |
| 166 | + |
| 167 | +function showHelp(): void { |
| 168 | + console.log(` |
| 169 | +Lock Workspace CLI Tool |
| 170 | +
|
| 171 | +Usage: |
| 172 | + npm run lock-workspace list List all workspaces and their lock status |
| 173 | + npm run lock-workspace lock <id|name> Lock a workspace by ID or name |
| 174 | + npm run lock-workspace unlock <id|name> Unlock a workspace by ID or name |
| 175 | + npm run lock-workspace show Show current locked workspaces config |
| 176 | +
|
| 177 | +Examples: |
| 178 | + npm run lock-workspace list |
| 179 | + npm run lock-workspace lock "Public Template" |
| 180 | + npm run lock-workspace unlock abc123-def456-... |
| 181 | +`); |
| 182 | +} |
| 183 | + |
| 184 | +async function main(): Promise<void> { |
| 185 | + const args = process.argv.slice(2); |
| 186 | + const command = args[0]; |
| 187 | + |
| 188 | + switch (command) { |
| 189 | + case 'list': |
| 190 | + await listWorkspaces(); |
| 191 | + break; |
| 192 | + case 'lock': |
| 193 | + if (!args[1]) { |
| 194 | + console.error('Error: Please provide a workspace ID or name to lock.'); |
| 195 | + process.exit(1); |
| 196 | + } |
| 197 | + await lockWorkspace(args[1]); |
| 198 | + break; |
| 199 | + case 'unlock': |
| 200 | + if (!args[1]) { |
| 201 | + console.error('Error: Please provide a workspace ID or name to unlock.'); |
| 202 | + process.exit(1); |
| 203 | + } |
| 204 | + await unlockWorkspace(args[1]); |
| 205 | + break; |
| 206 | + case 'show': |
| 207 | + showConfig(); |
| 208 | + break; |
| 209 | + case 'help': |
| 210 | + case '--help': |
| 211 | + case '-h': |
| 212 | + showHelp(); |
| 213 | + break; |
| 214 | + default: |
| 215 | + showHelp(); |
| 216 | + break; |
| 217 | + } |
| 218 | +} |
| 219 | + |
| 220 | +main().catch(err => { |
| 221 | + console.error('Error:', err.message); |
| 222 | + process.exit(1); |
| 223 | +}); |
0 commit comments