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