-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
92 lines (81 loc) · 3.21 KB
/
index.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
#!/usr/bin/env node
import {Command} from 'commander';
import * as crypto from "crypto";
import {PostMessageUseCase} from "./src/application/usecases/post-message.usecase";
import {EditMessageCommand, PostMessageCommand} from "./src/application/domain/message";
import {FileSystemMessageRepository} from "./src/infra/message.fs.repository";
import {ViewTimelineUsecase} from "./src/application/usecases/view-timeline.usecase";
import {EditMessageUseCase} from "./src/application/usecases/edit-message.usecase";
import {RealDateProvider} from "./src/infra/real-date-provider";
const program = new Command();
const messageRepository = new FileSystemMessageRepository();
const dateProvider = new RealDateProvider();
const postMessageUseCase = new PostMessageUseCase(messageRepository, dateProvider)
const editMessageUseCase = new EditMessageUseCase(messageRepository)
const viewTimelineUseCase = new ViewTimelineUsecase(messageRepository, dateProvider)
program
.version('1.0.0')
.description('Thruid CLI')
.addCommand(
new Command('post')
.argument('<user>', 'the current user')
.argument('<message>', 'the message to post')
.action(async (user, message) => {
const postMessageCommand: PostMessageCommand = {
id: crypto.randomBytes(16).toString('hex'),
text: message,
author: user
}
try {
await postMessageUseCase.handle(postMessageCommand);
console.log('Message posted')
// console.table([messageRepository.message])
process.exit(0)
} catch (e) {
console.log(e.message, "<---- error")
process.exit(1)
}
})
.alias('p')
)
.addCommand(
new Command('edit')
.argument('<message-id>', 'the message id of the message to edit')
.argument('<message>', 'the next message')
.action(async (messageId, message) => {
const editMessageCommand: EditMessageCommand = {
id: messageId,
text: message,
}
try {
await editMessageUseCase.handle(editMessageCommand);
console.log('Message are edited successfully')
// console.table([messageRepository.message])
process.exit(0)
} catch (e) {
console.log(e.message, "<---- error")
process.exit(1)
}
})
.alias('p')
)
.addCommand(
new Command('view')
.argument('<user>', 'the current user')
.action(async (user) => {
try {
const timeline = await viewTimelineUseCase.handle(user)
console.table(timeline)
process.exit(0)
} catch (e) {
console.log(e.message, "<---- error")
process.exit(1)
}
})
.alias('v')
)
;
async function main() {
await program.parseAsync(process.argv);
}
void main()