git-tm 0.2.0 → 0.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/src/cli.js CHANGED
@@ -1,234 +1,235 @@
1
- #!/usr/bin/env node
2
-
3
- import { program } from "commander";
4
- import path from "path";
5
- import fs from "fs";
6
- import chalk from "chalk";
7
- import boxen from "boxen";
8
- import centerAlign from "center-align";
9
- import { fileURLToPath } from "url";
10
- import { dirname } from "path";
11
- import { startServer } from "./server.js";
12
- import ora from "ora";
13
- import figlet from "figlet";
14
- import gradient from "gradient-string";
15
- import { readFile } from 'fs/promises';
16
-
17
- const __filename = fileURLToPath(import.meta.url);
18
- const __dirname = dirname(__filename);
19
-
20
- const packageJson = JSON.parse(
21
- await readFile(
22
- new URL('../package.json', import.meta.url)
23
- )
24
- );
25
-
26
- const VERSION = packageJson.version;
27
- const DEFAULT_PORT = 7890;
28
- const GIT_ORANGE = "#e84d31";
29
- const WHITE = "#ffffff";
30
-
31
- const sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
32
-
33
- async function displayBanner() {
34
- const banner = await new Promise((resolve, reject) => {
35
- figlet("Git Time Machine", {
36
- font: "Standard",
37
- horizontalLayout: "default",
38
- verticalLayout: "default",
39
- width: 80,
40
- whitespaceBreak: true,
41
- }, (err, data) => {
42
- if (err) {
43
- reject(err);
44
- return;
45
- }
46
- resolve(data);
47
- });
48
- });
49
-
50
- const bannerGradient = gradient(GIT_ORANGE, WHITE);
51
- console.log(bannerGradient(banner));
52
- }
53
-
54
- function createSpinner(text) {
55
- return ora({
56
- text,
57
- color: "cyan",
58
- spinner: {
59
- frames: ["⠋", "⠙", "⠹", "⠸", "⠼", "⠴", "⠦", "⠧", "⠇", "⠏"],
60
- },
61
- });
62
- }
63
-
64
- async function showErrorAndHelp(errorMessage, exitCode = 1) {
65
- await displayBanner();
66
- console.log(chalk.yellow(`⚠️ Error: ${errorMessage}\n`));
67
- console.log(await getHelpText() + "\n");
68
- console.log(chalk.red("Exiting...\n"));
69
- process.exit(exitCode);
70
- }
71
-
72
- async function validateGitRepo(repoPath) {
73
- const spinner = createSpinner("Validating repository...");
74
- spinner.start();
75
-
76
- const fullPath = path.resolve(process.cwd(), repoPath);
77
- const gitPath = path.join(fullPath, ".git");
78
-
79
- if (!fs.existsSync(fullPath)) {
80
- spinner.fail(chalk.red("Repository validation failed"));
81
- await showErrorAndHelp(
82
- `Directory does not exist: ${chalk.yellow(fullPath)}`
83
- );
84
- }
85
-
86
- if (!fs.existsSync(gitPath)) {
87
- spinner.fail(chalk.red("Repository validation failed"));
88
- await showErrorAndHelp(
89
- `Not a git repository: ${chalk.blue(
90
- fullPath
91
- )}\nMake sure the directory contains a .git folder`
92
- );
93
- }
94
-
95
- spinner.succeed(chalk.green("Repository validated successfully"));
96
- return fullPath;
97
- }
98
-
99
- async function getHelpText() {
100
- return [
101
- await displayBanner(),
102
- chalk.bold("Usage:"),
103
- ` $ ${chalk.hex(GIT_ORANGE)("git-tm")} ${chalk.cyan(
104
- "<repository-path>"
105
- )} [options]`,
106
- "",
107
- chalk.bold("Examples:"),
108
- ` $ ${chalk.hex(GIT_ORANGE)("git-tm")} . ${chalk.dim(
109
- "Start with current directory"
110
- )}`,
111
- ` $ ${chalk.hex(GIT_ORANGE)("git-tm")} ./my-project ${chalk.dim(
112
- "Start with specific repository"
113
- )}`,
114
- ` $ ${chalk.hex(GIT_ORANGE)("git-tm")} ${chalk.cyan("-p")} 8000 ./repo ${chalk.dim(
115
- "Start with custom port"
116
- )}`,
117
- "",
118
- chalk.bold("Options:"),
119
- ` ${chalk.cyan("-p, --port")} <number> ${chalk.dim(
120
- "Port to run the server (default: " + DEFAULT_PORT + ")"
121
- )}`,
122
- ` ${chalk.cyan("-h, --help")} ${chalk.dim(
123
- "Display this help message"
124
- )}`,
125
- ` ${chalk.cyan("-V, --version")} ${chalk.dim(
126
- "Output the version number"
127
- )}`,
128
- "",
129
- chalk.bold("More Info:"),
130
- ` ${chalk.blue("https://github.com/vHackBots/Git-Time-Machine")}`,
131
- ].join("\n");
132
- }
133
-
134
- function createBox(content, options = {}) {
135
- const terminalWidth = process.stdout.columns || 80;
136
- const boxWidth = Math.min(70, terminalWidth - 4);
137
-
138
- return boxen(content, {
139
- padding: 1,
140
- margin: {
141
- top: 1,
142
- bottom: 1,
143
- left: 2,
144
- right: 0,
145
- },
146
- borderStyle: "round",
147
- borderColor: "cyan",
148
- width: boxWidth,
149
- textAlignment: "left",
150
- float: "left",
151
- ...options,
152
- });
153
- }
154
-
155
- if (process.argv.includes('-v') || process.argv.includes('--version')) {
156
- console.log("\n"+chalk.hex("#e84d31")("git-tm") + ` version: ${chalk.cyan(VERSION)}`);
157
- process.exit(0);
158
- }
159
-
160
- program
161
- .name("git-tm")
162
- .description("Interactive Git repository visualization tool")
163
- .version(VERSION, '-v, --version', 'Output the current version')
164
- .argument("[repo-path]", "path to git repository")
165
- .option(
166
- "-p, --port <number>",
167
- "port to run the server on",
168
- DEFAULT_PORT.toString()
169
- )
170
- .helpOption(false)
171
- .addOption(new program.Option("-h, --help", "display help").hideHelp())
172
- .action(async (repoPath, options) => {
173
- if (options.help) {
174
- console.log(await getHelpText() + "\n");
175
- process.exit(0);
176
- }
177
- if (!repoPath) {
178
- console.log(await getHelpText() + '\n');
179
- process.exit(0);
180
- }
181
-
182
- await displayBanner();
183
- console.log();
184
-
185
- const fullPath = await validateGitRepo(repoPath);
186
- process.chdir(fullPath);
187
-
188
- const startingSpinner = createSpinner("Starting server...");
189
- startingSpinner.start();
190
-
191
- try {
192
- const port = await startServer(parseInt(options.port));
193
- startingSpinner.succeed(chalk.green("Server started successfully"));
194
-
195
- console.log(
196
- createBox(
197
- [
198
- chalk.cyan.bold("📂 Repository Information"),
199
- `${chalk.white("Path:")} ${chalk.yellow(fullPath)}`,
200
- "",
201
- chalk.cyan.bold("🚀 Server Information"),
202
- `${chalk.white("URL:")} ${chalk.green(
203
- `http://localhost:${port}`
204
- )}`,
205
- `${chalk.white("Port:")} ${chalk.cyan(port)}`,
206
- "",
207
- chalk.dim("Press Ctrl+C to stop the server"),
208
- ].join("\n")
209
- )
210
- );
211
-
212
- await sleep(5000);
213
-
214
- process.on("SIGINT", async () => {
215
- console.log(
216
- centerAlign(chalk.cyan("\nThanks for using Git Time Machine! 👋\n"))
217
- );
218
- process.exit(0);
219
- });
220
- } catch (error) {
221
- startingSpinner.fail(chalk.red("Failed to start server"));
222
- console.log(chalk.red(`Error: ${error.message}\n`));
223
- process.exit(1);
224
- }
225
- })
226
- .exitOverride(async (err) => {
227
- if (err.code === "commander.missingArgument") {
228
- console.log(await getHelpText() + "\n");
229
- process.exit(1);
230
- }
231
- throw err;
232
- });
233
-
234
- program.parse(process.argv);
1
+ #!/usr/bin/env node
2
+
3
+ import { program } from "commander";
4
+ import path from "path";
5
+ import fs from "fs";
6
+ import chalk from "chalk";
7
+ import boxen from "boxen";
8
+ import centerAlign from "center-align";
9
+ import { fileURLToPath } from "url";
10
+ import { dirname } from "path";
11
+ import { startServer } from "./server.js";
12
+ import ora from "ora";
13
+ import figlet from "figlet";
14
+ import gradient from "gradient-string";
15
+ import { readFile } from 'fs/promises';
16
+
17
+ const __filename = fileURLToPath(import.meta.url);
18
+ const __dirname = dirname(__filename);
19
+
20
+ const packageJson = JSON.parse(
21
+ await readFile(
22
+ new URL('../package.json', import.meta.url)
23
+ )
24
+ );
25
+
26
+ const VERSION = packageJson.version;
27
+ const DEFAULT_PORT = 7890;
28
+ const GIT_ORANGE = "#e84d31";
29
+ const WHITE = "#ffffff";
30
+
31
+ const sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
32
+
33
+ async function displayBanner() {
34
+ const banner = await new Promise((resolve, reject) => {
35
+ figlet("Git Time Machine", {
36
+ font: "Standard",
37
+ horizontalLayout: "default",
38
+ verticalLayout: "default",
39
+ width: 80,
40
+ whitespaceBreak: true,
41
+ }, (err, data) => {
42
+ if (err) {
43
+ reject(err);
44
+ return;
45
+ }
46
+ resolve(data);
47
+ });
48
+ });
49
+
50
+ const bannerGradient = gradient(GIT_ORANGE, WHITE);
51
+ console.log(bannerGradient(banner));
52
+ }
53
+
54
+ function createSpinner(text) {
55
+ return ora({
56
+ text,
57
+ color: "cyan",
58
+ spinner: {
59
+ frames: ["⠋", "⠙", "⠹", "⠸", "⠼", "⠴", "⠦", "⠧", "⠇", "⠏"],
60
+ },
61
+ });
62
+ }
63
+
64
+ async function showErrorAndHelp(errorMessage, exitCode = 1) {
65
+ console.log(chalk.yellow(`⚠️ Error: ${errorMessage}\n`));
66
+ console.log(await getHelpText() + "\n");
67
+ process.exit(exitCode);
68
+ }
69
+
70
+ async function validateGitRepo(repoPath) {
71
+ const spinner = createSpinner("Validating repository...");
72
+ spinner.start();
73
+
74
+ const fullPath = path.resolve(process.cwd(), repoPath);
75
+ const gitPath = path.join(fullPath, ".git");
76
+
77
+ if (!fs.existsSync(fullPath)) {
78
+ spinner.fail(chalk.red("Repository validation failed"));
79
+ await showErrorAndHelp(
80
+ `Directory does not exist: ${chalk.yellow(fullPath)}`
81
+ );
82
+ }
83
+
84
+ if (!fs.existsSync(gitPath)) {
85
+ spinner.fail(chalk.red("Repository validation failed"));
86
+ await showErrorAndHelp(
87
+ `Not a git repository: ${chalk.white(
88
+ fullPath
89
+ )}\nMake sure the directory contains a .git folder`
90
+ );
91
+ }
92
+
93
+ spinner.succeed(chalk.green("Repository validated successfully"));
94
+ return fullPath;
95
+ }
96
+
97
+ async function getHelpText() {
98
+ return [
99
+ "",
100
+ chalk.bold("Git Time Machine"),
101
+ chalk.dim("Interactive Git repository visualization tool"),
102
+ "",
103
+ chalk.bold("Usage:"),
104
+ ` $ ${chalk.hex(GIT_ORANGE)("git-tm")} ${chalk.cyan(
105
+ "<repository-path>"
106
+ )} [options]`,
107
+ "",
108
+ chalk.bold("Examples:"),
109
+ ` $ ${chalk.hex(GIT_ORANGE)("git-tm")} . ${chalk.dim(
110
+ "Start with current directory"
111
+ )}`,
112
+ ` $ ${chalk.hex(GIT_ORANGE)("git-tm")} ./my-project ${chalk.dim(
113
+ "Start with specific repository"
114
+ )}`,
115
+ ` $ ${chalk.hex(GIT_ORANGE)("git-tm")} ${chalk.cyan(
116
+ "-p"
117
+ )} 8000 ./repo ${chalk.dim("Start with custom port")}`,
118
+ "",
119
+ chalk.bold("Options:"),
120
+ ` ${chalk.cyan("-p, --port")} <number> ${chalk.dim(
121
+ "Port to run the server (default: " + DEFAULT_PORT + ")"
122
+ )}`,
123
+ ` ${chalk.cyan("-h, --help")} ${chalk.dim(
124
+ "Display this help message"
125
+ )}`,
126
+ ` ${chalk.cyan("-V, --version")} ${chalk.dim(
127
+ "Output the version number"
128
+ )}`,
129
+ "",
130
+ chalk.bold("More Info:"),
131
+ ` ${chalk.yellow("https://github.com/vHackBots/Git-Time-Machine")}`,
132
+ ].join("\n");
133
+ }
134
+
135
+ function createBox(content, options = {}) {
136
+ const terminalWidth = process.stdout.columns || 80;
137
+ const boxWidth = Math.min(70, terminalWidth - 4);
138
+
139
+ return boxen(content, {
140
+ padding: 1,
141
+ margin: {
142
+ top: 1,
143
+ bottom: 1,
144
+ left: 2,
145
+ right: 0,
146
+ },
147
+ borderStyle: "round",
148
+ borderColor: "cyan",
149
+ width: boxWidth,
150
+ textAlignment: "left",
151
+ float: "left",
152
+ ...options,
153
+ });
154
+ }
155
+
156
+ if (process.argv.includes('-v') || process.argv.includes('--version')) {
157
+ console.log("\n"+chalk.hex("#e84d31")("git-tm") + ` version: ${chalk.cyan(VERSION)}`);
158
+ process.exit(0);
159
+ }
160
+
161
+ program
162
+ .name("git-tm")
163
+ .description("Interactive Git repository visualization tool")
164
+ .version(VERSION, '-v, --version', 'Output the current version')
165
+ .argument("[repo-path]", "path to git repository")
166
+ .option(
167
+ "-p, --port <number>",
168
+ "port to run the server on",
169
+ DEFAULT_PORT.toString()
170
+ )
171
+ .helpOption(false)
172
+ .addOption(new program.Option("-h, --help", "display help").hideHelp())
173
+ .action(async (repoPath, options) => {
174
+ if (options.help) {
175
+ console.log(await getHelpText() + "\n");
176
+ process.exit(0);
177
+ }
178
+ if (!repoPath) {
179
+ console.log(await getHelpText() + '\n');
180
+ process.exit(0);
181
+ }
182
+
183
+ await displayBanner();
184
+ console.log();
185
+
186
+ const fullPath = await validateGitRepo(repoPath);
187
+ process.chdir(fullPath);
188
+
189
+ const startingSpinner = createSpinner("Starting server...");
190
+ startingSpinner.start();
191
+
192
+ try {
193
+ const port = await startServer(parseInt(options.port));
194
+ startingSpinner.succeed(chalk.green("Server started successfully"));
195
+
196
+ console.log(
197
+ createBox(
198
+ [
199
+ chalk.cyan.bold("📂 Repository Information"),
200
+ `${chalk.white("Path:")} ${chalk.yellow(fullPath)}`,
201
+ "",
202
+ chalk.cyan.bold("🚀 Server Information"),
203
+ `${chalk.white("URL:")} ${chalk.green(
204
+ `http://localhost:${port}`
205
+ )}`,
206
+ `${chalk.white("Port:")} ${chalk.cyan(port)}`,
207
+ "",
208
+ chalk.dim("Press Ctrl+C to stop the server"),
209
+ ].join("\n")
210
+ )
211
+ );
212
+
213
+ await sleep(5000);
214
+
215
+ process.on("SIGINT", async () => {
216
+ console.log(
217
+ centerAlign(chalk.cyan("\nThanks for using Git Time Machine! 👋\n"))
218
+ );
219
+ process.exit(0);
220
+ });
221
+ } catch (error) {
222
+ startingSpinner.fail(chalk.red("Failed to start server"));
223
+ console.log(chalk.red(`Error: ${error.message}\n`));
224
+ process.exit(1);
225
+ }
226
+ })
227
+ .exitOverride(async (err) => {
228
+ if (err.code === "commander.missingArgument") {
229
+ console.log(await getHelpText() + "\n");
230
+ process.exit(1);
231
+ }
232
+ throw err;
233
+ });
234
+
235
+ program.parse(process.argv);