export function getBuildifierFileType()

in src/buildifier/buildifier.ts [116:152]


export function getBuildifierFileType(fsPath: string): BuildifierFileType {
  // TODO(bazelbuild/buildtools#475, bazelbuild/buildtools#681): Switch to
  // `--path=<path>` rather than duplicate the logic from buildifier. The
  // catch is `--path` was already documented, but didn't work with stdin
  // until bazelbuild/buildtools#681, so we'd need to dual code path testing
  // --version to decide how to do things; so it likely is better to just
  // ignore things until the support has been out a while.

  // NOTE: The implementation here should be kept in sync with buildifier's
  // automatic format detection (see:
  // https://github.com/bazelbuild/buildtools/blob/d39e4d/build/lex.go#L88)
  // so that user actions in the IDE are consistent with the behavior they
  // would see running buildifier on the command line.
  const raw = fsPath.toLowerCase();
  let parsedPath = path.parse(raw);
  if (parsedPath.ext === ".oss") {
    parsedPath = path.parse(parsedPath.name);
  }
  if (parsedPath.ext === ".bzl" || parsedPath.ext === ".sky") {
    return "bzl";
  }
  if (
    parsedPath.ext === ".build" ||
    parsedPath.name === "build" ||
    parsedPath.name.startsWith("build.")
  ) {
    return "build";
  }
  if (
    parsedPath.ext === ".workspace" ||
    parsedPath.name === "workspace" ||
    parsedPath.name.startsWith("workspace.")
  ) {
    return "workspace";
  }
  return "bzl";
}