reassign-ids.js 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. // this reassigns all jokes' IDs. Always run this after something changes in the joke's order
  2. // run this with the command "npm run reassign-ids"
  3. const { resolve, join } = require("path");
  4. const fs = require("fs-extra");
  5. const settings = require("../settings");
  6. function reassignIds()
  7. {
  8. try
  9. {
  10. console.log(`\nReassigning joke IDs in files in path "${settings.jokes.jokesFolderPath}"...`);
  11. let totalReassignedFiles = 0;
  12. let totalReassignedIDs = 0;
  13. fs.readdirSync(settings.jokes.jokesFolderPath).forEach(fName => {
  14. if(fName.startsWith("template"))
  15. return;
  16. totalReassignedFiles++;
  17. let fPath = resolve(join(settings.jokes.jokesFolderPath, fName));
  18. let jokeFileObj = JSON.parse(fs.readFileSync(fPath).toString());
  19. let initialJokes = jokeFileObj.jokes;
  20. let initialInfo = jokeFileObj.info;
  21. let reassignedJokes = [];
  22. if(initialInfo.formatVersion != settings.jokes.jokesFormatVersion)
  23. initialInfo.formatVersion = settings.jokes.jokesFormatVersion;
  24. initialJokes.forEach((joke, i) => {
  25. let rJoke = joke;
  26. rJoke.id = i;
  27. reassignedJokes.push(rJoke);
  28. totalReassignedIDs++;
  29. });
  30. let doneFile = {
  31. info: initialInfo,
  32. jokes: reassignedJokes
  33. };
  34. fs.writeFileSync(fPath, JSON.stringify(doneFile, null, 4));
  35. });
  36. console.log(`\x1b[32m\x1b[1mDone reassigning IDs of all ${totalReassignedFiles} files (${totalReassignedIDs} reassigned joke IDs).\n\x1b[0m`);
  37. process.exit(0);
  38. }
  39. catch(err)
  40. {
  41. console.log(`\n\n\x1b[31m\x1b[1m>> Error while reassigning joke IDs:\n${err}\n\n\x1b[0m`);
  42. process.exit(1);
  43. }
  44. }
  45. reassignIds();