-
Notifications
You must be signed in to change notification settings - Fork 2
/
cli.ts
90 lines (71 loc) · 2.41 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
import { green, yellow, parseFlags } from './deps.ts'
import {
isGitRepository,
isTreeClean,
gitAdd,
gitCommit,
gitStatus,
} from './src/helpers/git.ts'
import { log } from './src/helpers/log.ts'
import { CliError } from './src/helpers/error.ts'
import {
typePrompt,
commitPrompt,
stagedPrompt,
} from './src/helpers/prompts.ts'
import { CLI_VERSION } from './src/constants.ts'
// Learn more at https://deno.land/manual/examples/module_metadata#concepts
if (import.meta.main) {
const { flags } = parseFlags(Deno.args)
if (flags.version) {
console.log(CLI_VERSION)
Deno.exit()
}
console.log(green('-'.repeat(50)))
console.log()
console.log(`Cocli - ${CLI_VERSION} 🌱🚀`)
console.log('Press Ctrl/Cmd + C, type help for more info')
console.log()
console.log(green('-'.repeat(50)))
await isGitRepository()
await isTreeClean()
const typeVal = await typePrompt()
const commitVal = await commitPrompt()
try {
const commit = `${typeVal}: ${commitVal}`
const { stdoutStatus, stderrStatus } = await gitStatus()
if (stderrStatus) throw new CliError(`An error occured: ${stderrStatus}`)
if (
stdoutStatus.includes('no changes added to commit') ||
stdoutStatus.includes(
'nothing added to commit but untracked files present'
)
) {
const type = stdoutStatus.includes('no changes added to commit')
? 'modified'
: 'untracked'
const addStagedFiles = await stagedPrompt(type)
if (addStagedFiles) {
const { stderrAdd } = await gitAdd()
if (stderrAdd) throw new CliError(`An error occured: ${stderrAdd}`)
const { stderrCommit } = await gitCommit({ commit: commit.trim() })
if (stderrCommit)
throw new CliError(`An error occured: ${stderrCommit}`)
console.log(green('-'.repeat(50)))
console.log()
console.log(green("You're all set 🎉"))
console.log(yellow('use "git push" to publish your local commits 🚀'))
}
Deno.exit(1)
}
const { stderrCommit } = await gitCommit({ commit: commit.trim() })
if (stderrCommit) throw new CliError(`An error occured: ${stderrCommit}`)
console.log(green('-'.repeat(50)))
console.log()
console.log(green("You're all set 🎉"))
console.log(yellow('use "git push" to publish your local commits 🚀'))
} catch (err) {
log({ type: 'error', msg: err.message })
Deno.exit(1)
}
}