|
| 1 | +import { CommandModule } from "yargs"; |
| 2 | +import { context } from "../../api/context"; |
| 3 | +import prompts from "prompts"; |
| 4 | +import { log } from "../../log"; |
| 5 | +import { cmd } from "../../lib/cmd"; |
| 6 | +import { Configuration } from "../../lib/Configuration"; |
| 7 | + |
| 8 | +type Options = { workdir: string }; |
| 9 | + |
| 10 | +const getGitRemoteUrl = async (workdir: string): Promise<string | undefined> => { |
| 11 | + try { |
| 12 | + const { stdout } = await cmd("git remote get-url origin", { cwd: workdir }); |
| 13 | + return stdout?.toString().trim(); |
| 14 | + } catch (error) { |
| 15 | + log.warn("Could not detect git remote origin URL."); |
| 16 | + return undefined; |
| 17 | + } |
| 18 | +}; |
| 19 | + |
| 20 | +export const link: CommandModule<object, Options> = { |
| 21 | + command: "link", |
| 22 | + describe: "Link the local repository with a Faable app", |
| 23 | + builder: (yargs) => { |
| 24 | + return yargs |
| 25 | + .option("workdir", { |
| 26 | + alias: "w", |
| 27 | + type: "string", |
| 28 | + description: "Working directory", |
| 29 | + }) |
| 30 | + .showHelpOnFail(false) as any; |
| 31 | + }, |
| 32 | + handler: async (args) => { |
| 33 | + const workdir = args.workdir || process.cwd(); |
| 34 | + const { api } = await context(); |
| 35 | + |
| 36 | + log.info("Checking local git repository..."); |
| 37 | + const gitUrl = await getGitRemoteUrl(workdir); |
| 38 | + |
| 39 | + log.info("Fetching your Faable apps..."); |
| 40 | + const apps = await api.list(); |
| 41 | + |
| 42 | + if (apps.length === 0) { |
| 43 | + log.error("No apps found in your account. Create one first at https://faable.cloud"); |
| 44 | + return; |
| 45 | + } |
| 46 | + |
| 47 | + const { selectedApp } = await prompts({ |
| 48 | + type: "select", |
| 49 | + name: "selectedApp", |
| 50 | + message: "Select the Faable app to link with this repository:", |
| 51 | + choices: apps.map((app) => ({ |
| 52 | + title: `${app.name} (${app.url})`, |
| 53 | + value: app, |
| 54 | + })), |
| 55 | + }); |
| 56 | + |
| 57 | + if (!selectedApp) { |
| 58 | + log.info("Link cancelled."); |
| 59 | + return; |
| 60 | + } |
| 61 | + |
| 62 | + log.info(`Linking to ${selectedApp.name}...`); |
| 63 | + |
| 64 | + // Update the app in the API |
| 65 | + if (gitUrl) { |
| 66 | + await api.updateApp(selectedApp.id, { github_repo: gitUrl }); |
| 67 | + log.info(`Updated app ${selectedApp.name} with github_repo: ${gitUrl}`); |
| 68 | + } else { |
| 69 | + log.warn("No git remote URL detected. Skipping API update for github_repo."); |
| 70 | + } |
| 71 | + |
| 72 | + // Save locally for CLI convenience |
| 73 | + Configuration.instance().saveConfig({ app_slug: selectedApp.name }); |
| 74 | + log.info(`Successfully linked local repository to ${selectedApp.name}.`); |
| 75 | + }, |
| 76 | +}; |
0 commit comments