reformat.js 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. // this reformats jokes from format v2 to format v3
  2. // run this with the command "npm run reformat"
  3. const fs = require("fs-extra");
  4. const isEmpty = require("svjsl").isEmpty;
  5. try
  6. {
  7. console.log(`\nReformatting jokes from file "./data/jokes.json" to new format and putting it in file "./data/jokes_new.json"...`);
  8. let initialJokes = JSON.parse(fs.readFileSync("./data/jokes.json").toString());
  9. let newJokes = [];
  10. let id = 0;
  11. initialJokes.jokes.forEach(joke => {
  12. if(joke.type == "single") newJokes.push({
  13. category: joke.category,
  14. type: "single",
  15. joke: joke.joke,
  16. flags: {
  17. nsfw: isEmpty(joke.flags.nsfw) ? false : true,
  18. racist: isEmpty(joke.flags.racist) ? false : true,
  19. sexist: isEmpty(joke.flags.sexist) ? false : true,
  20. religious: isEmpty(joke.flags.religious) ? false : true,
  21. political: isEmpty(joke.flags.political) ? false : true,
  22. explicit: isEmpty(joke.flags.explicit) ? false : true
  23. },
  24. id: id
  25. });
  26. if(joke.type == "twopart") newJokes.push({
  27. category: joke.category,
  28. type: "twopart",
  29. setup: joke.setup,
  30. delivery: joke.delivery,
  31. flags: {
  32. nsfw: isEmpty(joke.flags.nsfw) ? false : true,
  33. racist: isEmpty(joke.flags.racist) ? false : true,
  34. sexist: isEmpty(joke.flags.sexist) ? false : true,
  35. religious: isEmpty(joke.flags.religious) ? false : true,
  36. political: isEmpty(joke.flags.political) ? false : true,
  37. explicit: isEmpty(joke.flags.explicit) ? false : true
  38. },
  39. id: id
  40. });
  41. id++;
  42. });
  43. let doneFile = {
  44. "info": {
  45. "formatVersion": 3
  46. },
  47. "jokes": newJokes
  48. };
  49. fs.writeFileSync("./data/jokes_new.json", JSON.stringify(doneFile, null, 4));
  50. console.log(`\x1b[32m\x1b[1mDone reformatting all ${newJokes.length} jokes.\x1b[0m\n`);
  51. process.exit(0);
  52. }
  53. catch(err)
  54. {
  55. console.log(`\n\n\x1b[31m\x1b[1m>> Error while reformatting jokes:\n${err}\n\n\x1b[0m`);
  56. process.exit(1);
  57. }