1 | var Path = require('path');
|
2 | var required = require('required');
|
3 | var fs = require('fs-extra');
|
4 | require('colors');
|
5 | var path = [];
|
6 | var index = 0;
|
7 | var modules;
|
8 |
|
9 | var prefix = "denodify('replace',function(require, module, exports, __filename, __dirname, process) {";
|
10 | var postfix = "});";
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 |
|
23 |
|
24 |
|
25 | exports.wrap = function(moduleid, string) {
|
26 | console.log('in nodify.wrap', moduleid);
|
27 | if (string[0] === '\n') string = string.slice(1);
|
28 | var newPrefix = prefix.replace(/replace/g, Path.join('/', moduleid));
|
29 |
|
30 | return newPrefix + string + postfix;
|
31 | };
|
32 |
|
33 |
|
34 |
|
35 |
|
36 | exports.script = fs.readFileSync(__dirname + '/denodify.js');
|
37 |
|
38 |
|
39 |
|
40 |
|
41 |
|
42 |
|
43 |
|
44 |
|
45 |
|
46 |
|
47 |
|
48 |
|
49 |
|
50 |
|
51 |
|
52 |
|
53 |
|
54 | exports.tags = function(www, parent, module, callback, listOnly) {
|
55 | list(www, parent, module, callback, false);
|
56 | };
|
57 |
|
58 |
|
59 |
|
60 |
|
61 |
|
62 | exports.list = function(www, parent, module, callback, listOnly) {
|
63 | list(www, parent, module, callback, true);
|
64 | };
|
65 |
|
66 | exports.debug = false;
|
67 |
|
68 | function walk(module) {
|
69 | modules[module.id] = module;
|
70 | path.push(module.id);
|
71 | module.index = -1;
|
72 | module.deps.forEach(function(d) {
|
73 | d = modules[d.id] || d;
|
74 | if (d.index === undefined) walk(d);
|
75 | else if (d.index < 0) {
|
76 | var str = "Module " + module.id + " is dependent on module " + d.id +
|
77 | '. However, module ' + d.id + ' is also directly or indirectly dependent on module ' +
|
78 | module.id + ".\nDependency path to this point: \n" + path.join(' relies on \n') +
|
79 | ' relies on ' + d.id;
|
80 | console.log(str.red);
|
81 | }
|
82 | });
|
83 | module.index = index++;
|
84 | path.pop();
|
85 | }
|
86 |
|
87 | function endsWith(str, trail) {
|
88 | return (str.substr(str.length-trail.length, str.length-1) === trail);
|
89 | };
|
90 |
|
91 | function trailWith(str, trail) {
|
92 | return str ? (str + (!endsWith(str, trail) ? trail : '')) : undefined;
|
93 | };
|
94 |
|
95 | function list(www, parent, id, cb, listOnly) {
|
96 | try
|
97 | { www = Path.resolve(www);
|
98 | parent = Path.resolve(www, parent);
|
99 | debug('Resolving: ' + id + ' in directory ' + parent);
|
100 | var fileName = Path.resolve(parent, trailWith(id, '.js'));
|
101 | try {
|
102 | fs.statSync(fileName);
|
103 | } catch(e) { cb(e,null);
|
104 | return;}
|
105 |
|
106 | required(fileName, {
|
107 | includeSource: false
|
108 | }, function(err, deps) {
|
109 | if (err) throw err;
|
110 | else {
|
111 |
|
112 | modules = [];
|
113 | walk({
|
114 | id: id,
|
115 | filename: fileName,
|
116 | deps: deps,
|
117 | index: -1
|
118 | });
|
119 | var list = Object.keys(modules).map(function(m) {
|
120 | m = modules[m];
|
121 | var startWithWwwPath = m.filename.indexOf(www) === 0;
|
122 | if (!startWithWwwPath)
|
123 | throw 'Warning: ' + m.id + ' was found outside the www directory (' + www + ')';
|
124 | m.route = m.filename.slice(www.length);
|
125 | debug('module:',m);
|
126 | return m;
|
127 | }).sort(function(a, b) {
|
128 | return a.index > b.index;
|
129 | }).map(function(m) {
|
130 | return listOnly ?
|
131 | { id: m.id, route: m.route, filename: m.filename } :
|
132 | "<script type=\"text/javascript\" src=\"" + m.route + "\"></script>";
|
133 | });
|
134 | debug('Debug:\n', list);
|
135 | cb(null, list);
|
136 | }
|
137 | });
|
138 | } catch(e) {
|
139 | cb(e, null);
|
140 | }
|
141 | };
|
142 |
|
143 | function debug() {
|
144 | if (exports.debug) console.log.apply(console, arguments);
|
145 | }
|
146 |
|
147 |
|
148 |
|
149 |
|
150 |
|
151 |
|
152 |
|