All files / utils createSwaggerIndex.js

100% Statements 10/10
100% Branches 0/0
100% Functions 1/1
100% Lines 10/10
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 391x 1x   1x   1x 1x 1x                     6x 6x   6x   6x                              
const fs = require('fs');
const path = require('path');
 
const swaggerSpec = require('../swagger-ui-dist/swagger.spec.json');
 
const INDEX_DEFAULT_PATH = path.join(__dirname, '../swagger-ui-dist/index.html');
const INDEX_TEMPLATE_DEFAULT_PATH = path.join(__dirname, '../swagger-ui-dist/index.template.html');
const SPEC_TEMPLATE_PLACEHOLDER = '<-- This is where the Swagger JSON will be placed -->';
 
/**
 * Loads Swagger template and creates main Swagger index.html file
 * with all `spec` data loaded from `swagger.spec.json`.
 *
 * @category utils
 *
 * @return {String} The already saved content for the main Swagger index.html file
 */
function createSwaggerIndex() {
  const templateContent = fs.readFileSync(INDEX_TEMPLATE_DEFAULT_PATH, 'utf-8');
  const specContent = templateContent.replace(SPEC_TEMPLATE_PLACEHOLDER, JSON.stringify(swaggerSpec, undefined, 2));
 
  fs.writeFileSync(INDEX_DEFAULT_PATH, specContent, 'utf-8');
 
  return specContent;
}
 
/* Check if this file is called for starting the app or called as additional module to already started app */
/* istanbul ignore next: because this involves loading file via `require` or executing the file directly from the terminal */
if (require.main === module) {
  createSwaggerIndex();
} else {
  module.exports = {
    createSwaggerIndex,
    INDEX_DEFAULT_PATH,
    INDEX_TEMPLATE_DEFAULT_PATH,
    SPEC_TEMPLATE_PLACEHOLDER,
  };
}