All files / infrastructure/scripts/src/dev main.ts

96.77% Statements 30/31
93.75% Branches 15/16
85.71% Functions 6/7
96.42% Lines 27/28

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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                                  1x   1x   1x 5x 5x   5x   5x   5x     1x 1x   1x 2x 1x         1x           1x                     1x 5x   5x 5x 1x   4x 4x   5x   4x   1x     1x 2x    
import { autocomplete } from "@clack/prompts";
import process from "node:process";
import { isInteractive, out } from "../shared/adapters/io.ts";
import { orExit } from "../shared/adapters/prompts.ts";
import type { Args } from "../shared/cli.ts";
import { ExpectedError } from "../shared/errors.ts";
import { projectsWithDev } from "./adapters/nx.ts";
import { dev } from "./adapters/pnpm.ts";
import {
    findProject,
    matches,
    rowFor,
    SCOPE,
    toProjects,
    type DevProject,
} from "./domain/projects.ts";
 
const CANCELLED = "Cancelled — nothing started.";
 
const VISIBLE_ROWS = 12;
 
const list = (): DevProject[] => {
    const loading = out.spinner();
    loading.start("Asking Nx what it can run...");
 
    const projects = toProjects(projectsWithDev());
 
    loading.stop(`${projects.length} project${projects.length === 1 ? "" : "s"} to choose from.`);
 
    return projects;
};
 
const shortNames = (projects: DevProject[]): string =>
    projects.map(({ name }) => name.replace(SCOPE, "")).join(", ");
 
const choose = async (projects: DevProject[], search: string): Promise<DevProject> => {
    if (!isInteractive()) {
        throw new ExpectedError(
            `No terminal to pick with — name a project: pnpm dev <${shortNames(projects)}>`,
        );
    }
 
    return orExit(
        await autocomplete({
            message: "Which one?",
            placeholder: "Type to search, ↑↓ to browse",
            initialUserInput: search,
            maxItems: VISIBLE_ROWS,
            options: projects.map((project) => ({
                value: project,
                label: rowFor(project),
                hint: project.name,
            })),
            filter: (input, { value }) => matches(value, input),
        }),
        CANCELLED,
    );
};
 
export const main = async ({ positionals }: Args): Promise<void> => {
    out.begin("🚀 pnpm dev");
 
    const projects = list();
    if (projects.length === 0)
        throw new ExpectedError("Nothing in this workspace declares a `dev` script.");
 
    const requested = positionals.join(" ");
    const named = requested ? findProject(projects, requested) : undefined;
 
    if (requested && !named) out.warn(`No project here is called "${requested}" — search below.`);
 
    const project = named ?? (await choose(projects, requested));
 
    out.end(`pnpm --filter ${project.name} run dev`);
 
    // `null` means Ctrl+C, not a failure — avoids pnpm's ELIFECYCLE noise on top of a normal stop.
    const status = dev(project.name);
    if (status) process.exitCode = status;
};