#!/usr/bin/env node

/**
 * @description File size checker.
 * @author sota1235
 */

import { isUndefined, isEmpty } from 'lodash';
import clc from 'cli-color';
import glob from 'glob';
import program from 'commander';
import checkFiles from '../cli';

// get arguments
program
  .option('--files <target>', 'target files for lint')
  .option('--limit <Byte>', 'limit size', parseInt)
  .parse(process.argv);

const targetFiles = program.files;
const limitSize   = program.limit;

if (isUndefined(targetFiles)) {
  console.log(`${clc.red('Error')}: Please add -f option`);
  console.log('Try again please!');
  process.exit(1);
}

if (isUndefined(limitSize)) {
  console.log(`${clc.red('Error')}: Please add -l option`);
  console.log('Try again please!');
  process.exit(1);
}

glob(targetFiles, (err, files) => {
  if (err) {
    console.log(`Error: ${err.message}\n`);
    console.log('Try again please!\n');
    process.exit(1);
  }

  if (isEmpty(files)) {
    console.log('Target file is nothing :(');
    process.exit(0);
  }

  const promises = [];

  for (const file of files) {
    promises.push(checkFileSize(file, limitSize));
  }

  Promise.all(promises)
    .then(() => console.log('All check finished!'))
    .catch(e => {
      console.error(`something error, ${e}`);
      process.exit(1);
    });
});

@@ -789,16 +789,36 @@
 etFiles,
+ %7B realpath: true %7D,
  (err, f
@@ -1053,78 +1053,8 @@
 %0A%0A  
-const promises = %5B%5D;%0A%0A  for (const file of files) %7B%0A    promises.push(
 chec
@@ -1062,17 +1062,15 @@
 File
-Size
+s
 (file
+s
 , li
@@ -1081,90 +1081,109 @@
 ize)
-);
 %0A  
-%7D%0A%0A  Promise.all(promises)%0A    .then(() =%3E console.log('All check finished!')
+  .then(result =%3E %7B%0A      const exitStatus = result ? 0 : 1;%0A      process.exit(exitStatus);%0A    %7D
 )%0A  

fix for rebase