Created
February 28, 2014 14:28
-
-
Save anatomic/9271995 to your computer and use it in GitHub Desktop.
Recursively iterate through folders to document image assets
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
#!/usr/bin/env node | |
var fs = require('fs') | |
, util = require('util') | |
, path = require('path'); | |
var total = 0; | |
/** | |
* | |
* Iterate through the filesystem and create a file that documents assets created | |
* | |
*/ | |
function processDir(dir, done) { | |
var assets = []; | |
fs.readdir(dir, function(err, files){ | |
if( err ) throw err; | |
var pending = files.length; | |
total += files.length; | |
if(!pending) done(null, assets); | |
files.forEach(function(file) { | |
fs.stat(path.join(dir, file), function( err, stat ) { | |
if( err ) console.log(err); | |
if( stat && stat.isDirectory() && file != 'node_modules' ) { | |
processDir(path.join(dir, file), function(err, results){ | |
assets = assets.concat(results); | |
if(!--pending) { done(null, assets); } | |
}); | |
} | |
else{ | |
if( /\.(png|jpg|gif)/.test(file) ) assets.push(file); | |
if(!--pending) { done(null, assets); } | |
} | |
}); | |
}); | |
}); | |
} | |
var start = process.hrtime(); | |
processDir(__dirname, function( err, results) { | |
var diff = process.hrtime(start); | |
var ms = (diff[0] * 1e9 + diff[1]) * 1e-6; | |
console.log("Total number of assets produced:", results.length); | |
console.log("Total number of items checked:", total); | |
console.log("Total execution time: %dms", ms ); | |
fs.writeFile('results.txt', results.join('\n')); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment