Skip to content

Commit 6aa1880

Browse files
committed
updated README and fmt code
1 parent 229959c commit 6aa1880

15 files changed

+119
-114
lines changed

README.md

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
# git-mirror
22

3-
Deno script to clone a github/gitlab repo to `~/Projects` while keeping a tree structure close to the remote url. If the project is already present, then it'll fetch from the remote.
3+
Deno script to clone a github/gitlab repo to `~/Projects` while keeping a tree
4+
structure close to the remote url. If the project is already present, then it'll
5+
fetch from the remote.
46

5-
__WARNING__ this was developed on a MacOS, so no guaranty are offered to run on a different OS.
7+
**WARNING** this was developed on a MacOS, so no guaranty are offered to run on
8+
a different OS.
69

710
## install
811

@@ -16,7 +19,8 @@ __WARNING__ this was developed on a MacOS, so no guaranty are offered to run on
1619

1720
## Usage
1821

19-
Call `git mirror git@github.com:owner/repo.git` will clone to `~/Projects/owner/repo`.
22+
Call `git mirror git@github.com:owner/repo.git` will clone to
23+
`~/Projects/owner/repo`.
2024

2125
```sh
2226
Usage: clone <repo>
@@ -33,5 +37,5 @@ Options:
3337
-r, --root <rootDir> - The root directory. (Default: "/Users/<user>/Projects")
3438
-o, --open-vs-code - Open the repository in VS Code. (Default: true)
3539
--no-open-vs-code - Do not open the repository in VS Code.
36-
--dry-run - Print the command that would be run.
40+
--dry-run - Print the command that would be run.
3741
```

git-mirror.ts

Lines changed: 25 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
#!/usr/bin/env -S deno run --allow-env --allow-read --allow-write --allow-run
2-
import { Command } from 'jsr:@cliffy/command@^1.0.0-rc.7';
3-
import { Confirm } from 'jsr:@cliffy/prompt@1.0.0-rc.7/confirm';
4-
import { exists } from 'jsr:@std/fs';
5-
import chalk from 'npm:chalk';
6-
import { HOME_DIR } from './utils/constants.ts';
2+
import { Command } from "jsr:@cliffy/command@^1.0.0-rc.7";
3+
import { Confirm } from "jsr:@cliffy/prompt@1.0.0-rc.7/confirm";
4+
import { exists } from "jsr:@std/fs";
5+
import chalk from "npm:chalk";
6+
import { HOME_DIR } from "./utils/constants.ts";
77
import {
88
cloneRepo,
99
fetchRepo,
1010
findExecutable,
1111
getLocalPath,
1212
runExecutable,
13-
} from './utils/mod.ts';
13+
} from "./utils/mod.ts";
1414

