Created
May 2, 2016 07:08
-
-
Save lukrizal/d0f83c2495f9ca04cc1fcf58646621a2 to your computer and use it in GitHub Desktop.
get all urls defined on a project
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 routes = []; | |
var fs = require('fs-extra'); | |
var path = require('path'); | |
var _ = require('underscore'); | |
var json2csv = require('json2csv'); | |
var filesToScan = [ | |
process.cwd() + '/index.php' | |
]; | |
var jsdom = require("jsdom"); | |
var directoriesToScan = [ | |
'common', | |
'include', | |
'lib', | |
'prc', | |
'slot', | |
'pages' | |
]; | |
var readDir = function(currentDirPath, callback) { | |
fs.readdirSync(currentDirPath).forEach(function(name) { | |
var filePath = path.join(currentDirPath, name); | |
var stat = fs.statSync(filePath); | |
if (stat.isFile()) { | |
callback(filePath, stat); | |
} | |
if (stat.isDirectory()) { | |
readDir(filePath, callback); | |
} | |
}); | |
} | |
var fnPushFiles = function (filePath, stat) { | |
if (_.contains([ | |
'.php', '.html', '.js' | |
], path.extname(filePath))) { | |
filesToScan.push(path.join(filePath)); | |
} | |
} | |
directoriesToScan.forEach(function (x) { | |
var currentDirPath = process.cwd() + '/' + x; | |
readDir(currentDirPath, fnPushFiles); | |
}); | |
var fnRecordUrls = function(index, el) { | |
console.log(el); | |
}; | |
var fnFindMatches = function(string, pattern) { | |
var matches = string.match(pattern); | |
var urls = []; | |
if (matches) { | |
matches.forEach(function (x) { | |
if (x.substr(0, 2) == '"/' || x.substr(0, 2) == "'/") { | |
if (x.substr(-1, 1) == '"' || x.substr(-1, 1) == "'") { | |
x = x.substr(0, x.length - 1); | |
} | |
x = x.replace("'/", "/").replace('"/', "/") | |
if (_.findWhere(routes, {url: x})) { | |
return; | |
} | |
routes.push({ | |
url: x | |
}); | |
urls.push(x); | |
} else { | |
// console.log(x); | |
} | |
}); | |
} | |
return urls; | |
}; | |
var fnCrawlForUrls = function (x) { | |
var content = fs.readFileSync(x); | |
fnFindMatches(content.toString(), /(["|']\/?(prc|lib|common|include|pages|slot)\/\w.*["|'])/g); | |
}; | |
filesToScan.forEach(fnCrawlForUrls) | |
json2csv({data: _.sortBy(routes, 'url')}, function (err, csv) { | |
if (err) console.log(err); | |
fs.writeFileSync('./url.csv', csv); | |
}); | |
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
{ | |
"name": "", | |
"version": "1.0.0", | |
"description": "", | |
"main": "index.js", | |
"scripts": { | |
"test": "echo \"Error: no test specified\" && exit 1" | |
}, | |
"author": "", | |
"license": "ISC", | |
"dependencies": { | |
"fs-extra": "^0.30.0", | |
"jsdom": "^8.4.1", | |
"json2csv": "^3.3.0", | |
"regex": "^0.1.1", | |
"underscore": "^1.8.3" | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment