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 | 1× 1× 1× 1× 1× 1× 1× 1× 1× 1× 1× 1× 6× 1× 5× 5× 5× 5× 5× 5× 1× 5× 10× 5× 1× 1× 1× 1× 1× 1× 1× 1× 1× 1× 1× 1× 1× 1× 2× 2× 2× 2× 2× 2× 2× 2× 2× 1× 1× 1× 1× 1× 1× 1× 2× 1× 1× 1× 7× 1× 1× | "use strict"; Object.defineProperty(exports, "__esModule", { value: true }); var tslib_1 = require("tslib"); var fileUtil_1 = require("../commons/utils/fileUtil"); var directoryStructure_1 = require("../commons/constants/directoryStructure"); var types_1 = require("../generators/spec/types"); var examples_1 = require("../generators/spec/examples"); var spec_1 = require("../generators/spec/spec"); var utils_1 = require("../commons/utils/utils"); var cacheUtil_1 = require("../commons/utils/cacheUtil"); var config_1 = require("../configs/config"); /** * Class which implements the logic for implementing raml generation file related actions. * * @export * @class RamlFileOperations */ var RamlFileOperations = (function () { function RamlFileOperations(filePath) { this.filePath = fileUtil_1.default.normalizePath(filePath); } /** * Create directory structure for the application * @returns {Promise<boolean[]>} created directory structure. */ RamlFileOperations.prototype.createDirectoryStructure = function () { return tslib_1.__awaiter(this, void 0, void 0, function () { var _this = this; var promises; return tslib_1.__generator(this, function (_a) { promises = [Promise.resolve(true)]; /* istanbul ignore next */ Iif (!fileUtil_1.default.isDirectory(this.filePath)) { return [2 /*return*/, Promise.reject('Invalid directory path')]; } Object.keys(directoryStructure_1.default.RAML_STRUCTURE).map(function (directory) { promises.push(fileUtil_1.default.createDirectory(fileUtil_1.default.joinPaths(_this.filePath, directoryStructure_1.default.RAML_STRUCTURE[directory]))); }); return [2 /*return*/, Promise.all(promises)]; }); }); }; /** * Create .raml type file for every table inside schema * @param {Schema} schema - schema tables(list of tables) * @returns {Promise.<boolean[]>} generated type files. */ RamlFileOperations.prototype.generateSchemaTypeFiles = function (schema) { return tslib_1.__awaiter(this, void 0, void 0, function () { var _this = this; var promises; return tslib_1.__generator(this, function (_a) { promises = [Promise.resolve(true)]; schema.map(function (table) { promises.push(fileUtil_1.default.writeFile(fileUtil_1.default.joinPaths(_this.filePath, directoryStructure_1.default.RAML_STRUCTURE.TYPES, utils_1.default.toTitleCase(table.name) + ".raml"), types_1.default.generateTypeContent(table))); }); return [2 /*return*/, Promise.all(promises)]; }); }); }; /** * Generate api.raml spec for the whole api. * * @param {Schema} schema - database schema * @param {RAMLApplicationInfo} options - application info * @returns {Promise<boolean>} - true if spec generated */ RamlFileOperations.prototype.generateSchemaApiFiles = function (schema, options) { return tslib_1.__awaiter(this, void 0, void 0, function () { return tslib_1.__generator(this, function (_a) { return [2 /*return*/, fileUtil_1.default.writeFile(fileUtil_1.default.joinPaths(this.filePath, 'api.raml'), spec_1.default.generateContent(schema, options))]; }); }); }; /** * Generate .json entity for every table from schema. * File will be saved on path: ./raml/types/entityName.json * @param {Schema} schema - schema containing tables. * @return {Promise.<boolean[]>} generated examle files. */ RamlFileOperations.prototype.generateSchemaExampleFiles = function (schema) { return tslib_1.__awaiter(this, void 0, void 0, function () { var _this = this; var promises; return tslib_1.__generator(this, function (_a) { promises = [Promise.resolve(true)]; schema.map(function (table) { var typeExampleGenerated = examples_1.default.generateTypeExampleContent(schema, table, config_1.default.INITIAL_DEPTH_LEVEL); promises.push(fileUtil_1.default.writeFile(fileUtil_1.default.joinPaths(_this.filePath, directoryStructure_1.default.RAML_STRUCTURE.EXAMPLES, typeExampleGenerated.type + ".json"), utils_1.default.convertToJSON(typeExampleGenerated.data))); }); return [2 /*return*/, Promise.all(promises)]; }); }); }; /** * Generate .json Array entity for every array of objects saved in cache * @return {Promise.<boolean[]>} generated example files. */ RamlFileOperations.prototype.generateSchemaExamplesFilesFromCache = function () { return tslib_1.__awaiter(this, void 0, void 0, function () { var _this = this; var promises; return tslib_1.__generator(this, function (_a) { promises = [Promise.resolve(true)]; Object.keys(cacheUtil_1.default.getByPrimeKey(examples_1.default.PRIME_KEY)) .filter(function (key) { return key.includes('[]'); }) .map(function (key) { promises.push(fileUtil_1.default.writeFile(fileUtil_1.default.joinPaths(_this.filePath, directoryStructure_1.default.RAML_STRUCTURE.EXAMPLES, utils_1.default.pluraliseWordArray(key) + ".json"), utils_1.default.convertToJSON(cacheUtil_1.default.get(examples_1.default.PRIME_KEY, key)))); }); return [2 /*return*/, Promise.all(promises)]; }); }); }; /** * Returns file path. * * @returns {string} file path */ RamlFileOperations.prototype.getFilePath = function () { return this.filePath; }; return RamlFileOperations; }()); exports.default = RamlFileOperations; |