Last active
August 29, 2015 13:55
-
-
Save umjasnik/8751204 to your computer and use it in GitHub Desktop.
ringojs, list directory recursevely and return result as json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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