UNPKG

@otris/jsdoc-tsd

Version:

JSDoc Template for generate typescript definition files from JSDoc comments

171 lines (170 loc) 7.24 kB
"use strict"; var __values = (this && this.__values) || function(o) { var s = typeof Symbol === "function" && Symbol.iterator, m = s && o[s], i = 0; if (m) return m.call(o); if (o && typeof o.length === "number") return { next: function () { if (o && i >= o.length) o = void 0; return { value: o && o[i++], done: !o }; } }; throw new TypeError(s ? "Object is not iterable." : "Symbol.iterator is not defined."); }; Object.defineProperty(exports, "__esModule", { value: true }); exports.Configuration = void 0; var fs_1 = require("fs"); var path_1 = require("path"); var stripJsonComments = require("strip-json-comments"); var Logger_1 = require("./Logger"); var Configuration = /** @class */ (function () { function Configuration(filePath) { this._ignoreScopes = []; this._versionComparator = this.defaultVersionComparator; this._latestVersion = ""; this.skipUndocumented = true; this.ignoreSinceTag = false; this.logItemsSkippedBySince = true; if (filePath) { this.loadFromFile(filePath); } } Object.defineProperty(Configuration.prototype, "ignoreScopes", { /** * Scopes to ignore */ get: function () { return this._ignoreScopes; }, set: function (val) { this._ignoreScopes = val; }, enumerable: false, configurable: true }); Object.defineProperty(Configuration.prototype, "latestVersion", { /** * Latest version tag for the version comparator function */ get: function () { return this._latestVersion; }, set: function (value) { this._latestVersion = value; }, enumerable: false, configurable: true }); Object.defineProperty(Configuration.prototype, "versionComparator", { set: function (value) { // Its possible to pass the version comparator via the jsdoc config // either as function string or as a file path to a js-file which // the version comparator function var newVersionComparator; if (typeof value === "string") { var versionComparatorAsString = value; if (versionComparatorAsString.indexOf("{") > 0) { // Version comparator is a function string newVersionComparator = this.parseVersionComparatorFromString(value); } else if ((0, fs_1.existsSync)(value)) { // Version comparator is a file path if ((0, path_1.extname)(value) === ".js") { newVersionComparator = require(value); } else { throw new Error("Invalud version comparator: ".concat(value, ". Version comparator must be a JavaScript file.")); } } else { throw new Error("Version comparator must contain a valid path or a valid function as string, got ".concat(value)); } } else { newVersionComparator = value; } this._versionComparator = newVersionComparator; }, enumerable: false, configurable: true }); /** * Determines if the tagged version is in range of the latest version * using the provided version comparator * @param taggedVersion Current version tag * @param latestVersion Latest version tag from config */ Configuration.prototype.compareVersions = function (taggedVersion, latestVersion, itemName) { var isItemInRange = this.ignoreSinceTag || this._versionComparator(taggedVersion, latestVersion); if (!isItemInRange && this.logItemsSkippedBySince) { Logger_1.Logger.log("Skipping item ".concat(itemName, " because it's since tag (").concat(taggedVersion, ") is less then the latest tag (").concat(latestVersion, ")")); } return isItemInRange; }; Configuration.prototype.ignoreScope = function (scope) { return this.ignoreScopes.indexOf(scope) > -1; }; /** * Determines if the tagged version is in range of the latest version * by using semver-tags * @param taggedVersion Current version tag * @param latestVersion Latest version tag from config */ Configuration.prototype.defaultVersionComparator = function (taggedVersion, latestVersion) { if (taggedVersion.match(/v?([0-9]+\.){2}[0-9]+/i)) { if (typeof latestVersion === "string" && latestVersion.match(/v?([0-9]+\.){2}[0-9]+/i)) { var compare = require("node-version-compare"); var result = compare(latestVersion, taggedVersion); return result >= 0; } else { return true; } } else { return false; } }; Configuration.prototype.loadFromFile = function (filePath) { var e_1, _a; var jsonString = (0, fs_1.readFileSync)(filePath, { encoding: "utf-8" }); var configObj = JSON.parse(stripJsonComments(jsonString)); // quick and dirty iterate over public config properties var _config = new Configuration(); try { for (var _b = __values(Object.keys(_config)), _c = _b.next(); !_c.done; _c = _b.next()) { var property = _c.value; // setters latestVersion and versionComparator not in Object.keys() property = property.startsWith("_") ? property.slice(1) : property; if (configObj.hasOwnProperty(property)) { var privateProp = "_" + property; if (property === "versionComparator") { this.versionComparator = configObj[property]; } else if (this.hasOwnProperty(privateProp)) { // @ts-ignore this[privateProp] = configObj[property]; } else { // @ts-ignore this[property] = configObj[property]; } } } } catch (e_1_1) { e_1 = { error: e_1_1 }; } finally { try { if (_c && !_c.done && (_a = _b.return)) _a.call(_b); } finally { if (e_1) throw e_1.error; } } }; Configuration.prototype.parseVersionComparatorFromString = function (versionComparatorAsString) { var functionBody = versionComparatorAsString.substr(versionComparatorAsString.indexOf("{") + 1); functionBody = functionBody.substr(0, functionBody.length - 1).trim(); // @ts-ignore return new Function("param1", "param2", functionBody); }; return Configuration; }()); exports.Configuration = Configuration;