UNPKG

5.56 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3exports.loadPlugin = exports.parseFileContent = exports.readSchemaFromUrl = exports.readSchemasFromFile = exports.readSchemaFromStdin = exports.parseSchema = void 0;
4var tslib_1 = require("tslib");
5var fs_1 = tslib_1.__importDefault(require("fs"));
6var js_yaml_1 = tslib_1.__importDefault(require("js-yaml"));
7var path_1 = tslib_1.__importDefault(require("path"));
8var utils_1 = require("../utils");
9var schemaId_1 = tslib_1.__importDefault(require("./schemaId"));
10var jsonSchema_1 = require("./jsonSchema");
11function 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}
24exports.parseSchema = parseSchema;
25function 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}
39exports.readSchemaFromStdin = readSchemaFromStdin;
40function 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}
65exports.readSchemasFromFile = readSchemasFromFile;
66function 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}
80exports.readSchemaFromUrl = readSchemaFromUrl;
81function 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}
101exports.parseFileContent = parseFileContent;
102function deepCopy(obj) {
103 return JSON.parse(JSON.stringify(obj));
104}
105function 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}
135exports.loadPlugin = loadPlugin;
136//# sourceMappingURL=type.js.map
\No newline at end of file