UNPKG

lx-scan

Version:

License eXtension to find 5-tuples of all installed packages: name, version, project home page, license (e. g. Apache v2, BSD) and required notice. It includes a GUI to edit information for each package and to enter information if necessary.

111 lines (100 loc) 3.32 kB
var fs = require("fs"); var lxu = require("lxutils"); var pa = require("path"); var _ = require("underscore"); var async = require("async"); module.exports.get = function (req,res) { var host = req.get("Host"); var verbose = (req.query.verbose === "true"); var type = req.query.type||""; var scantype = req.query.scantype||""; var caller = req.query.caller||req.url; var jfile = req.query.jfile||""; var path = req.query.path||""; var rendering_arguments = { verb: verbose.toString(), ftype: type, calling: caller, jref: jfile, pathname: path, oldpath: req.query.oldpath||"", scantype: scantype, host: host }; // Set default path if one wasn't passed in the URL parameters if(path === "" || path === ".") { path = process.env.HOME || process.env.USERPROFILE || process.env.HOMEPATH; } rendering_arguments.pathname = path = pa.resolve("/","~",path).replace(/\\/g,"\\\\"); fs.stat(path, function (err, stats) { if(err) { if(err.code === "ENOENT") { var url = "/browse?jfile="+jfile+"&caller="+caller; if(type !=="") url+="&type=" + type; if(verbose) url+="&verbose=" + verbose; if(scantype) url+="&scantype=" + scantype; path = pa.dirname(path) url +="&path=" + path; res.redirect(url); } else { console.log(err); } return; } if(!stats.isDirectory()) { // If it isn't a directory, then we're going to assume that the user wanted to return with the file // indicated in the path if(pa.sep === "\\") { // Double the back slashes so that the path plays nice with the templating engine path = path.replace(/\//g,"\\"); path = path.replace(/\\/g,"\\\\"); } var url = "http://localhost:8888"+caller; if(!/^.*\?.*$/.test(caller)) url += "?"; url+="&jfile="+jfile+"&verbose="+req.query.verbose+"&path="+path+"&oldpath="+req.query.oldpath+"&scantype="+scantype; if(!/^.*type.*$/.test(caller)) { url+= ("&type="+type); } res.redirect(url); } else { // Read the current directory, send file names and additional arguments on to templating engine fs.readdir(path, function (err,files) { if(err) { throw err; } // Remove files beginning with a "." (period) for now because they're mostly systems files files = _.filter(files,function (filename) { return !(/^\..*$/).test(filename); }); var node_modules_check = false; async.map(files, function (file,callback) { if(file === "node_modules") node_modules_check = true; fs.stat(pa.join(path,file),function (err,stats) { if(err) return callback(err,null); callback(null,{name:file,dir:stats.isDirectory()}); }); },function (error,results) { results = _.union([{name:"..",dir:true}],results); if(pa.sep === "\\") { // Double the back slashes so that the path plays nice with the templating engine path = path.replace(/\//g,"\\"); path = path.replace(/\\/g,"\\\\"); } rendering_arguments.title = "LX File Browser"; rendering_arguments.filelist = results; rendering_arguments.node_modules_check = node_modules_check; res.render("browser",rendering_arguments); } ); }); } }); };