UNPKG

3.73 kBJavaScriptView Raw
1var fs = require('fs-extra');
2var Path = require('path');
3
4//Returns a string with the javascript code (denodify-browser.js) that needs to
5//be loaded in any web app that uses denodify-wrapped nodejs modules,
6
7//Pass in the list of modules with their dependencies and requirers as produced
8//by denodify.expand. A key value object is added to denodify-browser so that it
9//can resolve the require calls in any module.
10
11function makeScript(modules, wwwPath) {
12 modules = modules || [];
13 var byIndex = {};
14 var byId = {};
15
16 //A module looks like this:
17 // { filename: '/home/michieljoris/mysrc/javascript/denodify/www/scripts/b1.js',
18 // deps: [ 'cachejs', '../b1' ],
19 // requirers:
20 // [ '/home/michieljoris/mysrc/javascript/denodify/www/scripts/modules/r1.js',
21 // '/home/michieljoris/mysrc/javascript/denodify/www/scripts/modules/mymodule.js' ],
22 // ids: [ '../b1', '../b1.js' ],
23 // walked: true,
24 // index: 4,
25 // route: 'scripts/b1.js',
26 // resolve: { cachejs: 2, '../b1': 3 } },
27 //as produced by expand.
28
29
30 //Two new maps are extracted from this list of modules, one by index, and
31 //the other by id. One id can refer to different files so this needs to be
32 //untangled. These maps will help doing this.
33 modules.forEach(function(m) {
34 if (m.core) byIndex[m.ids[0]] = m;
35 else byIndex[m.index] = m;
36 if (m.ids)
37 m.ids.forEach(function(id) {
38 byId[id] = byId[id] || [];
39 byId[id].push(m);
40 });
41
42 });
43
44 //By going through all the modules once more, a new field can be added to
45 //every module that resolves every dependency (as in require('dependency'))
46 //to the proper file
47 modules.forEach(function(m) {
48 m.resolve = {};
49 if (m.deps) {
50 m.deps.forEach(function(d) {
51 if (byId[d]) {
52 if (byId[d].length > 1) {
53 m.resolve[d] = (function() {
54 var f;
55 byId[d].some(function(e) {
56 f = e;
57 return e.requirers && e.requirers.indexOf(m.filename) !== -1;
58 });
59 return f.index;
60 }());
61 }
62 else { m.resolve[d] = byId[d][0].core ? d : byId[d][0].index; }
63 }
64 });
65 }
66 });
67 // console.log('Byid:\n', byId);
68 console.log('By index:\n', byIndex);
69
70 //we need to make the softlinks for the lib modules that were requiredk
71 Object.keys(byIndex).forEach(function(k) {
72 // console.log(inde);
73 var m = byIndex[k];
74 if (m.lib) {
75 console.log('making softlink:\n', Path.join(wwwPath, m.route) + ' --> ' +
76 m.filename);
77 try {
78 fs.symlinkSync(m.filename, Path.join(wwwPath, m.route) );
79 }
80 catch(e) {
81 console.log(e);
82 }
83 }
84 });
85 // console.log(modules);
86
87 //strip the modules to the essentials:
88 var stripped = {};
89 Object.keys(byIndex).forEach(function(k) {
90 stripped[k] = { filename: byIndex[k].route, resolve: byIndex[k].resolve };
91 });
92
93 // console.log(stripped);
94 //get the template:
95 var script = fs.readFileSync(__dirname + '/denodify-browser.js');
96
97 //add the modules data:
98 script = script.slice(0, script.length-10) +
99 '\n//module info' +
100 '\n var m = ' + JSON.stringify(stripped, null, '\t') + ';' +
101 '\n})(this);\n';
102 return script;
103}
104
105module.exports = makeScript;