Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/state-defaults.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"create-baseplate": minor
---

The state question now has a default: Reflex in TypeScript games and Charm in Luau games. A TypeScript game with Vide takes Charm, since Reflex has no Vide binding. Pass `--state none` for a game without a state library.
16 changes: 10 additions & 6 deletions .github/scripts/generate-matrix.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import fs from "node:fs";
import path from "node:path";
import {
addOnsByLanguage,
choicesFor,
DEFAULT_LANGUAGE,
type Language,
languages,
Expand Down Expand Up @@ -67,15 +68,18 @@ export function generatorFor(language: Language) {
return entries.every((a, i) => entries.slice(i + 1).every((b) => !conflicts.has(pairKey(a, b))));
}

const defaults = (): Answers =>
Object.fromEntries(questions.map((question) => [question.name, question.defaultValue]));
/** `answers` filled in with what the CLI takes for the questions they leave out. */
const withDefaults = (answers: Answers = {}): Answers => choicesFor(language, answers);
const defaults = () => withDefaults();

/** The flags that create a game with `answers`; defaults are left out. */
/** The flags that create a game with `answers`; what the CLI would take anyway is left out. */
function flagsOf(answers: Answers): string {
const implied = (name: string) =>
withDefaults(Object.fromEntries(Object.entries(answers).filter(([other]) => other !== name)))[name];
return [
...(language === DEFAULT_LANGUAGE ? [] : [`--language ${language}`]),
...questions
.filter((question) => answers[question.name] !== question.defaultValue)
.filter((question) => answers[question.name] !== implied(question.name))
.map((question) => `--${question.name} ${answers[question.name]}`),
].join(" ");
}
Expand Down Expand Up @@ -143,15 +147,15 @@ export function generatorFor(language: Language) {

/** Each choice that isn't a default, alone. */
const alone = questions.flatMap((question) =>
question.values.slice(1).map((value) => ({ ...defaults(), [question.name]: value })),
question.values.slice(1).map((value) => withDefaults({ [question.name]: value })),
);

/**
* Every question answered with a choice, not its default or "none", once per UI. Where a question
* has several, the second game takes the next one, so both games differ as much as they can.
*/
const everything = (questions.find((question) => question.name === "ui")?.values ?? [""]).map((ui, i) => {
const answers: Answers = { ...defaults(), ui };
const answers: Answers = withDefaults({ ui });
for (const question of questions) {
if (question.name === "ui") continue;
const choices = question.values.filter((value) => value !== question.defaultValue && value !== "none");
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ The first question is the language, a fork in the road: `--language typescript`
| `--language` | `typescript`, `luau` |
| `--ui` | `react`, `vide` |
| `--places` | `single`, `multi` |
| `--state` | `none`, `charm`, `reflex` (TS) |
| `--state` | `reflex` (TS), `charm`, `none` |
| `--data` | `none`, `profilestore`, `lapis` |
| `--entities` | `none`, `components` (TS), `jecs` |
| `--assets` | `none`, `asphalt` |
Expand Down
2 changes: 1 addition & 1 deletion cli/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ With npm, put the flags after `--`, as in `npm create baseplate@latest my-game -
| `--language <language>` | `typescript` (default): roblox-ts with Flamework; or `luau`: Luau with Wally, StyLua, Selene and luau-lsp. Asked first; each language has its own questions. |
| `--ui <library>` | UI: `react` (default) or `vide`. |
| `--places <layout>` | `single` (default), or `multi`: a lobby and a game sharing code, each place a folder in `projects/`. |
| `--state <library>` | Client state: `charm`, `reflex` (TypeScript only) or `none` (default). |
| `--state <library>` | Client state: `reflex` (TypeScript only; its default), `charm` (Luau's default, and with Vide) or `none`. |
| `--data <library>` | Player data: `profilestore`, `lapis` or `none` (default). |
| `--entities <library>` | Entities: `components` (TypeScript only), `jecs` or `none` (default). |
| `--assets <tool>` | Assets: `asphalt`, to upload images and sounds and type their IDs, or `none` (default). |
Expand Down
3 changes: 2 additions & 1 deletion cli/src/cli/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
allAddOns,
choicesFor,
DEFAULT_LANGUAGE,
defaultFor,
findConflict,
flagProblem,
flagValuesOf,
Expand Down Expand Up @@ -180,7 +181,7 @@ export async function runCli(version: string): Promise<CliResults | undefined> {
if (givenAddOns[addOn.name] !== undefined) continue;
const answer = await p.select({
message: addOn.question,
initialValue: addOn.defaultValue,
initialValue: defaultFor(language, addOn, chosen),
options: [
// Leave out what can't go with the answers so far, such as Reflex with Vide.
...addOn.choices.filter(
Expand Down
9 changes: 7 additions & 2 deletions cli/src/consts.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
import fs from "node:fs";
import path from "node:path";
import { fileURLToPath } from "node:url";

// The build puts the template and the docs beside the bundle: dist/index.js, dist/template/<language>/
// and dist/docs/.
const DIST = path.dirname(fileURLToPath(import.meta.url));
// Run from source, as CI's matrix script does, the template is cli/template.
const TEMPLATE = fs.existsSync(path.join(DIST, "template"))
? path.join(DIST, "template")
: path.join(DIST, "..", "template");
/** The game a language's projects start from. */
export const templateBase = (language: string) => path.join(DIST, "template", language, "base");
export const templateBase = (language: string) => path.join(TEMPLATE, language, "base");
/** A language's add-ons, one folder per question and choice. */
export const templateExtras = (language: string) => path.join(DIST, "template", language, "extras");
export const templateExtras = (language: string) => path.join(TEMPLATE, language, "extras");
/** The docs site's pages, which `create-baseplate doc` prints (scripts/copyDocs.ts writes them). */
export const DOCS_DIR = path.join(DIST, "docs");
// Written by scripts/copyTemplate.ts: symbolic links, which npm doesn't pack, to recreate.
Expand Down
36 changes: 29 additions & 7 deletions cli/src/installers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,10 @@ export interface AddOn {
readonly valueName: string;
readonly question: string;
readonly choices: readonly { readonly value: string; readonly label: string; readonly hint: string }[];
/** The choice taken when none is given; "none" unless the question has no "none". */
/**
* The choice taken when none is given. Where it can't go with the other answers, as Reflex can't
* with Vide, the first choice that can is taken instead (see defaultFor).
*/
readonly defaultValue: string;
readonly allowNone: boolean;
}
Expand Down Expand Up @@ -89,10 +92,10 @@ export const addOnsByLanguage: Record<Language, readonly AddOn[]> = {
valueName: "library",
question: "Which library for client state?",
choices: [
{ value: "reflex", label: "Reflex", hint: "a Redux-like store" },
{ value: "charm", label: "Charm", hint: "signals, the most used" },
{ value: "reflex", label: "Reflex", hint: "a Redux-like store; no longer updated" },
],
defaultValue: NONE,
defaultValue: "reflex",
allowNone: true,
},
{
Expand Down Expand Up @@ -138,7 +141,7 @@ export const addOnsByLanguage: Record<Language, readonly AddOn[]> = {
valueName: "library",
question: "Which library for client state?",
choices: [{ value: "charm", label: "Charm", hint: "signals, the most used" }],
defaultValue: NONE,
defaultValue: "charm",
allowNone: true,
},
{
Expand Down Expand Up @@ -193,9 +196,28 @@ export const flagValuesOf = (name: string) => [
),
];

/** A language's answers: `given` where set, each question's default otherwise. */
export const choicesFor = (language: Language, given: Partial<AddOnChoices>): AddOnChoices =>
Object.fromEntries(addOnsByLanguage[language].map((addOn) => [addOn.name, given[addOn.name] ?? addOn.defaultValue]));
/**
* The choice `addOn` takes when none is given, with `chosen` the other answers: its default, or,
* where that can't go with them (Reflex with Vide), its first choice that can, else none.
*/
export function defaultFor(language: Language, addOn: AddOn, chosen: Partial<AddOnChoices>): string {
const fits = (value: string) =>
findConflict(language, { ...chosen, [addOn.name]: value } as AddOnChoices) === undefined;
if (fits(addOn.defaultValue)) return addOn.defaultValue;
return addOn.choices.map((choice) => choice.value).find(fits) ?? (addOn.allowNone ? NONE : addOn.defaultValue);
}

/** A language's answers: `given` where set, each question's default otherwise, in question order. */
export function choicesFor(language: Language, given: Partial<AddOnChoices>): AddOnChoices {
const chosen: AddOnChoices = {};
for (const addOn of addOnsByLanguage[language]) {
const known = Object.fromEntries(
Object.entries({ ...given, ...chosen }).filter(([, value]) => value !== undefined),
);
chosen[addOn.name] = given[addOn.name] ?? defaultFor(language, addOn, known);
}
return chosen;
}

/** Why a flag's value doesn't fit the language, such as Flamework Components in a Luau game. */
export function flagProblem(language: Language, given: Partial<AddOnChoices>): string | undefined {
Expand Down
21 changes: 11 additions & 10 deletions cli/test/create.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ const create = (cwd: string, ...args: string[]) =>

test("creates a named project an agent can read", () => {
const cwd = tempDir();
const run = create(cwd, "games/coin-rush", "--no-git");
const run = create(cwd, "games/coin-rush", "--no-git", "--state", "none");
assert.equal(run.status, 0, run.stderr);
const result = JSON.parse(run.stdout) as { ok: boolean; name: string; dir: string; nextSteps: string[] };
assert.equal(result.ok, true);
Expand Down Expand Up @@ -74,7 +74,7 @@ test("records which CLI and add-ons made the game", () => {

test("makes a git repo", () => {
const cwd = tempDir();
const run = create(cwd, "my-game");
const run = create(cwd, "my-game", "--state", "none");
assert.equal(run.status, 0, run.stderr);
const log = spawnSync("git", ["log", "--oneline"], { cwd: path.join(cwd, "my-game"), encoding: "utf8" });
assert.match(log.stdout, /Initial commit from create-baseplate/);
Expand Down Expand Up @@ -186,7 +186,7 @@ test("creates into a freshly cloned repo, and commits there", () => {
git("add", "--all");
git("-c", "user.name=t", "-c", "user.email=t@t", "commit", "--quiet", "--message", "Initial commit");

const run = create(cwd, "coin-rush");
const run = create(cwd, "coin-rush", "--state", "none");
assert.equal(run.status, 0, run.stderr);
const result = JSON.parse(run.stdout) as { ok: boolean; git: boolean; committed: boolean; warnings: string[] };
assert.equal(result.ok, true);
Expand Down Expand Up @@ -342,13 +342,13 @@ test("applies the chosen add-ons", () => {
assert.match(agents, /## Entities/);
});

test("takes no add-ons by default", () => {
test("takes Reflex and no other add-ons by default", () => {
const cwd = tempDir();
const run = create(cwd, "my-game", "--no-git");
assert.deepEqual(JSON.parse(run.stdout).addOns, {
ui: "react",
places: "single",
state: "none",
state: "reflex",
data: "none",
entities: "none",
assets: "none",
Expand All @@ -368,7 +368,7 @@ test("creates a Luau game from the Luau template", () => {
assert.deepEqual(result.addOns, {
ui: "react",
places: "single",
state: "none",
state: "charm",
data: "none",
entities: "none",
assets: "none",
Expand Down Expand Up @@ -524,7 +524,7 @@ test("refuses an add-on choice that doesn't exist", () => {
const cwd = tempDir();
const run = create(cwd, "my-game", "--no-git", "--state", "redux");
assert.equal(run.status, 1);
assert.match(JSON.parse(run.stdout).error, /charm, reflex, none/);
assert.match(JSON.parse(run.stdout).error, /reflex, charm, none/);
});

test("swaps React for Vide", () => {
Expand All @@ -548,14 +548,15 @@ test("swaps React for Vide", () => {
const agents = read("AGENTS.md");
assert.match(agents, /and Vide for the UI/);
assert.match(agents, /## UI\n\nVide/);
assert.doesNotMatch(agents, /react|ripple/i, "AGENTS.md names no React package");
assert.doesNotMatch(agents, /\bReact\b|@rbxts\/react|[Rr]ipple/, "AGENTS.md names no React package");
assert.doesNotMatch(read("README.md"), /React DevTools/);
});

test("uses Charm's Vide binding with Vide", () => {
test("takes Charm, with its Vide binding, by default with Vide", () => {
const cwd = tempDir();
const run = create(cwd, "my-game", "--no-git", "--ui", "vide", "--state", "charm");
const run = create(cwd, "my-game", "--no-git", "--ui", "vide");
assert.equal(run.status, 0, run.stderr);
assert.equal(JSON.parse(run.stdout).addOns.state, "charm");
const dir = path.join(cwd, "my-game");
const dependencies = JSON.parse(fs.readFileSync(path.join(dir, "package.json"), "utf8")).dependencies;
assert.ok(dependencies["@rbxts/vide-charm"] !== undefined);
Expand Down
8 changes: 7 additions & 1 deletion cli/test/matrix.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,16 @@ for (const [language, generator] of Object.entries(generators) as [Language, (ty
});

test(`${language}: creates a game with every question answered, once per UI`, () => {
// A question whose only choice besides "none" is its default, such as Luau's state, answers with it.
const onlyChoice = (q: (typeof questions)[number]) => q.values.filter((value) => value !== "none").length === 1;
const uis = questions.find((question) => question.name === "ui")?.values ?? [];
for (const ui of uis) {
const all = matrices.create.find(
(game) => game.ui === ui && questions.every((q) => q.name === "ui" || game[q.name] !== q.defaultValue),
(game) =>
game.ui === ui &&
questions.every(
(q) => q.name === "ui" || (game[q.name] !== "none" && (onlyChoice(q) || game[q.name] !== q.defaultValue)),
),
);
assert.ok(all, `every question with ${ui}`);
}
Expand Down
8 changes: 4 additions & 4 deletions www/src/content/docs/add-ons.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@ title: Add-ons
description: The UI library, the place layout, and the libraries and tools the CLI can add for state, player data, entities, assets and release builds; what each wires in, and how to take it out.
---

The base game brings Flamework and React with a few small helpers they work well with, and leaves libraries for state, data and entities to you. Like create-t3-app, the CLI asks about each, offering the most used libraries or none, which is the default. It also asks which UI library to use and how many places:
The base game brings Flamework and React with a few small helpers they work well with, and asks about libraries for state, data and entities. Like create-t3-app, the CLI asks about each, offering the most used libraries or none. None is the default, except for state: Reflex in TypeScript (Charm with Vide, which Reflex doesn't support) and Charm in Luau. It also asks which UI library to use and how many places:

| Question | Flag | Choices |
| --- | --- | --- |
| Language | `--language` | `typescript` (default, roblox-ts), `luau` |
| UI | `--ui` | `react` (default, [React](https://react.dev) for Roblox), `vide` ([Vide](https://github.com/centau/vide)) |
| Places | `--places` | `single` (default), `multi` |
| Client state | `--state` | `charm` ([Charm](https://github.com/littensy/charm)), `reflex` ([Reflex](https://github.com/littensy/reflex), no longer updated; TypeScript only), `none` |
| Client state | `--state` | `charm` ([Charm](https://github.com/littensy/charm); Luau's default), `reflex` ([Reflex](https://github.com/littensy/reflex); TypeScript only, its default), `none` |
| Player data | `--data` | `profilestore` ([ProfileStore](https://github.com/MadStudioRoblox/ProfileStore)), `lapis` ([Lapis](https://github.com/nezuo/lapis)), `none` |
| Entities | `--entities` | `components` ([Flamework Components](https://flamework.fireboltofdeath.dev/docs/additional-modules/components); TypeScript only), `jecs` ([Jecs](https://github.com/Ukendio/jecs)), `none` |
| Assets | `--assets` | `asphalt` ([Asphalt](https://github.com/jackTabsCode/asphalt)), `none` |
Expand Down Expand Up @@ -38,7 +38,7 @@ To remove it, put back the base game's `clicker-controller.ts` and `app.tsx` (fr

## State: Reflex

`--state reflex` adds one [Reflex](https://github.com/littensy/reflex) store, `src/store/client/client-store.ts`, made of a slice per feature (`clicker-slice.ts`, with a spec). `ClickerController` changes it through the store's actions, and `app.tsx` reads it with `useSelector` inside a `ReflexProvider`. Packages: `@rbxts/reflex`, `@rbxts/react-reflex`. Reflex is no longer updated and has no Vide binding.
`--state reflex` adds one [Reflex](https://github.com/littensy/reflex) store, `src/store/client/client-store.ts`, made of a slice per feature (`clicker-slice.ts`, with a spec). `ClickerController` changes it through the store's actions, and `app.tsx` reads it with `useSelector` inside a `ReflexProvider`. Packages: `@rbxts/reflex`, `@rbxts/react-reflex`. Reflex has no Vide binding, so a Vide game takes Charm by default.

To remove it, delete `src/store/` and `clicker-slice.ts` and its spec, put back the base game's `clicker-controller.ts` and `app.tsx`, and `pnpm remove @rbxts/reflex @rbxts/react-reflex`.

Expand Down Expand Up @@ -99,5 +99,5 @@ A Luau add-on's packages are in `wally.toml`; to remove one, delete its lines th

There's no command to remove one; the sections above list what each touches. Two things help:

- **See exactly what it changed.** Create two games with the same flags, one without the add-on, and compare them: `pnpm create baseplate /tmp/with --no-git --no-install --state charm`, the same without `--state charm`, then `diff -r /tmp/with /tmp/without`.
- **See exactly what it changed.** Create two games with the same flags, one without the add-on, and compare them: `pnpm create baseplate /tmp/with --no-git --no-install --data lapis`, the same without `--data lapis`, then `diff -r /tmp/with /tmp/without`.
- **Finish the same way each time.** Delete the add-on's section in `AGENTS.md`, set its key under `baseplate.addOns` in `package.json` to `none`, and run `mise run check`.
4 changes: 2 additions & 2 deletions www/src/content/docs/cli.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ With npm, the flags go after `--`: npm reads flags before it as its own, and the
| `--language <language>` | `typescript` (default): roblox-ts with Flamework; or `luau`: Luau with Wally, StyLua, Selene and luau-lsp. Asked first; each language has its own questions. |
| `--ui <library>` | UI: `react` (default) or `vide`. |
| `--places <layout>` | `single` (default), or `multi`: a lobby and a game sharing code, each place a folder in `projects/`. |
| `--state <library>` | Client state: `charm`, `reflex` (TypeScript only) or `none` (default). |
| `--state <library>` | Client state: `reflex` (TypeScript only; its default), `charm` (Luau's default, and the default with Vide, which Reflex doesn't support) or `none`. |
| `--data <library>` | Player data: `profilestore`, `lapis` or `none` (default). |
| `--entities <library>` | Entities: `components` (TypeScript only), `jecs` or `none` (default). |
| `--assets <tool>` | Assets: `asphalt`, to upload images and sounds and type their IDs, or `none` (default). |
Expand All @@ -42,7 +42,7 @@ Without a terminal, as when an agent or CI runs it, it asks nothing and takes th
"git": true,
"committed": true,
"installed": true,
"addOns": { "ui": "react", "places": "single", "state": "none", "data": "none", "entities": "none", "assets": "none", "release": "none" },
"addOns": { "ui": "react", "places": "single", "state": "reflex", "data": "none", "entities": "none", "assets": "none", "release": "none" },
"warnings": [],
"nextSteps": ["cd my-game", "mise run check", "mise run dev"]
}
Expand Down