import child_process from 'child_process';

export default class ADB {
 
  constructor() {
    this.device = [];
    this.selected_device;
  }

  devices() {
      
    let child = child_process.spawnSync("adb", ["devices"]);
    if(child.stderr && child.stderr.toString() !="") { 
      throw new Error(" "+child.stderr);
    }

    let output = child.stdout.toString();
    let match_devices = output.match(/[\w\d]+\s+device\n/ig);
      
    if(!match_devices.length) return this.device;
      
    match_devices = match_devices[0].split(/\n/);
    for(let i in match_devices) {
      if(match_devices[i] == "") continue;
      if(!/device/ig.test(match_devices[i])) continue;
      this.device.push(match_devices[i].replace(/\tdevice/ig, ""));
    }
    
    this.selected_device = this.device[0];
    return this.device;
  }

  selectDevice(device) {
    this.selected_device = device;
  }
  
  isPackageInstalled(_package_) {
    
    let child = child_process.spawnSync("adb", ["-s", this.selected_device, "shell" ,"pm", "list", "packages", "|", "grep", _package_]);
    if(child.stderr && child.stderr.toString() !="") { 
      throw new Error(" "+child.stderr);
    }

    let output = child.stdout.toString();
        output = output.replace(/[\n\t\s]*/ig, "");
    
    return output == "package:"+_package_ ? true : false;
  }

};

@@ -942,134 +942,753 @@
 let 
-child = child_process.spawnSync(%22adb%22, %5B%22-s%22, this.selected_device, %22shell%22 ,%22pm%22, %22list%22, %22packages%22, %22%7C%22, %22grep
+params = %5B%22shell%22 ,%22pm%22, %22list%22, %22packages%22, %22%7C%22, %22grep%22, _package_%5D;%0A    if(this.selected_device) %7B%0A      params.unshift(this.selected_device);%0A      params.unshift(%22-s%22);%0A    %7D%0A%0A    let child = child_process.spawnSync(%22adb%22, params);%0A    if(child.stderr && child.stderr.toString() !=%22%22) %7B %0A      throw new Error(%22 %22+child.stderr);%0A    %7D%0A%0A    let output = child.stdout.toString();%0A        output = output.replace(/%5B%5Cn%5Ct%5Cs%5Cr%5D*/ig, %22%22);%0A    %0A    return output == %22package:%22+_package_ ? true : false;%0A  %7D%0A%0A  listPackages() %7B%0A    %0A    let params = %5B%22shell%22 ,%22pm%22, %22list%22, %22packages%22%5D;%0A    if(this.selected_device) %7B%0A      params.unshift(this.selected_device);%0A      params.unshift(%22-s%22);%0A    %7D%0A%0A    let child = child_process.spawnSync(%22adb
 %22, 
-_
 pa
-ckage_%5D
+rams
 );%0A 
@@ -1781,32 +1781,36 @@
 .stderr);%0A    %7D%0A
+    
 %0A    let output 
@@ -1844,117 +1844,332 @@
     
-    output = output.replace(/%5B%5Cn%5Ct%5Cs%5D*/ig, %22%22);%0A    %0A    return output == %22package:%22+_package_ ? true : false
+let list = output.split(/%5Cn/);%0A    %0A    let packages = %5B%5D;%0A    if(!list.length) return packages;%0A    for(let i in list) %7B%0A      %0A      if(list%5Bi%5D == %22%22) continue;%0A%0A      let pkg = list%5Bi%5D;%0A          pkg = pkg.replace(%22package:%22,%22%22);%0A          pkg = pkg.replace(/%5Cr/,%22%22);%0A%0A      packages.push(pkg);%0A    %7D%0A%0A    return packages
 ;%0A  

add method to list all packages