|
| 1 | +// Copyright (c) 2025 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | + |
| 3 | +import { Command, Flags, Args } from '@oclif/core'; |
| 4 | +import path from 'node:path'; |
| 5 | +import fs from 'node:fs/promises'; |
| 6 | +import { spawnSync } from 'node:child_process'; |
| 7 | +import { printError, printStep, printKV } from '../../utils/format.js'; |
| 8 | +import { createApiClient, requireAuth } from '../../utils/api-client.js'; |
| 9 | +import { formatOutput } from '../../utils/output-formatter.js'; |
| 10 | + |
| 11 | +/** |
| 12 | + * `os projects bind` — bind a locally-compiled artifact to an existing |
| 13 | + * multi-project server project. |
| 14 | + * |
| 15 | + * Equivalent to `PATCH /api/v1/cloud/projects/<id>` with |
| 16 | + * `metadata.artifact_path = <absolute-path>`. The server's |
| 17 | + * AppBundleResolver picks up the path on the next per-project kernel |
| 18 | + * boot, registering the bundle's objects, views, and seed data. |
| 19 | + * |
| 20 | + * Use `--build` to compile `objectstack.config.ts` first so the artifact |
| 21 | + * reflects the latest source. |
| 22 | + */ |
| 23 | +export default class ProjectsBind extends Command { |
| 24 | + static override description = 'Bind a local objectstack artifact to an existing project'; |
| 25 | + |
| 26 | + static override examples = [ |
| 27 | + '$ os projects bind <project-id> --artifact ./dist/objectstack.json', |
| 28 | + '$ os projects bind <project-id> --artifact ./dist/objectstack.json --build', |
| 29 | + '$ os projects bind <project-id> --reseed', |
| 30 | + ]; |
| 31 | + |
| 32 | + static override args = { |
| 33 | + projectId: Args.string({ |
| 34 | + description: 'Target project id (UUID)', |
| 35 | + required: true, |
| 36 | + }), |
| 37 | + }; |
| 38 | + |
| 39 | + static override flags = { |
| 40 | + url: Flags.string({ char: 'u', description: 'Server URL', env: 'OBJECTSTACK_URL' }), |
| 41 | + token: Flags.string({ char: 't', description: 'Authentication token', env: 'OBJECTSTACK_TOKEN' }), |
| 42 | + artifact: Flags.string({ |
| 43 | + description: 'Path to a compiled objectstack.json artifact (default: ./dist/objectstack.json)', |
| 44 | + }), |
| 45 | + build: Flags.boolean({ |
| 46 | + description: 'Run `objectstack compile` before binding', |
| 47 | + default: false, |
| 48 | + }), |
| 49 | + reseed: Flags.boolean({ |
| 50 | + description: 'After binding, also re-run schema sync + bundle seeding via /cloud/projects/:id/reseed', |
| 51 | + default: false, |
| 52 | + }), |
| 53 | + format: Flags.string({ |
| 54 | + char: 'f', |
| 55 | + description: 'Output format', |
| 56 | + options: ['json', 'table', 'yaml'], |
| 57 | + default: 'table', |
| 58 | + }), |
| 59 | + }; |
| 60 | + |
| 61 | + async run(): Promise<void> { |
| 62 | + const { args, flags } = await this.parse(ProjectsBind); |
| 63 | + |
| 64 | + try { |
| 65 | + const artifactRel = flags.artifact ?? './dist/objectstack.json'; |
| 66 | + const artifactAbs = path.isAbsolute(artifactRel) |
| 67 | + ? artifactRel |
| 68 | + : path.resolve(process.cwd(), artifactRel); |
| 69 | + |
| 70 | + if (flags.build) { |
| 71 | + printStep('Compiling objectstack.config.ts → ' + artifactAbs); |
| 72 | + const binPath = process.argv[1]; |
| 73 | + const r = spawnSync( |
| 74 | + process.execPath, |
| 75 | + [binPath, 'compile', '--output', artifactAbs], |
| 76 | + { stdio: 'inherit', env: { ...process.env, NODE_ENV: 'development' } }, |
| 77 | + ); |
| 78 | + if (r.status !== 0) { |
| 79 | + printError('Compile failed — fix errors above before binding'); |
| 80 | + this.exit(1); |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + try { |
| 85 | + await fs.access(artifactAbs); |
| 86 | + } catch { |
| 87 | + printError(`Artifact not found: ${artifactAbs}`); |
| 88 | + if (!flags.build) { |
| 89 | + console.error(' Hint: pass --build to compile first, or check the path with --artifact.'); |
| 90 | + } |
| 91 | + this.exit(1); |
| 92 | + } |
| 93 | + |
| 94 | + const { client, token } = await createApiClient({ url: flags.url, token: flags.token }); |
| 95 | + requireAuth(token); |
| 96 | + |
| 97 | + // Fetch existing metadata so we don't blow it away. |
| 98 | + const current = await client.projects.get(args.projectId); |
| 99 | + const existingMeta: Record<string, unknown> = (current?.project?.metadata && typeof current.project.metadata === 'object') |
| 100 | + ? { ...current.project.metadata as Record<string, unknown> } |
| 101 | + : {}; |
| 102 | + // Drop the prior bind error so the UI doesn't show a stale failure. |
| 103 | + delete existingMeta.artifactBindError; |
| 104 | + existingMeta.artifact_path = artifactAbs; |
| 105 | + |
| 106 | + printKV('Project', args.projectId, '🎯'); |
| 107 | + printKV('Artifact', artifactAbs, '📦'); |
| 108 | + |
| 109 | + const res = await client.projects.update(args.projectId, { |
| 110 | + metadata: existingMeta, |
| 111 | + }); |
| 112 | + |
| 113 | + if (flags.reseed) { |
| 114 | + printStep('Reseeding bundle (POST /cloud/projects/:id/reseed)…'); |
| 115 | + try { |
| 116 | + // Best-effort: server may not expose a reseed endpoint yet. |
| 117 | + const reseed = await (client as any).fetch?.( |
| 118 | + `${(client as any).baseUrl}/api/v1/cloud/projects/${encodeURIComponent(args.projectId)}/reseed`, |
| 119 | + { method: 'POST' }, |
| 120 | + ); |
| 121 | + if (reseed && typeof reseed.ok === 'boolean' && !reseed.ok) { |
| 122 | + console.error(' ⚠ reseed endpoint returned ' + reseed.status + ' — bundle will be applied on next kernel boot.'); |
| 123 | + } |
| 124 | + } catch (e: any) { |
| 125 | + console.error(' ⚠ reseed failed: ' + (e?.message ?? e) + ' — bundle will be applied on next kernel boot.'); |
| 126 | + } |
| 127 | + } |
| 128 | + |
| 129 | + if (flags.format === 'json') { |
| 130 | + formatOutput(res, 'json'); |
| 131 | + } else if (flags.format === 'yaml') { |
| 132 | + formatOutput(res, 'yaml'); |
| 133 | + } else { |
| 134 | + console.log(`\n✓ Project bound to artifact`); |
| 135 | + console.log(` ${args.projectId} → ${artifactAbs}`); |
| 136 | + console.log(` The next request to this project will load the new bundle.`); |
| 137 | + console.log(''); |
| 138 | + } |
| 139 | + } catch (error: any) { |
| 140 | + if (flags.format === 'json') { |
| 141 | + console.log(JSON.stringify({ success: false, error: error.message }, null, 2)); |
| 142 | + this.exit(1); |
| 143 | + } |
| 144 | + printError(error.message || String(error)); |
| 145 | + this.exit(1); |
| 146 | + } |
| 147 | + } |
| 148 | +} |
0 commit comments