|
| 1 | +import { exit } from 'process'; |
| 2 | +import { readFileSync, existsSync, writeFileSync } from 'fs'; |
| 3 | +import { input } from '@inquirer/prompts'; |
| 4 | + |
| 5 | +if (!existsSync('config.jsonc')) { |
| 6 | + console.log('config.jsonc not found! Are you in the right directory?'); |
| 7 | + exit(1); |
| 8 | +} |
| 9 | + |
| 10 | +(async () => { |
| 11 | + const gameName = await input({ message: 'Enter the display name of the game\n>' }); |
| 12 | + const gamePath = await input({ message: 'Enter the path from games directory to the game:\n>' }); |
| 13 | + const gameAliases = await input({ message: 'Enter comma separated list of aliases for the game:\n>' }); |
| 14 | + const gameAliasesList = gameAliases.split(','); |
| 15 | + const gameCategories = await input({ message: 'Enter comma separated list of categories for the game:\n>' }); |
| 16 | + const gameCategoriesList = gameCategories.split(','); |
| 17 | + const newKey = `"${gameName}": { |
| 18 | + "path": "${gamePath}", |
| 19 | + "aliases": [${gameAliasesList.toString()}], |
| 20 | + "categories": [${gameCategoriesList.toString()}] |
| 21 | + }, |
| 22 | + `; |
| 23 | + let jsonc = readFileSync('config.jsonc', 'utf-8'); |
| 24 | + |
| 25 | + // searching only in games key value |
| 26 | + let i = jsonc.search(/"games":\s*{/); |
| 27 | + const end = jsonc.slice(i).search(/}[,\s]*}/) + i; |
| 28 | + i = jsonc.slice(i).indexOf('{') + i + 1; |
| 29 | + |
| 30 | + let curlyBracketDepth = 0; |
| 31 | + while (i < end) { |
| 32 | + const char = jsonc.charAt(i); |
| 33 | + const nextChar = jsonc.charAt(i + 1); |
| 34 | + // skipping comments |
| 35 | + if (char === '/') { |
| 36 | + if (nextChar === '/') { |
| 37 | + i = jsonc.indexOf('\n', i); |
| 38 | + continue; |
| 39 | + } else if (nextChar === '*') { |
| 40 | + i = jsonc.indexOf('*/', i + 2) + 1; |
| 41 | + continue; |
| 42 | + } |
| 43 | + } else if (char === '"' || char === "'") { |
| 44 | + // nextJump is index of next unescaped quote (single or double depending on starting quote) |
| 45 | + let nextJump; |
| 46 | + if (char === '"') { |
| 47 | + nextJump = jsonc.indexOf('"', i + 1); |
| 48 | + while (jsonc.charAt(nextJump - 1) === '\\') nextJump = jsonc.indexOf('"', nextJump + 1); |
| 49 | + } else { |
| 50 | + nextJump = jsonc.indexOf("'", i + 1); |
| 51 | + while (jsonc.charAt(nextJump - 1) === '\\') nextJump = jsonc.indexOf("'", nextJump + 1); |
| 52 | + } |
| 53 | + if (nextJump === -1 || nextJump > end) { |
| 54 | + console.log('Error while parsing file: Invalid JSONC data'); |
| 55 | + exit(1); |
| 56 | + } |
| 57 | + |
| 58 | + if (curlyBracketDepth === 0) { |
| 59 | + const selectedGameName = jsonc.substring(i + 1, nextJump); |
| 60 | + const comparison = selectedGameName.localeCompare(gameName); |
| 61 | + |
| 62 | + // selectedGameName first in alphanumerical order |
| 63 | + if (comparison === -1) { |
| 64 | + i = nextJump + 1; |
| 65 | + continue; |
| 66 | + } |
| 67 | + |
| 68 | + if (comparison === 0) { |
| 69 | + console.log(`Error while parsing file: game "${gameName}" is already added as an entry.`); |
| 70 | + exit(1); |
| 71 | + } |
| 72 | + |
| 73 | + if (comparison === 1) { |
| 74 | + jsonc = jsonc.substring(0, i) + newKey + jsonc.substring(i); |
| 75 | + writeFileSync('config.jsonc', jsonc); |
| 76 | + console.log('Saved to config.jsonc!'); |
| 77 | + exit(0); |
| 78 | + } |
| 79 | + |
| 80 | + // comparison only returns 1,0,-1 so the code should not reach this point |
| 81 | + console.log('Error while parsing file: comparison failed'); |
| 82 | + exit(1); |
| 83 | + } |
| 84 | + |
| 85 | + i = nextJump; |
| 86 | + } else if (char === '{') { |
| 87 | + ++curlyBracketDepth; |
| 88 | + } else if (char === '}') { |
| 89 | + --curlyBracketDepth; |
| 90 | + } |
| 91 | + |
| 92 | + ++i; |
| 93 | + } |
| 94 | + |
| 95 | + // now i = end, no error occured but game has not been added to config data |
| 96 | + // game display name might be last in alphanumerical order so append the entry to the end |
| 97 | + jsonc = |
| 98 | + jsonc.substring(0, end + 1) + |
| 99 | + ',\n ' + |
| 100 | + newKey.substring(0, newKey.lastIndexOf('\n')) + |
| 101 | + jsonc.substring(end + 1); |
| 102 | + |
| 103 | + writeFileSync('config.jsonc', jsonc); |
| 104 | + console.log('Saved to config.jsonc!'); |
| 105 | + exit(0); |
| 106 | +})(); |
0 commit comments