remoteLocation

tcmts.cli.sync.remoteLocation · #uyjcrz4b · function · runs on the server

uses node:path

Where a remote, as typed, is: an http(s) URL as it is; a directory that's there, by its full path; a host that isn't a directory here (tcm.codes, code.alice.dev/notes, localhost:7737), its server: https, or http for this machine's own (tcm serve serves it without TLS); anything else, a directory (one to be made, say).

import fs from "node:fs";
import path from "node:path";

export function remoteLocation(spec: string): string {
  if (/^https?:\/\//.test(spec)) return spec;
  const dir = path.resolve(spec);
  if (fs.existsSync(dir)) return dir;
  const host = /^([a-z0-9-]+(?:\.[a-z0-9-]+)*)(:\d+)?(\/.*)?$/i.exec(spec);
  if (!host || !(host[1]!.includes(".") || host[1] === "localhost" || host[2])) return dir;
  return `${/^(localhost|127\.\d+\.\d+\.\d+)$/.test(host[1]!) ? "http" : "https"}://${spec}`;
}

used by