async function getFullPath()

in packages/libs/codegen/src/exec.ts [52:106]


async function getFullPath(
  command: string,
  recursive = false,
  searchPath?: Array<string>,
): Promise<string | undefined> {
  command = command.replace(/"/g, "");
  const ext = path.extname(command);

  if (path.isAbsolute(command)) {
    // if the file has an extension, or we're not on win32, and this is an actual file, use it.
    if (ext || process.platform !== "win32") {
      if (await isFile(command)) {
        return command;
      }
    }

    // if we're on windows, look for a file with an acceptable extension.
    if (process.platform === "win32") {
      // try all the PATHEXT extensions to see if it is a recognized program
      const cmd = await realPathWithExtension(command);
      if (cmd) {
        return cmd;
      }
    }
    return undefined;
  }

  if (searchPath) {
    for (const folder of searchPath) {
      let fullPath = await getFullPath(path.resolve(folder, command));
      if (fullPath) {
        return fullPath;
      }
      if (recursive) {
        try {
          for (const entry of await readdir(folder)) {
            const folderPath = path.resolve(folder, entry);

            if (await isDirectory(folderPath)) {
              fullPath =
                (await getFullPath(path.join(folderPath, command))) || (await getFullPath(command, true, [folderPath]));
              if (fullPath) {
                return fullPath;
              }
            }
          }
        } catch {
          // ignore failures
        }
      }
    }
  }

  return undefined;
}