"use strict"; var __assign = (this && this.__assign) || function () { __assign = Object.assign || function(t) { for (var s, i = 1, n = arguments.length; i < n; i++) { s = arguments[i]; for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p]; } return t; }; return __assign.apply(this, arguments); }; Object.defineProperty(exports, "__esModule", { value: true }); exports.TStoJSONSchema = exports.CustomTs = exports.Utility = void 0; var ts = require("typescript"); var Utility = /** @class */ (function () { function Utility() { } Utility.capitalize = function (str) { return str.replace(/(?:^|\s|["'([{])+\S/g, function (match) { return match.toUpperCase(); }); }; Utility.reSpace = function (str) { var regex = /([A-Z])(?=[A-Z][a-z])|([a-z])(?=[A-Z])/g; return str.replace(regex, '$& '); }; Utility.fromCamelCaseToSentence = function (word) { return Utility.capitalize(Utility.reSpace(word)); }; return Utility; }()); exports.Utility = Utility; var CustomTs = /** @class */ (function () { function CustomTs() { } CustomTs.hasJSDocNodes = function (node) { var jsDoc = node.jsDoc; return !!jsDoc && jsDoc.length > 0; }; CustomTs.hasTypeNode = function (node) { var type = node.type; return !!type; }; return CustomTs; }()); exports.CustomTs = CustomTs; var TStoJSONSchema = /** @class */ (function () { function TStoJSONSchema() { } // static fileNodes(file: string, options: ts.CompilerOptions) { // const program = ts.createProgram([file], options); // const source = program.getSourceFile(file); // return source; // } TStoJSONSchema.inputNodes = function (input, options) { var target = (options === null || options === void 0 ? void 0 : options.target) || ts.ScriptTarget.ES5; var source = ts.createSourceFile('index.ts', input, target); return source; }; TStoJSONSchema.getText = function (node) { var escapedText = node.name; if (escapedText && ts.isIdentifier(escapedText)) { return escapedText.text; } return undefined; }; TStoJSONSchema.assertText = function (node) { var escapedText = node.name; if (escapedText && ts.isIdentifier(escapedText)) { return escapedText.text; } throw new Error('no title'); }; TStoJSONSchema.getType = function (node) { if (CustomTs.hasTypeNode(node)) { var kind = node.type.kind; if (kind === ts.SyntaxKind.BooleanKeyword) return 'boolean'; if (kind === ts.SyntaxKind.StringKeyword) return 'string'; throw new Error("kind not recognized " + kind); } throw new Error('not TypeNode'); }; TStoJSONSchema.getDefault = function (str) { if (str === '') return undefined; if (str === 'undefined') return undefined; if (str === 'true') return true; if (str === 'false') return false; var int = parseInt(str); if (!Number.isNaN(int)) return int; var isString = str.match(/["|'|`](.+)["|'|`]/); if (isString) return isString[1]; return undefined; }; TStoJSONSchema.getComment = function (node) { var _this = this; var description = ''; var def = ''; if (CustomTs.hasJSDocNodes(node)) { var docs = node.jsDoc; docs.forEach(function (doc) { if (doc.comment) description = doc.comment; if (doc.tags) { doc.tags.forEach(function (tag) { var _a; if (tag.tagName.escapedText === 'default') { var pieces = ((_a = tag.comment) === null || _a === void 0 ? void 0 : _a.split(' ')) || []; if (pieces[0]) { def = _this.getDefault(pieces[0]); } if (pieces.length > 1) { description = pieces.slice(1, pieces.length).join(' '); } } }); } }); } return { description: description, def: def }; }; TStoJSONSchema.getProperties = function (node) { var _this = this; var members = node.members; var properties = {}; members.forEach(function (member) { var text = _this.getText(member); if (text) { var type = _this.getType(member); var _a = _this.getComment(member), def = _a.def, description = _a.description; properties[text] = __assign(__assign({ title: Utility.fromCamelCaseToSentence(text), type: type }, (def !== '' ? { default: def } : {})), (description !== '' ? { description: description } : {})); } }); return properties; }; TStoJSONSchema.parseFromSource = function (source) { var _this = this; var results = {}; ts.forEachChild(source, function (node) { if (ts.isInterfaceDeclaration(node)) { var title = _this.assertText(node); var key = title.toLocaleLowerCase(); results[key] = { title: title, type: 'object', properties: _this.getProperties(node), }; } }); return results; }; TStoJSONSchema.parse = function (input) { var source = TStoJSONSchema.inputNodes(input); var properties = TStoJSONSchema.parseFromSource(source); return { properties: properties }; }; return TStoJSONSchema; }()); exports.TStoJSONSchema = TStoJSONSchema; //# sourceMappingURL=mod.js.map