-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcli.js
160 lines (155 loc) · 5.28 KB
/
cli.js
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
#!/usr/bin/env node
var removeMd = require('remove-markdown');
var WebSocket = require('ws');
function help() {
console.log("Usage: candy <command> [params]");
console.log("Commands:");
console.log("\treference <referencePath>");
console.log("\tDisplays the specified reference.");
console.log("\t\treferencePath");
console.log("\t\tPath of specified reference (ex. /helloWorld, /helloCatalog/helloWorld)");
console.log("");
console.log("\tlist <catalogPath>");
console.log("\tLists and seperates catalogs and references in the specified catalog.");
console.log("\t\tcatalogPath");
console.log("\t\tPath of specified catalog (ex. /, /helloCatalog/)");
console.log("");
console.log("\tcreate <reference|catalog> <path> [content]");
console.log("\tCreates a reference or catalog.");
console.log("\t\tpath");
console.log("\t\tPath of specified reference or catalog. (ex. /helloWorld, /helloCatalog/)");
console.log("\t\tcontent");
console.log("\t\tNew content of specified reference. (ex. \"# Hello World\\n\\nHello world!\\n\")");
console.log("");
console.log("\tedit <path> <content>");
console.log("\tEdits and existing reference.");
console.log("\t\tpath");
console.log("\t\tPath of specified reference or catalog. (ex. /helloWorld, /helloCatalog/)");
}
var ws = new WebSocket(process.env.CANDY_URL || 'ws://localhost:4540');
if (!process.argv[2]) {
help();
process.exit(0);
}
ws.on('open', function() {
switch(process.argv[2]) {
case "reference":
if (!process.argv[3]) {
console.log("Parameters not met.");
help();
process.exit(1);
}
ws.send(JSON.stringify({
type: "reference",
path: process.argv[3]
}));
break;
case "list":
if (!process.argv[3]) {
console.log("Parameters not met.");
help();
process.exit(1);
}
ws.send(JSON.stringify({
type: "list",
path: process.argv[3]
}));
break;
case "create":
if (!process.argv[3] || !process.argv[4]) {
console.log("Parameters not met.");
help();
process.exit(1);
}
switch (process.argv[3]) {
case "reference":
ws.send(JSON.stringify({
type: "newReference",
path: process.argv[4],
data: process.argv[5]
}));
break;
case "catalog":
ws.send(JSON.stringify({
type: "newCatalog",
path: process.argv[4]
}));
break;
default:
console.log("Unknown subcommand.");
help();
process.exit(1);
}
break;
case "edit":
if (!process.argv[3] || !process.argv[4]) {
console.log("Parameters not met.");
help();
process.exit(1);
}
ws.send(JSON.stringify({
type: "edit",
path: process.argv[3],
data: process.argv[4]
}));
break;
default:
console.log("Unknown command.");
help();
process.exit(1);
}
});
ws.on('message', function(data) {
switch(JSON.parse(data).type) {
case "reference":
var reference = JSON.parse(data).reference;
if (reference) {
console.log(removeMd(reference, {
stripListLeaders: true,
listUnicodeChar: '*',
gfm: true
}));
} else {
console.log("Reference not found.");
}
break;
case "list":
var items = JSON.parse(data).items;
if (items) {
console.log(`References: ${items.references.join(', ')}`);
console.log(`Catalogs: ${items.catalogs.join(', ')}`);
} else {
console.log("Catalog not found.");
}
break;
case "newReference":
var path = JSON.parse(data).path;
if (path) {
console.log("Reference created.");
} else {
console.log("Reference couldn't be created. (Missing parents.)");
}
break;
case "newCatalog":
var path = JSON.parse(data).path;
if (path) {
console.log("Catalog created.");
} else {
console.log("Catalog couldn't be created. (Missing parents.)");
}
break;
case "edit":
var path = JSON.parse(data).path;
if (path) {
console.log("File edited successfully.");
} else {
console.log("File couldn't be edited. (Missing parents.)");
}
break;
default:
console.log("Unknown command.");
help();
process.exit(1);
}
ws.close();
});