container.ts
Version:
Modular application framework
117 lines • 4.58 kB
JavaScript
;
var __extends = (this && this.__extends) || (function () {
var extendStatics = Object.setPrototypeOf ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
return function (d, b) {
extendStatics(d, b);
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
})();
Object.defineProperty(exports, "__esModule", { value: true });
var fs = require("fs");
var lodash_1 = require("lodash");
var path = require("path");
var container_1 = require("../../container");
var RxJS_1 = require("../../container/RxJS");
var error_1 = require("../error");
var node_validate_1 = require("../node-validate");
/** Assets error class. */
var AssetsError = /** @class */ (function (_super) {
__extends(AssetsError, _super);
function AssetsError(name, value, cause) {
return _super.call(this, { name: name, value: value }, cause) || this;
}
return AssetsError;
}(error_1.ErrorChain));
exports.AssetsError = AssetsError;
/** Assets read only files module. */
var Assets = /** @class */ (function (_super) {
__extends(Assets, _super);
function Assets(options) {
var _this = _super.call(this, options) || this;
_this.path = _this.envPath;
_this.cache = {};
// Debug environment variables.
_this.debug(Assets.ENV.PATH + "=\"" + _this.path + "\"");
return _this;
}
/** Returns true if target file is cached. */
Assets.prototype.isCached = function (target, encoding) {
var cacheKey = target + ":" + encoding;
return (this.cache[cacheKey] != null);
};
/**
* Read asset file contents.
* If encoding is specified a string is returned, else a Buffer.
* File is cached by default.
*/
Assets.prototype.readFile = function (target, options) {
if (options === void 0) { options = { cache: true }; }
return this.read(target, options);
};
/** Read asset file contents and parse JSON object. */
Assets.prototype.readJson = function (target, options) {
if (options === void 0) { options = { encoding: "utf8", cache: true }; }
return this.read(target, options)
.map(function (data) {
try {
return JSON.parse(data);
}
catch (error) {
throw new AssetsError(Assets.ERROR.JSON_PARSE, target, error);
}
});
};
Object.defineProperty(Assets.prototype, "envPath", {
get: function () {
return node_validate_1.NodeValidate.isDirectory(path.resolve(this.environment.get(Assets.ENV.PATH)));
},
enumerable: true,
configurable: true
});
Assets.prototype.read = function (target, options) {
var _this = this;
if (options === void 0) { options = {}; }
var cacheKey = target + ":" + options.encoding;
// Assets are read only, if contents defined in cache, return now.
if (!!options.cache) {
if (this.cache[cacheKey] != null) {
var value = this.cache[cacheKey];
return RxJS_1.Observable.of(value);
}
}
try {
// Check file exists, read file contents asynchronously.
var filePath = node_validate_1.NodeValidate.isFile(path.resolve(this.path, target));
var readFileCallback = fs.readFile.bind(this, filePath, options.encoding);
var readFile = RxJS_1.Observable.bindNodeCallback(readFileCallback);
return readFile()
.do(function (data) {
if (!!options.cache) {
// Save to cache if enabled in options.
_this.cache[cacheKey] = data;
}
});
}
catch (error) {
return RxJS_1.Observable.throw(new AssetsError(Assets.ERROR.READ_FILE, target, error));
}
};
/** Default module name. */
Assets.moduleName = "Assets";
/** Environment variable names. */
Assets.ENV = {
/** Assets directory path (required). */
PATH: "ASSETS_PATH",
};
/** Error names. */
Assets.ERROR = lodash_1.assign({}, container_1.Module.ERROR, {
READ_FILE: "AssetsReadFileError",
JSON_PARSE: "AssetsJsonParseError",
});
return Assets;
}(container_1.Module));
exports.Assets = Assets;
//# sourceMappingURL=Assets.js.map