skip to the page

Scaffolding from your own code

Two commands make the directory of components AI may use and add to it, and a person at a keyboard is one way to run them. There are others. A project template your team starts every repository from. A script that adds a dozen registrations at once. A test that holds your own naming rules before anybody commits a file.

The commands are a thin wrapper, and everything under them is published too. One function reads the words that were typed. A second decides what would be written. A third writes it. They run in that order, a caller may stop after any of them, and the interesting place to stop is the middle one.

Seeing what would be written

planCommand takes two things: a command, and a list of the paths already in the directory. It returns the files it would write, with their whole contents, and the lines it would print afterwards. It reads nothing and writes nothing, so asking what a command would do costs a function call.

import { describeCliError, parseArguments, planCommand } from "@jam-overture/loom/cli"

const command = parseArguments(["add", "primitive", "commerce.product-card"])

if (!command.ok) throw new Error(describeCliError(command.error))

const plan = planCommand(command.value, ["loom/primitives/app.page.ts"])

if (!plan.ok) throw new Error(describeCliError(plan.error))

for (const file of plan.value.files) {
  console.log(`${file.path} — ${file.contents.length} characters`)
}

parseArguments is the first of the three, and it is where the words become a CliCommand. Going through it rather than building that value by hand is worth the two lines: a plan made from a command nobody can type is a plan for a command that does not run.

Here is the starter command, planned against a directory nothing has been written to.

loom initplanned as this page built
the listing it was given — a directory nothing has been written to
(nothing)
would write loom/primitives/app.page.ts — 1,136 bytes
would write loom/primitives/registry.ts — 520 bytes
would write loom/primitives/registry.test.ts — 2,503 bytes
Scaffolded loom/primitives with one primitive, a generated registry, and a conformance test.
Wire the registry into your renderer as both the resolver and the validator, then run the test.

And the second command, planned against the directory the first one left behind. Notice the second file: the registry is rebuilt rather than appended to, so declaring one primitive plans a write to two paths.

loom add primitive commerce.product-cardplanned as this page built
the listing it was given — the same directory after the starter command
loom/primitives/app.page.ts
loom/primitives/registry.ts
loom/primitives/registry.test.ts
would write loom/primitives/commerce.product-card.ts — 1,174 bytes
would write loom/primitives/registry.ts — 608 bytes
Declared commerce.product-card in loom/primitives/commerce.product-card.ts.
Regenerated loom/primitives/registry.ts with 2 primitives.
Give it a description and a prop schema — the description is what a model reads when it chooses between primitives.

A refusal is the same call, with no disk involved

The commands refuse rather than half-writing, and this is the reason. The decision is made once, against the listing, before anything is opened — so a command that would clash comes back as a refusal from a function that had nothing to clash with.

loom initplanned as this page built
the listing it was given — a directory that already holds a registry
loom/primitives/app.page.ts
loom/primitives/registry.ts
loom/primitives/registry.test.ts
loom/primitives/app.page.ts already exists; nothing was written
nothing to write — the plan came back as a refusal, so there was no list of files

That is the whole mechanism. describeCliError turns the refusal into the sentence the command prints; the code beside it is a member of a published union, so a caller may react to a refusal rather than match on its prose. CLI_ERROR_CODES holds every one of them, in the order a reader meets them.

The disk is the part you replace

The third function is runCli, and its second argument is the only impure seam the whole tool has: an interface with two methods. nodeFileSystem is the one the published command uses. Anything with the same two methods does as well — a map in memory, a staging directory, a layer that records what was asked for and refuses everything.

import { ok } from "@jam-overture/loom"
import { describeCliError, runCli, type FileSystem } from "@jam-overture/loom/cli"

const inMemory = (files: Map<string, string>): FileSystem => ({
  list: async (directory) =>
    ok([...files.keys()].filter((path) => path.startsWith(`${directory}/`))),
  write: async (path, contents) => {
    files.set(path, contents)

    return ok(undefined)
  },
})

const written = new Map<string, string>()
const report = await runCli(["init"], inMemory(written))

console.log(report.ok ? report.value.written : describeCliError(report.error))

A directory nothing has written to lists as empty rather than failing, which is the ordinary case for the starter command and the one behavior a replacement has to match.

The files, with no disk at all

One step further in and the text itself is a function of a name. namesFor turns a primitive type into every spelling of it the scaffold needs, and primitiveModule and registryModule turn those into the source.

import { namesFor, primitiveModule, registryModule } from "@jam-overture/loom/cli"

const names = namesFor("commerce.product-card")

console.log(primitiveModule(names))
console.log(registryModule([names]))

That is the level to work at for a generator of your own — one that writes a schema into the starter module before saving it, say, or puts the registry somewhere the published layout does not offer.

The listing is the registration

The directory is the list. existingPrimitives is what the registry is built from — a list of paths in, registrations out. A file that is not a primitive module is skipped, and so is a name that would not be a valid type.

the listing it was given — a pair of modules, a generated registry and its test
loom/primitives/app.page.ts
loom/primitives/commerce.product-card.ts
loom/primitives/registry.ts
loom/primitives/registry.test.ts
ModuleTypeExport
app.page.tsapp.pageappPage
commerce.product-card.tscommerce.product-cardcommerceProductCard

2 of the 4 files in that listing are registrations — counted from what the function returned, not asserted.

The third column is the one worth reading. A module's name is the type verbatim, dots included, and the binding the generated registry imports is that type in camel case — which is a rule, not a guess, and namesFor is where it lives.

One name the tool will not write

loom and everything beneath it belong to the framework, and a registry refuses two definitions of one type — so a primitive scaffolded there would be unreachable in the application that asked for it.

That one is refused by parseArguments, before any directory is read, because whether a name belongs to the framework is a property of the string and of nothing else.

loom add primitive loom.cardrefused as this page built
"loom.card" is in the framework's namespace — @jam-overture/loom registers loom.* and a registry refuses two definitions with one type. Try "app.card"
no directory was read — the name was refused by the parser

The sentence hands back a replacement rather than leaving a caller to invent one. And it is a namespace rather than a list of taken names, deliberately: a list would have to be maintained against a library that grows every week, and the week it grew would break a scaffold written months earlier.

Both halves of that are yours to call directly: isFrameworkNamespaced asks the question and hostAlternativeFor picks the replacement, on this same import.