Skip to content

Commit

Permalink
Provide a meaningful error when CWD does not exist
Browse files Browse the repository at this point in the history
Prior to this commit, if a command was executed in a non-existing
current working directory, the "exec" method of the built-in
`child_process` module would throw an error that only partially
described the problem, e.g.

    Error: spawn /bin/sh ENOENT
        at ChildProcess._handle.onexit (node:internal/child_process:284:19)
        at onErrorNT (node:internal/child_process:477:16)
        at process.processTicksAndRejections (node:internal/process/task_queues:82:21) {
      errno: -2,
      code: 'ENOENT',
      syscall: 'spawn /bin/sh',
      path: '/bin/sh',
      spawnargs: [
        '-c',
        '/System/Library/Frameworks/CoreServices.framework/Versions/Current/Frameworks/LaunchServices.framework/Versions/Current/Support/lsregister -f -R -trusted -u MacOSATDriverServer.app'
      ],
      cmd: '/System/Library/Frameworks/CoreServices.framework/Versions/Current/Frameworks/LaunchServices.framework/Versions/Current/Support/lsregister -f -R -trusted -u MacOSATDriverServer.app',
      stdout: '',
      stderr: ''
    }

Detect this specific error condition and provide a more detailed
desription, e.g.

    Error: Cannot access directory: /Users/example/packages/macos-at-driver-server/MacOSATDriverServer/Build/Debug
        at exec (/Users/example/packages/macos-at-driver-server/shared/install/macos.js:42:13)
        at async unregisterExtensions (/Users/example/packages/macos-at-driver-server/shared/install/macos.js:195:3)
        at async exports.uninstall (/Users/example/packages/macos-at-driver-server/shared/install/macos.js:123:3)
        at async Object.handler (/Users/example/packages/macos-at-driver-server/shared/commands/uninstall.js:13:5) {
      [cause]: Error: spawn /bin/sh ENOENT
          at ChildProcess._handle.onexit (node:internal/child_process:284:19)
          at onErrorNT (node:internal/child_process:477:16)
          at process.processTicksAndRejections (node:internal/process/task_queues:82:21) {
        errno: -2,
        code: 'ENOENT',
        syscall: 'spawn /bin/sh',
        path: '/bin/sh',
        spawnargs: [
          '-c',
          '/System/Library/Frameworks/CoreServices.framework/Versions/Current/Frameworks/LaunchServices.framework/Versions/Current/Support/lsregister -f -R -trusted -u MacOSATDriverServer.app'
        ],
        cmd: '/System/Library/Frameworks/CoreServices.framework/Versions/Current/Frameworks/LaunchServices.framework/Versions/Current/Support/lsregister -f -R -trusted -u MacOSATDriverServer.app',
        stdout: '',
        stderr: ''
      }
    }
  • Loading branch information
jugglinmike committed Dec 11, 2024
1 parent 574347a commit eb4b492
Showing 1 changed file with 20 additions and 1 deletion.
21 changes: 20 additions & 1 deletion shared/install/macos.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
'use strict';

const { exec: _exec } = require('child_process');
const fs = require('fs/promises');
const { resolve } = require('path');
const { promisify } = require('util');

Expand All @@ -25,7 +26,25 @@ const SYSTEM_VOICE_IDENTIFIER = 'com.apple.Fred';
*/
const PLUGIN_TRIPLET_IDENTIFIER = 'ausp atdg BOCU';

const exec = promisify(_exec);
/**
* Execute the "exec" method from the built-in `child_process` module. In the
* event of failure due to the absence of the specified current working
* directory, provide a meaningful error description.
*/
const exec = async (...args) => {
try {
return await promisify(_exec)(...args);
} catch (error) {
const cwd = args[1]?.cwd || process.cwd();
const cwdExists = cwd && !!(await fs.stat(cwd).catch(() => null));

if (error.code === 'ENOENT' && !cwdExists) {
throw new Error(`Cannot access directory: ${cwd}`, {cause: error});
}

throw error;
}
};
const enableKeyAutomationPrompt = `This tool can only be installed on systems which allow automated key pressing.
Please allow the Terminal application to control your computer (the setting is
controlled in System Settings > Privacy & Security > Accessibility).`;
Expand Down

0 comments on commit eb4b492

Please sign in to comment.