require-list
Advanced tools
Comparing version 0.1.3 to 0.1.4
144
lib/index.js
@@ -14,27 +14,32 @@ /** | ||
var DYNAMIC_LOADING = 'Dynamic loading'; | ||
var COLOR = { | ||
BLACK: '\u001b[30m', | ||
RED: '\u001b[31m', | ||
GREEN: '\u001b[32m', | ||
YELLOW: '\u001b[33m', | ||
BLUE: '\u001b[34m', | ||
MAGENTA: '\u001b[35m', | ||
CYAN: '\u001b[36m', | ||
WHITE: '\u001b[37m', | ||
BLACK: '\u001b[30m', | ||
RED: '\u001b[31m', | ||
GREEN: '\u001b[32m', | ||
YELLOW: '\u001b[33m', | ||
BLUE: '\u001b[34m', | ||
MAGENTA: '\u001b[35m', | ||
CYAN: '\u001b[36m', | ||
WHITE: '\u001b[37m', | ||
RESET: '\u001b[0m' | ||
RESET: '\u001b[0m' | ||
}; | ||
var DYNAMIC_LOADING = 'Dynamic loading'; | ||
function findRequire(est) { | ||
if (est.type === esprima.Syntax.CallExpression && | ||
est.callee && | ||
est.callee.type === esprima.Syntax.Identifier && | ||
est.callee.name === 'require' && | ||
est.arguments && est.arguments.length) { | ||
/** | ||
* find require modules from AST | ||
* @param {AST} ast | ||
* @returns {Array} | ||
*/ | ||
function findRequire(ast) { | ||
if (ast.type === esprima.Syntax.CallExpression && | ||
ast.callee && | ||
ast.callee.type === esprima.Syntax.Identifier && | ||
ast.callee.name === 'require' && | ||
ast.arguments && ast.arguments.length) { | ||
if (est.arguments[0].type === esprima.Syntax.Literal) { | ||
return [ est.arguments[0].value ]; | ||
if (ast.arguments[0].type === esprima.Syntax.Literal) { | ||
return [ ast.arguments[0].value ]; | ||
} else { | ||
return [ DYNAMIC_LOADING + '_' + escodegen.generate(est) ]; | ||
return [ DYNAMIC_LOADING + '_' + escodegen.generate(ast) ]; | ||
} | ||
@@ -44,5 +49,5 @@ } | ||
var key, result = [], _result; | ||
for (key in est) { | ||
if (est[key] && typeof est[key] === 'object') { | ||
_result = findRequire(est[key]); | ||
for (key in ast) { | ||
if (ast[key] && typeof ast[key] === 'object') { | ||
_result = findRequire(ast[key]); | ||
Array.prototype.push.apply(result, _result); | ||
@@ -55,14 +60,20 @@ } | ||
function childTree(filepath, level) { | ||
level = level || 0; | ||
if (level > 10) { | ||
/** | ||
* generate require children tree | ||
* @param {String} filepath | ||
* @param {Number} depth | ||
* @returns {Object} | ||
*/ | ||
function childTree(filepath, depth) { | ||
depth = depth || 0; | ||
if (depth > 10) { | ||
return {}; | ||
} | ||
var result = {}; | ||
var code = fs.readFileSync(filepath, 'utf8'); | ||
code = code.replace(/^#\!.*\n/, ''); | ||
var est; | ||
var result = {}, | ||
code = fs.readFileSync(filepath, 'utf8').replace(/^#\!.*\n/, ''), | ||
ast; | ||
try { | ||
est = esprima.parse(code); | ||
ast = esprima.parse(code); | ||
} catch (e) { | ||
@@ -73,5 +84,9 @@ console.error(filepath); | ||
var childs = findRequire(est); | ||
var i = 0, len = childs.length, child, dcnt = 0; | ||
for (i; i < len; i++) { | ||
var childs = findRequire(ast), | ||
i, | ||
len, | ||
child, | ||
dcnt = 0; | ||
for (i = 0, len = childs.length; i < len; i++) { | ||
child = childs[i]; | ||
@@ -96,3 +111,3 @@ if (child.indexOf(DYNAMIC_LOADING + '_') === 0) { | ||
} | ||
result[child] = childTree(child, level + 1); | ||
result[child] = childTree(child, depth + 1); | ||
} | ||
@@ -103,13 +118,27 @@ | ||
function convertString(dirname, color, str, childs, indent) { | ||
/** | ||
* convert string for output from childTree object | ||
* @param {String} dirname entry dirname | ||
* @param {Boolean} color use output color | ||
* @param {String} str output string | ||
* @param {Object} childs childTree return object | ||
* @param {String} indent | ||
* @param {Object} checkModule | ||
* @returns {String} | ||
*/ | ||
function convertString(dirname, color, str, childs, indent, checkModule) { | ||
indent = indent || ''; | ||
var key, last = Object.keys(childs).pop(), curr_indent, next_indent; | ||
checkModule = checkModule || {}; | ||
var key, | ||
last = Object.keys(childs).pop(), | ||
currIndent, | ||
nextIndent; | ||
for (key in childs) { | ||
if (key === last) { | ||
curr_indent = indent + '└── '; | ||
next_indent = indent + ' '; | ||
currIndent = indent + '\u2514\u2500\u2500 '; | ||
nextIndent = indent + ' '; | ||
} else { | ||
curr_indent = indent + '├── '; | ||
next_indent = indent + '│ '; | ||
currIndent = indent + '\u251C\u2500\u2500 '; | ||
nextIndent = indent + '\u2502 '; | ||
} | ||
@@ -120,5 +149,5 @@ | ||
if (color) { | ||
str += curr_indent + COLOR.YELLOW + key + COLOR.RESET + '\n'; | ||
str += currIndent + COLOR.YELLOW + key + COLOR.RESET + '\n'; | ||
} else { | ||
str += curr_indent + key + '\n'; | ||
str += currIndent + key + '\n'; | ||
} | ||
@@ -130,5 +159,5 @@ continue; | ||
if (color) { | ||
str += curr_indent + COLOR.MAGENTA + key + COLOR.RESET + '\n'; | ||
str += currIndent + COLOR.MAGENTA + key + COLOR.RESET + '\n'; | ||
} else { | ||
str += curr_indent + key + '\n'; | ||
str += currIndent + key + '\n'; | ||
} | ||
@@ -138,9 +167,19 @@ continue; | ||
if (checkModule[key]) { | ||
if (color) { | ||
str += currIndent + COLOR.CYAN + path.relative(dirname, key) + COLOR.RESET + '\n'; | ||
} else { | ||
str += currIndent + path.relative(dirname, key) + '\n'; | ||
} | ||
continue; | ||
} | ||
if (childs[key]) { | ||
str += curr_indent + path.relative(dirname, key) + '\n'; | ||
str = convertString(dirname, color, str, childs[key], next_indent); | ||
str += currIndent + path.relative(dirname, key) + '\n'; | ||
checkModule[key] = true; | ||
str = convertString(dirname, color, str, childs[key], nextIndent, checkModule); | ||
} else if (color) { | ||
str += curr_indent + COLOR.GREEN + path.relative(dirname, key) + COLOR.RESET + '\n'; | ||
str += currIndent + COLOR.GREEN + path.relative(dirname, key) + COLOR.RESET + '\n'; | ||
} else { | ||
str += curr_indent + path.relative(dirname, key) + '\n'; | ||
str += currIndent + path.relative(dirname, key) + '\n'; | ||
} | ||
@@ -152,6 +191,13 @@ } | ||
/** | ||
* @param {String} entrypoint | ||
*/ | ||
module.exports = function requireList(entrypoint) { | ||
return childTree(path.resolve(entrypoint), 0); | ||
return childTree(path.resolve(entrypoint)); | ||
}; | ||
/** | ||
* @param {String} entrypoint | ||
* @param {Boolean} color | ||
*/ | ||
module.exports.string = function string(entrypoint, color) { | ||
@@ -158,0 +204,0 @@ entrypoint = path.resolve(entrypoint); |
{ | ||
"name": "require-list", | ||
"version": "0.1.3", | ||
"version": "0.1.4", | ||
"description": "require tree list", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
16358
240