setup

tcmts.examples.notes.setup · #g3egx3p4 · sh file

#!/bin/sh
# Build the notes example as a tcm codebase in <dir> (default ./notes-demo), then open <dir> in your editor.
# Uses the tcm on your PATH unless TCM is set (a command line for another one). Nothing to install: tcm
# provides React (and every other package the code imports) from its cache.
set -e
here=$(cd "$(dirname "$0")" && pwd)
dir=${1:-$PWD/notes-demo}
# a function, not a variable: this checkout's path may contain spaces. TCM_BIN (a tcm.js to run with Node) is
# what the test suites set.
tcm() {
  if [ -n "$TCM_BIN" ]; then node "$TCM_BIN" "$@"
  elif [ -n "$TCM" ]; then $TCM "$@"
  else command tcm "$@"; fi
}

if [ -e "$dir/.tcm" ]; then echo "$dir already has a codebase" >&2; exit 1; fi
mkdir -p "$dir"
cd "$dir"
tcm init > /dev/null
rm -f scratch.ts

# the page's look is a stylesheet, kept in the codebase as it's written: ui.tsx imports it (as a string)
cp "$here/styles.css" .
tcm add styles.css --into notes
rm styles.css

# React's routes, links and stylesheets, and the server API (tables, cookies): the libraries tcm ships, mounted
# read-only at notes.lib.react and notes.lib.server
tcm lib add tcm react > /dev/null
tcm lib add tcm server > /dev/null

# the code goes into the codebase file by file, as you'd add it from scratch files; then the files go away:
# from here on the code lives in .tcm, and the editor shows it from there
for f in model.ts markdown.tsx server.ts ui.tsx; do
  cp "$here/$f" .
  tcm add "$f"
  rm "$f"
done
cp "$here/playground.tsx" scratch.tsx
# (NO_TESTS=1 skips them: a server building it only to run it, with no browser for the one that needs it)
[ -n "${NO_TESTS:-}" ] || tcm test notes

cat <<MSG

The notes example is in $dir.
  tcm ls notes                what's in it
  tcm view notes.NoteList     a server component, and why it renders on the server
  tcm dev notes.App           run the app (open the URL it prints)
Open $dir in VS Code (with the tcm-ts extension) to see it in the editor.
MSG

used by