|
| 1 | +import { HOME_DIR } from "@scope/env"; |
| 2 | +import { findExecutable, runExecutable } from "@scope/exec"; |
| 3 | +import { getLocalPath } from "@scope/fs"; |
| 4 | +import { cloneRepo, fetchRepo } from "@scope/git"; |
| 5 | +import { colors } from "jsr:@cliffy/ansi@^1.0.0-rc.7/colors"; |
| 6 | +import { Confirm } from "jsr:@cliffy/prompt@1.0.0-rc.7/confirm"; |
| 7 | +import { exists } from "jsr:@std/fs"; |
| 8 | + |
| 9 | +interface CloneOptions { |
| 10 | + openVsCode?: boolean; |
| 11 | + rootDir?: string; |
| 12 | + dryRun?: boolean; |
| 13 | +} |
| 14 | + |
| 15 | +export const cloneAction = async (options: CloneOptions, repo: string) => { |
| 16 | + if (options.dryRun) { |
| 17 | + console.log( |
| 18 | + colors.bgYellow( |
| 19 | + "Dry run mode ... none of the commands will actually be run.", |
| 20 | + ), |
| 21 | + ); |
| 22 | + } |
| 23 | + |
| 24 | + const localRepo = getLocalPath( |
| 25 | + repo, |
| 26 | + options.rootDir ?? `${HOME_DIR}/Projects`, |
| 27 | + ); |
| 28 | + console.log( |
| 29 | + `Cloning repository: ${colors.green(repo)} to ${colors.green(localRepo)}`, |
| 30 | + ); |
| 31 | + |
| 32 | + const dirAlreadyExists = await exists(localRepo); |
| 33 | + if (dirAlreadyExists) { |
| 34 | + if (options.dryRun) { |
| 35 | + console.log( |
| 36 | + colors.yellow(`> Dry run: Fetching repository: ${localRepo}`), |
| 37 | + ); |
| 38 | + } else { |
| 39 | + await fetchRepo(localRepo); |
| 40 | + } |
| 41 | + } else { |
| 42 | + if (options.dryRun) { |
| 43 | + console.log(colors.yellow(`> Dry run: Cloning repository: ${repo}`)); |
| 44 | + } else { |
| 45 | + await cloneRepo(repo, localRepo); |
| 46 | + } |
| 47 | + } |
| 48 | + |
| 49 | + const openVsCode = options.openVsCode ?? |
| 50 | + (await Confirm.prompt({ |
| 51 | + message: "Open the repository in VS Code?", |
| 52 | + default: true, |
| 53 | + })); |
| 54 | + |
| 55 | + if (openVsCode) { |
| 56 | + if (options.dryRun) { |
| 57 | + console.log( |
| 58 | + colors.yellow(`> Dry run: Opening repository in VS Code: ${localRepo}`), |
| 59 | + ); |
| 60 | + } else { |
| 61 | + const vscode = await findExecutable("code"); |
| 62 | + runExecutable(vscode, [localRepo]); |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + console.log( |
| 67 | + `To move to the project's directory, please run: "cd ${ |
| 68 | + colors.green( |
| 69 | + localRepo, |
| 70 | + ) |
| 71 | + }"`, |
| 72 | + ); |
| 73 | +}; |
0 commit comments