revision-string-replacer.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. var cli = require('cli')
  2. , mongo = require('mongoose')
  3. , async = require('async')
  4. ;
  5. cli.setUsage('MONGO_URI=mongodb://user:password@host/dbnae node bin/revision-string-replacer.js --from=\'aaa\' --to=\'bbb\'\n\n This means that replace string "aaa" to "bbb" from all revisions.');
  6. cli.parse({
  7. from: [false, 'Specified string is a target to replace.', 'string'],
  8. to: [false, 'Replace string which specified by "from" to this string.', 'string'],
  9. dry: [false, 'Dry run', 'boolean'],
  10. });
  11. cli.main(function(args, options)
  12. {
  13. var app = {set: function(v) { }}
  14. , c = this
  15. , from = options.from
  16. , to = options.to
  17. , dry = options.dry
  18. ;
  19. console.log('This scriprt is not working now. Should be fixed.');
  20. cli.exit(1);
  21. if (!to || !from) {
  22. cli.error('"to" and "from" options are required.\n');
  23. cli.output(cli.getUsage());
  24. cli.exit(1);
  25. return ;
  26. }
  27. var mongoUri = process.env.MONGOLAB_URI || process.env.MONGOHQ_URL || process.env.MONGO_URI || false;
  28. if (!mongoUri) {
  29. cli.error('Please set MONGO_URI env.\n');
  30. cli.output(cli.getUsage());
  31. cli.exit(1);
  32. return;
  33. }
  34. mongo.connect(mongoUri);
  35. // あー config 読み込み&model読み込み周りを app.js から切り離さないといけないにゃぁ
  36. configModel = require('../lib/models/config')(app);
  37. async.series([
  38. function (next) {
  39. configModel.loadAllConfig(function(err, doc) {
  40. return next();
  41. });
  42. }, function (next) {
  43. var config = app.set('config');
  44. models = require('../lib/models')(app);
  45. models.Config = configModel;
  46. return next();
  47. }, function (next) {
  48. var limit = 100000;
  49. c.spinner('Load revisions..');
  50. models.Revision.find().limit(limit).exec(function(err, revs) {
  51. c.spinner('Load revisions.. done!\n', true);
  52. var count = Object.keys(revs).length
  53. , i = 0
  54. , matched = 0
  55. , matchedWords = 0
  56. ;
  57. c.output('Found ' + count + ' revisions.\n');
  58. c.output('Start replacing.\n');
  59. async.each(revs, function(rev, cb) {
  60. var regexp = new RegExp(from, 'g');
  61. c.progress(++i/count);
  62. var m = rev.body.match(regexp);
  63. if (!m) {
  64. return cb();
  65. }
  66. matched++;
  67. matchedWords += m.length;
  68. if (dry) {
  69. return cb();
  70. } else {
  71. rev.body = rev.body.replace(regexp, to);
  72. rev.save(function(err, s) {
  73. if (err) {
  74. c.error('Error on:' + rev.path);
  75. } else {
  76. }
  77. return cb();
  78. });
  79. }
  80. }, function(err) {
  81. if (dry) {
  82. cli.info(matchedWords + ' words in (' + matched + ' of ' + count + ') revisions will be replaced!');
  83. } else {
  84. cli.ok(matchedWords + ' words in (' + matched + ' of ' + count + ') revisions replaced!');
  85. }
  86. return next();
  87. });
  88. });
  89. }
  90. , function (next) {
  91. cli.ok('Finished!');
  92. mongo.disconnect();
  93. return next();
  94. }
  95. ]);
  96. });