Skip to content

Commit

Permalink
refactor cli a bit
Browse files Browse the repository at this point in the history
  • Loading branch information
kalinchernev committed Oct 20, 2020
1 parent 2b7d6a8 commit c3cdcc5
Showing 1 changed file with 27 additions and 44 deletions.
71 changes: 27 additions & 44 deletions bin/swagger-jsdoc.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,13 @@
#!/usr/bin/env node

/**
* Module dependencies.
*/
const program = require('commander');
const fs = require('fs');
const path = require('path');
const jsYaml = require('js-yaml');
const swaggerJSDoc = require('..');
const pkg = require('../package.json');

// Useful input.
const input = process.argv.slice(2);
// The spec, following a convention.
let output = 'swagger.json';

/**
Expand All @@ -22,18 +17,13 @@ let output = 'swagger.json';
* @param {string} fileName - Name the output file.
*/
function createSpecification(swaggerDefinition, apis, fileName) {
// Options for the swagger docs
let swaggerSpec;
const ext = path.extname(fileName);
const options = {
// Import swaggerDefinitions
swaggerDefinition,
// Path to the API docs
apis,
};

// Initialize swagger-jsdoc -> returns validated JSON or YAML swagger spec
let swaggerSpec;
const ext = path.extname(fileName);

if (ext === '.yml' || ext === '.yaml') {
swaggerSpec = jsYaml.dump(swaggerJSDoc(options), {
schema: jsYaml.JSON_SCHEMA,
Expand All @@ -43,49 +33,42 @@ function createSpecification(swaggerDefinition, apis, fileName) {
swaggerSpec = JSON.stringify(swaggerJSDoc(options), null, 2);
}

fs.writeFile(fileName, swaggerSpec, (err) => {
if (err) {
throw err;
}
console.log('Swagger specification is ready.');
});
fs.writeFileSync(fileName, swaggerSpec);
console.log('Swagger specification is ready.');
}

function loadJsSpecification(data, resolvedPath) {
// eslint-disable-next-line
return require(resolvedPath);
}

const YAML_OPTS = {
// OpenAPI spec mandates JSON-compatible YAML
schema: jsYaml.JSON_SCHEMA,
};

function loadYamlSpecification(data) {
return jsYaml.load(data, YAML_OPTS);
}
/**
* Get an object of the definition file configuration.
* @param {string} defPath
* @param {object} swaggerDefinition
*/
function loadDefinition(defPath, swaggerDefinition) {
const resolvedPath = path.resolve(defPath);
const extName = path.extname(resolvedPath);

const LOADERS = {
'.js': loadJsSpecification,
'.json': JSON.parse,
'.yml': loadYamlSpecification,
'.yaml': loadYamlSpecification,
};
// eslint-disable-next-line
const loadJs = () => require(resolvedPath);
const loadJson = () => JSON.parse(swaggerDefinition);
const loadYaml = () =>
jsYaml.load(swaggerDefinition, {
schema: jsYaml.JSON_SCHEMA, // OpenAPI spec mandates JSON-compatible YAML
});

// Get an object of the definition file configuration.
function loadSpecification(defPath, data) {
const resolvedPath = path.resolve(defPath);
const LOADERS = {
'.js': loadJs,
'.json': loadJson,
'.yml': loadYaml,
'.yaml': loadYaml,
};

const extName = path.extname(resolvedPath);
const loader = LOADERS[extName];

// Check whether the definition file is actually a usable file
if (loader === undefined) {
throw new Error('Definition file should be .js, .json, .yml or .yaml');
}

const swaggerDefinition = loader(data, resolvedPath);
return swaggerDefinition;
return loader();
}

program
Expand Down Expand Up @@ -124,7 +107,7 @@ fs.readFile(program.definition, 'utf-8', (err, data) => {
let swaggerDefinition;

try {
swaggerDefinition = loadSpecification(program.definition, data);
swaggerDefinition = loadDefinition(program.definition, data);
} catch (error) {
const message = `Error while loading definition file '${program.definition}':\n${error.message}`;
return console.log(message);
Expand Down

0 comments on commit c3cdcc5

Please sign in to comment.