-
Notifications
You must be signed in to change notification settings - Fork 814
Expand file tree
/
Copy pathadd-game-entry.js
More file actions
119 lines (102 loc) · 4.47 KB
/
add-game-entry.js
File metadata and controls
119 lines (102 loc) · 4.47 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
import { exit } from 'process';
import { fork } from 'child_process'
import { readFileSync, existsSync, writeFileSync } from 'fs';
import { input, confirm } from '@inquirer/prompts';
if (!existsSync('config.jsonc')) {
console.log('config.jsonc not found! Are you in the right directory?');
exit(1);
}
(async () => {
const gameName = await input({ message: 'Enter the display name of the game\n>' });
const gamePath = await input({ message: 'Enter the path from games directory to the game:\n>' });
const gameAliases = await input({ message: 'Enter comma separated list of aliases for the game:\n>' });
const gameAliasesList = gameAliases.split(',');
const gameCategories = await input({ message: 'Enter comma separated list of categories for the game:\n>' });
const gameCategoriesList = gameCategories.split(',');
const newKey = `"${gameName}": {
"path": "${gamePath}",
"aliases": [${gameAliasesList.toString()}],
"categories": [${gameCategoriesList.toString()}]
},
`;
let jsonc = readFileSync('config.jsonc', 'utf-8');
// searching only in games key value
let i = jsonc.search(/"games":\s*{/);
const end = jsonc.slice(i).search(/}[,\s]*}/) + i;
i = jsonc.slice(i).indexOf('{') + i + 1;
let curlyBracketDepth = 0;
while (i < end) {
const char = jsonc.charAt(i);
const nextChar = jsonc.charAt(i + 1);
// skipping comments
if (char === '/') {
if (nextChar === '/') {
i = jsonc.indexOf('\n', i);
continue;
} else if (nextChar === '*') {
i = jsonc.indexOf('*/', i + 2) + 1;
continue;
}
} else if (char === '"' || char === "'") {
// nextJump is index of next unescaped quote (single or double depending on starting quote)
let nextJump;
if (char === '"') {
nextJump = jsonc.indexOf('"', i + 1);
while (jsonc.charAt(nextJump - 1) === '\\') nextJump = jsonc.indexOf('"', nextJump + 1);
} else {
nextJump = jsonc.indexOf("'", i + 1);
while (jsonc.charAt(nextJump - 1) === '\\') nextJump = jsonc.indexOf("'", nextJump + 1);
}
if (nextJump === -1 || nextJump > end) {
console.log('Error while parsing file: Invalid JSONC data');
exit(1);
}
if (curlyBracketDepth === 0) {
const selectedGameName = jsonc.substring(i + 1, nextJump);
const comparison = selectedGameName.localeCompare(gameName);
// selectedGameName first in alphanumerical order
if (comparison === -1) {
i = nextJump + 1;
continue;
}
if (comparison === 0) {
console.log(`Error while parsing file: game "${gameName}" is already added as an entry.`);
exit(1);
}
if (comparison === 1) {
jsonc = jsonc.substring(0, i) + newKey + jsonc.substring(i);
writeFileSync('config.jsonc', jsonc);
console.log('Saved to config.jsonc!');
exit(0);
}
// comparison only returns 1,0,-1 so the code should not reach this point
console.log('Error while parsing file: comparison failed');
exit(1);
}
i = nextJump;
} else if (char === '{') {
++curlyBracketDepth;
} else if (char === '}') {
--curlyBracketDepth;
}
++i;
}
// now i = end, no error occured but game has not been added to config data
// game display name might be last in alphanumerical order so append the entry to the end
jsonc =
jsonc.substring(0, end + 1) +
',\n ' +
newKey.substring(0, newKey.lastIndexOf('\n')) +
jsonc.substring(end + 1);
writeFileSync('config.jsonc', jsonc);
console.log('Saved to config.jsonc!');
const shouldRebuildConfig = await confirm({ message: 'Do you want to rebuild the config now? (the site will not see the game until you do this)' });
if (shouldRebuildConfig) {
if (!existsSync('build-config.js')) {
console.log("`build-config.js` not found! Are you in the right directory?");
} else {
fork("build-config.js");
};
}
exit(0);
})();