summaryrefslogtreecommitdiff
path: root/tools/format.js
blob: a829c86dd0f2fade316840468c96b7dbd2f262e5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#!/usr/bin/env -S deno run --unstable --allow-write --allow-read --allow-run
// Copyright 2018-2022 the Deno authors. All rights reserved. MIT license.
import { getPrebuiltToolPath, join, ROOT_PATH } from "./util.js";

async function dprint() {
  const configFile = join(ROOT_PATH, ".dprint.json");
  const execPath = getPrebuiltToolPath("dprint");
  const p = Deno.run({
    cmd: [execPath, "fmt", "--config=" + configFile],
  });
  const { success } = await p.status();
  if (!success) {
    throw new Error("dprint failed");
  }
  p.close();
}

async function main() {
  await Deno.chdir(ROOT_PATH);
  await dprint();

  if (Deno.args.includes("--check")) {
    const git = Deno.run({
      cmd: ["git", "status", "-uno", "--porcelain", "--ignore-submodules"],
      stdout: "piped",
    });

    const { success } = await git.status();
    if (!success) {
      throw new Error("git status failed");
    }
    const out = new TextDecoder().decode(await git.output());
    git.close();

    if (out) {
      console.log("run tools/format.js");
      console.log(out);
      Deno.exit(1);
    }
  }
}

await main();