0

I'm working in a project with electronjs + vue to implement adb operations in desktop application. For the implementation of this i am using the node library adbkit.

When I tried to load script i get an error during the require('adbkit') operation during webpack. The error that i got is the following:

enter image description here

My configuration for webpack is the following:

const { defineConfig } = require('@vue/cli-service')
module.exports = defineConfig({
  transpileDependencies: true,
  
  configureWebpack: {
    resolve: {
      fallback: {
        "crypto": require.resolve("crypto-browserify"),
      },
      
    },
    externals:{
      "fs": require("fs"),
      "child_process": require("child_process"),

    }
  },
})

The script that i am using to get device storage:

/* eslint-disable */
export function memory() {
  var Promise = require('bluebird');
  var adb = require('adbkit');
  const client = adb.createClient();
  //var readline = require('readline');
  var capaccity = null;
  var used = null;
  var free = null;
  var stat = null;


  return new Promise(function (resolve) {
    client.listDevices()
      .then(function (devices) {
        if(devices.length != 0) {
          console.log('Device %s ', devices[0].id)
          var check = false;
          client.shell(devices[0].id, "df -h", function (err, output) {
            if (err) {
              console.log(err);
            }
            var readStream = output;
            
            var importantdata = [];
            readStream
              .on('data', function (data) {
                //console.log('Data!', data.toString());  
                var lines = data.toString().split('n');
                //if(lines.includes('/dev/fuse')){
                //    console.log(lines);
                //}
                lines.forEach(element => {
                  if (element.includes('/dev/fuse')) {
                    //console.log(element)
                    var info = element.toString().split(' ');
                    info.forEach(info => {
                      if (info != '') {
                        importantdata.push(info)
                      }
                    })

                  }
                });

                if (importantdata.length != 0) {
                  capaccity = importantdata[1] + 'B'
                  used = importantdata[2] + 'B'
                  free = importantdata[3] + 'B'
                  stat = '"'+importantdata[4]+'"'
                  check = true
                  //console.log([capaccity, used, free, stat]);
                  resolve([capaccity, used, free, stat])
                }
              })
              .on('error', function (err) { console.error('Error', err); })


          })
          if (check == false) {
            client.shell(devices[0].id, "df", function (err, output, capaccity, used, free, stat, importantdata) {
              if (err) {
                console.log(err);
              }
              var readStream = output;
              importantdata = [];
              readStream
                .on('data', function (data) {
                  //console.log('Data!', data.toString());  
                  var lines = data.toString().split('n');
                  lines.forEach(element => {
                    if (element.includes('/storage/emulated')) {
                      //console.log(element)
                      var info = element.toString().split(' ');
                      info.forEach(info => {
                        if (info != '') {
                          importantdata.push(info)

                        }
                      })

                    }
                  });
                  if (importantdata.length != 0) {
                    capaccity = importantdata[1] + 'B'
                    used = importantdata[2] + 'B'
                    free = importantdata[3] + 'B'
                    stat = '"'+((parseFloat(importantdata[2].replace('G', '')) / parseFloat(importantdata[1].replace('G', ''))) * 100.0).toFixed(2) + '%"';
                    //return [capaccity, used, free, percentage]
                    //console.log([capaccity, used, free, stat]);
                    resolve([capaccity, used, free, stat])
                  }
                })
                .on('error', function (err) { console.error('Error', err); })



            })
          }
        }



      })
  })

}

And the view when i execute the following script:

<template >
  <div class="home">
    <img src="../assets/mobile_image.png" class="imagemobile" />
    <h1 class="textheader">
      <font size="5" id="brand">Teléfono Móvil</font>
    </h1>
    <h2 class="modelo">
      <font size="3" id="model"> Modelo </font>
    </h2>
    
    <h3 class="textcontent">
      <i> Información sobre el Teléfono </i>
    </h3>

    <div class="barcontent">
      
      <div style="text-align: left;">Almacenamiento</div>
      
      
      <div class="progress-bar">
        <div class="progress-bar-fill-ocuppied"></div> <!--- PREGUNTAR SI ASI O ABAJO -->
      </div>
      
      <div style="display:inline">
        
        <p id="alma-ocupado" class="info-alma-ocupado">Ocupados 114GB</p>
        
        <p id="alma-disp" class="info-alma-libre">Libres 24GB</p>
      </div>
      <div class="totalcapacity">Almacenamiento total: 128 GB</div>
      
      
    </div>
    <img src="../assets/android.jpg" class="imageandroid" />
  </div>
</template>

<script>
import {memory} from './prueba'

