UNPKG

ceri-dev-server

Version:

a small development server for building ceri components

76 lines (69 loc) 2.53 kB
(function() { var fs, fstools, loaderUtils, path; fs = require("fs"); path = require("path"); fstools = require("./fstools"); loaderUtils = require("loader-utils"); module.exports = function(source, map) { var cb, getStructure, options, routes, structure, structureToRoutes; options = loaderUtils.getOptions(this); getStructure = function(currentPath) { var entries, entry, entryPath, ext, folder, i, j, len, len1, ref, structure, target; entries = fs.readdirSync(currentPath); structure = { name: path.basename(currentPath), folders: [], components: [] }; for (i = 0, len = entries.length; i < len; i++) { entry = entries[i]; entryPath = path.resolve(currentPath, entry); if (fstools.isDirectory(entryPath)) { folder = getStructure(entryPath); if (folder.components.length > 0 || folder.folders.length > 0) { structure.folders.push(folder); } } else { ext = path.extname(entry); ref = options.extensions; for (j = 0, len1 = ref.length; j < len1; j++) { target = ref[j]; if (ext === "." + target) { structure.components.push({ name: path.basename(entry, ext), path: entryPath, ext: ext }); } } } } return structure; }; structureToRoutes = function(structure, rootpath) { var comp, folder, i, j, len, len1, ref, ref1, routeName, routePath, routes; if (rootpath == null) { rootpath = ""; } routes = ""; ref = structure.folders; for (i = 0, len = ref.length; i < len; i++) { folder = ref[i]; routes += structureToRoutes(folder, rootpath + "/" + folder.name); } ref1 = structure.components; for (j = 0, len1 = ref1.length; j < len1; j++) { comp = ref1[j]; routeName = rootpath.replace(path.sep, "/") + "/" + comp.name; routePath = comp.path.replace(/\\/g, "\\\\"); routes += " \"" + routeName + "\": require(\"" + routePath + "\"),\n"; } return routes; }; structure = getStructure(options.workingDir); routes = structureToRoutes(structure); routes = "routes = {\n" + routes + "\n};"; cb = (typeof this.async === "function" ? this.async() : void 0) || this.callback; return cb(null, source.replace(/getRoutes\(\);/g, routes), map); }; }).call(this);