123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176 |
- #!/usr/bin/env node
- 'use strict';
- /*
- * apidoc
- * https://apidocjs.com
- *
- * Authors:
- * Peter Rottmann <rottmann@inveris.de>
- * Nicolas CARPi @ Deltablot
- * Copyright (c) 2013 inveris OHG
- * Licensed under the MIT license.
- */
- const os = require('os');
- const path = require('path');
- // library for watch mode
- const nodemon = require('nodemon');
- // library to parse arguments/options
- const { Command } = require('commander');
- const apidoc = require(path.join(__dirname, '..', 'lib', 'index'));
- /**
- * Transform parameters to object
- *
- * @param {String|String[]} filters
- * @returns {Object}
- */
- function transformToObject (filters) {
- if (!filters) { return; }
- if (typeof filters === 'string') { filters = [filters]; }
- const result = {};
- filters.forEach(function (filter) {
- const splits = filter.split('=');
- if (splits.length === 2) {
- result[splits[0]] = path.resolve(splits[1], '');
- }
- });
- return result;
- }
- // init program
- const program = new Command();
- // set the version from package.json so it is available with --version
- const pkgjson = require(path.join('..', 'package.json'));
- program.version(pkgjson.version);
- program
- .option('-c, --config <config>', 'Path to config file (json or javascript).', '')
- .option('--no-color', 'Turn off log color.', false)
- .option('-d, --debug', 'Show debug messages.', false)
- .option('--definitions', 'Include definitions file rather than copying definitions.', false)
- .option('--encoding <encoding>', 'Set the encoding of the source code. [utf8].', 'utf8')
- .option('-e, --exclude-filters <exclude-filters...>', 'RegEx-Filter to select files / dirs that should not be parsed (many -e can be used). node_modules and apidoc.config.js are always excluded.', [])
- .option('-f --file-filters <file-filters...>', 'RegEx-Filter to select files that should be parsed (multiple -f can be used).', [])
- .option('--filter-by <tag-filter=value>', 'Filter documentation by tag', '')
- .option('-i, --input <input...>', 'Input/source dirname.', ['./src'])
- .option('--line-ending <line-ending>', 'Turn off autodetect line-ending. Allowed values: LF, CR, CRLF.', os.EOL)
- .option('--log-format <format>', 'Change log format. Allowed values: simple, json.', 'simple')
- .option('--markdown [markdown]', 'Turn off default markdown parser or set a file to a custom parser.', true)
- .option('-n, --dry-run', 'Parse source files but do not write any output files.', false)
- .option('-o, --output <output>', 'Output dirname.', 'doc/')
- .option('--write-json', 'Will create api-data.json file with parsed API info.', false)
- .option('--parse-filters <parse-filters...>', 'Optional user defined filters. Format name=filename', [])
- .option('--parse-languages <parse-languages...>', 'Optional user defined languages. Format name=filename', [])
- .option('--parse-parsers <parse-parsers...>', 'Optional user defined parsers. Format name=filename', [])
- .option('--parse-workers <parse-workers...>', 'Optional user defined workers. Format name=filename', [])
- .option('-p, --private', 'Include private APIs in output.', false)
- .option('-q, --quiet', 'Turn all output off.', false)
- .option('-S, --single', 'Output to single file.', false)
- .option('-t, --template <template>', 'Use template for output files.', path.join(__dirname, '../template/'))
- .option('-v, --verbose', 'Verbose output.', false)
- .option('--warn-error', 'Treat warnings as error and exit with error code.', false)
- .option('-w, --watch', 'Watch input files for changes to rebuild the docs.', false)
- .parse(process.argv);
- const argv = program.opts();
- const defaultIncludeFilters = ['.*\\.(clj|cls|coffee|cpp|cs|dart|erl|exs?|go|groovy|ino?|java|js|jsx|kt|litcoffee|lua|mjs|p|php?|pl|pm|py|rb|scala|ts|tsx|vue)$'];
- const options = {
- excludeFilters: ['apidoc.config.js', 'node_modules'].concat(argv.excludeFilters.length ? argv.excludeFilters : []),
- includeFilters: argv.fileFilters.length ? argv.fileFilters : defaultIncludeFilters,
- src: argv.input,
- dest: argv.output,
- template: argv.template,
- config: argv.config,
- apiprivate: argv.private,
- verbose: argv.verbose,
- single: argv.single,
- debug: argv.debug,
- colorize: argv.color,
- filters: transformToObject(argv.parseFilters),
- languages: transformToObject(argv.parseLanguages),
- parsers: transformToObject(argv.parseParsers),
- workers: transformToObject(argv.parseWorkers),
- silent: argv.quiet,
- dryRun: argv.dryRun,
- markdown: argv.markdown,
- lineEnding: argv.lineEnding,
- encoding: argv.encoding,
- copyDefinitions: !argv.definitions,
- filterBy: argv.filterBy,
- logFormat: argv.logFormat,
- warnError: argv.warnError,
- writeJson: argv.writeJson,
- };
- if (options.debug) {
- console.debug('[debug] Parsed options:\n');
- console.debug(options);
- }
- // display ascii art in verbose mode (but not for json format)
- if (options.verbose && options.logFormat === 'simple') {
- const asciiArt = `
- _ _
- __ _ _ __ (_) __| | ___ ___
- / _' | '_ \\| |/ _' |/ _ \\ / __|
- | (_| | |_) | | (_| | (_) | (__
- \\__,_| .__/|_|\\__,_|\\___/ \\___|
- |_| v${pkgjson.version}
- `;
- console.info(asciiArt);
- }
- // Watch mode (-w/--watch)
- if (argv.watch) {
- nodemon({ watch: options.src });
- console.info('Watch mode enabled\nListening for changes on input files...');
- nodemon.on('quit', function () {
- console.info('\nProcess terminated.');
- process.exit();
- }).on('restart', function () {
- console.info('Changes detected, rebuilding...');
- apidoc.createDoc(options);
- const d = new Date();
- console.info(`Build finished at ${d.getHours()}:${d.getMinutes()}:${d.getSeconds()}`);
- });
- }
- // Create documentation
- if (apidoc.createDoc(options) === false) {
- console.error('[error] apidoc encountered an error during documentation generation!');
- process.exit(1);
- }
|