aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/packages/turbo-workspaces/src/commands/summary/index.ts
blob: 9696a14441e8404932e61dbcb25b8974d9ac56a1 (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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
import inquirer from "inquirer";
import path from "path";
import { Logger } from "../../logger";
import chalk from "chalk";
import { SummaryCommandArgument } from "./types";
import { directoryInfo } from "../../utils";
import getWorkspaceDetails from "../../getWorkspaceDetails";
import { Workspace } from "../../types";

export default async function summary(directory: SummaryCommandArgument) {
  const logger = new Logger();
  logger.hero();

  const answer = await inquirer.prompt<{
    directoryInput?: string;
  }>({
    type: "input",
    name: "directoryInput",
    message: "Where is the root of the repo?",
    when: !directory,
    default: ".",
    validate: (directory: string) => {
      const { exists, absolute } = directoryInfo({ directory });
      if (exists) {
        return true;
      } else {
        return `Directory ${chalk.dim(`(${absolute})`)} does not exist`;
      }
    },
    filter: (directory: string) => directory.trim(),
  });

  const { directoryInput: selectedDirectory = directory as string } = answer;
  const { exists, absolute: root } = directoryInfo({
    directory: selectedDirectory,
  });
  if (!exists) {
    console.error(`Directory ${chalk.dim(`(${root})`)} does not exist`);
    return process.exit(1);
  }

  const project = await getWorkspaceDetails({ root });

  const numWorkspaces = project.workspaceData.workspaces.length;
  const hasWorkspaces = numWorkspaces > 0;
  // group workspaces
  const workspacesByDirectory: Record<string, Array<Workspace>> = {};
  project.workspaceData.workspaces.forEach((workspace) => {
    const workspacePath = path.relative(root, workspace.paths.root);
    const rootDirectory = workspacePath.split(path.sep)[0];
    if (!workspacesByDirectory[rootDirectory]) {
      workspacesByDirectory[rootDirectory] = [];
    }
    workspacesByDirectory[rootDirectory].push(workspace);
  });

  const renderWorkspace = (w: Workspace) => {
    return `${w.name} (${chalk.italic(
      `./${path.relative(root, w.paths.root)}`
    )})`;
  };

  const renderDirectory = ({
    number,
    directory,
    workspaces,
  }: {
    number: number;
    directory: string;
    workspaces: Array<Workspace>;
  }) => {
    logger.indented(2, `${number}. ${chalk.bold(directory)}`);
    workspaces.forEach((workspace, idx) => {
      logger.indented(3, `${idx + 1}. ${renderWorkspace(workspace)}`);
    });
  };

  // repo header
  logger.header(`Repository Summary`);
  logger.indented(1, `${chalk.underline(project.name)}:`);
  // workspace manager header
  logger.indented(
    1,
    `Package Manager: ${chalk.bold(chalk.italic(project.packageManager))}`
  );
  if (hasWorkspaces) {
    // workspaces header
    logger.indented(1, `Workspaces (${chalk.bold(numWorkspaces.toString())}):`);
    Object.keys(workspacesByDirectory).forEach((directory, idx) => {
      renderDirectory({
        number: idx + 1,
        directory,
        workspaces: workspacesByDirectory[directory],
      });
    });
    logger.blankLine();
  }
}