aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/packages/turbo-ignore/src/getWorkspace.ts
blob: e0b3167832ddab27aadca95938667c4944a770dd (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
31
32
33
34
35
36
37
import fs from "fs";
import path from "path";
import { error, info } from "./logger";
import { TurboIgnoreArgs } from "./types";

export function getWorkspace(args: TurboIgnoreArgs): string | null {
  const { directory = process.cwd(), workspace } = args;

  // if the workspace is provided via args, use that
  if (workspace) {
    info(`Using "${workspace}" as workspace from arguments`);
    return workspace;
  }

  // otherwise, try and infer it from a package.json in the current directory
  const packageJsonPath = path.join(directory, "package.json");
  try {
    const raw = fs.readFileSync(packageJsonPath, "utf8");
    const packageJsonContent: Record<string, string> & { name: string } =
      JSON.parse(raw);

    if (!packageJsonContent.name) {
      error(`"${packageJsonPath}" is missing the "name" field (required).`);
      return null;
    }

    info(
      `Inferred "${packageJsonContent.name}" as workspace from "package.json"`
    );
    return packageJsonContent.name;
  } catch (e) {
    error(
      `"${packageJsonPath}" could not be found. turbo-ignore inferencing failed`
    );
    return null;
  }
}