aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/packages/create-turbo/src/transforms/git-ignore.ts
blob: bb61ca756f82a35f6c69855458c17fe8f1edac0b (plain) (blame)
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
import path from "path";
import fs from "fs-extra";
import { DEFAULT_IGNORE } from "../utils/git";
import { TransformInput, TransformResult } from "./types";
import { TransformError } from "./errors";

const meta = {
  name: "git-ignore",
};

export async function transform(args: TransformInput): TransformResult {
  const { prompts } = args;
  const ignorePath = path.join(prompts.root, ".gitignore");
  try {
    if (!fs.existsSync(ignorePath)) {
      fs.writeFileSync(ignorePath, DEFAULT_IGNORE);
    } else {
      return { result: "not-applicable", ...meta };
    }
  } catch (err) {
    // existsSync cannot throw, so we don't need to narrow here and can
    // assume this came from writeFileSync
    throw new TransformError("Unable to write .gitignore", {
      transform: meta.name,
      fatal: false,
    });
  }

  return { result: "success", ...meta };
}