|
| 1 | +import { createApiClient as defaultCreateApiClient } from '../api/client.js'; |
| 2 | +import { loadConfig as defaultLoadConfig } from '../utils/config-loader.js'; |
| 3 | +import { getApiUrl as defaultGetApiUrl } from '../utils/environment-config.js'; |
| 4 | +import { getAccessToken as defaultGetAccessToken } from '../utils/global-config.js'; |
| 5 | +import * as defaultOutput from '../utils/output.js'; |
| 6 | +import { saveProjectLink as defaultSaveProjectLink } from '../utils/project-link-store.js'; |
| 7 | + |
| 8 | +export function parseProjectSelector(selector, options = {}) { |
| 9 | + let organizationSlug = options.org || null; |
| 10 | + let projectSlug = options.project || null; |
| 11 | + |
| 12 | + if (selector) { |
| 13 | + let parts = selector.split('/'); |
| 14 | + if (parts.length === 2) { |
| 15 | + organizationSlug = organizationSlug || parts[0]; |
| 16 | + projectSlug = projectSlug || parts[1]; |
| 17 | + } else if (parts.length === 1) { |
| 18 | + projectSlug = projectSlug || parts[0]; |
| 19 | + } |
| 20 | + } |
| 21 | + |
| 22 | + return { organizationSlug, projectSlug }; |
| 23 | +} |
| 24 | + |
| 25 | +export function validateProjectLinkOptions(selector, options = {}) { |
| 26 | + let errors = []; |
| 27 | + let { organizationSlug, projectSlug } = parseProjectSelector( |
| 28 | + selector, |
| 29 | + options |
| 30 | + ); |
| 31 | + |
| 32 | + if (!organizationSlug) { |
| 33 | + errors.push( |
| 34 | + 'Organization is required. Use <org>/<project> or --org <slug>.' |
| 35 | + ); |
| 36 | + } |
| 37 | + if (!projectSlug) { |
| 38 | + errors.push( |
| 39 | + 'Project is required. Use <org>/<project> or --project <slug>.' |
| 40 | + ); |
| 41 | + } |
| 42 | + |
| 43 | + return errors; |
| 44 | +} |
| 45 | + |
| 46 | +export async function projectLinkCommand( |
| 47 | + selector, |
| 48 | + options = {}, |
| 49 | + globalOptions = {}, |
| 50 | + deps = {} |
| 51 | +) { |
| 52 | + let { |
| 53 | + createApiClient = defaultCreateApiClient, |
| 54 | + getAccessToken = defaultGetAccessToken, |
| 55 | + getApiUrl = defaultGetApiUrl, |
| 56 | + loadConfig = defaultLoadConfig, |
| 57 | + output = defaultOutput, |
| 58 | + saveProjectLink = defaultSaveProjectLink, |
| 59 | + exit = code => process.exit(code), |
| 60 | + } = deps; |
| 61 | + |
| 62 | + output.configure({ |
| 63 | + json: globalOptions.json, |
| 64 | + verbose: globalOptions.verbose, |
| 65 | + color: !globalOptions.noColor, |
| 66 | + }); |
| 67 | + |
| 68 | + let { organizationSlug, projectSlug } = parseProjectSelector( |
| 69 | + selector, |
| 70 | + options |
| 71 | + ); |
| 72 | + |
| 73 | + try { |
| 74 | + let config = await loadConfig(globalOptions.config, globalOptions); |
| 75 | + let userToken = config.userToken || (await getAccessToken()); |
| 76 | + |
| 77 | + if (!userToken) { |
| 78 | + output.error('Login required before linking a project'); |
| 79 | + output.hint('Run "vizzly login" first, then try project link again'); |
| 80 | + output.cleanup(); |
| 81 | + exit(1); |
| 82 | + return; |
| 83 | + } |
| 84 | + |
| 85 | + output.startSpinner(`Linking ${organizationSlug}/${projectSlug}...`); |
| 86 | + |
| 87 | + let apiUrl = config.apiUrl || getApiUrl(); |
| 88 | + let client = createApiClient({ |
| 89 | + baseUrl: apiUrl, |
| 90 | + token: userToken, |
| 91 | + command: 'project-link', |
| 92 | + }); |
| 93 | + |
| 94 | + let response = await client.request(`/api/cli/${projectSlug}/link-token`, { |
| 95 | + method: 'POST', |
| 96 | + headers: { |
| 97 | + 'Content-Type': 'application/json', |
| 98 | + 'X-Organization': organizationSlug, |
| 99 | + }, |
| 100 | + body: JSON.stringify({ |
| 101 | + name: options.name, |
| 102 | + expiresAt: options.expiresAt, |
| 103 | + }), |
| 104 | + }); |
| 105 | + |
| 106 | + let linkedProject = await saveProjectLink({ |
| 107 | + apiUrl, |
| 108 | + organizationSlug: response.organization?.slug || organizationSlug, |
| 109 | + organizationName: response.organization?.name, |
| 110 | + projectSlug: response.project?.slug || projectSlug, |
| 111 | + projectName: response.project?.name, |
| 112 | + token: response.token.token, |
| 113 | + tokenId: response.token.id, |
| 114 | + tokenPrefix: response.token.token_prefix, |
| 115 | + expiresAt: response.token.expires_at, |
| 116 | + createdAt: response.token.created_at, |
| 117 | + }); |
| 118 | + |
| 119 | + output.stopSpinner(); |
| 120 | + |
| 121 | + if (globalOptions.json) { |
| 122 | + output.data({ |
| 123 | + linked: true, |
| 124 | + organizationSlug: linkedProject.organizationSlug, |
| 125 | + projectSlug: linkedProject.projectSlug, |
| 126 | + tokenPrefix: linkedProject.tokenPrefix, |
| 127 | + storage: linkedProject.storage, |
| 128 | + }); |
| 129 | + output.cleanup(); |
| 130 | + return; |
| 131 | + } |
| 132 | + |
| 133 | + output.complete( |
| 134 | + `Linked ${linkedProject.organizationSlug}/${linkedProject.projectSlug}` |
| 135 | + ); |
| 136 | + output.hint(`Cloud uploads will use ${linkedProject.tokenPrefix}...`); |
| 137 | + output.cleanup(); |
| 138 | + } catch (error) { |
| 139 | + output.stopSpinner(); |
| 140 | + output.error('Failed to link project', error); |
| 141 | + output.cleanup(); |
| 142 | + exit(1); |
| 143 | + } |
| 144 | +} |
0 commit comments