[Pkg-javascript-commits] [node-module-deps] 140/444: set the pkg properly for the initial mains list

Bastien Roucariès rouca at moszumanska.debian.org
Fri Dec 15 09:47:50 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 8a6881e85b14ffda7a74a43bbafa80f4a26ea00d
Author: James Halliday <mail at substack.net>
Date:   Sat Dec 28 19:29:00 2013 -0800

    set the pkg properly for the initial mains list
---
 index.js          | 47 ++++++++++++++++++++++++++++++++++++-----------
 test/tr_global.js | 11 ++++-------
 2 files changed, 40 insertions(+), 18 deletions(-)

diff --git a/index.js b/index.js
index f34d13d..781cde0 100644
--- a/index.js
+++ b/index.js
@@ -11,7 +11,7 @@ var concat = require('concat-stream');
 module.exports = function (mains, opts) {
     if (!opts) opts = {};
     var cache = opts.cache;
-    var pkgCache = opts.packageCache;
+    var pkgCache = opts.packageCache || {};
     
     if (!Array.isArray(mains)) mains = [ mains ].filter(Boolean);
     var basedir = opts.basedir || process.cwd();
@@ -38,17 +38,41 @@ module.exports = function (mains, opts) {
     }
     
     var top = { id: '/', filename: '/', paths: [] };
-    mains.forEach(function (main, ix) {
-        if (typeof main === 'object') {
-            walk({ stream: main, file: entries[ix] }, top);
-        }
-        else walk(main, top)
-    });
     
-    if (mains.length === 0) {
-        output.pause();
-        output.queue(null);
-        process.nextTick(function () { output.resume() });
+    (function () {
+        var pkgCount = mains.length;
+        if (pkgCount === 0) next();
+        
+        mains.forEach(function (main) {
+            if (typeof main === 'object') return done();
+            var id = path.resolve(basedir, main);
+            if (pkgCache[id]) return done();
+            
+            var pkgfile = path.join(path.dirname(main), 'package.json');
+            fs.readFile(pkgfile, function (err, src) {
+                if (err) return done();
+                try { var pkg = JSON.parse(src) }
+                catch (err) { return done() }
+                pkgCache[id] = pkg;
+                done();
+            });
+        });
+        function done () { if (--pkgCount === 0) next() }
+    })();
+    
+    function next () {
+        mains.forEach(function (main, ix) {
+            if (typeof main === 'object') {
+                walk({ stream: main, file: entries[ix] }, top);
+            }
+            else walk(main, top)
+        });
+        
+        if (mains.length === 0) {
+            output.pause();
+            output.queue(null);
+            process.nextTick(function () { output.resume() });
+        }
     }
     
     return output;
@@ -101,6 +125,7 @@ module.exports = function (mains, opts) {
             }
             
             visited[file] = true;
+            if (!pkg && pkgCache[file]) pkg = pkgCache[file];
             
             var trx = getTransform(pkg);
             
diff --git a/test/tr_global.js b/test/tr_global.js
index 72b82c8..9e21085 100644
--- a/test/tr_global.js
+++ b/test/tr_global.js
@@ -2,6 +2,7 @@ var mdeps = require('../');
 var test = require('tap').test;
 var JSONStream = require('JSONStream');
 var packer = require('browser-pack');
+var concat = require('concat-stream');
 
 test('global transforms', function (t) {
     t.plan(1);
@@ -16,15 +17,11 @@ test('global transforms', function (t) {
     });
     var pack = packer();
     
-    p.pipe(JSONStream.stringify()).pipe(pack);
-    
-    var src = '';
-    pack.on('data', function (buf) { src += buf });
-    pack.on('end', function () {
-        Function(['console','t'], src)(t, {
+    p.pipe(JSONStream.stringify()).pipe(pack).pipe(concat(function (src) {
+        Function(['console'], src)({
             log: function (msg) {
                 t.equal(msg, '111111');
             }
         });
-    });
+    }));
 });

-- 
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