forked from monkeytypegame/monkeytype-bot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
insertQuote.js
82 lines (74 loc) · 1.81 KB
/
insertQuote.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
const fs = require("fs");
const simpleGit = require("simple-git");
const git = simpleGit("../../monkeytype");
let arguments = process.argv.slice(2);
//quote content
//quote source
//quote language
if (arguments.length !== 3) {
console.log(
JSON.stringify({
status: false,
message: "Exactly 3 arguments expected.",
})
);
return;
}
try {
let quoteObject = {
text: arguments[0],
source: arguments[1],
length: arguments[0].length,
};
let returnValue = {
status: false,
message: "No message",
};
const fileDir = `../../monkeytype/static/quotes/${arguments[2]}.json`;
if (fs.existsSync(fileDir)) {
let quoteFile = fs.readFileSync(fileDir);
quoteFile = JSON.parse(quoteFile.toString());
let newid =
Math.max.apply(
Math,
quoteFile.quotes.map(function (q) {
return q.id;
})
) + 1;
quoteObject.id = newid;
quoteFile.quotes.push(quoteObject);
returnValue.status = true;
returnValue.message = `Added quote to ${arguments[2]}.json.`;
} else {
//file doesnt exist, create it
quoteObject.id = 1;
fs.writeFileSync(
fileDir,
JSON.stringify({
language: arguments[2],
groups: [
[0, 100],
[101, 300],
[301, 600],
[601, 9999],
],
quotes: [quoteObject],
})
);
returnValue.status = true;
returnValue.message = `Created file ${arguments[2]}.json and added quote.`;
}
git.pull("upstream", "master");
git.add([`static/quotes/${arguments[2]}.json`]);
git.commit(`Added quote to ${arguments[2]}.json`);
git.push("origin", "master");
console.log(JSON.stringify(returnValue));
} catch (e) {
console.log(
JSON.stringify({
status: false,
message: `Something went wrong: ${e}`,
})
);
return;
}