-
Notifications
You must be signed in to change notification settings - Fork 6
/
masterUtils.js
246 lines (216 loc) · 6.51 KB
/
masterUtils.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
/*jslint node: true */
"use strict";
var path = require('path');
var fs = require('fs');
var async = require('async');
var bower = require('bower');
// var mainBowerFiles = require('main-bower-files');
var U=require("underscore");
var npm = require("npm");
var glob = require("glob");
var config=require('./core/config.js');
module.exports.generateBowers = function(cb) {
var mainBower = {
name: "master",
dependencies: {}
};
async.each(Object.keys(config.masterModules), function(moduleName, cb) {
var module = config.masterModules[moduleName];
var bModuleName = "masterModule_" + moduleName;
var jsonFile = path.join(module.dir, 'master.json');
fs.exists(jsonFile, function(exists) {
if (exists) {
mainBower.dependencies[bModuleName]= module.dir;
fs.readFile(jsonFile, function(err, contents) {
if (err) return cb(err);
var mMasterConfig;
try {
mMasterConfig = JSON.parse(contents);
} catch (err2) {
return cb(err2);
}
var mBowerConfig = {
name: bModuleName,
dependencies: mMasterConfig.bowerDependencies,
resolutions: mMasterConfig.bowerResolutions,
ignore: ["*", "!bower.json"]
};
fs.writeFile(path.join(module.dir, "bower.json"), JSON.stringify(mBowerConfig, null, 1), cb);
});
} else {
cb();
}
});
}, function(err) {
if (err) {
return cb(err);
}
fs.writeFile(path.join(process.cwd(), "bower.json"), JSON.stringify(mainBower, null, 1), function(err) {
if (err) {
console.error(err);
return cb(err);
}
bower.commands.update().on('end', function(results) {
cb();
}).on('error', function(err) {
return cb(err);
}).on('prompt', function(prompts, callback) {
var err = new Error("Bower prompts event: " + prompts);
return cb(err);
});
});
});
};
module.exports.generateNpmPackages = function(cb) {
var mainNpm = {
name: "master",
dependencies: {
winston: "^0.9.0",
nconf: "^0.7.1"
}
};
async.each(Object.keys(config.masterModules), function(moduleName, cb) {
var module = config.masterModules[moduleName];
var jsonFile = path.join(module.dir, 'master.json');
fs.exists(jsonFile, function(exists) {
if (exists) {
fs.readFile(jsonFile, function(err, contents) {
if (err) return cb(err);
var mMasterConfig;
try {
mMasterConfig = JSON.parse(contents);
} catch (err2) {
return cb(err2);
}
for (var d in mMasterConfig.npmDependencies) {
mainNpm.dependencies[d] = mMasterConfig.npmDependencies[d];
}
cb();
});
} else {
cb();
}
});
}, function(err) {
if (err) return (err);
fs.readFile(path.join(process.cwd(), "master.json"), function(err, contents) {
if (!err) {
var mainConfig;
try {
mainConfig = JSON.parse(contents);
} catch (err2) {
}
for (var d in mainConfig.npmDependencies) {
mainNpm.dependencies[d] = mainConfig.npmDependencies[d];
}
mainNpm.version = mainConfig.version || "0.0.1";
mainNpm.description = mainConfig.description || "";
mainNpm.repository = mainConfig.repository || ".";
mainNpm.license = mainConfig.license || "Private";
}
fs.writeFile(path.join(process.cwd(), "package.json"), JSON.stringify(mainNpm, null, 1), function(err) {
if (err) return err;
npm.load({}, function (err, npm) {
// use the npm object, now that it's loaded.
//
if (err) return cb(err);
npm.commands.update(cb);
});
});
});
});
};
function ensureExists(path, mask, cb) {
if (typeof mask == 'function') { // allow the `mask` parameter to be optional
cb = mask;
mask = '0777';
}
fs.mkdir(path, mask, function(err) {
if (err) {
if (err.code == 'EEXIST') cb(null); // ignore the error if the folder already exists
else cb(err); // something else went wrong
} else cb(null); // successfully created folder
});
}
function getHeadBodyTemplates(cb) {
var headTemplates = [];
var bodyTemplates = [];
async.each(Object.keys(config.masterModules), function(moduleName, cb) {
var module = config.masterModules[moduleName];
async.parallel([
function(cb3) {
var headFile=path.join(module.dir, "client", "head.jade");
fs.exists(headFile , function(exist) {
if (exist) {
headTemplates.push(headFile);
}
cb3();
});
},
function(cb3) {
var bodyFile=path.join(module.dir, "client", "body.jade");
fs.exists(bodyFile , function(exist) {
if (exist) {
bodyTemplates.push(bodyFile);
}
cb3();
});
},
], cb);
}, function(err) {
if (err) return cb(err);
cb(null, headTemplates, bodyTemplates);
});
}
module.exports.generateIndexJade = function(cb) {
var tmpPath = path.join(process.cwd(), "tmp");
getHeadBodyTemplates(function(err, headTemplates, bodyTemplates) {
if (err) return cb(err);
var jsFiles = [];
var cssFiles = [];
var html = [];
html.push("doctype html");
html.push('html(lang="en")');
html.push('\thead');
headTemplates.forEach(function(f) {
html.push("\t\tinclude "+path.relative(tmpPath, f));
});
html.push('\t\tlink(rel="stylesheet",href="app.css")');
html.push("\tbody");
bodyTemplates.forEach(function(f) {
html.push("\t\tinclude "+path.relative(tmpPath, f));
});
html.push('\t\tscript(src="app.js")');
html.push('');
ensureExists(tmpPath, function(err) {
if (err) return cb();
fs.writeFile(path.join(tmpPath, "index.jade"), html.join("\n"), cb);
});
});
};
var quoteString = function(S) {
return '"'+S.replace(/(["\s'$`\\])/g,'\\$1')+'"';
};
module.exports.generateRequiresModule = function(cb) {
var output = "";
async.each(Object.keys(config.masterModules), function(moduleName, cb) {
var module = config.masterModules[moduleName];
glob(path.join(process.cwd(), module.dir, 'client', '**', '*.js' ), {realpath:true, ignore: path.join(process.cwd(), module.dir, 'client', 'static', '**' ) }, function(err, files) {
U.each(files, function(filename) {
output += "require(" + quoteString(filename) +");\n";
});
cb();
});
}, function(err) {
if (err) return cb(err);
ensureExists(path.join(process.cwd(), "tmp"), function(err2) {
fs.writeFile(path.join(process.cwd(), "tmp", "modules.js"), output, cb);
});
});
};
module.exports.generateClientConfigModule = function(cb) {
ensureExists(path.join(process.cwd(), "tmp"), function(err) {
if (err) return cb(err);
fs.writeFile(path.join(process.cwd(), "tmp", "client_config.js"), "module.exports = " + JSON.stringify(config.clientConfig, null, 1) + ";\n", cb);
});
};