export function checkBuildifierIsAvailable()

in src/buildifier/buildifier_availability.ts [32:65]


export function checkBuildifierIsAvailable() {
  const buildifierExecutable = getDefaultBuildifierExecutablePath();
  which(buildifierExecutable, async (err, _) => {
    if (err) {
      await showBuildifierDownloadPrompt("Buildifier was not found");
      return;
    }

    // If we found it, make sure it's a compatible version by running
    // buildifier on an empty input and see if it exits successfully and the
    // output parses.
    const process = child_process.execFile(
      buildifierExecutable,
      ["--format=json", "--mode=check"],
      {},
      (error: Error, stdout: string, stderr: string) => {
        if (!error && stdout) {
          try {
            JSON.parse(stdout);
            return;
          } catch {
            // Swallow the error; we'll display the prompt below.
          }
        }
        // If we didn't get valid JSON back, we don't have a compatible version.
        // tslint:disable-next-line:no-floating-promises
        showBuildifierDownloadPrompt(
          "Buildifier is too old (0.25.1 or higher is needed)",
        );
      },
    );
    process.stdin.end();
  });
}