UNPKG

3.63 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3exports.loadPlugin = exports.parseFileContent = exports.readSchemaFromUrl = exports.readSchemasFromFile = exports.readSchemaFromStdin = exports.parseSchema = exports.isJsonSchemaDraft04 = void 0;
4const tslib_1 = require("tslib");
5const fs = tslib_1.__importStar(require("fs"));
6const path_1 = require("path");
7const js_yaml_1 = require("js-yaml");
8const utils_1 = require("../utils");
9const jsonSchema_1 = require("./jsonSchema");
10const schemaId_1 = tslib_1.__importDefault(require("./schemaId"));
11exports.ts = require("typescript");
12function isJsonSchemaDraft04(_content, type) {
13 return type === 'Draft04';
14}
15exports.isJsonSchemaDraft04 = isJsonSchemaDraft04;
16function parseSchema(content, url) {
17 const { type, openApiVersion } = (0, jsonSchema_1.selectSchemaType)(content);
18 let id;
19 if (typeof content !== 'boolean') {
20 if (url != null) {
21 (0, jsonSchema_1.setId)(type, content, url);
22 }
23 id = (0, jsonSchema_1.getId)(type, content);
24 }
25 return {
26 type,
27 openApiVersion,
28 id: id ? new schemaId_1.default(id) : schemaId_1.default.empty,
29 content,
30 };
31}
32exports.parseSchema = parseSchema;
33async function readSchemaFromStdin() {
34 const data = await (0, utils_1.readStream)(process.stdin);
35 const content = parseFileContent(data);
36 return parseSchema(content);
37}
38exports.readSchemaFromStdin = readSchemaFromStdin;
39async function readSchemasFromFile(pattern) {
40 const files = await (0, utils_1.globFiles)(pattern);
41 return Promise.all(files.map(async (file) => {
42 const data = await fs.promises.readFile(file, {
43 encoding: 'utf8',
44 });
45 const content = parseFileContent(data);
46 return parseSchema(content);
47 }));
48}
49exports.readSchemasFromFile = readSchemasFromFile;
50async function readSchemaFromUrl(url) {
51 const data = await (0, utils_1.readUrl)(url);
52 const content = parseFileContent(data, url);
53 return parseSchema(content, url);
54}
55exports.readSchemaFromUrl = readSchemaFromUrl;
56function parseFileContent(content, filename) {
57 const ext = filename ? (0, path_1.extname)(filename).toLowerCase() : '';
58 const maybeYaml = ext === '.yaml' || ext === '.yml';
59 try {
60 if (maybeYaml) {
61 return deepCopy((0, js_yaml_1.load)(content));
62 }
63 else {
64 return JSON.parse(content);
65 }
66 }
67 catch (e) {
68 if (maybeYaml) {
69 return JSON.parse(content);
70 }
71 else {
72 return deepCopy((0, js_yaml_1.load)(content));
73 }
74 }
75}
76exports.parseFileContent = parseFileContent;
77function deepCopy(obj) {
78 return JSON.parse(JSON.stringify(obj));
79}
80async function loadPlugin(name, option) {
81 if (!option) {
82 return undefined;
83 }
84 const mod = (await Promise.resolve(`${name}`).then(s => tslib_1.__importStar(require(s))));
85 if (!mod.default) {
86 console.warn(`The plugin (${name}) is invalid module. That is not default export format.`);
87 return undefined;
88 }
89 const plugin = mod.default;
90 if (plugin.preProcess != null && typeof plugin.preProcess !== 'function') {
91 console.warn(`The plugin (${name}) is invalid module. The 'preProcess' is not a function.`);
92 return undefined;
93 }
94 if (plugin.postProcess != null &&
95 typeof plugin.postProcess !== 'function') {
96 console.warn(`The plugin (${name}) is invalid module. The 'postProcess' is not a function.`);
97 return undefined;
98 }
99 return plugin;
100}
101exports.loadPlugin = loadPlugin;