How do I run an executable from within electron app?

37 Views Asked by At

I've created an electron app which makes some api calls, and does some document and folder creation using the results. I'm trying to create an outlook folder from within the app. I saw that the microsoft api would require an azure AD account which I'm not willing to spring for. I've done this type of folder management in the past using powershell.

Executable function

function runExecutable(args) {
    return new Promise((resolve, reject) => {
      
      const executablePath = './createOutlookFolder.exe';
      console.log('Args:', args);
      // Ensure args are properly quoted
      const command = `"${executablePath}" "${args.join('" "')}"`;

      console.log(`Executing command: ${command}`);

      sudo.exec(command, {name: 'BKLProposalGenerator'}, (error, stdout, stderr) => {
        if (error) reject(new Error(`Execution Failed: ${error.message}`));
        else if (stderr) reject(new Error(`Executable Error Output: ${stderr}`));
        else resolve(stdout.trim());
      });

      
      
    });
}

Package.Json

{
  "name": "proposal-generator-app",
  "version": "1.0.0",
  "description": "This app generates proposals using the Pipedrive API.",
  "main": "main.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "start": "electron .",
    "build": "electron-builder"
  },
  "assets": [
    "Letterhead-for-scripting.docx",
    "createOutlookFolder.exe"
  ],
  "build": {
    "appId": "com.bklproposalgen",
    "productName": "ProposalGenerator",
    "win": {
      "target": "nsis"
    },
    "files": [
      "**/*",
      "assets/wallpaper.jpg",
      "createOutlookFolder.exe",
      "Letterhead.dotx",
      "./style.css",
      "Letterhead-for-scripting.docx"
    ]
  },
  "keywords": [],
  "author": "---",
  "license": "ISC",
  "bin": {
    "my-app": "main.js"
  },
  "dependencies": {
    "axios": "^1.5.1",
    "docxtemplater": "^3.40.2",
    "express": "^4.18.2",
    "pipedrive": "^22.3.0",
    "pizzip": "^3.1.4",
    "sudo-prompt": "^9.2.1"
  },
  "devDependencies": {
    "electron": "^27.0.2",
    "electron-builder": "^24.9.1"
  }
}

I was able to create a powershell script which takes in a folder name as an argument and creates the desired folder. I then packaged it as an executable and called it from within the electron app. This works while in development mode but as soon as I build the app the executable stops getting called. I've added the file to my package.json and according to chatGPT it should work now. What am I missing?

0

There are 0 best solutions below