PageRenderTime 61ms CodeModel.GetById 30ms RepoModel.GetById 0ms app.codeStats 0ms

/scripts/generate-session-object.js

https://bitbucket.org/raphox/slack-history
JavaScript | 131 lines | 104 code | 20 blank | 7 comment | 23 complexity | 7f75673c529accc1aba251a5bfddc74d MD5 | raw file
  1. const argv = require('argv');
  2. const fs = require('fs');
  3. const path = require('path');
  4. const readline = require('readline');
  5. const jsonfile = require('jsonfile');
  6. const marked = require('marked');
  7. const uuid = require('uuid/v4');
  8. const options = [{
  9. name: 'path',
  10. short: 'p',
  11. type: 'path',
  12. description: "Defines an path to load session file. Session file is a TXT file with CTRL+C/V from a Slack's channel.",
  13. example: "'script --path=value' or 'script -p value'"
  14. }];
  15. var args = argv.option(options).run();
  16. if (!args.options.path) {
  17. console.log("Error: 'path' argument is required.");
  18. console.log("Eg:" + options.find(item => item.name == 'path').example)
  19. } else {
  20. const file_path = args.options.path;
  21. if (fs.existsSync(file_path)) {
  22. console.log('File found.');
  23. const destination = "./public/sessions";
  24. !fs.existsSync(destination) && fs.mkdirSync(destination);
  25. let obj = {
  26. info: {
  27. details: "",
  28. highlights: {},
  29. authors: {}
  30. },
  31. messages: []
  32. }
  33. let messages = [];
  34. const regex = /^(.+)\[(.+)\]$/;
  35. const rl = readline.createInterface({
  36. input: fs.createReadStream(file_path),
  37. crlfDelay: Infinity
  38. });
  39. rl.on('line', (line) => {
  40. // discard empty lines
  41. if (line) messages.push(line);
  42. }).on('close', () => {
  43. let j = 0;
  44. let author = time = "";
  45. let message, last_message, data_match;
  46. for (let i = 0; i < messages.length; i++) {
  47. message = messages[i];
  48. if (message != '---') {
  49. last_message = messages[i - 1];
  50. data_match = regex.exec(message);
  51. if (data_match) {
  52. author = data_match[1].trim();
  53. time = data_match[2].trim();
  54. obj.info.authors[author] = true;
  55. if (!last_message || last_message == '---') {
  56. obj.messages[j] = {
  57. id: uuid(),
  58. img: null,
  59. username: author,
  60. time: time,
  61. msg: marked(messages[++i].trim()),
  62. messages: []
  63. };
  64. }
  65. } else {
  66. highlights_terms(message, obj.info.highlights);
  67. let last_message = obj.messages[j].messages[obj.messages[j].messages.length - 1];
  68. if (!last_message || last_message.username != author) {
  69. obj.messages[j].messages.push({
  70. id: uuid(),
  71. img: null,
  72. username: author,
  73. time: time,
  74. msg: marked(message.trim())
  75. });
  76. } else {
  77. last_message.msg += marked(message.trim());
  78. }
  79. }
  80. } else {
  81. j++;
  82. }
  83. }
  84. obj.info.authors = Object.keys(obj.info.authors).sort();
  85. obj.info.highlights = Object.keys(obj.info.highlights)
  86. .sort((a, b) => {
  87. return obj.info.highlights[b] - obj.info.highlights[a]
  88. })
  89. .reduce((a, v) => {
  90. a[v] = obj.info.highlights[v];
  91. return a;
  92. }, {});
  93. jsonfile.writeFileSync(path.join(destination, path.basename(file_path, '.txt') + '.json'), obj, { spaces: 2 });
  94. });
  95. } else {
  96. console.log("Error: File in 'path' not found.");
  97. }
  98. }
  99. /**
  100. * Process the message and count the times a important word occurs
  101. *
  102. * @param {string} message
  103. * @param {object} highlights
  104. */
  105. const highlights_terms = (message, highlights = {}) => {
  106. const terms = ["react", "redux", "pacote", "package", "github", "bitbucket", "repositorio", "repository", "native", "mobile", "desktop", "ios", "android", "windows", "linux", "web", "animations", "cocoapod", "gradle", "open source", "async", "apollo", "relay", "graphql", "meteor", "styled", "components", "backend", "frontend", "fullstack", "form", "ui", "ux", "css", "typescript", "babel", "thread", "jsx", "reasonml", "ssr", "spa", "pwa", "book"];
  107. for (let term of terms) {
  108. let count = message.toLowerCase().split(term).length - 1 + (highlights[term] || 0);
  109. if (count > 0) highlights[term] = count;
  110. }
  111. };