| 1 |
var __extends = this.__extends || function (d, b) { |
| 2 |
for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; |
| 3 |
function __() { this.constructor = d; } |
| 4 |
__.prototype = b.prototype; |
| 5 |
d.prototype = new __(); |
| 6 |
}; |
| 7 |
///<reference path="typings/lib.core.es6.d.ts" /> |
| 8 |
///<reference path="typings/console.d.ts" /> |
| 9 |
///<reference path="typings/node/node.d.ts" /> |
| 10 |
var path = require("path"); |
| 11 |
var fs = require("fs"); |
| 12 |
var events = require("events"); |
| 13 |
var child_process = require("child_process"); |
| 14 |
var Paths = (function () { |
| 15 |
function Paths() { |
| 16 |
this.bin = path.join(__dirname, ".."); |
| 17 |
this.base = path.join(this.bin, ".."); |
| 18 |
this.src = path.join(this.base, "src"); |
| 19 |
this.ts = path.join(this.src, "ts"); |
| 20 |
this.js = path.join(this.src, "js"); |
| 21 |
this.doc = path.join(this.base, "doc"); |
| 22 |
this.lib = path.join(this.base, "lib"); |
| 23 |
} |
| 24 |
return Paths; |
| 25 |
})(); |
| 26 |
var Config = (function () { |
| 27 |
function Config() { |
| 28 |
this.paths = new Paths(); |
| 29 |
} |
| 30 |
return Config; |
| 31 |
})(); |
| 32 |
var Task = (function (_super) { |
| 33 |
__extends(Task, _super); |
| 34 |
function Task(config, name) { |
| 35 |
_super.call(this); |
| 36 |
this.config = config; |
| 37 |
if (name === undefined) { |
| 38 |
if (this["constructor"] && this["constructor"].name) |
| 39 |
this.name = this["constructor"].name; |
| 40 |
else |
| 41 |
this.name = "Undefined Task"; |
| 42 |
} |
| 43 |
else |
| 44 |
this.name = name; |
| 45 |
this.init(); |
| 46 |
} |
| 47 |
Task.prototype.run = function () { |
| 48 |
this.emit("done"); |
| 49 |
return true; |
| 50 |
}; |
| 51 |
Task.prototype.init = function () { |
| 52 |
}; |
| 53 |
return Task; |
| 54 |
})(events.EventEmitter); |
| 55 |
var CompileTask = (function (_super) { |
| 56 |
__extends(CompileTask, _super); |
| 57 |
function CompileTask() { |
| 58 |
_super.apply(this, arguments); |
| 59 |
} |
| 60 |
CompileTask.prototype.run = function () { |
| 61 |
var _this = this; |
| 62 |
var cp = child_process; |
| 63 |
var cmd = "tsc -p " + this.config.paths.ts; |
| 64 |
if (!this.comment) |
| 65 |
cmd += " --removeComments"; |
| 66 |
if (this.dist) |
| 67 |
cmd += " -out \"" + this.dist + "\""; |
| 68 |
if (this.declaration) |
| 69 |
cmd += " --declaration"; |
| 70 |
cp.exec(cmd, function (error, stdout, stderr) { |
| 71 |
if (error) { |
| 72 |
console.error("build error: " + error); |
| 73 |
_this.emit("error", error); |
| 74 |
return; |
| 75 |
} |
| 76 |
_this.emit("done"); |
| 77 |
}); |
| 78 |
return true; |
| 79 |
}; |
| 80 |
return CompileTask; |
| 81 |
})(Task); |
| 82 |
//TODO: ���������jsdoc��������������������������������������������������������������������� |
| 83 |
var DocumentTask = (function (_super) { |
| 84 |
__extends(DocumentTask, _super); |
| 85 |
function DocumentTask() { |
| 86 |
_super.apply(this, arguments); |
| 87 |
} |
| 88 |
DocumentTask.prototype.run = function () { |
| 89 |
//nothing todo |
| 90 |
this.emit("done"); |
| 91 |
return true; |
| 92 |
}; |
| 93 |
return DocumentTask; |
| 94 |
})(Task); |
| 95 |
var UnlinkTask = (function (_super) { |
| 96 |
__extends(UnlinkTask, _super); |
| 97 |
function UnlinkTask() { |
| 98 |
_super.apply(this, arguments); |
| 99 |
} |
| 100 |
UnlinkTask.prototype.run = function () { |
| 101 |
var _this = this; |
| 102 |
this.targets = this.targets ? this.targets : [this.target]; |
| 103 |
this.cnt = 0; |
| 104 |
this.targets.forEach(function (target) { |
| 105 |
_this.process(target); |
| 106 |
}); |
| 107 |
return true; |
| 108 |
}; |
| 109 |
UnlinkTask.prototype.process = function (path) { |
| 110 |
var _this = this; |
| 111 |
fs.unlink(path, function (error) { |
| 112 |
if (_this._exit) |
| 113 |
return; |
| 114 |
if (error) { |
| 115 |
_this._exit = true; |
| 116 |
console.error("Can not delete " + path); |
| 117 |
_this.emit("error", error); |
| 118 |
} |
| 119 |
else { |
| 120 |
_this.cnt++; |
| 121 |
if (_this.cnt == _this.targets.length) { |
| 122 |
_this._exit = true; |
| 123 |
_this.emit("done"); |
| 124 |
} |
| 125 |
} |
| 126 |
}); |
| 127 |
}; |
| 128 |
return UnlinkTask; |
| 129 |
})(Task); |
| 130 |
var CopyTask = (function (_super) { |
| 131 |
__extends(CopyTask, _super); |
| 132 |
function CopyTask() { |
| 133 |
_super.apply(this, arguments); |
| 134 |
} |
| 135 |
CopyTask.prototype.run = function () { |
| 136 |
var _this = this; |
| 137 |
var rd = fs.createReadStream(this.src); |
| 138 |
var wr = fs.createWriteStream(this.dist); |
| 139 |
rd.on("error", function (error) { |
| 140 |
_this.emit("error", error); |
| 141 |
}); |
| 142 |
wr.on("error", function (error) { |
| 143 |
_this.emit("error", error); |
| 144 |
}); |
| 145 |
wr.on("close", function (ex) { |
| 146 |
if (!_this.remove) { |
| 147 |
_this.emit("done"); |
| 148 |
return; |
| 149 |
} |
| 150 |
var unlink = new UnlinkTask(_this.config); |
| 151 |
unlink.target = _this.src; |
| 152 |
unlink.on("error", function (error) { |
| 153 |
_this.emit("error", error); |
| 154 |
}); |
| 155 |
unlink.on("done", function () { |
| 156 |
_this.emit("done"); |
| 157 |
}); |
| 158 |
unlink.run(); |
| 159 |
}); |
| 160 |
rd.pipe(wr); |
| 161 |
return true; |
| 162 |
}; |
| 163 |
return CopyTask; |
| 164 |
})(Task); |
| 165 |
var MinifiedTask = (function (_super) { |
| 166 |
__extends(MinifiedTask, _super); |
| 167 |
function MinifiedTask() { |
| 168 |
_super.apply(this, arguments); |
| 169 |
} |
| 170 |
MinifiedTask.prototype.run = function () { |
| 171 |
var _this = this; |
| 172 |
var UglifyJS = require("uglify-js"); |
| 173 |
var result = UglifyJS.minify(this.src); |
| 174 |
var wr = fs.createWriteStream(this.dist); |
| 175 |
wr.on("error", function (error) { |
| 176 |
_this.emit("error", error); |
| 177 |
}); |
| 178 |
wr.on("close", function (ex) { |
| 179 |
_this.emit("done"); |
| 180 |
}); |
| 181 |
wr.write(result.code); |
| 182 |
wr.end(); |
| 183 |
return true; |
| 184 |
}; |
| 185 |
return MinifiedTask; |
| 186 |
})(Task); |
| 187 |
var DeployTask = (function (_super) { |
| 188 |
__extends(DeployTask, _super); |
| 189 |
function DeployTask() { |
| 190 |
_super.apply(this, arguments); |
| 191 |
} |
| 192 |
DeployTask.prototype.run = function () { |
| 193 |
this.sub_tasks = []; |
| 194 |
var jgame_js = new CopyTask(this.config); |
| 195 |
jgame_js.src = path.join(this.config.paths.js, "jgame.js"); |
| 196 |
jgame_js.dist = path.join(this.config.paths.base, "jgame.js"); |
| 197 |
var jgame_d_ts = new CopyTask(this.config); |
| 198 |
jgame_d_ts.src = path.join(this.config.paths.ts, "jgame.d.ts"); |
| 199 |
jgame_d_ts.dist = path.join(this.config.paths.lib, "jgame.d.ts"); |
| 200 |
jgame_d_ts.remove = true; |
| 201 |
var jgame_min_js = new MinifiedTask(this.config); |
| 202 |
jgame_min_js.src = path.join(this.config.paths.js, "jgame.js"); |
| 203 |
jgame_min_js.dist = path.join(this.config.paths.base, "jgame.min.js"); |
| 204 |
this.sub_tasks.push(jgame_js); |
| 205 |
this.sub_tasks.push(jgame_d_ts); |
| 206 |
this.sub_tasks.push(jgame_min_js); |
| 207 |
for (var i = 0; i < this.sub_tasks.length; i++) |
| 208 |
this.processTask(this.sub_tasks[i]); |
| 209 |
return true; |
| 210 |
}; |
| 211 |
DeployTask.prototype.processTask = function (task) { |
| 212 |
var _this = this; |
| 213 |
task.on("error", function (error) { |
| 214 |
if (_this._exit) |
| 215 |
return; |
| 216 |
_this._exit = true; |
| 217 |
var mes = task.name + " error: " + error; |
| 218 |
console.error(mes); |
| 219 |
_this.emit("error", mes); |
| 220 |
}); |
| 221 |
task.on("done", function (error) { |
| 222 |
console.log("done!" + task.name + "," + task["src"]); |
| 223 |
if (_this._exit) |
| 224 |
return; |
| 225 |
_this.sub_tasks.splice(_this.sub_tasks.indexOf(task), 1); |
| 226 |
if (_this.sub_tasks.length == 0) { |
| 227 |
_this._exit = true; |
| 228 |
_this.emit("done"); |
| 229 |
} |
| 230 |
}); |
| 231 |
return task.run(); |
| 232 |
}; |
| 233 |
return DeployTask; |
| 234 |
})(Task); |
| 235 |
var TaskManager = (function (_super) { |
| 236 |
__extends(TaskManager, _super); |
| 237 |
function TaskManager() { |
| 238 |
_super.apply(this, arguments); |
| 239 |
} |
| 240 |
TaskManager.prototype.init = function () { |
| 241 |
this.tasks = []; |
| 242 |
}; |
| 243 |
TaskManager.prototype.addTask = function (task) { |
| 244 |
this.tasks.push(task); |
| 245 |
}; |
| 246 |
TaskManager.prototype._finishTask = function (task) { |
| 247 |
console.timeEnd(task.name); |
| 248 |
console.log("--------------------"); |
| 249 |
console.log(" "); |
| 250 |
this.nextTask(); |
| 251 |
}; |
| 252 |
TaskManager.prototype.nextTask = function () { |
| 253 |
var _this = this; |
| 254 |
if (this.tasks.length == this.taskIndex) { |
| 255 |
this.emit("done", true); |
| 256 |
return false; |
| 257 |
} |
| 258 |
var task = this.tasks[this.taskIndex++]; |
| 259 |
console.time(task.name); |
| 260 |
console.log("-----" + task.name + "-----"); |
| 261 |
task.on("done", function () { |
| 262 |
_this._finishTask(task); |
| 263 |
}); |
| 264 |
task.on("error", function () { |
| 265 |
console.error(task.name + " is error"); |
| 266 |
_this._finishTask(task); |
| 267 |
}); |
| 268 |
return task.run(); |
| 269 |
}; |
| 270 |
TaskManager.prototype.run = function () { |
| 271 |
this.taskIndex = 0; |
| 272 |
if (this.tasks.length == 0) { |
| 273 |
//this.emit("error", "no task/"); |
| 274 |
throw "no task"; |
| 275 |
} |
| 276 |
this.nextTask(); |
| 277 |
return true; |
| 278 |
}; |
| 279 |
return TaskManager; |
| 280 |
})(Task); |
| 281 |
module.exports = function () { |
| 282 |
var argv = require("argv"); |
| 283 |
argv.info("jgame.js builder\nSupported only \"node build deploy\" command."); |
| 284 |
argv.version("1.0"); |
| 285 |
var args = argv.run(); |
| 286 |
var mode = args.targets.length > 0 ? args.targets[0] : null; |
| 287 |
var modes = { |
| 288 |
deploy: [] |
| 289 |
}; |
| 290 |
if (!mode || !modes[mode]) { |
| 291 |
argv.help(); |
| 292 |
return; |
| 293 |
} |
| 294 |
var config = new Config(); |
| 295 |
var compiler = new CompileTask(config, "TypeScript Compiler"); |
| 296 |
var define = new CompileTask(config, "Define Builder"); |
| 297 |
compiler.dist = path.join(config.paths.js, "jgame.js"); |
| 298 |
define.dist = path.join(config.paths.ts, "jgame.js"); |
| 299 |
define.declaration = true; |
| 300 |
define.comment = true; |
| 301 |
modes.deploy.push(compiler); |
| 302 |
modes.deploy.push(define); |
| 303 |
modes.deploy.push(new DeployTask(config, "Publisher")); |
| 304 |
modes.deploy.push(new DocumentTask(config, "Document Generator")); |
| 305 |
var cleaner = new UnlinkTask(config, "Cleaner"); |
| 306 |
cleaner.targets = [ |
| 307 |
path.join(config.paths.ts, "jgame.js") |
| 308 |
]; |
| 309 |
modes.deploy.push(cleaner); |
| 310 |
var manager = new TaskManager(config); |
| 311 |
for (var i = 0; i < modes[mode].length; i++) { |
| 312 |
var task = modes[mode][i]; |
| 313 |
manager.addTask(task); |
| 314 |
} |
| 315 |
manager.run(); |
| 316 |
manager.on("done", function () { |
| 317 |
console.error("finished."); |
| 318 |
}); |
| 319 |
manager.on("error", function () { |
| 320 |
console.error("stopped."); |
| 321 |
}); |
| 322 |
}; |