detect-invalid-requires
Advanced tools
Comparing version 0.0.1 to 0.1.0
@@ -5,7 +5,6 @@ #!/usr/bin/env node | ||
var detector = require('./Index'); | ||
var invalid = detector(process.argv.slice(2)); | ||
var detector = require('./index'); | ||
function outputInvalid(source) { | ||
if ('path' in source) | ||
if (typeof source.path !== 'undefined') | ||
console.log('Invalid require: '.red + source.path.yellow + ' in ' + source.file.yellow); | ||
@@ -16,2 +15,4 @@ else | ||
invalid.forEach(outputInvalid); | ||
detector(process.argv[2], function (invalid) { | ||
invalid.forEach(outputInvalid); | ||
}); |
'use strict'; | ||
var esprima = require('esprima'); | ||
var esquery = require('esquery'); | ||
var fs = require('fs'); | ||
var path = require('path'); | ||
require('colors'); | ||
var walker = require('walker'); | ||
@@ -12,20 +14,2 @@ function getDirOf(file) { | ||
// Stolen from https://github.com/ariya/esprima/blob/master/examples/findbooleantrap.js | ||
// Executes visitor on the object and its children (recursively). | ||
function traverse(object, visitor) { | ||
var key, child; | ||
if (visitor.call(null, object) === false) { | ||
return; | ||
} | ||
for (key in object) { | ||
if (object.hasOwnProperty(key)) { | ||
child = object[key]; | ||
if (typeof child === 'object' && child !== null) { | ||
traverse(child, visitor); | ||
} | ||
} | ||
} | ||
} | ||
function chompShebang(script) { | ||
@@ -35,34 +19,35 @@ return script.replace(/^\#\![^\n]+/, ''); | ||
module.exports = function (files) { | ||
if (!Array.isArray(files)) files = [files]; | ||
module.exports = function (inputPath, callback) { | ||
var invalid = []; | ||
files.filter(function (file) { | ||
if (!fs.existsSync(getDirOf(file))) { | ||
invalid.push({ file: file }); | ||
return false; | ||
} | ||
return true; | ||
}).forEach(function (file) { | ||
var script = fs.readFileSync(getDirOf(file), 'utf-8'); | ||
inputPath = path.resolve(process.cwd(), inputPath || '.'); | ||
walker(inputPath).on('file', function (file) { | ||
if (!file.match(/\.js$/)) return; | ||
var script = fs.readFileSync(file, { encoding: 'utf-8' }); | ||
var content = esprima.parse(chompShebang(script), { tolerant: true }); | ||
traverse(content, function (node) { | ||
if (node.type === 'CallExpression' && node.callee.name === 'require') { | ||
if (!node.arguments.length || !node.arguments[0].value) return; | ||
var requireStr = node.arguments[0].value; | ||
//remove non-relative paths | ||
if (['.', '\\', '/'].indexOf(requireStr.slice(0, 1)) === -1) return; | ||
var targetDir = path.join(path.dirname(file), path.dirname(requireStr)); | ||
var requires = esquery(content, 'CallExpression[callee.name="require"]'); | ||
//error out invalid path | ||
if (!fs.existsSync(targetDir)) return invalid.push({file: file, path: requireStr }); | ||
requires.forEach(function (node) { | ||
var requireStr = node.arguments[0].value; | ||
var dirFiles = fs.readdirSync(targetDir); | ||
if (dirFiles.indexOf(path.basename(requireStr)) !== -1 || dirFiles.indexOf(path.basename(requireStr) + '.js') !== -1) return; | ||
//remove non-relative paths | ||
if (typeof requireStr === 'string' && ['.', '\\', '/'].indexOf(requireStr.slice(0, 1)) === -1) return; | ||
var targetDir = path.resolve(path.dirname(file), path.dirname(requireStr)); | ||
//error out invalid path | ||
if (!fs.existsSync(targetDir)) { | ||
invalid.push({file: file, path: requireStr }); | ||
return; | ||
} | ||
var dirFiles = fs.readdirSync(targetDir); | ||
if (dirFiles.indexOf(path.basename(requireStr)) !== -1 || dirFiles.indexOf(path.basename(requireStr) + '.js') !== -1) return; | ||
invalid.push({file: file, path: requireStr }); | ||
}); | ||
}).on('end', function() { | ||
callback(invalid); | ||
}); | ||
return invalid; | ||
}; |
{ | ||
"name": "detect-invalid-requires", | ||
"version": "0.0.1", | ||
"version": "0.1.0", | ||
"description": "utility to detect requires that are not legal in a case-sensitive OS", | ||
@@ -12,8 +12,12 @@ "main": "index.js", | ||
"bin": "lib/cli.js", | ||
"repository": "https://github.com/justinjmoses/detect-invalid-requires", | ||
"dependencies": { | ||
"esprima": "=1.2.x" | ||
"esprima": "1.2.x", | ||
"esquery": "0.3.x", | ||
"walker": "1.0.x" | ||
}, | ||
"devDependencies": { | ||
"chai": "=1.9.x" | ||
"chai": "1.9.x", | ||
"mocha": "1.21.x" | ||
} | ||
} |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
No repository
Supply chain riskPackage does not have a linked source code repository. Without this field, a package will have no reference to the location of the source code use to generate the package.
Found 1 instance in 1 package
3966
3
2
75
+ Addedesquery@0.3.x
+ Addedwalker@1.0.x
+ Addedesquery@0.3.0(transitive)
+ Addedestraverse@1.3.2(transitive)
+ Addedmakeerror@1.0.12(transitive)
+ Addedtmpl@1.0.5(transitive)
+ Addedwalker@1.0.8(transitive)
Updatedesprima@1.2.x