runInit

tcmts.cli.run.runInit · #g5oc5x65 · term · runs on the server

uses node:path

Runs tcm init, without opening the codebase.

import { Codebase } from "#codebase/tcmts/cli/codebase";
import { routeInit } from "#codebase/tcmts/cli/commands";
import { execute } from "#codebase/tcmts/cli/main";
import type { Started } from "#codebase/tcmts/cli/run";
import { dim } from "#codebase/tcmts/cli/style";
import { interactive, intro, outro, success } from "#codebase/tcmts/cli/ui";
import { parse } from "@bomb.sh/router";
import fs from "node:fs";
import path_1 from "node:path";

export function runInit({ cmd, rest, cwd }: Started): number {
  execute(cmd, parse(routeInit(), { argv: rest }));
  intro("tcm init");
  const cb = Codebase.init(cwd);
  // (the new codebase's, whatever codebase TCM_CODEBASE names)
  const file = path_1.join(cb.lay.root, "scratch.ts");
  if (!fs.existsSync(file)) {
    fs.writeFileSync(
      file,
      `// Definitions here are checked and hashed by \`tcm\`; bare expressions are watch expressions.
// Import saved definitions with: import { name } from "#codebase/namespace"

/** Square a number. */
export const square = (x: number): number => x * x;

square(12);
`,
    );
  }
  success(`initialized codebase in ${cb.lay.tcmDir}`);
  outro(
    `next: run \`tcm\` and edit scratch.ts${interactive() ? dim(" · `source <(tcm complete zsh)` completes commands and names") : ""}`,
  );
  return 0;
}

uses

used by