[Pkg-javascript-commits] [node-module-deps] 01/444: working code and example

Bastien Roucariès rouca at moszumanska.debian.org
Fri Dec 15 09:47:38 UTC 2017


This is an automated email from the git hooks/post-receive script.

rouca pushed a commit to branch master
in repository node-module-deps.

commit 5baf5dc543d0a4b78c9d9d68375a0abe483db029
Author: James Halliday <mail at substack.net>
Date:   Mon Feb 11 04:53:37 2013 -0800

    working code and example
---
 example/files/bar.js  |  5 ++++
 example/files/foo.js  |  5 ++++
 example/files/main.js |  2 ++
 example/files/xyz.js  |  2 ++
 example/json.js       |  7 +++++
 index.js              | 80 +++++++++++++++++++++++++++++++++++++++++++++++++++
 6 files changed, 101 insertions(+)

diff --git a/example/files/bar.js b/example/files/bar.js
new file mode 100644
index 0000000..356a3ae
--- /dev/null
+++ b/example/files/bar.js
@@ -0,0 +1,5 @@
+var m = require('./foo');
+
+module.exports = function (n) {
+    return n * 100;
+};
diff --git a/example/files/foo.js b/example/files/foo.js
new file mode 100644
index 0000000..c62e638
--- /dev/null
+++ b/example/files/foo.js
@@ -0,0 +1,5 @@
+var bar = require('./bar');
+
+module.exports = function (n) {
+    return n * 111 + bar(n);
+};
diff --git a/example/files/main.js b/example/files/main.js
new file mode 100644
index 0000000..8c71d79
--- /dev/null
+++ b/example/files/main.js
@@ -0,0 +1,2 @@
+var foo = require('./foo');
+console.log('main: ' + foo(5));
diff --git a/example/files/xyz.js b/example/files/xyz.js
new file mode 100644
index 0000000..dff6877
--- /dev/null
+++ b/example/files/xyz.js
@@ -0,0 +1,2 @@
+var foo = require('./foo');
+console.log('xyz: ' + foo(6));
diff --git a/example/json.js b/example/json.js
new file mode 100644
index 0000000..74d3819
--- /dev/null
+++ b/example/json.js
@@ -0,0 +1,7 @@
+var parser = require('../');
+var JSONStream = require('JSONStream');
+
+var stringify = JSONStream.stringify();
+stringify.pipe(process.stdout);
+
+parser(process.argv.slice(2)).pipe(stringify)
diff --git a/index.js b/index.js
new file mode 100644
index 0000000..af57493
--- /dev/null
+++ b/index.js
@@ -0,0 +1,80 @@
+var fs = require('fs');
+var path = require('path');
+
+var required = require('required');
+var Stream = require('stream');
+
+module.exports = function (mains) {
+    if (!Array.isArray(mains)) mains = [ mains ].filter(Boolean);
+    mains = mains.map(function (file) {
+        return path.resolve(file);
+    });
+    
+    var files = {};
+    var cache = {};
+    var pending = 0;
+    
+    var output = new Stream;
+    output.readable = true;
+    
+    var opts = { cache: cache, includeSource: true };
+    
+    mains.forEach(function (file) {
+        pending ++;
+        var p = 2, src, rows;
+        
+        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.emit('data', files[file]);
+            };
+            
+            walk(rows);
+            if (--pending === 0) output.emit('end');
+        }
+        
+        fs.readFile(file, 'utf8', function (err, s) {
+            if (err) return output.emit('error', err);
+            src = s;
+            if (--p === 0) done();
+        });
+        
+        required(file, opts, function (err, r) {
+            if (err) return output.emit('error', err);
+            rows = r;
+            if (--p === 0) done();
+        });
+    });
+    
+    if (pending === 0) process.nextTick(output.emit.bind(output, 'end'));
+    
+    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;
+            }
+            output.emit('data', r);
+            
+            walk(row.deps);
+        });
+    }
+};

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-javascript/node-module-deps.git



More information about the Pkg-javascript-commits mailing list