/*
* @Author: Manraj Singh
* @Date:   2016-08-13 20:28:25
* @Last Modified by:   Manraj Singh
* @Last Modified time: 2016-08-16 21:02:42
*/

'use strict';

const mv = require('mv');
const fs = require('fs');
const path = require('path');

class Helper {

  constructor() { }

  mkdir(path) {
    try {
      fs.mkdirSync(path);
    } catch (err) {
      if (err.code != 'EEXIST') {
        throw new Error("Error occurred while creating a new directory");
      }
    }
  }

  getExtension(fileName) {
    let i = fileName.lastIndexOf('.');
    return (i < 0) ? '' : fileName.substr(i + 1);
  }

  getFileNames(directory) {
    return fs.readdirSync(directory);
  }

  organize(directory, fileName, type) {
    let dir = path.resolve(directory, type);
    this.mkdir(dir);
    mv(path.resolve(directory, fileName), path.resolve(dir, fileName), function (err) {
      if (err) {
        throw new Error("Couldn't move " + fileName + " because of following error: " + err);
      }
    });
  }
}

module.exports = Helper;

/*
* @Author: Manraj Singh
* @Date:   2016-08-13 20:28:25
* @Last Modified by:   Manraj Singh
* @Last Modified time: 2017-01-03 22:37:23
*/

'use strict';

const mv = require('mv');
const fs = require('fs');
const path = require('path');

class Helper {

  constructor() { }

  mkdir(path) {
    try {
      fs.mkdirSync(path);
    } catch (err) {
      if (err.code !== 'EEXIST') {
        throw new Error("Error occurred while creating a new directory");
      }
    }
  }

  getExtension(fileName) {
    let i = fileName.lastIndexOf('.');
    return (i < 0) ? '' : fileName.substr(i + 1);
  }

  getFileNames(directory) {
    return fs.readdirSync(directory);
  }

  organize(directory, fileName, type) {
    let dir = path.resolve(directory, type);
    this.mkdir(dir);

    mv(path.resolve(directory, fileName), path.resolve(dir, fileName), (err) => {
      if (err) {
        throw new Error("Couldn't move " + fileName + " because of following error: " + err);
      }
    });
  }
}

module.exports = Helper;

Use of Arrow functions