1 | var VOW = require('dougs_vow');
|
2 | var Path = require('path');
|
3 | var fs = require('fs-extra');
|
4 |
|
5 | var makeScript = require('./make-script');
|
6 | var modules;
|
7 |
|
8 | var resolve = require('./resolve');
|
9 |
|
10 |
|
11 | var debug;
|
12 |
|
13 |
|
14 | function trailWith(str, trail) {
|
15 | return str ? (str + (!endsWith(str, trail) ? trail : '')) : undefined;
|
16 | };
|
17 |
|
18 | function endsWith(str, trail) {
|
19 | return (str.substr(str.length-trail.length, str.length-1) === trail);
|
20 | };
|
21 |
|
22 |
|
23 | function insertScriptList(files, index, wwwPath, scriptPath, moduleId) {
|
24 | var vow = VOW.make();
|
25 |
|
26 | resolve.resolve(wwwPath,Path.join(scriptPath, Path.dirname(moduleId)), Path.basename(moduleId),
|
27 | function(err, list) {
|
28 | if (err) vow.breek(err);
|
29 | else {
|
30 |
|
31 | files[index] = list;
|
32 |
|
33 | vow.keep(); }
|
34 | }, false, debug);
|
35 | return vow.promise;
|
36 | }
|
37 |
|
38 |
|
39 | function processOneScriptBlock(wwwPath, sb) {
|
40 | var vow = VOW.make();
|
41 | var vows = [];
|
42 | var files = sb.files;
|
43 | var index = 0;
|
44 | var containsModules = false;
|
45 |
|
46 | files.forEach(function(f) {
|
47 |
|
48 |
|
49 | if (typeof f !== 'string') {
|
50 |
|
51 | vows.push(insertScriptList(files, index, wwwPath, sb.path, f[0]));
|
52 | }
|
53 | else vows.push(VOW.kept(sb.files));
|
54 | index++;
|
55 | });
|
56 | if (!vows.length) vow.keep(sb);
|
57 |
|
58 | else VOW.every(vows).when(
|
59 | function() {
|
60 | var newList = [];
|
61 |
|
62 | files.forEach(function(f) {
|
63 | if (typeof f === 'string') newList.push(Path.join(sb.path || '', f));
|
64 | else {
|
65 | f.forEach(function(f) {
|
66 | modules.push(f);
|
67 | var route = f.route;
|
68 |
|
69 |
|
70 | newList.push(route + (f.core ? '' : "?module=" + f.index));
|
71 |
|
72 | });
|
73 | }
|
74 | });
|
75 | sb.files = newList;
|
76 | sb.path = '';
|
77 | vow.keep(sb);
|
78 | },
|
79 | function(err) {
|
80 | vow.breek(err);
|
81 | }
|
82 | );
|
83 | return vow.promise;
|
84 | }
|
85 |
|
86 | function deduplicate(blocks) {
|
87 | var listed = {};
|
88 | blocks.forEach(function(b) {
|
89 | b.files = b.files.filter(function(f) {
|
90 | var path = Path.join(b.path, f);
|
91 | var isDuplicate = listed[path];
|
92 | listed[path] = true;
|
93 | return !isDuplicate;
|
94 | });
|
95 | });
|
96 | }
|
97 |
|
98 | function expand(scriptBlock, wwwPath, cb, isDebug) {
|
99 | debug = isDebug;
|
100 |
|
101 | var denodifyPath = Path.join(wwwPath, 'denodify.js');
|
102 |
|
103 |
|
104 |
|
105 |
|
106 |
|
107 |
|
108 | var vows = [];
|
109 | modules = [];
|
110 | resolve.reset();
|
111 | scriptBlock.forEach(function(sb) {
|
112 |
|
113 | vows.push(processOneScriptBlock(wwwPath, sb));
|
114 | });
|
115 | VOW.every(vows).when(
|
116 | function(blocks) {
|
117 | deduplicate(blocks);
|
118 | blocks[0].files = ['denodify.js'].concat(blocks[0].files);
|
119 | fs.outputFileSync(Path.resolve(denodifyPath), makeScript(modules, Path.resolve(wwwPath)));
|
120 | cb(null, blocks);
|
121 | },
|
122 | function(err) {
|
123 | cb({ err: err}, scriptBlock);
|
124 | });
|
125 | }
|
126 |
|
127 | module.exports = expand;
|
128 |
|
129 |
|
130 |
|
131 |
|
132 |
|
133 |
|
134 |
|
135 |
|
136 |
|
137 |
|
138 |
|
139 |
|
140 |
|