#!/usr/bin/env node

import yargs from 'yargs';
import {
    writeIndexCli
} from '../utilities';

const argv = yargs
  .demand(1)
  .options({
    recursive: {
      alias: 'r',
      default: false,
      description: 'Create/update index files recursively. Halts on any unsafe "index.js" files.',
      type: 'boolean'
    }
  })
  .options({
    defaultConfig: {
      alias: 'c',
      default: '',
      description: 'Default configuration to put after newly created @create-index comments.',
      type: 'string'
    }
  })
  .options({
    ignoreUnsafe: {
      alias: 'i',
      default: false,
      description: 'Ignores unsafe "index.js" files instead of halting.',
      type: 'boolean'
    }
  })
  .options({
    ignoreDirectories: {
      alias: 'd',
      default: false,
      description: 'Ignores importing directories into the index file, even if they have a safe "index.js".',
      type: 'boolean'
    }
  })
  .options({
    update: {
      alias: 'u',
      default: false,
      description: 'Updates only previously created index files (recursively).',
      type: 'boolean'
    }
  })
  .options({
    banner: {
      description: 'Add a custom banner at the top of the index file',
      type: 'string'
    }
  })
  .options({
    stripExtension: {
      alias: 'S',
      default: false,
      description: 'Strip the extension when importing',
      type: 'boolean'
    }
  })
  .options({
    indexExtension: {
      alias: 'X',
      default: 'js',
      description: 'The extension to use for the index file',
      type: 'string'
    }
  })
  .options({
    extensions: {
      alias: 'x',
      default: ['js'],
      description: 'Allows some extensions to be parsed as valid source. First extension will always be preferred to homonyms with another allowed extension.',
      type: 'array'
    }
  })
  .example(
    'create-index ./src ./src/utilities',
    'Creates or updates an existing create-index index file in the target (./src, ./src/utilities) directories.'
  )
  .example(
    'create-index --update ./src ./tests',
    'Finds all create-index index files in the target directories and descending directories. Updates found index files.'
  )
  .example(
    'create-index ./src --extensions js jsx',
    'Creates or updates an existing create-index index file in the target (./src) directory for both .js and .jsx extensions.'
  )
  .argv;

writeIndexCli(argv._, {
  banner: argv.banner,
  defaultConfig: argv.defaultConfig,
  extensions: argv.extensions,
  ignoreDirectories: argv.ignoreDirectories,
  ignoreUnsafe: argv.ignoreUnsafe,
  indexExtension: argv.indexExtension,
  recursive: argv.recursive,
  stripExtension: argv.stripExtension,
  updateIndex: argv.update
});
