module-deps
Advanced tools
Comparing version 0.2.6 to 0.3.0
186
index.js
var fs = require('fs'); | ||
var path = require('path'); | ||
var spawn = require('child_process').spawn; | ||
var parseShell = require('shell-quote').parse; | ||
var duplexer = require('duplexer'); | ||
var required = require('required'); | ||
var browserResolve = require('browser-resolve'); | ||
var nodeResolve = require('resolve'); | ||
var detective = require('detective'); | ||
var through = require('through'); | ||
@@ -13,5 +18,5 @@ | ||
var files = {}; | ||
var visited = {}; | ||
var pending = 0; | ||
var cache = {}; | ||
var pending = 0; | ||
@@ -21,65 +26,144 @@ var output = through(); | ||
if (!opts) opts = {}; | ||
if (opts.cache === undefined) opts.cache = cache; | ||
opts.includeSource = true; | ||
var transforms = [].concat(opts.transform).filter(Boolean); | ||
mains.forEach(function (file) { | ||
var resolve = opts.resolve || browserResolve; | ||
var top = { id: '/', filename: '/', paths: [] }; | ||
mains.forEach(function (main) { walk(main, top) }); | ||
if (mains.length === 0) { | ||
process.nextTick(output.queue.bind(output, null)); | ||
} | ||
return output; | ||
function walk (id, parent, cb) { | ||
pending ++; | ||
var p = 2, src, rows; | ||
var trx = []; | ||
parent.packageFilter = function (pkg) { | ||
if (opts.packageFilter) pkg = opts.packageFilter(pkg); | ||
if (opts.transformKey) { | ||
var n = pkg; | ||
opts.transformKey.forEach(function (key) { | ||
if (n && typeof n === 'object') n = n[key]; | ||
}); | ||
trx = [].concat(n).filter(Boolean); | ||
} | ||
return pkg; | ||
}; | ||
resolve(id, parent, function (err, file) { | ||
if (err) return output.emit('error', err); | ||
if (cb) cb(file); | ||
if (visited[file]) { --pending; return }; | ||
visited[file] = true; | ||
fs.readFile(file, 'utf8', function (err, src) { | ||
if (err) return output.emit('error', err); | ||
applyTransforms(file, trx, src); | ||
}); | ||
}); | ||
} | ||
function applyTransforms (file, trx, src) { | ||
var isTopLevel = mains.some(function (main) { | ||
var m = path.relative(path.dirname(main), file); | ||
return m.split('/').indexOf('node_modules') < 0; | ||
}); | ||
var transf = (isTopLevel ? transforms : []).concat(trx); | ||
if (transf.length === 0) return done(); | ||
(function ap (trs) { | ||
if (trs.length === 0) return done(); | ||
var s = makeTransform(file, trs[0]); | ||
s.on('error', output.emit.bind(output, 'error')); | ||
var data = ''; | ||
s.on('data', function (buf) { data += buf }); | ||
s.on('end', function () { | ||
src = data; | ||
ap(trs.slice(1)); | ||
}); | ||
s.end(src); | ||
})(transf); | ||
function done () { | ||
if (!files[file]) { | ||
files[file] = { | ||
id: file, | ||
source: src, | ||
entry: true, | ||
deps: rows.reduce(function (acc, dep) { | ||
acc[dep.id] = dep.filename; | ||
return acc; | ||
}, {}) | ||
}; | ||
output.queue(files[file]); | ||
parseDeps(file, src); | ||
} | ||
} | ||
function parseDeps (file, src) { | ||
var deps = detective(src); | ||
var p = deps.length; | ||
var current = { id: file, filename: file, paths: [] }; | ||
var resolved = {}; | ||
deps.forEach(function (id) { | ||
walk(id, current, function (r) { | ||
resolved[id] = r; | ||
if (--p === 0) done(); | ||
}); | ||
}); | ||
if (deps.length === 0) done(); | ||
function done () { | ||
var rec = { | ||
id: file, | ||
source: src, | ||
deps: resolved | ||
}; | ||
walk(rows); | ||
if (mains.indexOf(file) >= 0) { | ||
rec.entry = true; | ||
} | ||
output.queue(rec); | ||
if (--pending === 0) output.queue(null); | ||
} | ||
} | ||
function makeTransform (file, tr) { | ||
if (/\s/.test(tr)) return cmdTransform(file, tr); | ||
fs.readFile(file, 'utf8', function (err, s) { | ||
var tout = through(), tin = through(); | ||
tin.pause(); | ||
var params = { basedir: path.dirname(file) }; | ||
nodeResolve(tr, params, function (err, res) { | ||
if (err) return output.emit('error', err); | ||
src = s; | ||
if (--p === 0) done(); | ||
var t = res | ||
? require(res)(file) | ||
: cmdTransform(file, tr) | ||
; | ||
t.pipe(tout); | ||
tin.pipe(t); | ||
tin.resume(); | ||
}); | ||
return duplexer(tin, tout); | ||
} | ||
function cmdTransform (file, tr) { | ||
var cmd = parseShell(tr); | ||
var env = Object.create(process.env); | ||
env._ = tr; | ||
env.FILENAME = file; | ||
var current = { id: file, filename: file, paths: [] }; | ||
required(file, opts, function (err, r) { | ||
if (err) return output.emit('error', err); | ||
rows = r; | ||
if (--p === 0) done(); | ||
var ps = spawn(cmd[0], cmd.slice(1), { | ||
cwd: path.dirname(file), | ||
env: env | ||
}); | ||
}); | ||
if (pending === 0) process.nextTick(function () { | ||
output.queue(null); | ||
}); | ||
return output; | ||
function walk (rows) { | ||
rows.forEach(function (row) { | ||
if (files[row.filename]) return; | ||
var r = files[row.filename] = { | ||
id: row.filename, | ||
source: row.source, | ||
deps: (row.deps || []).reduce(function (acc, dep) { | ||
acc[dep.id] = dep.filename; | ||
return acc; | ||
}, {}) | ||
}; | ||
if (mains.indexOf(row.filename) >= 0) { | ||
r.entry = true; | ||
var error = ''; | ||
ps.stderr.on('data', function (buf) { error += buf }); | ||
ps.on('close', function (code) { | ||
if (code !== 0) { | ||
return output.emit('error', [ | ||
'process ' + tr + ' exited with code ' + code, | ||
' while parsing ' + file + '\n', | ||
error.split('\n').join('\n ') | ||
].join('')); | ||
} | ||
output.queue(r); | ||
walk(row.deps || []); | ||
}); | ||
return duplexer(ps.stdin, ps.stdout); | ||
} | ||
}; |
{ | ||
"name": "module-deps", | ||
"version": "0.2.6", | ||
"version": "0.3.0", | ||
"description": "walk the dependency graph to generate json output that can be fed into browser-pack", | ||
@@ -10,5 +10,9 @@ "main": "index.js", | ||
"dependencies": { | ||
"through": "~2.2.0", | ||
"duplexer": "~0.0.3", | ||
"JSONStream": "~0.4.3", | ||
"required": "~0.3.0", | ||
"through": "~2.2.0" | ||
"browser-resolve": "~0.0.3", | ||
"resolve": "~0.3.0", | ||
"shell-quote": "~0.0.1", | ||
"detective": "~1.1.0" | ||
}, | ||
@@ -15,0 +19,0 @@ "devDependencies": { |
@@ -9,2 +9,3 @@ var parser = require('../'); | ||
var p = parser(__dirname + '/files/main.js'); | ||
p.on('error', t.fail.bind(t)); | ||
var pack = packer(); | ||
@@ -11,0 +12,0 @@ |
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
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
Shell access
Supply chain riskThis module accesses the system shell. Accessing the system shell increases the risk of executing arbitrary code.
Found 1 instance in 1 package
Uses eval
Supply chain riskPackage uses dynamic code execution (e.g., eval()), which is a dangerous practice. This can prevent the code from running in certain environments and increases the risk that the code may contain exploits or malicious behavior.
Found 1 instance in 1 package
Dynamic require
Supply chain riskDynamic require can indicate the package is performing dangerous or unsafe dynamic code execution.
Found 1 instance in 1 package
Environment variable access
Supply chain riskPackage accesses environment variables, which may be a sign of credential stuffing or data theft.
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
16960
32
305
138
7
4
5
+ Addedbrowser-resolve@~0.0.3
+ Addeddetective@~1.1.0
+ Addedduplexer@~0.0.3
+ Addedresolve@~0.3.0
+ Addedshell-quote@~0.0.1
+ Addedbase64-js@0.0.2(transitive)
+ Addedbrowser-resolve@0.0.5(transitive)
+ Addedbuffer-browserify@0.0.4(transitive)
+ Addedconcat-stream@0.0.8(transitive)
+ Addedconsole-browserify@0.1.6(transitive)
+ Addedcrypto-browserify@0.2.1(transitive)
+ Addeddetective@1.1.0(transitive)
+ Addedduplexer@0.0.4(transitive)
+ Addedesprima@1.0.4(transitive)
+ Addedhttp-browserify@0.1.6(transitive)
+ Addedresolve@0.3.00.3.1(transitive)
+ Addedshell-quote@0.0.1(transitive)
+ Addedvm-browserify@0.0.1(transitive)
+ Addedzlib-browserify@0.0.1(transitive)
- Removedrequired@~0.3.0
- Removeddetective@2.2.0(transitive)
- Removedescodegen@0.0.15(transitive)
- Removedesprima@1.0.2(transitive)
- Removedrequired@0.3.2(transitive)
- Removedsource-map@0.7.4(transitive)