1
0
mirror of https://github.com/redis/node-redis.git synced 2025-08-04 15:02:09 +03:00

Auto update of new commands from redis.io (Dave Hoover)

Run this: node generate_commands.js

To fetch redis.io/commands.json and save it to a file that node_redis will read at startup.
This commit is contained in:
Matt Ranney
2011-06-12 14:25:57 -10:00
parent d6b5e8b7d3
commit 3d36711563
5 changed files with 46 additions and 41 deletions

View File

@@ -2,38 +2,39 @@ var http = require("http"),
sys = require("sys"),
fs = require("fs");
http.get({host: "redis.io", path: "/commands.json"}, function(res) {
console.log("Response from redis.io/commands.json: " + res.statusCode);
var commandString = "";
res.on('data', function(chunk) {
commandString += chunk;
});
res.on('end', function() {
var commands = JSON.parse(commandString);
writeCommandsToFile(commands, "lib/commands.js");
})
}).on('error', function(e) {
console.log("Got error: " + e.message);
});
function writeCommandsToFile(commands, path) {
console.log("Writing " + path);
var fileContents = "// This file was generated by ./generate_commands.js on " + prettyCurrentTime() + "\n";
var lowerCommands = [];
for (var command in commands) {
lowerCommands.push(command.toLowerCase());
}
fileContents += "exports.Commands = " + JSON.stringify(lowerCommands, null, " ") + ";\n";
fs.writeFile(path, fileContents);
function prettyCurrentTime() {
var date = new Date();
return date.toLocaleString();
}
function prettyCurrentTime() {
var date = new Date();
return date.toLocaleString();
}
function write_file(commands, path) {
var file_contents, out_commands;
console.log("Writing " + Object.keys(commands).length + " commands to " + path);
file_contents = "// This file was generated by ./generate_commands.js on " + prettyCurrentTime() + "\n";
out_commands = Object.keys(commands).map(function (key) {
return key.toLowerCase();
});
file_contents += "exports.Commands = " + JSON.stringify(out_commands, null, " ") + ";\n";
fs.writeFile(path, file_contents);
}
http.get({host: "redis.io", path: "/commands.json"}, function (res) {
var body = "";
console.log("Response from redis.io/commands.json: " + res.statusCode);
res.on('data', function (chunk) {
body += chunk;
});
res.on('end', function () {
write_file(JSON.parse(body), "lib/commands.js");
});
}).on('error', function (e) {
console.log("Error fetching command list from redis.io: " + e.message);
});