UNPKG

4.68 kBJavaScriptView Raw
1var VOW = require('dougs_vow');
2var Path = require('path');
3var fs = require('fs-extra');
4
5var makeScript = require('./make-script');
6var modules;
7
8var resolve = require('./resolve');
9
10
11var debug;
12
13
14function trailWith(str, trail) {
15 return str ? (str + (!endsWith(str, trail) ? trail : '')) : undefined;
16};
17
18function endsWith(str, trail) {
19 return (str.substr(str.length-trail.length, str.length-1) === trail);
20};
21
22
23function insertScriptList(files, index, wwwPath, scriptPath, moduleId) {
24 var vow = VOW.make();
25 // console.log(moduleId);
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 // files[index] = list.map(function(m) { return m.route; });
31 files[index] = list;
32 // console.log('found modules:\n', files[index]);
33 vow.keep(); }
34 }, false, debug); //tags, debug;
35 return vow.promise;
36}
37
38// function processOneScriptBlock(wwwPath, sb, denodifyPath) {
39function 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 //TODO to be autodetected later by being clever using recast, detective and caching
48 //for now just indicate it is a module by clamping it within [ and ]
49 if (typeof f !== 'string') {
50 // containsModules = true;
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 // if (containsModules) newList.push(denodifyPath);
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 // var ext = Path.extname(route);
69 // route = Path.dirname(route) + '/' + Path.basename(route, ext) + ext;
70 newList.push(route + (f.core ? '' : "?module=" + f.index));
71 // console.log(f);
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
86function 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
98function expand(scriptBlock, wwwPath, cb, isDebug) {
99 debug = isDebug;
100 //make sure there is a denodify script in the scripts directory to load
101 var denodifyPath = Path.join(wwwPath, 'denodify.js');
102 // try {
103 // fs.statSync(Path.resolve(denodifyPath));
104 // } catch (e) {
105 // console.log('demodularify: scripts/denodify not found, adding one.'.red);
106 // fs.outputFileSync(Path.resolve(denodifyPath), makeScript());
107 // }
108 var vows = [];
109 modules = [];
110 resolve.reset();
111 scriptBlock.forEach(function(sb) {
112 // vows.push(processOneScriptBlock(wwwPath, sb, Path.join(sb.path || '', 'denodify.js')));
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
127module.exports = expand;
128
129// //----------------test----------------
130// demodularify([
131// { id:'b1', path: 'somepath', files: ['a', 'b', 'c', ['modules/m1'], 'e', '/modules/m2', 'f']}
132// ,{ id:'b2', path: 'somepath', files: ['g', 'f', 'h', 'modules/m3', 'i', '/modules/m4', 'j']}
133// ], Path.resolve('./test'), function(err, sb) {
134// if (err) console.log(err);
135// else console.log(sb);
136// });
137
138// var p = 'a/b/c/name.ext';
139// console.log(Path.dirname(p));
140// console.log(Path);