Forked from joemccann/create_images_json_object_from_dir.js
Created
October 10, 2016 21:27
-
-
Save aerohub/22cf74e942f6de0bfa08feae7bd3761a to your computer and use it in GitHub Desktop.
Scan a directory and create a JSON object of images (and write it to a file) of all the images in that directory.
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
var sys = require("sys"), | |
fs = require("fs"); | |
function processImageDir(path, outFilename, cb) { | |
fs.readdir(path, function(err, files) { | |
var imgfiles = []; | |
// Check for images and push on the array if it's a match. | |
files.forEach(function(name) { | |
name.substr(-4).match(/(png|jpeg|jpg|gif)/) && imgfiles.push(name) | |
}); | |
fs.writeFile(__dirname + '/' + outFilename, JSON.stringify({ | |
images: imgfiles | |
}), function(err) { | |
if (err) throw err; | |
cb && cb("Sweet."); | |
}); | |
}); | |
} | |
processImageDir('/path/to/your/images/dir', "preload-images.json", function(message) { | |
console.log(message); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment