locales.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. var fs = require('fs'),
  2. path = require('path'),
  3. https = require('https');
  4. var localeDir = path.join('src', 'locale');
  5. var args = process.argv.slice(2);
  6. function help() {
  7. console.log(process.argv[1], '[list|mention|find-commenters] ARGS');
  8. console.log();
  9. console.log(' list show all authors in all locales');
  10. console.log(' mention show all authors in all locales, ready to copy-paste in github issue');
  11. console.log(' find-commenters #ID finds all people that participated in a github conversation');
  12. }
  13. function extract() {
  14. var authors = {};
  15. fs.readdirSync(localeDir).forEach(function (locale) {
  16. var content = fs.readFileSync(path.join(localeDir, locale), {encoding: 'utf-8'}),
  17. localeCode = locale.split('.')[0],
  18. localeAuthors = [];
  19. content.split('\n').forEach(function (line) {
  20. var match = line.match(/^\/\/! author.*github[.]com\/(.*)$/);
  21. if (match !== null) {
  22. // console.log(" ", line);
  23. localeAuthors.push(match[1]);
  24. }
  25. });
  26. if (localeAuthors.length === 0) {
  27. // use to debug
  28. content.split('\n').forEach(function (line) {
  29. var match = line.match(/^\/\/! author(.*)$/);
  30. if (match !== null) {
  31. localeAuthors.push('---' + match[1]);
  32. }
  33. });
  34. console.log(localeCode, localeAuthors);
  35. } else {
  36. authors[localeCode] = localeAuthors;
  37. }
  38. });
  39. return authors;
  40. }
  41. function list() {
  42. var authors = extract();
  43. Object.keys(authors).forEach(function (localeCode) {
  44. console.log(localeCode, authors[localeCode]);
  45. });
  46. }
  47. function mention() {
  48. var authors = extract();
  49. Object.keys(authors).forEach(function (localeCode) {
  50. console.log('- [ ]', localeCode, authors[localeCode].map(function (author) { return '@' + author; }).join(' '));
  51. });
  52. }
  53. function findCommenters(postId) {
  54. function fetchComments(page, callback) {
  55. var options = {
  56. hostname: 'api.github.com',
  57. port: 443,
  58. path: '/repos/moment/moment/issues/' + postId + '/comments?page=' + page,
  59. method: 'GET',
  60. headers: {
  61. 'User-Agent': 'node script'
  62. }
  63. },
  64. links = {};
  65. console.log('fetching', options.path);
  66. https.get(options, function (res) {
  67. if ('link' in res.headers) {
  68. res.headers.link.split(', ').forEach(function(linkStr) {
  69. var pieces = linkStr.split('; ');
  70. var key = pieces[1].split('=')[1];
  71. var link = pieces[0];
  72. key = key.substr(1, key.length - 2);
  73. link = link.substr(1, link.length - 2);
  74. links[key] = link;
  75. });
  76. }
  77. var bodyChunks = [], body;
  78. res.on('data', function (chunk) {
  79. bodyChunks.push(chunk);
  80. });
  81. res.on('end', function () {
  82. body = bodyChunks.join('');
  83. callback(page, JSON.parse(body), links);
  84. });
  85. });
  86. }
  87. var commenters = {};
  88. var maxPage = 1;
  89. fetchComments(1, function (page, body, links) {
  90. handleBody(body, page);
  91. if ('last' in links) {
  92. maxPage = parseInt(links.last.split('=')[1], 10);
  93. }
  94. var pagesLeft = maxPage - 1;
  95. for (var p = 2; p <= maxPage; p += 1) {
  96. fetchComments(p, function (page, body, links) {
  97. handleBody(body, page);
  98. pagesLeft -= 1;
  99. if (pagesLeft === 0) {
  100. handleCommenters(Object.keys(commenters));
  101. }
  102. });
  103. }
  104. });
  105. function handleBody(body, page) {
  106. body.forEach(function (comment) {
  107. console.log(page, comment.user.login);
  108. commenters[comment.user.login] = 1;
  109. });
  110. }
  111. function handleCommenters(commenters) {
  112. console.log('len of commenters', commenters.length);
  113. console.log(commenters);
  114. }
  115. }
  116. if (args.length === 0) {
  117. help();
  118. process.exit(0);
  119. }
  120. switch (args[0]) {
  121. case 'list':
  122. list();
  123. break;
  124. case 'mention':
  125. mention();
  126. break;
  127. case 'find-commenters':
  128. findCommenters(args[1]);
  129. break;
  130. default:
  131. console.log('unknown argument', args[0]);
  132. break;
  133. }