-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcli.js
More file actions
executable file
ยท165 lines (140 loc) ยท 3.31 KB
/
cli.js
File metadata and controls
executable file
ยท165 lines (140 loc) ยท 3.31 KB
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
#!/usr/bin/env node
"use strict";
const meow = require("meow");
const envFile = `${__dirname}/env.json`;
const env = require(envFile);
const term = require("terminal-kit").terminal;
const iconv = require("iconv-lite");
iconv.skipDecodeWarning = true;
const moment = require("moment");
const opn = require("opn");
const { getBullets, addBullet } = require("./requests");
const { updateSettings } = require("./utils");
const cli = meow(
`
๐ Usage:
$ journal Your message
๐ Options:
--site Open the web app
--help Show the help of your journal
--key Set the API key for this CLI
--api Set the API url for this CLI
--list Read your journal from this CLI
๐ Examples:
$ journal I learned how to create a CLI
$ journal --key Mrq5wMtD1yMX6gh694ORhgtxaYZtkAFWX3pko4Do
`,
{
flags: {
site: {
type: "boolean",
alias: "s",
isMultiple: false,
},
key: {
type: ["string", "boolean"],
alias: "k",
isMultiple: false,
},
api: {
type: ["string", "boolean"],
alias: "a",
isMultiple: false,
},
},
}
);
/**
* Set the key
*/
if (cli.flags.site) {
opn(env.api.replace("/api", ""));
term.green("๐ Opening the webapp linked to that CLI...\n");
return;
}
/**
* Set the key
*/
if (cli.flags.key) {
if (cli.flags.key === "" || cli.flags.key === true) {
return term.red("๐ You must specify a key using the --key parameter!\n");
}
env.key = cli.flags.key;
updateSettings(
envFile,
env,
"๐ Your API key is set, you are ready to use the Journapi CLI!\n",
"๐ An error occured while setting your API key. Maybe try again?\n"
);
return;
}
/**
* Set the API url
*/
if (cli.flags.api) {
if (cli.flags.api === "" || cli.flags.api === true) {
return term.red("๐ You must specify an url using the --api parameter!\n");
}
env.api = cli.flags.api;
updateSettings(
envFile,
env,
"๐ Your API url is set, you are ready to use the Journapi CLI!\n",
"๐ An error occured while setting your API url. Maybe try again?\n"
);
return;
}
/**
* Check if the key is set before continuing
*/
if (!env.key || env.key === "") {
term.red("๐ You need to specify an API key before you use this CLI.\n");
return;
}
/**
* Set the API url
*/
if (cli.flags.list) {
getBullets()
.then((r) => {
const data = r.data.data;
term.green("๐ Here is your journal:\n\n");
for (const day of Object.keys(data)) {
const bullets = data[day];
term.green(`๐
${day}\n`);
for (const bullet of bullets) {
const text = iconv.decode(bullet.bullet, "utf-8");
const date = moment(bullet.published_at).format("HH:mm:ss");
term(`๐ `).blue(date)(` > ${text}\n`);
}
}
})
.catch((e) => {
console.log(e);
term.red(
"๐ An error occured retrieving your journal. Maybe try again?\n"
);
});
return;
}
/**
* Add a bullet
*/
if (cli.input.length > 0) {
const message = cli.input.join(" ");
addBullet(message)
.then((s) => {
term.green(`๐ The following bullet has been added to your journal:\n`);
term.green(`"${message}"\n`);
})
.catch((e) => {
console.log(e);
term.red(
"๐ An error occured while saving your bullet into your journal. Maybe try again?\n"
);
});
return;
} else {
term.red("๐ You need to specify a message for your bullet.\n");
return;
}