{"size": 2405, "ext": "js", "max_stars_repo_path": "src/index.js", "max_stars_repo_name": "chinjs/chin", "max_stars_repo_stars_event_min_datetime": "2020-03-26T09:47:46.000Z", "max_stars_repo_stars_event_max_datetime": "2020-03-26T09:47:46.000Z", "max_issues_repo_path": "src/index.js", "max_issues_repo_name": "chinjs/chin", "max_issues_repo_issues_event_min_datetime": "2018-06-26T03:57:54.000Z", "max_issues_repo_issues_event_max_datetime": "2018-12-16T08:06:47.000Z", "max_forks_repo_path": "src/index.js", "max_forks_repo_name": "chinjs/chin", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": 1, "max_issues_count": 7, "max_forks_count": null, "avg_line_length": 30.0625, "max_line_length": 104, "alphanum_fraction": 0.6212058212}
// @flow
import assert from 'assert'
import figures from 'figures'
import chalk from 'chalk'
import { normalize, join } from 'path'
import prepare from './prepare'
import zap from './zap'
import watchprocess from './watch'
import type { Chin, ChinWatch } from '../index.js.flow'

const success = (msg) => chalk.cyan(msg)
const PRE_SUCC = chalk.green(figures.tick)
const PRE_FAIL = chalk.red(figures.cross)

const zapAll = (map, verbose) =>
  !verbose
  ? zapAllQuiet(map)
  : zapAllVerbose(map)

const zapAllQuiet = (map) =>
  Promise.all(
    []
    .concat(...[...map.values()])
    .map(zap)
  )

const zapAllVerbose = (map) =>
  recursiveZapDir(
    map.size === 1,
    [].concat([...map.entries()])
  ).then(count =>
    console.log(success(`${figures.pointer} ${count} files`))
  )

const recursiveZapDir = async (isOneDir, entries, count = 0) => {

  const [ dirpath, eggs ] = entries.splice(0, 1)[0]

  if (eggs.length) {
    console.log((isOneDir ? `` : `${dirpath}: `) + success(`${eggs.length} files`))

    let countByDir = 0
    await Promise.all(eggs.map(egg =>
      zap(egg)
      .then(msg => console.log(`${PRE_SUCC} ${chalk.gray(`${egg.filepath}${msg ? `: ${msg}` : ''}`)}`))
      .then(() => countByDir++)
      .catch(err => console.log(`${PRE_FAIL} ${chalk.gray(`${egg.filepath}: ${err.message}`)}`))
    ))
    count += countByDir
  }

  return entries.length ? recursiveZapDir(isOneDir, entries, count) : count
}

const init = (config = {}) => {
  assert(config.put && typeof config.put === 'string', '')
  assert(config.out && typeof config.out === 'string', '')
  config.put = normalize(config.put)
  config.out = normalize(config.out)
  process.env.CHIN_PUT = config.put
  process.env.CHIN_OUT = config.out
  return config
}

export const chin: Chin = async (config) => {
  const { put, out, ignored, processors, verbose } = init(config)
  const { map } = await prepare(put, out, processors, ignored)
  await zapAll(map, verbose)
  return
}

export const watch: ChinWatch = async (config) => {
  const { put, out, ignored, processors, verbose, watch: watchOpts } = init(config)
  const { map, f2t } = await prepare(put, out, processors, ignored)
  await zapAll(map, verbose)
  const watcher = watchprocess({ map, f2t, put, out, watchOpts, ignored, verbose })
  return watcher
}

export default chin