import { getBaseDestination } from '../storage/storage';

const youtubedl = require('youtube-dl');
const path = require('path');
const fs = require('fs');
const { remote } = require('electron');

let baseDestination;
let filePath;

export var init = function () {
  baseDestination = getBaseDestination();
};

export var downloadVideo = function (link, onInfo, onError, onEnd) {
  if (! fs.existsSync(baseDestination)) {
    fs.mkdirSync(baseDestination);
  }

  const video = youtubedl(
    link,
    ['--format=18'],
    { cwd: baseDestination }
  );

  video.on('info', function (info) {
    onInfo(info);

    const destination = getDestination(info);
    if (! fs.existsSync(destination)) {
      fs.mkdirSync(destination);
    }

    filePath = path.join(destination, info._filename);
    video.pipe(fs.createWriteStream(filePath));
  });

  video.on('error', onError);
  video.on('end', () => {
    console.log('finished downloading!');
    onEnd(filePath);
  });
};

function getDestination(info) {
  return path.join(baseDestination, info.uploader);
}

@@ -193,118 +193,40 @@
 );%0A%0A
-let baseDestination;%0Alet filePath;%0A%0Aexport var init = function () %7B%0A  baseDestination = getBaseDestination();%0A
+export var init = function () %7B 
 %7D;%0A%0A
@@ -294,16 +294,81 @@
 nEnd) %7B%0A
+  let filePath;%0A%0A  const baseDestination = getBaseDestination();%0A
   if (! 
@@ -625,24 +625,41 @@
 Destination(
+baseDestination, 
 info);%0A    i
@@ -972,16 +972,16 @@
 %7D);%0A%7D;%0A%0A
-
 function
@@ -996,16 +996,33 @@
 ination(
+baseDestination, 
 info) %7B%0A

Refactor downloader