1515
interface CloneOptions {
1616
openVsCode?: boolean;
@@ -21,10 +21,10 @@ interface CloneOptions {
2121
const cloneAction = async (options: CloneOptions, repo: string) => {
2222
const localRepo = getLocalPath(
2323
repo,
24-
options.rootDir ?? `${HOME_DIR}/Projects`
24+
options.rootDir ?? `${HOME_DIR}/Projects`,
2525
);
2626
console.log(
27-
`Cloning repository: ${chalk.green(repo)} to ${chalk.green(localRepo)}`
27+
`Cloning repository: ${chalk.green(repo)} to ${chalk.green(localRepo)}`,
2828
);
2929

3030
const dirAlreadyExists = await exists(localRepo);
@@ -42,43 +42,44 @@ const cloneAction = async (options: CloneOptions, repo: string) => {
4242
}
4343
}
4444

45-
const openVsCode =
46-
options.openVsCode ??
45+
const openVsCode = options.openVsCode ??
4746
(await Confirm.prompt({
48-
message: 'Open the repository in VS Code?',
47+
message: "Open the repository in VS Code?",
4948
default: true,
5049
}));
5150

5251
if (openVsCode) {
5352
if (options.dryRun) {
5453
console.log(
55-
chalk.bgYellow(`Dry run: Opening repository in VS Code: ${localRepo}`)
54+
chalk.bgYellow(`Dry run: Opening repository in VS Code: ${localRepo}`),
5655
);
5756
} else {
58-
const vscode = await findExecutable('code');
57+
const vscode = await findExecutable("code");
5958
runExecutable(vscode, [localRepo]);
6059
}
6160
}
6261

6362
console.log(
64-
`To move to the project's directory, please run: "cd ${chalk.green(
65-
localRepo
66-
)}"`
63+
`To move to the project's directory, please run: "cd ${
64+
chalk.green(
65+
localRepo,
66+
)
67+
}"`,
6768
);
6869
};
6970

7071
await new Command()
71-
.name('clone')
72-
.version('0.1.2')
73-
.description('Clone a Git repository into the ~/Projects directory.')
74-
.arguments('<repo:string>')
75-
.option('-r, --root <rootDir>', 'The root directory.', {
72+
.name("clone")
73+
.version("0.1.2")
74+
.description("Clone/Fetch a Git repository into a 'Projects' directory")
75+
.arguments("<repo:string>")
76+
.option("-r, --root <rootDir>", "The root directory.", {
7677
default: `${HOME_DIR}/Projects`,
7778
})
78-
.option('-o, --open-vs-code', 'Open the repository in VS Code.', {
79+
.option("-o, --open-vs-code", "Open the repository in VS Code.", {
7980
default: true,
8081
})
81-
.option('--no-open-vs-code', 'Do not open the repository in VS Code.')
82-
.option('--dry-run', 'Print the command that would be run.')
82+
.option("--no-open-vs-code", "Do not open the repository in VS Code.")
83+
.option("--dry-run", "Print the command that would be run.")
8384
.action(cloneAction)
8485
.parse(Deno.args);

utils/constants.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
export const HOME_DIR = Deno.env.get('HOME');
1+
export const HOME_DIR = Deno.env.get("HOME");

utils/exec/find-exec.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
import chalk from 'npm:chalk';
1+
import chalk from "npm:chalk";
22

33
export const findExecutable = async (executable: string): Promise<string> => {
44
try {
5-
const command = await new Deno.Command('which', {
5+
const command = await new Deno.Command("which", {
66
args: [executable],
7-
stdout: 'piped',
7+
stdout: "piped",
88
});
99
const { stdout } = await command.output();
1010

utils/exec/run-exec.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
export const runExecutable = async (
22
executable: string,
3-
args: string[]
3+
args: string[],
44
): Promise<Deno.CommandStatus> => {
55
const command = await new Deno.Command(executable, {
66
args,
7-
stdin: 'piped',
7+
stdin: "piped",
88
});
99
const child = await command.spawn();
1010
const status = await child.status;

utils/file/get-dir-path-from-repo.test.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
1-
import { assertEquals } from 'jsr:@std/assert';
2-
import { describe, it } from 'jsr:@std/testing/bdd';
3-
import { getDirPathFromRepo } from './get-dir-path-from-repo.ts';
1+
import { assertEquals } from "jsr:@std/assert";
2+
import { describe, it } from "jsr:@std/testing/bdd";
3+
import { getDirPathFromRepo } from "./get-dir-path-from-repo.ts";
44

5-
describe('getDirPathFromRepo', () => {
5+
describe("getDirPathFromRepo", () => {
66
const TEST_CASES = [
77
{
8-
repo: 'git@git-host.com:organisation/project.git',
9-
expected: 'organisation/project',
8+
repo: "git@git-host.com:organisation/project.git",
9+
expected: "organisation/project",
1010
},
1111
{
12-
repo: 'https://git-host.com/organisation/project.git',
13-
expected: 'organisation/project',
12+
repo: "https://git-host.com/organisation/project.git",
13+
expected: "organisation/project",
1414
},
1515
];
1616

utils/file/get-dir-path-from-repo.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
1-
import chalk from 'npm:chalk';
1+
import chalk from "npm:chalk";
22

33
export const getDirPathFromRepo = (repo: string): string => {
44
let dirPath: string;
55

6-
if (repo.startsWith('git@')) {
7-
const repoParts = repo.split(':');
8-
dirPath = repoParts[1].replace('.git', '');
9-
} else if (repo.startsWith('https://')) {
10-
const repoParts = repo.split('/');
11-
dirPath = repoParts.slice(3).join('/').replace('.git', '');
6+
if (repo.startsWith("git@")) {
7+
const repoParts = repo.split(":");
8+
dirPath = repoParts[1].replace(".git", "");
9+
} else if (repo.startsWith("https://")) {
10+
const repoParts = repo.split("/");
11+
dirPath = repoParts.slice(3).join("/").replace(".git", "");
1212
} else {
1313
console.error(chalk.bgRedBright(`Invalid Git repository URL: ${repo}`));
1414
Deno.exit(1);

utils/file/get-host-from-repo.test.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
1-
import { assertEquals } from 'jsr:@std/assert';
2-
import { describe, it } from 'jsr:@std/testing/bdd';
3-
import { getHostFromRepo } from './get-host-from-repo.ts';
1+
import { assertEquals } from "jsr:@std/assert";
2+
import { describe, it } from "jsr:@std/testing/bdd";
3+
import { getHostFromRepo } from "./get-host-from-repo.ts";
44

5-
describe('getHostFromRepo', () => {
5+
describe("getHostFromRepo", () => {
66
const TEST_CASES = [
77
{
8-
repo: 'git@git-host.com:organisation/project.git',
9-
expected: 'git-host',
8+
repo: "git@git-host.com:organisation/project.git",
9+
expected: "git-host",
1010
},
1111
{
12-
repo: 'https://git-host.com/organisation/project.git',
13-
expected: 'git-host',
12+
repo: "https://git-host.com/organisation/project.git",
13+
expected: "git-host",
1414
},
1515
];
1616

utils/file/get-host-from-repo.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
1-
import chalk from 'npm:chalk';
1+
import chalk from "npm:chalk";
22

33
export const getHostFromRepo = (repo: string): string => {
44
let host: string;
55

6-
if (repo.startsWith('git@')) {
7-
const repoParts = repo.split(':');
8-
host = repoParts[0].split('@')[1].split('.')[0];
9-
} else if (repo.startsWith('https://')) {
10-
const repoParts = repo.split('/');
11-
host = repoParts[2].split('.')[0];
6+
if (repo.startsWith("git@")) {
7+
const repoParts = repo.split(":");
8+
host = repoParts[0].split("@")[1].split(".")[0];
9+
} else if (repo.startsWith("https://")) {
10+
const repoParts = repo.split("/");
11+
host = repoParts[2].split(".")[0];
1212
} else {
1313
console.error(chalk.bgRedBright(`Invalid Git repository URL: ${repo}`));
1414
Deno.exit(1);

utils/file/get-local-path.test.ts

Lines changed: 26 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,44 @@
1-
import { assertEquals } from 'jsr:@std/assert';
2-
import { describe, it } from 'jsr:@std/testing/bdd';
3-
import { getLocalPath } from './get-local-path.ts';
4-
import { shortenPath } from './shorten-path.ts';
1+
import { assertEquals } from "jsr:@std/assert";
2+
import { describe, it } from "jsr:@std/testing/bdd";
3+
import { getLocalPath } from "./get-local-path.ts";
4+
import { shortenPath } from "./shorten-path.ts";
55

6-
describe('getLocalPath', () => {
6+
describe("getLocalPath", () => {
77
const TEST_CASES = [
88
{
9-
targetBaseDir: '/home/user/Projects',
10-
repo: 'https://git-host.com/project/project.git',
11-
expected: '/home/user/Projects/git-host/project/project',
9+
targetBaseDir: "/home/user/Projects",
10+
repo: "https://git-host.com/project/project.git",
11+
expected: "/home/user/Projects/git-host/project/project",
1212
},
1313
{
14-
targetBaseDir: '/home/user/Projects',
15-
repo: 'https://git-host.com/organisation/project.git',
16-
expected: '/home/user/Projects/git-host/organisation/project',
14+
targetBaseDir: "/home/user/Projects",
15+
repo: "https://git-host.com/organisation/project.git",
16+
expected: "/home/user/Projects/git-host/organisation/project",
1717
},
1818
{
19-
targetBaseDir: '/home/user/Projects/git-host',
20-
repo: 'https://git-host.com/organisation/project.git',
21-
expected: '/home/user/Projects/git-host/organisation/project',
19+
targetBaseDir: "/home/user/Projects/git-host",
20+
repo: "https://git-host.com/organisation/project.git",
21+
expected: "/home/user/Projects/git-host/organisation/project",
2222
},
2323
{
24-
targetBaseDir: '/home/user/Projects',
25-
repo: 'https://git-host.com/organisation/project.git',
26-
expected: '/home/user/Projects/git-host/organisation/project',
24+
targetBaseDir: "/home/user/Projects",
25+
repo: "https://git-host.com/organisation/project.git",
26+
expected: "/home/user/Projects/git-host/organisation/project",
2727
},
2828
{
29-
targetBaseDir: '/home/user/Projects/git-host',
30-
repo: 'https://git-host.com/organisation/project.git',
31-
expected: '/home/user/Projects/git-host/organisation/project',
29+
targetBaseDir: "/home/user/Projects/git-host",
30+
repo: "https://git-host.com/organisation/project.git",
31+
expected: "/home/user/Projects/git-host/organisation/project",
3232
},
3333
{
34-
targetBaseDir: '/home/user/Projects/git-host/organisation',
35-
repo: 'https://git-host.com/organisation/project.git',
36-
expected: '/home/user/Projects/git-host/organisation/project',
34+
targetBaseDir: "/home/user/Projects/git-host/organisation",
35+
repo: "https://git-host.com/organisation/project.git",
36+
expected: "/home/user/Projects/git-host/organisation/project",
3737
},
3838
{
39-
targetBaseDir: '/home/user/Projects/git-host/organisation/project',
40-
repo: 'https://git-host.com/organisation/project.git',
41-
expected: '/home/user/Projects/git-host/organisation/project',
39+
targetBaseDir: "/home/user/Projects/git-host/organisation/project",
40+
repo: "https://git-host.com/organisation/project.git",
41+
expected: "/home/user/Projects/git-host/organisation/project",
4242
},
4343
];
4444

0 commit comments

Comments
 (0)