Skip to content

Instantly share code, notes, and snippets.

@umjasnik
Last active August 29, 2015 13:55
Show Gist options
  • Save umjasnik/8751204 to your computer and use it in GitHub Desktop.
Save umjasnik/8751204 to your computer and use it in GitHub Desktop.
ringojs, list directory recursevely and return result as json
/**
* usage: ringo fs_to_json.js
* ringo fs_to_json.js path
* ringo fs_to_json.js path outputfile
* result:
* [{"name":"filename"},{"name": "directoryname", files:[]}]
*/
var system = require("system");
var fs = require("fs");
var rootPath = system.args[1] || ".";
var targetFile = system.args[2];
var getFiles = function(path)
{
var files = [];
fs.list(path).forEach(function(file) {
var fileObject = {
name: file
}
var newPath = fs.join(path, file);
if (fs.isDirectory(newPath)) {
fileObject.files = getFiles(newPath);
}
files.push(fileObject);
});
return files;
}
var files = getFiles(rootPath);
if (targetFile)
{
fs.write(targetFile, JSON.stringify(files));
}
else
{
print(JSON.stringify(files));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment