|
| 1 | +const fs = require("fs"); |
| 2 | +const path = require("path"); |
| 3 | +const inquirer = require("inquirer"); |
| 4 | +const chalk = require("chalk"); |
| 5 | +const prompt = inquirer.createPromptModule(); |
| 6 | +const yaml = require("js-yaml"); |
| 7 | +const slugify = require("slugify"); |
| 8 | +const { execSync } = require('child_process'); |
| 9 | + |
| 10 | +const swaggerRepo = require("swagger-repo"); |
| 11 | + |
| 12 | +const { |
| 13 | + copy, |
| 14 | + copyDirSync, |
| 15 | + render, |
| 16 | + getGhPagesBaseUrl, |
| 17 | + validateSpecFileName, |
| 18 | + getCurrentGitHubRepo |
| 19 | +} = require("./lib/utils"); |
| 20 | + |
| 21 | +const { installDeps } = require("./lib/install-deps"); |
| 22 | + |
| 23 | +const REDOCLY_RC = ".redoclyrc"; |
| 24 | + |
| 25 | +async function ask() { |
| 26 | + console.log("Welcome to the " + chalk.green("OpenAPI-Repo") + " generator!"); |
| 27 | + |
| 28 | + const { haveSpec } = await prompt({ |
| 29 | + type: "confirm", |
| 30 | + name: "haveSpec", |
| 31 | + message: "Do you already have OpenAPI/Swagger spec for your API?", |
| 32 | + default: false |
| 33 | + }); |
| 34 | + |
| 35 | + let specFileName; |
| 36 | + if (haveSpec) { |
| 37 | + specFileName = (await prompt({ |
| 38 | + type: "input", |
| 39 | + name: "specFileName", |
| 40 | + message: |
| 41 | + "Please specify the path to the OpenAPI/Swagger spec (local file):", |
| 42 | + validate(fileName) { |
| 43 | + return validateSpecFileName(fileName); |
| 44 | + } |
| 45 | + })).specFileName; |
| 46 | + } |
| 47 | + |
| 48 | + let spec; |
| 49 | + if (haveSpec) { |
| 50 | + spec = yaml.safeLoad(fs.readFileSync(specFileName, "utf8")); |
| 51 | + } |
| 52 | + |
| 53 | + const { apiTitle } = await prompt({ |
| 54 | + type: "input", |
| 55 | + name: "apiTitle", |
| 56 | + message: "API Name:", |
| 57 | + default: haveSpec ? spec.title : undefined, |
| 58 | + validate: i => (i.length > 0 ? true : `API Name can't be empty`) |
| 59 | + }); |
| 60 | + |
| 61 | + const { splitSpec } = await prompt({ |
| 62 | + type: "confirm", |
| 63 | + name: "splitSpec", |
| 64 | + message: `Split spec into separate files: paths/*, definitions/* ${chalk.yellow( |
| 65 | + "[Experimental]" |
| 66 | + )}?`, |
| 67 | + default: true |
| 68 | + }); |
| 69 | + |
| 70 | + const { codeSamples } = await prompt({ |
| 71 | + type: "confirm", |
| 72 | + name: "codeSamples", |
| 73 | + message: `Prepare manual code samples folder?`, |
| 74 | + default: true |
| 75 | + }); |
| 76 | + |
| 77 | + const { swaggerUI } = await prompt({ |
| 78 | + type: "confirm", |
| 79 | + name: "swaggerUI", |
| 80 | + message: `Install SwaggerUI?`, |
| 81 | + default: false |
| 82 | + }); |
| 83 | + |
| 84 | + const { travis } = await prompt({ |
| 85 | + type: "confirm", |
| 86 | + name: "travis", |
| 87 | + message: `Set up Travis CI?`, |
| 88 | + default: true |
| 89 | + }); |
| 90 | + |
| 91 | + let repo; |
| 92 | + if (travis) { |
| 93 | + repo = (await prompt({ |
| 94 | + type: "input", |
| 95 | + name: "repo", |
| 96 | + message: `Specify name of GitHub repo in format ${chalk.blue( |
| 97 | + "User/Repo" |
| 98 | + )}:`, |
| 99 | + default: getCurrentGitHubRepo, |
| 100 | + validate: function(input) { |
| 101 | + return input.indexOf("/") > 0 ? true : 'Repo Name must contain "/"'; |
| 102 | + } |
| 103 | + })).repo; |
| 104 | + } |
| 105 | + |
| 106 | + return { |
| 107 | + specFileName, |
| 108 | + apiTitle, |
| 109 | + splitSpec, |
| 110 | + codeSamples, |
| 111 | + swaggerUI, |
| 112 | + travis, |
| 113 | + repo |
| 114 | + }; |
| 115 | +} |
| 116 | + |
| 117 | +function printSuccess(opts, root) { |
| 118 | + console.log(`${chalk.green("Success!")} Created ${chalk.green( |
| 119 | + path.dirname(root) |
| 120 | + )} at ${chalk.blue(root)} |
| 121 | +Inside that directory, you can run several commands: |
| 122 | + |
| 123 | + ${chalk.blue(`npm start`)} |
| 124 | + Starts the development server. |
| 125 | +
|
| 126 | + ${chalk.blue(`npm run build`)} |
| 127 | + Bundles the spec and prepares ${chalk.blue( |
| 128 | + "web_deploy" |
| 129 | + )} folder with static assets. |
| 130 | +
|
| 131 | + ${chalk.blue(`npm test`)} |
| 132 | + Validates the spec. |
| 133 | + |
| 134 | +${opts.travis && |
| 135 | + ` ${chalk.blue(`npm run deploy`)} |
| 136 | + Deploys the spec to GitHub Pages. You don't need to run it manually if you have Travis CI configured. |
| 137 | +`} |
| 138 | +
|
| 139 | +We suggest that you begin by typing: |
| 140 | +
|
| 141 | + ${chalk.blue("cd")} ${path.dirname(root)} |
| 142 | + ${chalk.blue("npm start")}`); |
| 143 | +} |
| 144 | + |
| 145 | +async function run() { |
| 146 | + const specRoot = process.argv[2]; |
| 147 | + |
| 148 | + if (!specRoot) { |
| 149 | + console.log(`Please specify the spec directory: |
| 150 | + ${chalk.blue("create-openapi-repo")} <spec-directory> |
| 151 | +
|
| 152 | +For example: |
| 153 | + ${chalk.blue("create-openapi-repo")} my-spec`); |
| 154 | + |
| 155 | + process.exit(1); |
| 156 | + } |
| 157 | + |
| 158 | + if ( |
| 159 | + fs.existsSync(specRoot) && |
| 160 | + fs.existsSync(path.join(specRoot, REDOCLY_RC)) |
| 161 | + ) { |
| 162 | + console.log(`The directory ${chalk.green( |
| 163 | + specRoot |
| 164 | + )} already contains ${chalk.green(REDOCLY_RC)} |
| 165 | +
|
| 166 | +Choose another directory or remove contents. |
| 167 | +`); |
| 168 | + process.exit(1); |
| 169 | + } |
| 170 | + |
| 171 | + if (!fs.existsSync(specRoot)) { |
| 172 | + fs.mkdirSync(specRoot); |
| 173 | + } |
| 174 | + |
| 175 | + const opts = await ask(); |
| 176 | + |
| 177 | + const data = { |
| 178 | + ...opts, |
| 179 | + packageName: slugify(opts.apiTitle), |
| 180 | + ghPagesBaseUrl: opts.repo ? getGhPagesBaseUrl(opts.repo) : undefined |
| 181 | + }; |
| 182 | + |
| 183 | + let { specFileName } = opts; |
| 184 | + if (!specFileName) { |
| 185 | + specFileName = require.resolve("openapi-template"); |
| 186 | + } |
| 187 | + |
| 188 | + process.chdir(specRoot); |
| 189 | + |
| 190 | + console.log( |
| 191 | + `\nCreating a new OpenAPI repo in ${chalk.blue(path.resolve("."))}\n` |
| 192 | + ); |
| 193 | + await copy(".gitignore"); |
| 194 | + await copy("LICENSE"); |
| 195 | + await render("package.json", data); |
| 196 | + await render("README.md", data); |
| 197 | + await copy("spec/README.md"); |
| 198 | + |
| 199 | + if (opts.splitSpec) { |
| 200 | + copyDirSync("spec/definitions"); |
| 201 | + copyDirSync("spec/paths"); |
| 202 | + } |
| 203 | + |
| 204 | + if (opts.codeSamples) { |
| 205 | + copyDirSync("spec/code_samples"); |
| 206 | + } |
| 207 | + |
| 208 | + if (opts.travis) { |
| 209 | + await copy(".travis.yml"); |
| 210 | + } |
| 211 | + |
| 212 | + copyDirSync("web"); |
| 213 | + |
| 214 | + swaggerRepo.syncWithSwagger(fs.readFileSync(specFileName).toString()); |
| 215 | + |
| 216 | + fs.writeFileSync(REDOCLY_RC, yaml.safeDump(opts, { skipInvalid: true })); |
| 217 | + |
| 218 | + console.log("Installing packages. This might take a couple of minutes.\n"); |
| 219 | + |
| 220 | + await installDeps(opts); |
| 221 | + console.log(); |
| 222 | + |
| 223 | + try { |
| 224 | + execSync(`git init`, {stdio: 'inherit'}); |
| 225 | + execSync(`git add . && git commit -m "Initial commit from create-openapi-repo"`); |
| 226 | + } catch(e) { |
| 227 | + // skip error |
| 228 | + } |
| 229 | + |
| 230 | + printSuccess(opts, path.resolve(".")); |
| 231 | +} |
| 232 | + |
| 233 | +try { |
| 234 | + run(); |
| 235 | +} catch (e) { |
| 236 | + console.log(e); |
| 237 | +} |
0 commit comments