jevstrudel.git / tools / dbpatch / dbpatch.mjs
1import { parse } from 'csv-parse/sync';
2import { readFileSync } from 'fs';
3import { stringify } from 'csv-stringify/sync';
4
5const hasCpsCall = (code) =>
6  ['setcps', 'setCps', 'setCpm', 'setcpm'].reduce((acc, m) => acc || code.includes(`${m}`), false);
7
8function withCps(code, cps) {
9  if (hasCpsCall(code)) {
10    return code;
11  }
12  const lines = code.split('\n');
13  const firstNonLineComment = lines.findIndex((l) => !l.startsWith('//'));
14  const cpsCall = `setcps(${cps})`;
15  lines.splice(firstNonLineComment, 0, cpsCall);
16  return lines.join('\n');
17}
18
19const dumpNew = readFileSync('./code_rows.csv', { encoding: 'utf-8' });
20
21const records = parse(dumpNew, {
22  columns: true,
23  skip_empty_lines: true,
24});
25
26const edited = records.map((entry) => ({
27  ...entry,
28  code: withCps(entry.code, 1),
29}));
30
31console.log(stringify(edited));