async function runGlean()

in glean/src/cli.ts [220:270]


async function runGlean(parserArgs: string[]) {
  const spinner = getStartedSpinner();

  const pythonBinary = process.env.GLEAN_PYTHON ?? getSystemPythonBinName();
  const pythonBin = path.join(getPythonVenvBinariesPath(VIRTUAL_ENVIRONMENT_DIR), pythonBinary);

  const isOnlineArg = process.env.OFFLINE ? "offline" : "online";

  // Trying to pass PYTHON_SCRIPT as a string in the command line arguments
  // causes issues on Windows machines. The issue exists because you cannot
  // pass a multi-line script using the `-c` argument without manually formatting
  // your script with `\n` characters.
  //
  // To workaround this, we can write the script to a file inside of the OS tmpdir
  // and execute it from there. Then once we are finished with the script, we can
  // remove the entire directory.
  let tmpDir = "";
  const appPrefix = "glean.js";
  const scriptName = "script.py";
  const tempDirectory = os.tmpdir();
  try {
    tmpDir = fs.mkdtempSync(path.join(tempDirectory, appPrefix));
    fs.writeFileSync(path.join(tmpDir, scriptName), PYTHON_SCRIPT);
  } catch (error) {
    log(
      LOG_TAG,
      ["Unable to write utility script to tmp directory.\n", error],
      LoggingLevel.Error
    );
    process.exit(1);
  }

  const cmd = `${pythonBin} ${tmpDir}/${scriptName} ${isOnlineArg} glean_parser ${GLEAN_PARSER_VERSION} ${parserArgs.join(" ")}`;

  const {err, stdout, stderr} = await new Promise<{err: exec.ExecException | null, stdout: string, stderr: string}>(resolve => {
    exec.exec(cmd, (err, stdout, stderr) => {
      resolve({err, stdout, stderr});

      // Remove the temp directory now that we are finished with it.
      fs.rmSync(tmpDir, { recursive: true });
    });
  });

  stopSpinner(spinner);
  log(LOG_TAG, `${stdout}`);

  if (err) {
    log(LOG_TAG, `${stderr}`);
    process.exit(1);
  }
}