#!/usr/bin/env node 'use strict'; function parseJSON(file) { return JSON.parse(fs.readFileSync(file, "utf8")); } const DEFAULT_CONFIG = 'ng-swagger-gen.json'; const SCHEMA = 'ng-swagger-gen-schema.json'; const fs = require('fs'); const path = require('path'); const ArgumentParser = require('argparse').ArgumentParser; const pkg = parseJSON(path.join(__dirname, "package.json")); const schema = parseJSON(path.join(__dirname, SCHEMA)); var argParser = new ArgumentParser({ version: pkg.version, addHelp: true, description: "Swagger API client generator for Angular 2+ projects." + " Either uses a configuration file (defaults to " + DEFAULT_CONFIG + ")" + " in the current directory, but the file can be specified)" + " allowing the program to be called without arguments, or the swagger input" + " file must be specified. A configuration file can be generated with all" + " default values if --gen-config is specified, together with the swagger" + " input file, which is also required in this case." }); argParser.addArgument( ["-i", "--input", "-s", "--swagger"], { help: "The swagger input file or URL. If not specified, it is required" + " that a configuration file called " + DEFAULT_CONFIG + " exist in the current directory, or a configuration file can be" + " specified with the -c or --config option.", dest: "swagger", metavar: "SWAGGER" } ); argParser.addArgument( ["-o", "--output"], { help: "The root directory where files will be generated.", dest: "output" } ); argParser.addArgument( ["-c", "--config"], { help: "Specifies the configuration file name.", defaultValue: DEFAULT_CONFIG, dest: "config" } ); argParser.addArgument( ["--gen-config"], { help: "Generates the configuration file " + DEFAULT_CONFIG + " in the current directory, or the file specified with -c or --config." + " No Swagger client classes are generated." + " If input and output are specified, their values are stored in the" + " generated file as well.", action: "storeTrue", dest: "genConfig" } ); argParser.addArgument( ["--timeout"], { help: "The amount of time (in milliseconds) to wait for a response from the" + " server when downloading files. The default is 20 seconds.", type: "int", dest: "timeout" } ); argParser.addArgument( ["--skip-proxy-setup"], { help: "Skip the proxy setup when unable to generate from localhost.", action: "storeTrue", dest: "skipProxySetup" } ); var args = argParser.parseArgs(); var config = args.config; // Check the action var configExists = fs.existsSync(config); if (args.genConfig) { if (configExists) { // Ask for confirmation askThenGenerateConfig(); } else { // Write the configuration file generateConfig(); } } else { if (configExists) { // The configuration file exists, so read it const options = parseJSON(config); // Allow overriding both swagger and output via arguments if (args.swagger) { options.swagger = args.swagger; } if (args.output) { options.output = args.output; } if (args.timeout) { options.timeout = args.timeout; } if (args.skipProxySetup) { options.skipProxySetup = args.skipProxySetup; } run(options); } else if (args.swagger) { // The args variables are the same one as the options run(args); } else { // No configuration file. Show the usage and exit. argParser.parseArgs(["--help"]); } } /** * Generates a configuration file in the current directory */ function generateConfig() { var options = { "$schema": "./node_modules/ng-swagger-gen/ng-swagger-gen-schema.json" }; options.swagger = args.swagger ? args.swagger : ""; if (args.output) { options.output = args.output; } var properties = schema.properties; for (var propName in properties) { var propDef = properties[propName]; if (propDef.default && !options.hasOwnProperty(propName)) { var value = propDef.default; if (propDef.type == "boolean") { value = value === 'true'; } else if (propDef.type == "integer") { value = parseInt(value, 10); } options[propName] = value; } } var json = JSON.stringify(options, null, 2); fs.writeFileSync(config, json, {encoding: "utf8"}); console.info("Wrote " + config); } /** * Asks if the configuration file should be overridden, then, if so, do it */ function askThenGenerateConfig() { const readline = require('readline'); const rl = readline.createInterface({ input: process.stdin, output: process.stdout }); rl.question("The configuration file " + config + " already exists.\n" + "Do you want to override it? [y/N]", (answer) => { if (answer == 'y' || answer == 'Y') { generateConfig(); } rl.close(); }); } /** * Runs the ng-swagger-gen generation */ function run(options) { var ngSwaggerGen = require("./ng-swagger-gen.js"); if (options.output == null) { options.output = schema.properties.output.default; } if (options.customFileSuffix == null) { options.customFileSuffix = schema.properties.customFileSuffix.default; } if (options.timeout == null) { options.timeout = schema.properties.timeout.default; } if (options.skipProxySetup == null) { options.skipProxySetup = schema.properties.skipProxySetup.default; } ngSwaggerGen(options); }