export default {
  name: "HomeView",
  mounted(){
    
    const pb1 = new ProgressBar(document.querySelector('.progress-bar'));
    pb1.setValue("0%");
    
    memory().then(function (resolve) {
      pb1.setValue(resolve[3])
    })
    

    

  },
};
class ProgressBar{
  constructor(element,initialValue="0%"){
    this.Ocuppied = element.querySelector('.progress-bar-fill-ocuppied');
    this.setValue(initialValue);
  }

  setValue (newValue){
    this.value = newValue;
    this.update();
  }

  update(){
    const percentageoccupied = this.value;

    this.Ocuppied.style.width = percentageoccupied;
    
  }

 
}
   
</script>

<style>
.info-alma-ocupado{
  text-align: left;
  
  display: inline;
  
  width: 50%;
  padding-left: 0px;
  margin-right: 33%;
}
.info-alma-libre{
  display: inline;
  width: 100%;
  height: 50%;
  text-align: right !important;
  align-items: right !important;
  margin-left: 33%;
}
.bloque{
  display: inline-block;
  align-content: initial;
}
.imagemobile {
  float: left;
  margin-left: 10%;
  margin-top: 5%;
  width: 15%;
  height: auto;
}
.imageandroid {
  margin-right: 10%;
  margin-top: 5%;
  float: right;
  width: 20%;
  height: auto;
}
.textheader {
  margin: 0;
  position: absolute;
  margin-top: 5%;
  left: 50%;
  transform: translate(-50%, -50%);
  font-size: 90%;
}

.modelo {
  margin: 0;
  position: absolute;
  margin-top: 10%;
  left: 50%;
  transform: translate(-50%, -50%);
  font-size: 90%;
}
.textcontent {
  margin: 0;
  position: absolute;
  margin-top: 20%;
  left: 50%;
  transform: translate(-50%, -50%);
  font-size: 60%;
}

.almacenamiento {
  text-align: left;
}

.barcontent {
  margin: 0;
  position: absolute;
  margin-top: 35%;
  left: 50%;
  transform: translate(-50%, -50%);
  font-size: 90%;
  border: 20px;
  
  /*border: 10px solid #000; */
}


.progress-bar {
  position: relative;
  float: center;
  width: 600px;
  height: 50px;
  border: 1px solid #000;
  background-color: #0a9905;
}

.progress-bar-fill-ocuppied {
  float: left;
  height: 100%;
  width: 40%;
  background: #9b0b0b;
  transition: width 0.5s;
  position: absolute;
}


</style>

The result of the script should be that i got the percentatge of storage and then show it in front

  • You can check the exact location in the file that throws an error to know what this is all about. But the obvious thing is that adbkit is Node package. And you use it in renderer process. – Estus Flask Apr 04 '22 at 11:39
  • Thank you for answering the question, this is my first time working with vue and electron where and how is supposed to use it? – Santiago Galiano Segura Apr 04 '22 at 14:05
  • In main process. Main and renderer processes are supposed to interact with Electron IPC. Possible duplicate of https://stackoverflow.com/questions/66455289/unable-to-use-node-js-apis-in-renderer-process – Estus Flask Apr 04 '22 at 14:08
  • But in my case , i work with vue framework and the configuration is the same as the link you share. – Santiago Galiano Segura Apr 04 '22 at 14:21
  • This is not obvious. There's no contextIsolation in the question – Estus Flask Apr 04 '22 at 14:51
  • Sorry, is my first time asking in stack overflow. The error i get it from the following sentence ` extend = function(child, parent) { for (var key in parent) { if (hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; },` I have to mentioned that i have to change my webpack to not receive error during compiltation and i don't know if it is configure properly. The webpack configuration is on the question section. Thanks – Santiago Galiano Segura Apr 04 '22 at 14:56
  • Consider checking answers in dupe question. It's not Webpack config (at least not directly) but the way you use Electron. – Estus Flask Apr 04 '22 at 15:10
  • I created a empty project of vue.js with out electron and i got the same error. Do you have any idea? – Santiago Galiano Segura Apr 04 '22 at 20:19
  • I'm unaware what relevant parts in empty project and your current project look like. See https://stackoverflow.com/help/mcve, you need this in SO questions. You need to follow things from linked question, it won't work itself by magic. Basically you need to either set contextIsolation explicitly, or expose things from main process (Node) to renderer process (Chrome) manually. The second way is preferable. As I said above, you need to check Electron IPC. I can't help further with more specific code as I don't use this setup currently – Estus Flask Apr 04 '22 at 20:38

0 Answers0