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 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 | 1 2 2 1 1 1 1 1 1 1 1 1 1 1 1 61 61 1 1 5 5 5 5 5 5 5 5 5 5 1 5 5 5 1 1 1 1 1 1 1 1 376 376 376 1 1 10 10 10 10 10 1 3 3 3 3 3 1 1 1 193 193 193 193 193 1 163 163 163 163 163 1 7 7 7 1 6 7 1 128 128 128 128 128 128 128 128 1 111 111 111 111 111 1 111 111 111 111 111 111 111 111 1 2 2 2 2 2 1 54 54 1 12 1 11 11 11 11 11 11 11 11 11 11 1 13 1 11 1 1 1660 1660 1660 1660 1660 1 1 6 6 6 6 1 1 30 30 30 27 1 6 6 6 1 5 6 1 2 2 2 2 1 1 1 1 1 1 1 120 120 120 1614 1614 1614 1614 104 104 6 104 1510 120 1 2 2 2 2 2 2 127 2 2 2 2 2 2 1 1 1 1 1 | ///<reference path=".d.ts"/> "use strict"; var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) { Iif (typeof Reflect === "object" && typeof Reflect.decorate === "function") return Reflect.decorate(decorators, target, key, desc); switch (arguments.length) { case 2: return decorators.reduceRight(function(o, d) { return (d && d(o)) || o; }, target); case 3: return decorators.reduceRight(function(o, d) { return (d && d(target, key)), void 0; }, void 0); case 4: return decorators.reduceRight(function(o, d) { return (d && d(target, key, o)) || o; }, desc); } }; var fs = require("fs"); var Future = require("fibers/future"); var path = require("path"); var rimraf = require("rimraf"); var minimatch = require("minimatch"); var decorators = require("./decorators"); var injector = require("./yok"); var crypto = require("crypto"); var FileSystem = (function () { function FileSystem($injector, $hostInfo) { this.$injector = $injector; this.$hostInfo = $hostInfo; } FileSystem.prototype.zipFiles = function (zipFile, files, zipPathCallback) { var $logger = this.$injector.resolve("logger"); var zipstream = require("zipstream"); var zip = zipstream.createZip({ level: 9 }); var outFile = fs.createWriteStream(zipFile); zip.pipe(outFile); var result = new Future(); outFile.on("error", function (err) { return result.throw(err); }); var fileIdx = -1; var zipCallback = function () { fileIdx++; if (fileIdx < files.length) { var file = files[fileIdx]; var relativePath = zipPathCallback(file); relativePath = relativePath.replace(/\\/g, "/"); $logger.trace("zipping as '%s' file '%s'", relativePath, file); zip.addFile(fs.createReadStream(file), { name: relativePath }, zipCallback); } else { outFile.on("finish", function () { return result.return(); }); zip.finalize(function (bytesWritten) { $logger.debug("zipstream: %d bytes written", bytesWritten); outFile.end(); }); } }; zipCallback(); return result; }; FileSystem.prototype.unzip = function (zipFile, destinationDir, options, fileFilters) { var _this = this; return (function () { var shouldOverwriteFiles = !(options && options.overwriteExisitingFiles === false); var isCaseSensitive = !(options && options.caseSensitive === false); _this.createDirectory(destinationDir).wait(); var proc; Iif (_this.$hostInfo.isWindows) { proc = path.join(__dirname, "resources/platform-tools/unzip/win32/unzip"); } else Eif (_this.$hostInfo.isDarwin) { proc = "unzip"; } else if (_this.$hostInfo.isLinux) { proc = "unzip"; } if (!isCaseSensitive) { zipFile = _this.findFileCaseInsensitive(zipFile); } var args = _.flatten(["-b", shouldOverwriteFiles ? "-o" : "-n", isCaseSensitive ? [] : "-C", zipFile, fileFilters || [], "-d", destinationDir]); var $childProcess = _this.$injector.resolve("childProcess"); $childProcess.spawnFromEvent(proc, args, "close", { stdio: "ignore", detached: true }).wait(); }).future()(); }; FileSystem.prototype.findFileCaseInsensitive = function (file) { var dir = path.dirname(file); var basename = path.basename(file); var entries = this.readDirectory(dir).wait(); var match = minimatch.match(entries, basename, { nocase: true, nonegate: true, nonull: true })[0]; var result = path.join(dir, match); return result; }; FileSystem.prototype.exists = function (path) { var future = new Future(); fs.exists(path, function (exists) { return future.return(exists); }); return future; }; FileSystem.prototype.tryExecuteFileOperation = function (path, operation, enoentErrorMessage) { var _this = this; return (function () { try { operation().wait(); } catch (e) { _this.$injector.resolve("$logger").trace("tryExecuteFileOperation failed with error %s.", e); if (enoentErrorMessage) { var message = (e.code === "ENOENT") ? enoentErrorMessage : e.message; _this.$injector.resolve("$errors").failWithoutHelp(message); } } }).future()(); }; FileSystem.prototype.deleteFile = function (path) { var future = new Future(); fs.unlink(path, function (err) { Iif (err && err.code !== "ENOENT") { future.throw(err); } else { future.return(); } }); return future; }; FileSystem.prototype.deleteDirectory = function (directory) { var future = new Future(); rimraf(directory, function (err) { Iif (err) { future.throw(err); } else { future.return(); } }); return future; }; FileSystem.prototype.getFileSize = function (path) { var _this = this; return (function () { var stat = _this.getFsStats(path).wait(); return stat.size; }).future()(); }; FileSystem.prototype.futureFromEvent = function (eventEmitter, event) { var future = new Future(); eventEmitter.once(event, function () { var args = _.toArray(arguments); if (event === "error") { var err = args[0]; future.throw(err); return; } switch (args.length) { case 0: future.return(); break; case 1: future.return(args[0]); break; default: future.return(args); break; } }); return future; }; FileSystem.prototype.createDirectory = function (path) { var future = new Future(); require("mkdirp")(path, function (err) { Iif (err) { future.throw(err); } else { future.return(); } }); return future; }; FileSystem.prototype.readDirectory = function (path) { var future = new Future(); fs.readdir(path, function (err, files) { Iif (err) { future.throw(err); } else { future.return(files); } }); return future; }; FileSystem.prototype.readFile = function (filename) { var future = new Future(); fs.readFile(filename, function (err, data) { if (err) { future.throw(err); } else { future.return(data); } }); return future; }; FileSystem.prototype.readText = function (filename, options) { options = options || { encoding: "utf8" }; Iif (_.isString(options)) { options = { encoding: options }; } Iif (!options.encoding) { options.encoding = "utf8"; } var future = new Future(); fs.readFile(filename, options, function (err, data) { Iif (err) { future.throw(err); } else { future.return(data); } }); return future; }; FileSystem.prototype.readJson = function (filename, encoding) { var _this = this; return (function () { var data = _this.readText(filename, encoding).wait(); Eif (data) { return JSON.parse(data.replace(/^\uFEFF/, "")); } return null; }).future()(); }; FileSystem.prototype.writeFile = function (filename, data, encoding) { var _this = this; return (function () { _this.createDirectory(path.dirname(filename)).wait(); var future = new Future(); fs.writeFile(filename, data, { encoding: encoding }, function (err) { Iif (err) { future.throw(err); } else { future.return(); } }); future.wait(); }).future()(); }; FileSystem.prototype.appendFile = function (filename, data, encoding) { var future = new Future(); fs.appendFile(filename, data, { encoding: encoding }, function (err) { Iif (err) { future.throw(err); } else { future.return(); } }); return future; }; FileSystem.prototype.writeJson = function (filename, data, space, encoding) { if (space === void 0) { space = "\t"; } return this.writeFile(filename, JSON.stringify(data, null, space), encoding); }; FileSystem.prototype.copyFile = function (sourceFileName, destinationFileName) { if (path.resolve(sourceFileName) === path.resolve(destinationFileName)) { return Future.fromResult(); } var res = new Future(); this.createDirectory(path.dirname(destinationFileName)).wait(); var source = this.createReadStream(sourceFileName); var target = this.createWriteStream(destinationFileName); source.on("error", function (e) { if (!res.isResolved()) { res.throw(e); } }); target.on("finish", function () { Eif (!res.isResolved()) { res.return(); } }) .on("error", function (e) { if (!res.isResolved()) { res.throw(e); } }); source.pipe(target); return res; }; FileSystem.prototype.createReadStream = function (path, options) { return fs.createReadStream(path, options); }; FileSystem.prototype.createWriteStream = function (path, options) { return fs.createWriteStream(path, options); }; FileSystem.prototype.chmod = function (path, mode) { var future = new Future(); fs.chmod(path, mode, function (err) { if (err) { future.throw(err); } else { future.return(); } }); return future; }; FileSystem.prototype.getFsStats = function (path) { var future = new Future(); fs.stat(path, function (err, data) { Iif (err) { future.throw(err); } else { future.return(data); } }); return future; }; FileSystem.prototype.getUniqueFileName = function (baseName) { var _this = this; return (function () { if (!_this.exists(baseName).wait()) { return baseName; } var extension = path.extname(baseName); var prefix = path.basename(baseName, extension); for (var i = 2;; ++i) { var numberedName = prefix + i + extension; if (!_this.exists(numberedName).wait()) { return numberedName; } } }).future()(); }; FileSystem.prototype.isEmptyDir = function (directoryPath) { var _this = this; return (function () { var directoryContent = _this.readDirectory(directoryPath).wait(); return directoryContent.length === 0; }).future()(); }; FileSystem.prototype.isRelativePath = function (p) { var normal = path.normalize(p); var absolute = path.resolve(p); return normal !== absolute; }; FileSystem.prototype.ensureDirectoryExists = function (directoryPath) { var _this = this; return (function () { if (!_this.exists(directoryPath).wait()) { _this.createDirectory(directoryPath).wait(); } }).future()(); }; FileSystem.prototype.rename = function (oldPath, newPath) { var future = new Future(); fs.rename(oldPath, newPath, function (err) { if (err) { future.throw(err); } else { future.return(); } }); return future; }; FileSystem.prototype.renameIfExists = function (oldPath, newPath) { var _this = this; return (function () { try { _this.rename(oldPath, newPath).wait(); return true; } catch (e) { Eif (e.code === "ENOENT") { return false; } throw e; } }).future()(); }; FileSystem.prototype.symlink = function (sourcePath, destinationPath, type) { var future = new Future(); fs.symlink(sourcePath, destinationPath, type, function (err) { if (err) { future.throw(err); } else { future.return(); } }); return future; }; FileSystem.prototype.closeStream = function (stream) { var future = new Future(); stream.close(function (err, data) { if (err) { future.throw(err); } else { future.return(); } }); return future; }; FileSystem.prototype.setCurrentUserAsOwner = function (path, owner) { var _this = this; return (function () { var $childProcess = _this.$injector.resolve("childProcess"); if (!_this.$hostInfo.isWindows) { var chown = $childProcess.spawn("chown", ["-R", owner, path], { stdio: "ignore", detached: true }); _this.futureFromEvent(chown, "close").wait(); } }).future()(); }; FileSystem.prototype.enumerateFilesInDirectorySync = function (directoryPath, filterCallback, opts, foundFiles) { foundFiles = foundFiles || []; var contents = this.readDirectory(directoryPath).wait(); for (var i = 0; i < contents.length; ++i) { var file = path.join(directoryPath, contents[i]); var stat = this.getFsStats(file).wait(); Iif (filterCallback && !filterCallback(file, stat)) { continue; } if (stat.isDirectory()) { Iif (opts && opts.enumerateDirectories) { foundFiles.push(file); } if (opts && opts.includeEmptyDirectories && this.readDirectory(file).wait().length === 0) { foundFiles.push(file); } this.enumerateFilesInDirectorySync(file, filterCallback, opts, foundFiles); } else { foundFiles.push(file); } } return foundFiles; }; FileSystem.prototype.getFileShasum = function (fileName, encoding) { var future = new Future(); encoding = encoding || "sha1"; var logger = this.$injector.resolve("$logger"); var shasumData = crypto.createHash(encoding); var fileStream = this.createReadStream(fileName); fileStream.on("data", function (data) { shasumData.update(data); }); fileStream.on("end", function () { var shasum = shasumData.digest("hex"); logger.trace("Shasum of file " + fileName + " is " + shasum); future.return(shasum); }); fileStream.on("error", function (err) { future.throw(err); }); return future; }; FileSystem.prototype.readStdin = function () { var future = new Future(); var buffer = ''; process.stdin.on('data', function (data) { return buffer += data; }); process.stdin.on('end', function () { return future.return(buffer); }); return future; }; Object.defineProperty(FileSystem.prototype, "getFileSize", __decorate([ decorators.exportedPromise("fs") ], FileSystem.prototype, "getFileSize", Object.getOwnPropertyDescriptor(FileSystem.prototype, "getFileSize"))); FileSystem = __decorate([ injector.register("fs") ], FileSystem); return FileSystem; })(); exports.FileSystem = FileSystem; |