forked from git-truck/git-truck
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcli.ts
126 lines (104 loc) · 3.37 KB
/
cli.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
import path from "path"
import pkg from "../package.json"
import open from "open"
import latestVersion from "latest-version"
import { GitCaller } from "./analyzer/git-caller.server"
import { getArgsWithDefaults, parseArgs } from "./analyzer/args.server"
import { getPathFromRepoAndHead } from "./util"
import { createApp } from "@remix-run/serve"
import { semverCompare } from "./components/util"
import { describeAsyncJob } from "./analyzer/util.server"
import { log, setLogLevel } from "./analyzer/log.server"
async function main() {
const args = parseArgs()
if (args?.log) {
setLogLevel(args.log as string)
}
const options = getArgsWithDefaults()
const currentV = pkg.version
let updateMessage = ""
try {
const latestV = await latestVersion(pkg.name)
// Soft clear the console
process.stdout.write("\u001b[2J\u001b[0;0H")
console.log()
updateMessage =
latestV && semverCompare(latestV, currentV) === 1
? ` [!] Update available: ${latestV}
To update, run:
npx git-truck@latest
Or to install globally:
npm install -g git-truck@latest
`
: " (latest)"
} catch (e) {
// ignore
}
console.log(`Git Truck Beta version ${currentV}${updateMessage}\n`)
if (args.h || args.help) {
console.log()
console.log(`See
${pkg.homepage}
for usage instructions.`)
console.log()
process.exit(0)
}
const getPortLib = await import("get-port")
const getPort = getPortLib.default
const port = await getPort({
port: [...getPortLib.portNumbers(3000, 4000)],
})
// Serve application build
const onListen = async () => {
const url = `http://localhost:${port}`
const [extension, extensionError] = await describeAsyncJob(
async () => {
// If CWD or path argument is a git repo, go directly to that repo in the visualizer
if (await GitCaller.isGitRepo(options.path)) {
const repo = await GitCaller.getRepoMetadata(options.path)
if (repo) {
return `/${getPathFromRepoAndHead(repo.name, repo.currentHead)}`
} else return ""
}
},
"Checking for git repo",
"Done checking for git repo",
"Failed to check for git repo"
)
if (extensionError) {
console.error(extensionError)
}
if (process.env.NODE_ENV !== "development") {
const openURL = url + (extension ?? "")
log.debug(`Opening ${openURL}`)
let err : Error | null = null
if (!args.headless) {
[, err] = await describeAsyncJob(
() => open(openURL),
"Opening Git Truck Beta in your browser",
`Successfully opened Git Truck Beta in your browser`,
`Failed to open Git Truck Beta in your browser. To continue, open this link manually:\n\n${openURL}`
)
}
if (!err) console.log(`\nApplication available at ${url}`)
}
}
describeAsyncJob(
async () => {
const app = createApp(
path.join(__dirname, "build"),
process.env.NODE_ENV ?? "production",
"/build",
path.join(__dirname, "public", "build")
)
const server = process.env.HOST ? app.listen(port, process.env.HOST, onListen) : app.listen(port, onListen)
;["SIGTERM", "SIGINT"].forEach((signal) => {
process.once(signal, () => server?.close(console.error))
})
},
"Starting Git Truck",
"Git Truck Beta started",
"Failed to start Git Truck"
)
}
main()