Comparing version 1.0.0 to 1.1.0
@@ -6,11 +6,28 @@ #!/usr/bin/env node | ||
var fs = require('fs'); | ||
var argv = require('yargs') | ||
.options('t', { | ||
alias : 'to', | ||
default : '_dereq_', | ||
describe: 'token to change the variable into' | ||
}) | ||
.options('f', { | ||
alias : 'from', | ||
default : 'require', | ||
describe: 'token to find and change' | ||
}) | ||
.help('h') | ||
.alias('h', 'help') | ||
.version(require('../package.json').version, 'v') | ||
.alias('v', 'version') | ||
.argv; | ||
var file = argv._[0]; | ||
var input; | ||
if (file && file !== '-') { | ||
input = fs.createReadStream(file); | ||
} else { | ||
input = process.stdin; | ||
} | ||
var file = process.argv[2]; | ||
var input = file && file !== '-' | ||
? fs.createReadStream(process.argv[2]) | ||
: process.stdin | ||
; | ||
input.pipe(concat(function(buf) { | ||
console.log(derequire(buf.toString('utf8'))); | ||
console.log(derequire(buf.toString('utf8'), argv.t, argv.f)); | ||
})); |
79
index.js
'use strict'; | ||
var estraverse = require('estraverse'); | ||
@@ -6,44 +7,54 @@ var esprima = require('esprima-fb'); | ||
var _requireRegexp = /require.*\(.*['"]/m; | ||
function requireRegexp(token) { | ||
if (token === 'require') { | ||
return _requireRegexp; | ||
} | ||
return new RegExp(token + '.*\\(.*[\'"]', 'm'); | ||
if (token === 'require') { | ||
return _requireRegexp; | ||
} | ||
return new RegExp(token + '.*\\(.*[\'"]', 'm'); | ||
} | ||
function testParse (code) { | ||
try{ | ||
return esprima.parse(code,{range:true}); | ||
}catch(e){} | ||
try { | ||
return esprima.parse(code, { range: true }); | ||
} catch (e) {} | ||
} | ||
function rename(code, tokenTo, tokenFrom) { | ||
tokenTo = tokenTo || '_dereq_'; | ||
tokenFrom = tokenFrom || 'require'; | ||
if(tokenTo.length !== tokenFrom.length){ | ||
throw new Error('bad stuff will happen if you try to change tokens of different length'); | ||
tokenTo = tokenTo || '_dereq_'; | ||
tokenFrom = tokenFrom || 'require'; | ||
if(tokenTo.length !== tokenFrom.length){ | ||
throw new Error('bad stuff will happen if you try to change tokens of different length'); | ||
} | ||
if (!requireRegexp(tokenFrom).test(code)) { | ||
return code; | ||
} | ||
var inCode = '!function(){'+code+'\n;}'; | ||
var ast = testParse(inCode); | ||
if(!ast){ | ||
return code; | ||
} | ||
var ctx = new esrefactor.Context(inCode); | ||
estraverse.traverse(ast,{ | ||
enter:function(node, parent) { | ||
var test = parent && | ||
(parent.type === 'FunctionDeclaration' || parent.type === 'FunctionExpression' || | ||
parent.type === 'VariableDeclarator') && | ||
node.name === tokenFrom && node.type === 'Identifier'; | ||
if (test) { | ||
ctx._code = ctx.rename(ctx.identify(node.range[0]), tokenTo); | ||
} | ||
} | ||
if (!requireRegexp(tokenFrom).test(code)) return code; | ||
var inCode = '!function(){'+code+'\n;}'; | ||
var ast = testParse(inCode); | ||
if(!ast){ | ||
return code; | ||
} | ||
var ctx = new esrefactor.Context(inCode); | ||
estraverse.traverse(ast,{ | ||
enter:function(node, parent) { | ||
var test = | ||
parent && | ||
(parent.type === 'FunctionDeclaration' || parent.type === 'FunctionExpression' || parent.type === 'VariableDeclarator') && | ||
node.name === tokenFrom && | ||
node.type === 'Identifier'; | ||
if(test){ | ||
ctx._code = ctx.rename(ctx.identify(node.range[0]), tokenTo); | ||
} | ||
} | ||
}); | ||
return ctx._code.slice(12, -3); | ||
}); | ||
return ctx._code.slice(12, -3); | ||
} | ||
module.exports = rename; |
{ | ||
"name": "derequire", | ||
"version": "1.0.0", | ||
"version": "1.1.0", | ||
"description": "remove requires", | ||
@@ -26,3 +26,4 @@ "main": "index.js", | ||
"esrefactor": "~0.1.0", | ||
"estraverse": "~1.5.0" | ||
"estraverse": "~1.5.0", | ||
"yargs": "^1.3.1" | ||
}, | ||
@@ -29,0 +30,0 @@ "devDependencies": { |
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
752792
21096
5
+ Addedyargs@^1.3.1
+ Addedyargs@1.3.3(transitive)