1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | exports.loadPlugin = exports.parseFileContent = exports.readSchemaFromUrl = exports.readSchemasFromFile = exports.readSchemaFromStdin = exports.parseSchema = void 0;
|
4 | var tslib_1 = require("tslib");
|
5 | var fs_1 = tslib_1.__importDefault(require("fs"));
|
6 | var js_yaml_1 = tslib_1.__importDefault(require("js-yaml"));
|
7 | var path_1 = tslib_1.__importDefault(require("path"));
|
8 | var utils_1 = require("../utils");
|
9 | var schemaId_1 = tslib_1.__importDefault(require("./schemaId"));
|
10 | var jsonSchema_1 = require("./jsonSchema");
|
11 | function parseSchema(content, url) {
|
12 | var _a = jsonSchema_1.selectSchemaType(content), type = _a.type, openApiVersion = _a.openApiVersion;
|
13 | if (url != null) {
|
14 | jsonSchema_1.setId(type, content, url);
|
15 | }
|
16 | var id = jsonSchema_1.getId(type, content);
|
17 | return {
|
18 | type: type,
|
19 | openApiVersion: openApiVersion,
|
20 | id: id ? new schemaId_1.default(id) : schemaId_1.default.empty,
|
21 | content: content,
|
22 | };
|
23 | }
|
24 | exports.parseSchema = parseSchema;
|
25 | function readSchemaFromStdin() {
|
26 | return tslib_1.__awaiter(this, void 0, void 0, function () {
|
27 | var data, content;
|
28 | return tslib_1.__generator(this, function (_a) {
|
29 | switch (_a.label) {
|
30 | case 0: return [4, utils_1.readStream(process.stdin)];
|
31 | case 1:
|
32 | data = _a.sent();
|
33 | content = parseFileContent(data);
|
34 | return [2, parseSchema(content)];
|
35 | }
|
36 | });
|
37 | });
|
38 | }
|
39 | exports.readSchemaFromStdin = readSchemaFromStdin;
|
40 | function readSchemasFromFile(pattern) {
|
41 | return tslib_1.__awaiter(this, void 0, void 0, function () {
|
42 | var files;
|
43 | var _this = this;
|
44 | return tslib_1.__generator(this, function (_a) {
|
45 | switch (_a.label) {
|
46 | case 0: return [4, utils_1.globFiles(pattern)];
|
47 | case 1:
|
48 | files = _a.sent();
|
49 | return [2, Promise.all(files.map(function (file) { return tslib_1.__awaiter(_this, void 0, void 0, function () {
|
50 | var data, content;
|
51 | return tslib_1.__generator(this, function (_a) {
|
52 | switch (_a.label) {
|
53 | case 0: return [4, fs_1.default.promises.readFile(file, { encoding: 'utf-8' })];
|
54 | case 1:
|
55 | data = _a.sent();
|
56 | content = parseFileContent(data);
|
57 | return [2, parseSchema(content)];
|
58 | }
|
59 | });
|
60 | }); }))];
|
61 | }
|
62 | });
|
63 | });
|
64 | }
|
65 | exports.readSchemasFromFile = readSchemasFromFile;
|
66 | function readSchemaFromUrl(url) {
|
67 | return tslib_1.__awaiter(this, void 0, void 0, function () {
|
68 | var data, content;
|
69 | return tslib_1.__generator(this, function (_a) {
|
70 | switch (_a.label) {
|
71 | case 0: return [4, utils_1.readUrl(url)];
|
72 | case 1:
|
73 | data = _a.sent();
|
74 | content = parseFileContent(data, url);
|
75 | return [2, parseSchema(content, url)];
|
76 | }
|
77 | });
|
78 | });
|
79 | }
|
80 | exports.readSchemaFromUrl = readSchemaFromUrl;
|
81 | function parseFileContent(content, filename) {
|
82 | var ext = filename ? path_1.default.extname(filename).toLowerCase() : '';
|
83 | var maybeYaml = ext === '.yaml' || ext === '.yml';
|
84 | try {
|
85 | if (maybeYaml) {
|
86 | return deepCopy(js_yaml_1.default.safeLoad(content));
|
87 | }
|
88 | else {
|
89 | return JSON.parse(content);
|
90 | }
|
91 | }
|
92 | catch (e) {
|
93 | if (maybeYaml) {
|
94 | return JSON.parse(content);
|
95 | }
|
96 | else {
|
97 | return deepCopy(js_yaml_1.default.safeLoad(content));
|
98 | }
|
99 | }
|
100 | }
|
101 | exports.parseFileContent = parseFileContent;
|
102 | function deepCopy(obj) {
|
103 | return JSON.parse(JSON.stringify(obj));
|
104 | }
|
105 | function loadPlugin(name, option) {
|
106 | return tslib_1.__awaiter(this, void 0, void 0, function () {
|
107 | var mod, plugin;
|
108 | return tslib_1.__generator(this, function (_a) {
|
109 | switch (_a.label) {
|
110 | case 0:
|
111 | if (!option) {
|
112 | return [2, undefined];
|
113 | }
|
114 | return [4, Promise.resolve().then(function () { return tslib_1.__importStar(require(name)); })];
|
115 | case 1:
|
116 | mod = _a.sent();
|
117 | if (!('default' in mod)) {
|
118 | console.warn("The plugin (" + name + ") is invalid module. That is not default export format.");
|
119 | return [2, undefined];
|
120 | }
|
121 | plugin = mod.default;
|
122 | if (plugin.preProcess != null && typeof plugin.preProcess !== 'function') {
|
123 | console.warn("The plugin (" + name + ") is invalid module. The 'preProcess' is not a function.");
|
124 | return [2, undefined];
|
125 | }
|
126 | if (plugin.postProcess != null && typeof plugin.postProcess !== 'function') {
|
127 | console.warn("The plugin (" + name + ") is invalid module. The 'postProcess' is not a function.");
|
128 | return [2, undefined];
|
129 | }
|
130 | return [2, plugin];
|
131 | }
|
132 | });
|
133 | });
|
134 | }
|
135 | exports.loadPlugin = loadPlugin;
|
136 |
|
\ | No newline at end of file |