{"size": 2325, "ext": "js", "max_stars_repo_path": "packages/cml-component-parser/bin/cli.js", "max_stars_repo_name": "ReusLi/chameleon", "max_stars_repo_stars_event_min_datetime": "2019-11-07T14:05:06.000Z", "max_stars_repo_stars_event_max_datetime": "2019-11-07T14:05:06.000Z", "max_issues_repo_path": "packages/cml-component-parser/bin/cli.js", "max_issues_repo_name": "ReusLi/chameleon", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "packages/cml-component-parser/bin/cli.js", "max_forks_repo_name": "ReusLi/chameleon", "max_forks_repo_forks_event_min_datetime": "2020-03-06T07:26:06.000Z", "max_forks_repo_forks_event_max_datetime": "2020-03-06T07:26:06.000Z"}
{"max_stars_count": 1, "max_issues_count": null, "max_forks_count": 1, "avg_line_length": 41.5178571429, "max_line_length": 147, "alphanum_fraction": 0.6877419355}
#!/usr/bin/env node
const path = require('path');
const program = require('commander');
const packageJson = require('../package.json');
const Parser = require('../index');

program
  .version(packageJson.version)
  .usage('-i <dir> -o <dir> -f [format] -n [fileName]')
  .option('-i, --in-dir <dir>', 'The directory which contians components with interface files.')
  .option('-o, --out-dir <dir>', 'The directory which is used as output readme files folder.')
  .option('-f, --out-format [format]', 'The file format for output files, default type is markdown. valid values are: markdown or json.')
  .option('-n, --file-name [fileName]', 'If this option is set, then all outputs will be written into one single file named with file name value.')
  .parse(process.argv);

let inDir = program.inDir;
let outDir = program.outDir;
let outFormat = program.outFormat || 'markdown';
let fileName = program.fileName || '';
let isSingleFile = !!fileName;
let isMarkdown = outFormat === 'markdown';
let parser = new Parser();

if (inDir.indexOf('components') === -1) {
  inDir = path.resolve(process.cwd(), inDir, 'components');
} else {
  inDir = path.resolve(process.cwd(), inDir);
}

outDir = path.resolve(process.cwd(), outDir);

let singleFileContent = isMarkdown ? [] : {};

Parser.flatEntrance(inDir).forEach(async (interfaceFile) => {
  if (isSingleFile) {
    let content = parser.resetPath(interfaceFile)[isMarkdown ? 'getReadmeContent' : 'getJsonResultsWithComponentName']();
    isMarkdown ? singleFileContent.push(content) : (singleFileContent[content.name] = content.content);
  } else {
    await parser.resetPath(interfaceFile)[isMarkdown ? 'writeReadmeFileToDir' : 'writeJsonFileToDir'](outDir, fileName);
  }
});

// If output is targeted as single file, then write all content at this point once.
if (isSingleFile) {
  isMarkdown && (singleFileContent = singleFileContent.join(outFormat === 'json' ? ',\n' : '\n\n'));
  !isMarkdown && (singleFileContent = JSON.stringify(singleFileContent, null, '\t'));
  parser[outFormat === 'markdown' ? 'writeReadmeFileToDir' : 'writeJsonFileToDir'](outDir, fileName, singleFileContent)
    .then((res) => {
      console.info(`write ${outFormat} content into file ${fileName} ` + res);
      return res;
    })
    .catch((err) => {
      console.error(err.message);
    });
}
