build.js 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. var fs = require('fs')
  2. var path = require('path')
  3. var stringify = require('safe-stable-stringify')
  4. var commandPath = path.join(__dirname, '..', 'commands.json')
  5. var redisCommands = require('../')
  6. var Redis = require('ioredis')
  7. var redis = new Redis(process.env.REDIS_URI)
  8. redis.command().then(function (res) {
  9. redis.disconnect()
  10. // Find all special handled cases
  11. var movableKeys = String(redisCommands.getKeyIndexes).match(/case '[a-z-]+':/g).map(function (entry) {
  12. return entry.replace(/^case '|':$/g, '')
  13. })
  14. var commands = res.reduce(function (prev, current) {
  15. var currentCommandPos = movableKeys.indexOf(current[0])
  16. if (currentCommandPos !== -1 && current[2].indexOf('movablekeys') !== -1) {
  17. movableKeys.splice(currentCommandPos, 1)
  18. }
  19. // https://github.com/antirez/redis/issues/2598
  20. if (current[0] === 'brpop' && current[4] === 1) {
  21. current[4] = -2
  22. }
  23. prev[current[0]] = {
  24. arity: current[1] || 1, // https://github.com/antirez/redis/pull/2986
  25. flags: current[2],
  26. keyStart: current[3],
  27. keyStop: current[4],
  28. step: current[5]
  29. }
  30. return prev
  31. }, {})
  32. // Future proof. Redis might implement this at some point
  33. // https://github.com/antirez/redis/pull/2982
  34. if (!commands.quit) {
  35. commands.quit = {
  36. arity: 1,
  37. flags: [
  38. 'loading',
  39. 'stale',
  40. 'readonly'
  41. ],
  42. keyStart: 0,
  43. keyStop: 0,
  44. step: 0
  45. }
  46. }
  47. if (movableKeys.length !== 0) {
  48. throw new Error('Not all commands (\'' + movableKeys.join('\', \'') + '\') with the "movablekeys" flag are handled in the code')
  49. }
  50. // Use safe-stable-stringify instead fo JSON.stringify
  51. // for easier diffing
  52. var content = stringify(commands, null, ' ')
  53. fs.writeFileSync(commandPath, content)
  54. })