{"size": 3141, "ext": "js", "max_stars_repo_path": "bin/mongodb-fixtures.js", "max_stars_repo_name": "cdimascio/node-mongodb-fixtures", "max_stars_repo_stars_event_min_datetime": "2017-07-18T03:17:54.000Z", "max_stars_repo_stars_event_max_datetime": "2021-11-16T15:17:56.000Z", "max_issues_repo_path": "bin/mongodb-fixtures.js", "max_issues_repo_name": "cdimascio/node-mongodb-fixtures", "max_issues_repo_issues_event_min_datetime": "2018-02-10T15:58:39.000Z", "max_issues_repo_issues_event_max_datetime": "2021-10-01T23:55:08.000Z", "max_forks_repo_path": "bin/mongodb-fixtures.js", "max_forks_repo_name": "cdimascio/node-mongodb-fixtures", "max_forks_repo_forks_event_min_datetime": "2017-07-19T16:33:18.000Z", "max_forks_repo_forks_event_max_datetime": "2019-11-07T13:29:41.000Z"}
{"max_stars_count": 92, "max_issues_count": 17, "max_forks_count": 14, "avg_line_length": 24.7322834646, "max_line_length": 80, "alphanum_fraction": 0.6144539955}
#!/usr/bin/env node

/*
  Command line utility (CLI) to create and tear down MongoDB test fixtures
 */

const fs = require('fs');
const program = require('commander');
const path = require('path');
const Fixtures = require('../index');
const pkg = require('../package.json');

const DEFAULT_PATH = 'fixtures';

const required = (val, name) => {
  if (!val) {
    console.error(`${name} required.`);
    process.exit(1);
  }
};

let command;
program.version(pkg.version);

program.command('load').action(cmd => (command = 'load'));

program.command('unload').action(cmd => (command = 'unload'));

program.command('rebuild').action(cmd => (command = 'rebuild'));

program
  .option('-u --url <url>', 'mongo connection string')
  .option('-s --ssl', 'use SSL', false)
  .option('-d --db_name <name>', 'database name', false)
  .option('-n --ssl_novalidate', 'use SSL with no verification', false)
  .option('-c --ssl_ca <path/to/cert>', 'path to cert', false)
  .option('-p --path <path>', 'resource path. Default ./' + DEFAULT_PATH, false)
  .option(
    '-f --filter <pattern>',
    "regex pattern to filter fixture names e.g. '.*people'",
    false
  )
  .option('-b --verbose', 'verbose logs', false);

program.parse(process.argv);

validate();

const dir = program.path
  ? path.isAbsolute(program.path)
    ? program.path
    : path.join(process.cwd(), program.path)
  : path.join(process.cwd(), DEFAULT_PATH);

main({
  command,
  program: program,
  dir,
});

function main(opts) {
  const command = opts.command;
  const program = opts.program;
  const dir = opts.dir;
  const filter = program.filter;

  const uri = program.url;
  const dbName = program.db_name;
  const mongoOptions = { useUnifiedTopology: true };

  if (opts.program.ssl_novalidate) {
    mongoOptions.ssl = true;
    mongoOptions.sslValidate = false;
  }
  if (opts.program.ssl) {
    mongoOptions.ssl = true;
    mongoOptions.sslValidate = true;
  }
  if (opts.program.ssl_ca) {
    mongoOptions.ssl = true;
    mongoOptions.sslCA = [fs.readFileSync(opts.program.ssl_ca)];
  }

  const fixtures = new Fixtures({ dir: dir, filter: filter });

  switch (command) {
    case 'load':
      fixtures
        .connect(uri, mongoOptions, dbName)
        .then(() => fixtures.load())
        .catch(e => console.error(e.message))
        .finally(() => fixtures.disconnect());
      break;
    case 'unload':
      fixtures
        .connect(uri, mongoOptions, dbName)
        .then(() => fixtures.unload())
        .catch(e => console.error(e.message))
        .finally(() => fixtures.disconnect());
      break;
    case 'rebuild':
      fixtures
        .connect(uri, mongoOptions, dbName)
        .then(() => fixtures.unload())
        .catch(e => {})
        .then(() => fixtures.load())
        .catch(e => console.error(e.message))
        .finally(() => fixtures.disconnect());
      break;
    default:
      exit('Invalid command.');
  }
}

function validate() {
  if (!command) exit('Invalid command.');
  if (!program.url) exit('Url required.');
}

function exit(msg) {
  if (msg) {
    console.log('Error: ' + msg);
  }
  program.help();
  process.exit(1);
}
