setProduction

tcmts.studio.setProduction · #gqfh7wxn · term · runs on the server

uses node:fs

Keep production's address (url, the server tcm start runs the app on) and traces token (its TCM_TRACES_TOKEN), for the studio's traces to read; its traces fetched before go.

import { HttpError } from "#codebase/tcmts/server";
import { editableCodebase, productionFile, productionTraces } from "#codebase/tcmts/studio";
import fs from "node:fs";
import path from "node:path";

export async function setProduction(url: string, token: string): Promise<void> {
  const cb = editableCodebase();
  let u: URL;
  try {
    u = new URL(url.trim());
  } catch {
    throw new HttpError(400, `${url} isn't an address: https://… , where the app runs.`);
  }
  if (!/^https?:$/.test(u.protocol))
    throw new HttpError(400, "Production is an https:// (or http://) address.");
  if (!token.trim()) throw new HttpError(400, "Its traces token is the server's TCM_TRACES_TOKEN.");
  fs.mkdirSync(path.dirname(productionFile(cb)), { recursive: true });
  fs.writeFileSync(
    productionFile(cb),
    `${JSON.stringify({ url: u.origin + u.pathname.replace(/\/+$/, ""), token: token.trim() }, null, 2)}\n`,
    { mode: 0o600 },
  );
  fs.chmodSync(productionFile(cb), 0o600);
  fs.rmSync(productionTraces(cb), { force: true });
}

uses

used by