Skip to content

Commit 79fa603

Browse files
committed
reorganising the code
1 parent 649898c commit 79fa603

32 files changed

+146
-147
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
dist/
2+
!env/

cli/action.ts

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
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+
};

cli/command.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import { HOME_DIR, VERSION } from "@scope/env";
2+
import { Command } from "jsr:@cliffy/command@^1.0.0-rc.7";
3+
import { cloneAction } from "./action.ts";
4+
5+
export const gitMirrorCommand = new Command()
6+
.name("git-mirror")
7+
.version(VERSION)
8+
.description("Clone/Fetch a Git repository into a 'Projects' directory")
9+
.arguments("<repo:string>")
10+
.option("-r, --root <rootDir>", "The root directory.", {
11+
default: `${HOME_DIR}/Projects`,
12+
})
13+
.option("-o, --open-vs-code", "Open the repository in VS Code.", {
14+
default: false,
15+
})
16+
.option("--dry-run", "Print the command that would be run.")
17+
.action(cloneAction);

cli/deno.jsonc

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"name": "@scope/cli",
3+
"version": "0.0.1",
4+
"exports": "./mod.ts"
5+
}

cli/mod.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export { gitMirrorCommand } from "./command.ts";

deno.jsonc

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,5 +19,6 @@
1919
"target",
2020
"node_modules"
2121
]
22-
}
22+
},
23+
"workspace": ["exec", "fs", "git", "env"]
2324
}

deno.lock

Lines changed: 10 additions & 46 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

env/constants.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
export const HOME_DIR: string = Deno.env.get("HOME") ||
2+
Deno.env.get("USERPROFILE") || "";

env/deno.jsonc

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"name": "@scope/env",
3+
"version": "0.0.1",
4+
"exports": "./mod.ts"
5+
}

env/mod.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
export * from "./constants.ts";
2+
export { VERSION } from "./version.ts";

0 commit comments

Comments
 (0)