1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | exports.loadPlugin = exports.parseFileContent = exports.readSchemaFromUrl = exports.readSchemasFromFile = exports.readSchemaFromStdin = exports.parseSchema = exports.isJsonSchemaDraft04 = void 0;
|
4 | const tslib_1 = require("tslib");
|
5 | const fs = tslib_1.__importStar(require("fs"));
|
6 | const path_1 = require("path");
|
7 | const js_yaml_1 = require("js-yaml");
|
8 | const utils_1 = require("../utils");
|
9 | const jsonSchema_1 = require("./jsonSchema");
|
10 | const schemaId_1 = tslib_1.__importDefault(require("./schemaId"));
|
11 | exports.ts = require("typescript");
|
12 | function isJsonSchemaDraft04(_content, type) {
|
13 | return type === 'Draft04';
|
14 | }
|
15 | exports.isJsonSchemaDraft04 = isJsonSchemaDraft04;
|
16 | function 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 | }
|
32 | exports.parseSchema = parseSchema;
|
33 | async function readSchemaFromStdin() {
|
34 | const data = await (0, utils_1.readStream)(process.stdin);
|
35 | const content = parseFileContent(data);
|
36 | return parseSchema(content);
|
37 | }
|
38 | exports.readSchemaFromStdin = readSchemaFromStdin;
|
39 | async 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 | }
|
49 | exports.readSchemasFromFile = readSchemasFromFile;
|
50 | async function readSchemaFromUrl(url) {
|
51 | const data = await (0, utils_1.readUrl)(url);
|
52 | const content = parseFileContent(data, url);
|
53 | return parseSchema(content, url);
|
54 | }
|
55 | exports.readSchemaFromUrl = readSchemaFromUrl;
|
56 | function 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 | }
|
76 | exports.parseFileContent = parseFileContent;
|
77 | function deepCopy(obj) {
|
78 | return JSON.parse(JSON.stringify(obj));
|
79 | }
|
80 | async 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 | }
|
101 | exports.loadPlugin = loadPlugin;
|