function main()

in index.js [124:163]


function main() {
  // Find clang-format in node_modules of the project of the .js file, or cwd.
  const nonDashArgs = process.argv.filter((arg, idx) => idx > 1 && arg[0] != '-');

  // Using the last file makes it less likely to collide with clang-format's argument parsing.
  const lastFileArg = nonDashArgs[nonDashArgs.length - 1];
  const basedir = lastFileArg ? path.dirname(lastFileArg) :  // relative to the last .js file given.
      process.cwd();                                         // or relative to the cwd()
  let resolvedClangFormat;
  let clangFormatLocation;
  try {
    clangFormatLocation = resolve('clang-format', {basedir});
    resolvedClangFormat = require(clangFormatLocation);
  } catch (e) {
    // Ignore and use the clang-format that came with this package.
  }
  let actualSpawnFn;
  if (!resolvedClangFormat) {
    actualSpawnFn = spawnClangFormat;
  } else if (resolvedClangFormat.spawnClangFormat) {
    actualSpawnFn = resolvedClangFormat.spawnClangFormat;
  } else {
    throw new Error(`Incompatible clang-format loaded from ${clangFormatLocation}`);
  }
  // Run clang-format.
  try {
    // Pass all arguments to clang-format, including e.g. -version etc.
    actualSpawnFn(process.argv.slice(2), function(e) {
      if (e instanceof Error) {
        console.error(e);
        process.exit(1);
      } else {
        process.exit(e);
      }
    }, 'inherit');
  } catch (e) {
    process.stdout.write(e.message);
    process.exit(1);
  }
}