Created
February 5, 2015 20:28
-
-
Save cecilemuller/342f3eb612d3bb79a316 to your computer and use it in GitHub Desktop.
Aggregates files from package.json of all modules given an entry point script for Browserify
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 fs = require('fs'); | |
var path = require('path'); | |
var mdeps = require('module-deps'); | |
var through = require('through'); | |
function get_dependency_files(filepath, callback){ | |
'use strict'; | |
var scripts = []; | |
var mytransform = through( | |
function(dependency){ | |
scripts.push(dependency.file); | |
}, | |
function(data){ | |
var files = []; | |
var l = scripts.length; | |
for (var i = 0; i < l; i++){ | |
var js_filepath = scripts[i]; | |
var base = path.join(js_filepath, '..'); | |
try { | |
var code = fs.readFileSync(path.join(base, 'package.json'), 'utf8'); | |
var definition = JSON.parse(code); | |
if (definition && definition.files){ | |
var subfiles = definition.files; | |
var l2 = subfiles.length; | |
for (var j = 0; j < l2; j++){ | |
files.push(path.join(base, subfiles[j])); | |
} | |
} | |
} catch(e){ | |
// | |
} | |
} | |
callback(files); | |
mytransform.queue(null); | |
} | |
); | |
var md = mdeps({transformKey: ['browserify', 'transform']}); | |
md.pipe(mytransform); | |
md.end({file: filepath}); | |
} | |
get_dependency_files('./src/application/index.js', function(files){ | |
'use strict'; | |
var stylesheets = files.filter(function(filepath){ | |
return filepath.match(/\.styl$/) || filepath.match(/\.css$/); | |
}); | |
console.log('--------------- STYLUS ---------------'); | |
console.log(stylesheets); | |
console.log('--------------------------------------'); | |
var images = files.filter(function(filepath){ | |
return filepath.match(/\.jpg$/) || filepath.match(/\.png$/) || filepath.match(/\.gif$/) || filepath.match(/\.svg$/); | |
}); | |
console.log('--------------- IMAGES ---------------'); | |
console.log(images); | |
console.log('--------------------------------------'); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment