|
| 1 | +import { readYaml } from "../src/common/util"; |
| 2 | +import fs from "fs"; |
| 3 | +import _ from "lodash"; |
| 4 | +import path from "path"; |
| 5 | +import { authParamsConfig, retrieveSpec } from "../src/plugins/openApi/parse"; |
| 6 | +import postManToOpenApi from "postman-to-openapi"; |
| 7 | +import { program } from "commander"; |
| 8 | + |
| 9 | +interface Options { |
| 10 | + force: boolean; |
| 11 | + name: string; |
| 12 | + url: string; |
| 13 | + postMan: boolean; |
| 14 | + postManCollectionFile: string; |
| 15 | + id?: string; |
| 16 | +} |
| 17 | + |
| 18 | +async function gen(options: Options) { |
| 19 | + const { postMan, force, postManCollectionFile, name, url: specUrl, id: pluginId } = options; |
| 20 | + const id = pluginId ?? _.camelCase(name); |
| 21 | + const pluginDir = path.join(path.dirname(__dirname), "src/plugins", id); |
| 22 | + const pluginEntryFile = path.join(pluginDir, "index.ts"); |
| 23 | + const pluginSpecYamlFile = path.join(pluginDir, `${id}.spec.yaml`); |
| 24 | + const pluginSpecJsonFile = path.join(pluginDir, `${id}.spec.json`); |
| 25 | + const pluginDefaultCollectionFile = path.join(pluginDir, `${id}.collection.json`); |
| 26 | + |
| 27 | + console.info(); |
| 28 | + console.info("start generate plugin, id:", id, "name:", name); |
| 29 | + |
| 30 | + if (postMan) { |
| 31 | + console.info("is PostMan Collection start transforming..."); |
| 32 | + const collection = postManCollectionFile ?? pluginDefaultCollectionFile; |
| 33 | + await postManToOpenApi(collection, pluginSpecYamlFile, { defaultTag: "General" }); |
| 34 | + } |
| 35 | + |
| 36 | + if (!fs.existsSync(pluginDir)) { |
| 37 | + fs.mkdirSync(pluginDir, { recursive: true }); |
| 38 | + console.info(`plugin dir ${id} created.`); |
| 39 | + } |
| 40 | + |
| 41 | + if (!force && fs.existsSync(pluginEntryFile)) { |
| 42 | + console.info(`plugin: ${id} is already existed.`); |
| 43 | + return; |
| 44 | + } |
| 45 | + |
| 46 | + // 1. fetch spec url |
| 47 | + let spec: any; |
| 48 | + const isYamlSpecExist = fs.existsSync(pluginSpecYamlFile); |
| 49 | + const isJsonSpecExist = fs.existsSync(pluginSpecJsonFile); |
| 50 | + if (!isYamlSpecExist && !isJsonSpecExist) { |
| 51 | + if (!specUrl) { |
| 52 | + console.error("specUrl is required to fetch OpenAPI spec."); |
| 53 | + return; |
| 54 | + } |
| 55 | + console.info(`start fetching:`, specUrl); |
| 56 | + const { spec } = await retrieveSpec(specUrl); |
| 57 | + fs.writeFileSync(pluginSpecJsonFile, JSON.stringify(spec, null, 2)); |
| 58 | + console.info(`${name}spec.json saved`); |
| 59 | + } else { |
| 60 | + if (isJsonSpecExist) { |
| 61 | + const specJson = fs.readFileSync(pluginSpecJsonFile).toString(); |
| 62 | + spec = JSON.parse(specJson); |
| 63 | + console.info("got spec from json file:", pluginSpecJsonFile); |
| 64 | + } |
| 65 | + if (isYamlSpecExist) { |
| 66 | + spec = readYaml(pluginSpecYamlFile); |
| 67 | + console.info("got spec from yaml file:", pluginSpecYamlFile); |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + if (!spec) { |
| 72 | + console.error("can not get spec"); |
| 73 | + return; |
| 74 | + } |
| 75 | + |
| 76 | + // 2. get data source params |
| 77 | + const dataSourceParams = await authParamsConfig(spec); |
| 78 | + |
| 79 | + // 3. gen code |
| 80 | + const template = fs |
| 81 | + .readFileSync(path.join(__dirname, "./openApiDataSourceTemplate.tpl")) |
| 82 | + .toString(); |
| 83 | + const data = { |
| 84 | + id, |
| 85 | + name, |
| 86 | + isJsonSpec: isJsonSpecExist, |
| 87 | + isYamlSpec: isYamlSpecExist, |
| 88 | + dataSourceParams: JSON.stringify(dataSourceParams, null, 2), |
| 89 | + }; |
| 90 | + const compiledTemplate = _.template(template); |
| 91 | + const code = compiledTemplate(data); |
| 92 | + fs.writeFileSync(pluginEntryFile, code); |
| 93 | + console.info("success generate plugin:", pluginDir); |
| 94 | + console.info(); |
| 95 | +} |
| 96 | + |
| 97 | +const plugins = [ |
| 98 | + // ["Jira", "https://developer.atlassian.com/cloud/jira/platform/swagger-v3.v3.json"], |
| 99 | + [], |
| 100 | +]; |
| 101 | + |
| 102 | +program |
| 103 | + .option("--force") |
| 104 | + .option("--post-man") |
| 105 | + .option("-f, --post-man-collection-file [postman collection file path]") |
| 106 | + .option("-n, --name <char>") |
| 107 | + .option("-i, --id [plugin id]") |
| 108 | + .option("--url [spec-download-url]"); |
| 109 | + |
| 110 | +program.parse(); |
| 111 | + |
| 112 | +const options = program.opts<Options>(); |
| 113 | + |
| 114 | +gen(options); |
0 commit comments