Skip to content

Commit

Permalink
Resolves #12, replace cli by yargs
Browse files Browse the repository at this point in the history
  • Loading branch information
ggrossetie committed Apr 14, 2018
1 parent 9ecb400 commit 92585a8
Show file tree
Hide file tree
Showing 3 changed files with 433 additions and 76 deletions.
237 changes: 163 additions & 74 deletions lib/cli.js
Original file line number Diff line number Diff line change
@@ -1,36 +1,41 @@
'use strict';

var cli = require('cli').enable('status');
var fs = require('fs');
var path = require('path');
var mkdirp = require('mkdirp');
const yargs = require('yargs')
var asciidoctor = require('asciidoctor.js')();

function convertOptions (cliOptions) {
var backend = cliOptions['backend'];
var doctype = cliOptions['doctype'];
var safeMode = cliOptions['safe-mode'];
var noHeaderFooter = cliOptions['no-header-footer'];
var sectionNumbers = cliOptions['section-numbers'];
var baseDir = cliOptions['base-dir'];
var destinationDir = cliOptions['destination-dir'];
var outFile = cliOptions['out-file'];
var quiet = cliOptions['quiet'];
var verbose = cliOptions['verbose'];
var timings = cliOptions['timings'];
var trace = cliOptions['trace'];
var requireLib = cliOptions['require'];
cli.debug('require ' + requireLib);
cli.debug('backend ' + backend);
cli.debug('doctype ' + doctype);
cli.debug('header_footer ' + !noHeaderFooter);
cli.debug('section-numbers ' + sectionNumbers);
cli.debug('quiet ' + quiet);
cli.debug('verbose ' + verbose);
cli.debug('timings ' + timings);
cli.debug('trace ' + trace);
cli.debug('baseDir ' + baseDir);
cli.debug('destinationDir ' + destinationDir);
function logDebug(message, verbose) {
if (verbose) {
console.log(`converting file ${file}`);
}
}

function convertOptions (argv) {
var backend = argv['backend'];
var doctype = argv['doctype'];
var safeMode = argv['safe-mode'];
var noHeaderFooter = argv['no-header-footer'];
var sectionNumbers = argv['section-numbers'];
var baseDir = argv['base-dir'];
var destinationDir = argv['destination-dir'];
var outFile = argv['out-file'];
var quiet = argv['quiet'];
var verbose = argv['verbose'];
var timings = argv['timings'];
var trace = argv['trace'];
var requireLib = argv['require'];
if (verbose) {
console.log('require ' + requireLib);
console.log('backend ' + backend);
console.log('doctype ' + doctype);
console.log('header-footer ' + !noHeaderFooter);
console.log('section-numbers ' + sectionNumbers);
console.log('quiet ' + quiet);
console.log('verbose ' + verbose);
console.log('timings ' + timings);
console.log('trace ' + trace);
console.log('base-dir ' + baseDir);
console.log('destination-dir ' + destinationDir);
}
if (requireLib) {
require(requireLib);
}
Expand All @@ -42,12 +47,14 @@ function convertOptions (cliOptions) {
if (sectionNumbers) {
attributes = attributes.concat('sectnums ');
}
var cliAttributes = cliOptions['attribute'];
var cliAttributes = argv['attribute'];
if (cliAttributes) {
attributes = attributes.concat(cliAttributes);
}
cli.debug('verboseMode ' + verboseMode);
cli.debug('attributes ' + attributes);
if (verbose) {
console.log('verbose-mode ' + verboseMode);
console.log('attributes ' + attributes);
}
var options = {
backend: backend,
doctype: doctype,
Expand All @@ -69,56 +76,138 @@ function convertOptions (cliOptions) {
options.mkdirs = true;
}
options.attributes = attributes;
cli.debug('options ' + JSON.stringify(options));
if (verbose) {
console.log('options ' + JSON.stringify(options));
}
if (options.to_file === '-') {
options.to_file = Opal.gvars.stdout;
}
return options;
}

function run () {
cli.parse({
'backend': ['b', 'set output format backend (default: html5)', 'string', 'html5'],
'doctype': ['d', 'document type to use when converting document: [article, book, manpage, inline] (default: article)', 'string', 'article'],
'out-file': ['o', 'output file (default: based on path of input file); use \'\' to output to STDOUT', 'file'],
'safe-mode': ['S', 'set safe mode level explicitly: [unsafe, safe, server, secure] (default: unsafe)), disables potentially dangerous macros in source files, such as include::[]', 'string', 'unsafe'],
'no-header-footer': ['s', 'suppress output of header and footer (default: false)', 'boolean', false],
'section-numbers': ['n', 'auto-number section titles in the HTML backend; disabled by default', 'boolean', false],
'base-dir': ['B', 'base directory containing the document and resources (default: directory of source file)', 'path'],
'destination-dir': ['D', 'destination output directory (default: directory of source file)', 'path'],
'quiet': ['q', 'suppress warnings (default: false)', 'boolean', 'false'],
'trace': [false, 'include backtrace information on errors (default: false)', 'boolean', false],
'verbose': ['v', 'enable verbose mode (default: false)', 'boolean', false],
'timings': ['t', 'enable timings mode (default: false)', 'boolean', false],
'attribute': ['a', 'a document attribute to set in the form of key, key! or key=value pair', 'string', ''],
'require': ['r', 'require the specified library before executing the processor, using the standard Node require', 'string', ''],
'version': ['V', 'display the version and runtime environment (or -v if no other flags or arguments)', 'boolean', false]
});
const argv = yargs
.detectLocale(false)
.wrap(Math.min(120, yargs.terminalWidth()))
.command('$0 [files...]', '', function (yargs) {
return yargs
.option('backend', {
alias: 'b',
default: 'html5',
describe: 'set output format backend',
type: 'string'
})
.option('doctype', {
alias: 'd',
default: 'article',
describe: 'document type to use when converting document',
choices: ['article', 'book', 'manpage', 'inline']
})
.option('out-file', {
alias: 'o',
describe: 'output file (default: based on path of input file); use \'\' to output to STDOUT',
type: 'string'
})
.option('safe-mode', {
alias: 'S',
default: 'unsafe',
describe: 'set safe mode level explicitly, disables potentially dangerous macros in source files, such as include::[]',
choices: ['unsafe', 'safe', 'server', 'secure']
})
.option('no-header-footer', {
alias: 's',
default: false,
describe: 'suppress output of header and footer',
type: 'boolean'
})
.option('section-numbers', {
alias: 'n',
default: false,
describe: 'auto-number section titles in the HTML backend; disabled by default',
type: 'boolean'
})
.option('base-dir', {
// FIXME: check that the directory exists ? coerce to a directory ?
alias: 'B',
describe: 'base directory containing the document and resources (default: directory of source file)',
type: 'string'
})
.option('destination-dir', {
// FIXME: check that the directory exists ? coerce to a directory ?
alias: 'D',
describe: 'destination output directory (default: directory of source file)',
type: 'string'
})
.option('quiet', {
alias: 'q',
default: false,
describe: 'suppress warnings',
type: 'boolean'
})
.option('trace', {
default: false,
describe: 'include backtrace information on errors',
type: 'boolean'
})
.option('verbose', {
alias: 'v',
default: false,
describe: 'enable verbose mode',
type: 'boolean'
})
.option('timings', {
alias: 't',
default: false,
describe: 'enable timings mode',
type: 'boolean'
})
.option('attribute', {
alias: 'a',
array: true,
describe: 'a document attribute to set in the form of key, key! or key=value pair',
type: 'string'
})
.option('require', {
alias: 'r',
array: true,
describe: 'require the specified library before executing the processor, using the standard Node require',
type: 'string'
})
.option('version', {
alias: 'V',
default: false,
describe: 'display the version and runtime environment (or -v if no other flags or arguments)',
type: 'boolean'
})
})
.help()
.argv

cli.main(function (cliArgs, cliOptions) {
var options = convertOptions(cliOptions);
var verbose = cliOptions['verbose'];
var version = cliOptions['version'];
if (version || (verbose && cliArgs.length === 0)) {
cli.info("Asciidoctor " + asciidoctor.getCoreVersion() + " [http://asciidoctor.org]");
var releaseName = process.release ? process.release.name : 'node';
cli.info("Runtime Environment (" + releaseName + " " + process.version + " on " + process.platform + ")");
} else if (cliArgs.length) {
cliArgs.forEach(function (file) {
cli.debug('converting file ' + file);
if (options.timings) {
var timings = asciidoctor.Timings.$new();
var instanceOptions = Object.assign({}, options, { timings: timings })
asciidoctor.convertFile(file, instanceOptions);
timings.$print_report(Opal.gvars.stderr, file);
} else {
asciidoctor.convertFile(file, options);
}
});
} else {
cli.getUsage();
}
});
const verbose = argv['verbose'];
const version = argv['version'];
const files = argv['files'];
const options = convertOptions(argv);
if (version || (verbose && files && files.length === 0)) {
console.log("Asciidoctor " + asciidoctor.getCoreVersion() + " [http://asciidoctor.org]");
const releaseName = process.release ? process.release.name : 'node';
console.log("Runtime Environment (" + releaseName + " " + process.version + " on " + process.platform + ")");
} else if (files && files.length > 0) {
files.forEach(function (file) {
if (verbose) {
console.log(`converting file ${file}`);
}
if (argv['timings']) {
const timings = asciidoctor.Timings.$new();
const instanceOptions = Object.assign({}, options, {timings: timings})
asciidoctor.convertFile(file, instanceOptions);
timings.$print_report(Opal.gvars.stderr, file);
} else {
asciidoctor.convertFile(file, options);
}
});
} else {
yargs.showHelp();
}
}

module.exports = run;
3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,7 @@
},
"homepage": "https://github.com/asciidoctor/asciidoctor-cli.js",
"dependencies": {
"cli": "^0.6.5",
"mkdirp": "^0.5.0"
"yargs": "11.0.0"
},
"devDependencies": {
"asciidoctor.js": "^1.5.6-rc.1"
Expand Down
Loading

0 comments on commit 92585a8

Please sign in to comment.