Skip to content

Commit

Permalink
Merge pull request #49 from kalramanoj2002/feature/upgrade-packages
Browse files Browse the repository at this point in the history
upgrade apidoc-core, fs-extra and use markdown-it
  • Loading branch information
fsbahman authored Dec 11, 2019
2 parents b73be12 + c8ce291 commit b1462f0
Show file tree
Hide file tree
Showing 8 changed files with 6,177 additions and 125 deletions.
49 changes: 1 addition & 48 deletions bin/apidocSwagger.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,30 +48,6 @@ var argv = nomnom
// markdown settings
.option('markdown', { flag: true, 'default': true, help: 'Turn off markdown parser.' })

.option('marked-config', { 'default': '',
help: 'Enable custom markdown parser configs. It will overwite all other marked settings.' })

.option('marked-gfm', { flag: true, 'default': true,
help: 'Enable GitHub flavored markdown.' })

.option('marked-tables', { flag: true, 'default': true,
help: 'Enable GFM tables. This option requires the gfm option to be true.' })

.option('marked-breaks', { flag: true, 'default': false,
help: 'Enable GFM line breaks. This option requires the gfm option to be true.' })

.option('marked-pedantic', { flag: true, 'default': false,
help: 'Conform to obscure parts of markdown.pl as much as possible.' })

.option('marked-sanitize', { flag: true, 'default': false,
help: 'Sanitize the output. Ignore any HTML that has been input.' })

.option('marked-smartLists', { flag: true, 'default': false,
help: 'Use smarter list behavior than the original markdown.' })

.option('marked-smartypants', { flag: true, 'default': false,
help: 'Use \'smart\' typograhic punctuation for things like quotes and dashes.' })

.parse()
;

Expand Down Expand Up @@ -99,28 +75,6 @@ function transformToObject(filters) {
return result;
}

/**
* Sets configuration for markdown
*
* @param {Array} argv
* @returns {Object}
*/
function resolveMarkdownOptions(argv) {
if (argv['marked-config']) {
return require(path.resolve(argv['marked-config']));
} else {
return {
gfm : argv['marked-gfm'],
tables : argv['marked-tables'],
breaks : argv['marked-breaks'],
pedantic : argv['marked-pedantic'],
sanitize : argv['marked-sanitize'],
smartLists : argv['marked-smartLists'],
smartypants: argv['marked-smartypants']
};
}
}

