All files / src cli.ts

0% Statements 0/37
0% Branches 0/12
0% Functions 0/4
0% Lines 0/37

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81                                                                                                                                                                 
#! /usr/bin/env node
 
import chalk from 'chalk';
import fs from 'fs';
import path from 'path';
import beginCrawl from './';
import getConfig from './config';
import { CWD, MESSAGE_PREFIX } from './constants';
import * as rules from './rules';
 
function init() {
  const [, , filePath] = process.argv;
 
  Iif (!filePath) {
    throw new Error('No directory specified');
  }
 
  const fullPath = path.resolve(CWD, filePath);
 
  Iif (!fs.existsSync(fullPath)) {
    throw new Error(`Provided path ${fullPath} does not exist`);
  }
 
  Iif (!fs.lstatSync(fullPath).isDirectory()) {
    throw new Error(`Provided path ${fullPath} is not a directory`);
  }
 
  const { configPath, config } = getConfig(fullPath);
 
  // eslint-disable-next-line no-console
  console.error(
    `${MESSAGE_PREFIX}${chalk.green(`Found config file at ${configPath}`)}`
  );
 
  const ruleInstances = Object.keys(config.rules).map(
    (ruleName: keyof typeof rules) => {
      const ruleConfig = config.rules[ruleName];
      return new rules[ruleName](ruleConfig);
    }
  );
 
  let warningCount = 0;
  let errorCount = 0;
 
  function reportWarning() {
    warningCount += 1;
  }
 
  function reportError() {
    errorCount += 1;
  }
 
  beginCrawl(fullPath, ruleInstances, reportWarning, reportError);
 
  if (!warningCount && !errorCount) {
    // eslint-disable-next-line no-console
    console.error(
      `${MESSAGE_PREFIX}${chalk.green('Completed with no warnings or errors!')}`
    );
  } else {
    const warnings = chalk.yellow(`${warningCount} warnings`);
    const errors = chalk.red(`${errorCount} errors`);
    // eslint-disable-next-line no-console
    console.error(`${MESSAGE_PREFIX}Completed with ${warnings} and ${errors}`);
  }
 
  Iif (errorCount) {
    process.exit(1);
  }
}
 
try {
  init();
} catch (error) {
  const message = error && error.message ? error.message : error;
 
  // eslint-disable-next-line no-console
  console.error(`${MESSAGE_PREFIX}${chalk.red(message)}`);
  process.exit(1);
}