setupOrder

tcmts.dev.setupOrder · #5qop7fkw · term · runs anywhere

The order an example's setup script saves its files in, with the namespace a file kept as written goes into: tcm add styles.css --into userinput.design lines, then the files of its for f in …; do loop.

export function setupOrder(setup: string): { file: string; into?: string }[] {
  const kept = [...setup.matchAll(/^tcm add (\S+) --into (\S+)$/gm)].map((m) => ({
    file: m[1]!,
    into: m[2]!,
  }));
  const loop = /^for f in ([^;]+); do/m.exec(setup)?.[1]?.trim().split(/\s+/) ?? [];
  // (`tcm add "$f.ts"`: the loop names them without their extension)
  const ext = /^\s*tcm add "\$f(\.\w+)"/m.exec(setup)?.[1] ?? "";
  return [...kept, ...loop.map((f) => ({ file: `${f}${ext}` }))];
}

used by