var options = {
excludeFilters: argv['exclude-filters'],
includeFilters: argv['file-filters'],
Expand All @@ -136,8 +90,7 @@ var options = {
workers : transformToObject(argv['parse-workers']),
silent : argv['silent'],
simulate : argv['simulate'],
markdown : argv['markdown'],
marked : resolveMarkdownOptions(argv)
markdown : argv['markdown']
};

if (apidocSwagger.createApidocSwagger(options) === false) {
Expand Down
4 changes: 2 additions & 2 deletions lib/apidocToSwagger.js
Original file line number Diff line number Diff line change
Expand Up @@ -293,9 +293,9 @@ function createPathParameters(verbs, pathKeys) {
function groupByUrl(apidocJson) {
return _.chain(apidocJson)
.groupBy("url")
.pairs()
.toPairs()
.map(function (element) {
return _.object(_.zip(["url", "verbs"], element));
return _.zipObject(["url", "verbs"], element);
})
.value();
}
Expand Down
84 changes: 39 additions & 45 deletions lib/index.js
Original file line number Diff line number Diff line change
@@ -1,45 +1,35 @@
var _ = require('lodash');
var apidoc = require('apidoc-core');
var winston = require('winston');
var path = require('path');
var markdown = require('marked');
var nomnom = require('nomnom');
var fs = require('fs-extra');
var _ = require('lodash');
var apidoc = require('apidoc-core');
var winston = require('winston');
var path = require('path');
var markdown = require('markdown-it');
var nomnom = require('nomnom');
var fs = require('fs-extra');
var PackageInfo = require('./package_info');

var apidocSwagger = require('./apidocToSwagger');

var defaults = {
dest : path.join(__dirname, '../doc/'),
dest: path.join(__dirname, '../doc/'),
template: path.join(__dirname, '../template/'),

debug : false,
silent : false,
verbose : false,
debug: false,
silent: false,
verbose: false,
simulate: false,
parse : false, // only parse and return the data, no file creation
parse: false, // only parse and return the data, no file creation
colorize: true,
markdown: true,

marked: {
gfm : true,
tables : true,
breaks : false,
pedantic : false,
sanitize : false,
smartLists : false,
smartypants: false
}
markdown: true
};

var app = {
log : {},
log: {},
markdown: false,
options : {}
options: {}
};

// uncaughtException
process.on('uncaughtException', function(err) {
process.on('uncaughtException', function (err) {
console.error((new Date()).toUTCString() + ' uncaughtException:', err.message);
console.error(err.stack);
process.exit(1);
Expand All @@ -53,7 +43,7 @@ function createApidocSwagger(options) {
options = _.defaults({}, options, defaults);

// paths
options.dest = path.join(options.dest, './');
options.dest = path.join(options.dest, './');

// options
app.options = options;
Expand All @@ -62,30 +52,34 @@ function createApidocSwagger(options) {
app.log = new (winston.Logger)({
transports: [
new (winston.transports.Console)({
level : app.options.debug ? 'debug' : app.options.verbose ? 'verbose' : 'info',
silent : app.options.silent,
level: app.options.debug ? 'debug' : app.options.verbose ? 'verbose' : 'info',
silent: app.options.silent,
prettyPrint: true,
colorize : app.options.colorize,
timestamp : false
colorize: app.options.colorize,
timestamp: false
}),
]
});

// markdown
if(app.options.markdown === true) {
app.markdown = markdown;
app.markdown.setOptions(app.options.marked);
if (app.options.markdown === true) {
app.markdown = new markdown({
breaks: false,
html: true,
linkify: false,
typographer: false
});
}

try {
packageInfo = new PackageInfo(app);

// generator information
var json = JSON.parse( fs.readFileSync(apidocPath + 'package.json', 'utf8') );
var json = JSON.parse(fs.readFileSync(apidocPath + 'package.json', 'utf8'));
apidoc.setGeneratorInfos({
name : json.name,
time : new Date(),
url : json.homepage,
name: json.name,
time: new Date(),
url: json.homepage,
version: json.version
});
apidoc.setLogger(app.log);
Expand All @@ -101,35 +95,35 @@ function createApidocSwagger(options) {
if (api === false)
return false;

if (app.options.parse !== true){
if (app.options.parse !== true) {
var apidocData = JSON.parse(api.data);
var projectData = JSON.parse(api.project);
api["swaggerData"] = JSON.stringify(apidocSwagger.toSwagger(apidocData , projectData));
api["swaggerData"] = JSON.stringify(apidocSwagger.toSwagger(apidocData, projectData));
createOutputFile(api);
}

app.log.info('Done.');
return api;
} catch(e) {
} catch (e) {
app.log.error(e.message);
if (e.stack)
app.log.debug(e.stack);
return false;
}
}

function createOutputFile(api){
function createOutputFile(api) {
if (app.options.simulate)
app.log.warn('!!! Simulation !!! No file or dir will be copied or created.');

app.log.verbose('create dir: ' + app.options.dest);
if ( ! app.options.simulate)
if (!app.options.simulate)
fs.mkdirsSync(app.options.dest);

//Write swagger
app.log.verbose('write swagger json file: ' + app.options.dest + 'swagger.json');
if( ! app.options.simulate)
fs.writeFileSync(app.options.dest + './swagger.json', api.swaggerData);
if (!app.options.simulate)
fs.writeFileSync(app.options.dest + './swagger.json', api.swaggerData);
}

module.exports = {
Expand Down
Loading

0 comments on commit b1462f0

Please sign in to comment.