/ .setup / macos / validate-executable.mjs
validate-executable.mjs
 1  import fs from 'fs';
 2  
 3  export function validateExecutable(path) {
 4    existsOnSystem(path)
 5    isNotADirectory(path)
 6    isExecutable(path)
 7  }
 8  
 9  function existsOnSystem(path) {
10    if (!fs.existsSync(path)) {
11      throw new Error(`Path ${path} does not exist`);
12    }
13  }
14  
15  function isNotADirectory(path) {
16    if (fs.statSync(path).isDirectory()) {
17      throw new Error(`${path} is a directory. Please provide the path to an executable.`);
18    }
19  }
20  
21  function isExecutable(path) {
22    try {
23      fs.accessSync(path, fs.constants.X_OK);
24    } catch (err) {
25      throw new Error(`${path} is not executable`);
26    }
27  }