1

I've figured that since Vue CLI has a ton of node_modules I can make a Vue component capable of executing a shell command (ls -l, for example).

I have added Electron via vue add electron-builder.
I have VueRouter in mode: process.env.IS_ELECTRON ? 'hash' : 'history',.
I am running the project with npm run electron:serve.

This is the ShellExec.vue component:

<template>
  <div id="wrapper">
    <div class="input_row">
      <input type="text" class="input_text" id="shell" v-model="shell_command" />
      <label class="label_" for="shell">Type Shell Command</label>
    </div>
    <button type="button" class="button" @click="shellExec">Submit !</button>
  </div>
</template>

<script>
import { exec } from "child_process";

export default {
  name: "ShellExec",
  components: {},
  data() {
    return {
      shell_command: ""
    };
  },
  methods: {
    async shell() {
      return new Promise((resolve, reject) => {
        exec(this.shell_command, (err, stdout, stderr) => {
          if (err) {
            reject(err);
          } else {
            resolve({ stdout, stderr });
          }
        });
      });
    },

    async shellExec() {
      let { stdout } = await this.shell();
      for (let line of stdout.split("\n")) {
        console.log(`ls: ${line}`);
      }
    }
  }
};
</script>

<style scoped>
</style>

The JS scripts come from here: https://stackoverflow.com/a/31897900/13268871

shell_command takes what ever value I type in the input, so that appears to work properly.
I am trying a simple ls -l command, but when I press on the Submit ! button, I receive the following errors:

vue.runtime.esm.js?2b0e:619 [Vue warn]: Error in v-on handler (Promise/async): "TypeError: Object(...) is not a function"

found in

---> <ShellExec> at src/components/ShellExec.vue
       <App> at src/App.vue
         <Root>
vue.runtime.esm.js?2b0e:1888 TypeError: Object(...) is not a function
    at eval (ShellExec.vue?face:25)
    at new Promise (<anonymous>)
    at _callee$ (ShellExec.vue?face:23)
    at tryCatch (runtime.js?96cf:45)
    at Generator.invoke [as _invoke] (runtime.js?96cf:274)
    at Generator.prototype.<computed> [as next] (runtime.js?96cf:97)
    at asyncGeneratorStep (asyncToGenerator.js?1da1:3)
    at _next (asyncToGenerator.js?1da1:25)
    at eval (asyncToGenerator.js?1da1:32)
    at new Promise (<anonymous>)

enter image description here

Jorje12
  • 415
  • 1
  • 3
  • 14
  • 2
    You can't run shell commands in browser... You can however make an ajax call to your server and then execute that shell command on server – ljubadr Jul 11 '20 at 13:46
  • Meaning that I can make a simple web server using `Express JS` and then use ajax calls to execute shell commands ? – Jorje12 Jul 11 '20 at 13:50
  • Simple express server should be more than enough. If you plan to use this locally it will work. If you want to share this with other people, they will need to have your express server code to run cli commands on their computer. If this is the case, you could go with the electron approach - package your vue app into application that can run on any computer – ljubadr Jul 11 '20 at 14:12
  • I would like to deploy this as a Web Interface on a Raspberry Pi. What to do in this case ? – Jorje12 Jul 11 '20 at 15:27
  • For Raspberry Pi web interface go with express and vue – ljubadr Jul 11 '20 at 19:30
  • @ljubadr That what I thought. But how can I set that up ? – Jorje12 Jul 12 '20 at 15:11
  • Create `client` and `server` folders to separate the frontend and backend logic - vue logic would go into `client` and express logic would go into `server` folder. [This answer](https://stackoverflow.com/a/55882798/3226121) could also help you out. Start working on it and then post any new questions that you have (don't update this thread) – ljubadr Jul 14 '20 at 19:24
  • @ljubadr I've managed to do it a couple of days ago. Thank you ! Also, that answer has some good info in it. – Jorje12 Jul 17 '20 at 10:55

1 Answers1

0

You are testing in the browser with npm run serve which of course doesn't work, as the browser doesn't allow it. Running via npm run electron:serve works. Without any changes to the webpack config. Just importing via import { exec } from 'child_process'; is enough.

reference : https://github.com/nklayman/vue-cli-plugin-electron-builder/issues/244

San
  • 453
  • 3
  • 14
  • 1
    I have installed Electron by using `vue add electron-builder` and then ran using `npm run electron:serve` and I have the same exact errors. – Jorje12 Jul 11 '20 at 15:26