2022-12-27 12:37:58 +05:30
|
|
|
/*
|
|
|
|
* Copyright 2022 Collate.
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
2022-04-13 16:57:33 +05:30
|
|
|
/* eslint-disable */
|
|
|
|
const $RefParser = require('@apidevtools/json-schema-ref-parser');
|
|
|
|
const path = require('path');
|
|
|
|
const fs = require('fs');
|
2022-04-22 06:10:48 +05:30
|
|
|
const fse = require('fs-extra');
|
2023-05-18 20:33:54 +05:30
|
|
|
const process = require('process');
|
2022-04-13 16:57:33 +05:30
|
|
|
|
2022-04-22 06:10:48 +05:30
|
|
|
const cwd = process.cwd();
|
|
|
|
|
|
|
|
const schemaDir =
|
2022-09-14 23:14:02 -07:00
|
|
|
'../../../../../openmetadata-spec/src/main/resources/json/schema';
|
2022-04-22 06:10:48 +05:30
|
|
|
|
|
|
|
const rootDir = 'connTemp';
|
|
|
|
const srcDir = 'schema/entity/services/connections';
|
2022-04-13 16:57:33 +05:30
|
|
|
const destDir = 'src/jsons/connectionSchemas/connections';
|
2022-04-22 06:10:48 +05:30
|
|
|
|
2022-06-11 10:32:06 +05:30
|
|
|
const playDir = `${rootDir}/${srcDir}`;
|
2022-04-13 16:57:33 +05:30
|
|
|
|
|
|
|
const globalParserOptions = {
|
|
|
|
continueOnError: true,
|
|
|
|
dereference: {
|
|
|
|
circular: true,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
2023-05-18 20:33:54 +05:30
|
|
|
const parser = new $RefParser(globalParserOptions);
|
|
|
|
|
2022-04-13 16:57:33 +05:30
|
|
|
async function parseSchema(filePath, destPath) {
|
|
|
|
try {
|
2022-06-11 10:32:06 +05:30
|
|
|
const fileDir = `${cwd}/${path.dirname(filePath)}`;
|
|
|
|
const fileName = path.basename(filePath);
|
|
|
|
process.chdir(fileDir);
|
2023-05-18 20:33:54 +05:30
|
|
|
const parsedSchema = await parser.parse(fileName);
|
|
|
|
const schema = await parser.dereference(parsedSchema);
|
2022-04-13 16:57:33 +05:30
|
|
|
const api = await parser.bundle(schema);
|
2022-04-22 06:10:48 +05:30
|
|
|
const dirname = `${cwd}/${path.dirname(destPath)}`;
|
2022-04-13 16:57:33 +05:30
|
|
|
if (!fs.existsSync(dirname)) {
|
|
|
|
try {
|
|
|
|
fs.mkdirSync(dirname, { recursive: true });
|
|
|
|
} catch (err) {
|
|
|
|
console.log(err);
|
|
|
|
}
|
|
|
|
}
|
2022-04-22 06:10:48 +05:30
|
|
|
fs.writeFileSync(`${cwd}/${destPath}`, JSON.stringify(api, null, 2));
|
2022-04-13 16:57:33 +05:30
|
|
|
} catch (err) {
|
|
|
|
console.log(err);
|
2022-06-11 10:32:06 +05:30
|
|
|
} finally {
|
|
|
|
process.chdir(cwd);
|
2022-04-13 16:57:33 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-11 10:32:06 +05:30
|
|
|
async function traverseDirectory(Directory) {
|
|
|
|
const Files = fs.readdirSync(Directory);
|
|
|
|
for (const File of Files) {
|
2022-04-13 16:57:33 +05:30
|
|
|
const Absolute = path.join(Directory, File);
|
|
|
|
if (fs.statSync(Absolute).isDirectory()) {
|
2022-06-11 10:32:06 +05:30
|
|
|
await traverseDirectory(Absolute);
|
2022-04-13 16:57:33 +05:30
|
|
|
} else {
|
2022-06-11 10:32:06 +05:30
|
|
|
const name = Absolute.replace(playDir, destDir);
|
|
|
|
await parseSchema(Absolute, name);
|
2022-04-13 16:57:33 +05:30
|
|
|
}
|
2022-06-11 10:32:06 +05:30
|
|
|
}
|
2022-04-13 16:57:33 +05:30
|
|
|
}
|
|
|
|
|
2022-04-22 06:10:48 +05:30
|
|
|
function copySourceFiles() {
|
|
|
|
try {
|
|
|
|
fse.copySync(schemaDir, `${rootDir}/schema`);
|
|
|
|
} catch (err) {
|
|
|
|
console.error(err);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-11 10:32:06 +05:30
|
|
|
async function main() {
|
2022-04-13 16:57:33 +05:30
|
|
|
try {
|
2022-04-22 06:10:48 +05:30
|
|
|
if (fs.existsSync(destDir)) {
|
|
|
|
fs.rmSync(destDir, { recursive: true });
|
|
|
|
}
|
2022-04-13 16:57:33 +05:30
|
|
|
fs.mkdirSync(destDir, { recursive: true });
|
2022-04-22 06:10:48 +05:30
|
|
|
copySourceFiles();
|
2022-06-11 10:32:06 +05:30
|
|
|
|
|
|
|
await traverseDirectory(`${playDir}`);
|
2022-04-13 16:57:33 +05:30
|
|
|
} catch (err) {
|
|
|
|
console.log(err);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
main();
|