Created
March 3, 2013 19:42
-
-
Save pyrobot/5077906 to your computer and use it in GitHub Desktop.
reads app component.json and serves the files associated with them
This file contains hidden or 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 path = require('path'), | |
| fs = require('fs'); | |
| module.exports = function (rootComponentPath) { | |
| var componentBase, modules, modPaths, modRoutes; | |
| modPaths = []; | |
| modRoutes = {}; | |
| rootComponentPath = rootComponentPath || '.'; | |
| componentBase = require(path.resolve(rootComponentPath, './component.json')); | |
| modules = Object.keys(componentBase.dependencies); | |
| modules.forEach(function (componentName) { | |
| var componentPath = path.resolve('./components', componentName), | |
| componentJsonPath = path.join(componentPath, 'component.json'), | |
| componentJson = require(componentJsonPath); | |
| switch (typeof componentJson.main) { | |
| case 'object': | |
| var arr; | |
| if (componentJson.main instanceof Array) { | |
| arr = componentJson.main; | |
| } else { | |
| arr = Object.keys(componentJson.main); | |
| } | |
| arr.forEach(function(k) { modPaths.push(path.resolve(componentPath, k)); }); | |
| break; | |
| case 'string': | |
| modPaths.push(path.resolve(componentPath, componentJson.main)); | |
| break; | |
| default: | |
| //noop | |
| break; | |
| } | |
| }); | |
| modPaths.forEach(function (modPath) { | |
| var fileName = modPath.split('/').pop(); | |
| modRoutes[fileName] = modPath; | |
| }); | |
| return function (req, res, next) { | |
| var fileName = req.url.substring(1), | |
| filePath = modRoutes[fileName]; | |
| if (filePath) { | |
| var fileStream = fs.createReadStream(filePath); | |
| res.writeHead(200); | |
| fileStream.pipe(res); | |
| } else { | |
| next(); | |
| } | |
| }; | |
| }; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment