UNPKG

5.67 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, {
54 encoding: 'utf-8',
55 })];
56 case 1:
57 data = _a.sent();
58 content = parseFileContent(data);
59 return [2, parseSchema(content)];
60 }
61 });
62 }); }))];
63 }
64 });
65 });
66}
67exports.readSchemasFromFile = readSchemasFromFile;
68function readSchemaFromUrl(url) {
69 return tslib_1.__awaiter(this, void 0, void 0, function () {
70 var data, content;
71 return tslib_1.__generator(this, function (_a) {
72 switch (_a.label) {
73 case 0: return [4, utils_1.readUrl(url)];
74 case 1:
75 data = _a.sent();
76 content = parseFileContent(data, url);
77 return [2, parseSchema(content, url)];
78 }
79 });
80 });
81}
82exports.readSchemaFromUrl = readSchemaFromUrl;
83function parseFileContent(content, filename) {
84 var ext = filename ? path_1.default.extname(filename).toLowerCase() : '';
85 var maybeYaml = ext === '.yaml' || ext === '.yml';
86 try {
87 if (maybeYaml) {
88 return deepCopy(js_yaml_1.default.safeLoad(content));
89 }
90 else {
91 return JSON.parse(content);
92 }
93 }
94 catch (e) {
95 if (maybeYaml) {
96 return JSON.parse(content);
97 }
98 else {
99 return deepCopy(js_yaml_1.default.safeLoad(content));
100 }
101 }
102}
103exports.parseFileContent = parseFileContent;
104function deepCopy(obj) {
105 return JSON.parse(JSON.stringify(obj));
106}
107function loadPlugin(name, option) {
108 return tslib_1.__awaiter(this, void 0, void 0, function () {
109 var mod, plugin;
110 return tslib_1.__generator(this, function (_a) {
111 switch (_a.label) {
112 case 0:
113 if (!option) {
114 return [2, undefined];
115 }
116 return [4, Promise.resolve().then(function () { return tslib_1.__importStar(require(name)); })];
117 case 1:
118 mod = _a.sent();
119 if (!('default' in mod)) {
120 console.warn("The plugin (" + name + ") is invalid module. That is not default export format.");
121 return [2, undefined];
122 }
123 plugin = mod.default;
124 if (plugin.preProcess != null && typeof plugin.preProcess !== 'function') {
125 console.warn("The plugin (" + name + ") is invalid module. The 'preProcess' is not a function.");
126 return [2, undefined];
127 }
128 if (plugin.postProcess != null &&
129 typeof plugin.postProcess !== 'function') {
130 console.warn("The plugin (" + name + ") is invalid module. The 'postProcess' is not a function.");
131 return [2, undefined];
132 }
133 return [2, plugin];
134 }
135 });
136 });
137}
138exports.loadPlugin = loadPlugin;
139//# sourceMappingURL=type.js.map
\No newline at end of file