The Trails Command Line
See also: Guides index · trailties package · thor package · rake package
Most of the time spent building a Trails application outside the editor is spent at the trails command. This guide covers:
- Creating an application with
trails new, and choosing what it includes. - Running the server, a console and one-off scripts.
- Generating and destroying code.
- Database, notes, routes, test and housekeeping commands.
- Writing your own tasks.
The command is shipped by @blazetrails/trailties. Commands and generators are built on the thor port and tasks on the rake port, so both behave as they do in Rails. Option names are kebab-case on the command line (--skip-git) and camelCase in code (skipGit); see CLAUDE.md, "Casing is translated, never mirrored".
Creating a Trails App
trails new
trails new creates an application directory: the app/, config/, db/ and test/ trees, a package.json, and a pnpm-lock.yaml. A trails app is a pnpm project.
pnpm dlx @blazetrails/trailties new blog
cd blogIt installs dependencies when it finishes. From then on, run every command through the project's own copy, pnpm trails <command> (or bin/trails).
Preconfigure a Different Database
SQLite is the default. --database (or -d) picks another, and writes the matching config/database.ts and driver dependency:
pnpm dlx @blazetrails/trailties new blog --database postgresqlThe choices are sqlite3, postgresql, mysql and mariadb-mysql.
Skipping Defaults
A generated app includes every framework. Each has a --skip-* flag that leaves it out: --skip-action-mailer, --skip-action-mailbox, --skip-action-text, --skip-active-job, --skip-active-storage, --skip-action-cable, --skip-test, --skip-system-test, --skip-git, --skip-docker, and more; trails new --help lists them all. --api builds an API-only application (see Using Trails for API-only Applications).
JavaScript is served through Vite, so --javascript defaults to vite. The importmap and propshaft options Rails offers are not ported; the ruling is CLAUDE.md, "importmap-rails and propshaft are out of scope".
Command Line Basics
pnpm trails --help lists every command the application has, including tasks added by installed packages and by the application itself. Each command also answers --help.
trails server
Starts the application's web server, by default on localhost:3000. -p changes the port and -b the address it binds to:
pnpm trails server -p 4000The server serves the Rack app only and does not start Vite. In development, bin/dev runs both from Procfile.dev; requests for assets are proxied to the Vite dev server.
trails generate
Generators write code from templates: a model with its migration and test, a controller with its views, a whole scaffold. With no arguments trails generate lists every generator; with a generator name and --help it shows that generator's options.
pnpm trails generate model Article title:string body:text
pnpm trails generate controller Greetings helloA field is name:type, and the types are Active Record's column types. Generator names are kebab-case: scaffold-controller, integration-test, application-record. trails g is short for trails generate.
A scaffold generates the model, migration, controller, views, routes and tests for a resource at once:
pnpm trails generate scaffold HighScore game:string score:integer
pnpm trails db:migratetrails console
Opens a REPL with the application loaded, so you can query models and call your own code. Top-level await works, since every query is async:
pnpm trails console--sandbox wraps the session in a transaction that is rolled back on exit, so nothing you change in the console persists. -e picks the environment.
trails dbconsole
Opens the command-line client of the configured database (sqlite3, psql or mysql) with the connection settings filled in. --database picks a database in a multi-database application.
trails runner
Runs a piece of code, or a file, with the application loaded and exits:
pnpm trails runner "console.log(await Article.count())"
pnpm trails runner script/backfill.ts-e chooses the environment, as with the console.
trails destroy
The inverse of generate: it removes the files a generator created and undoes the edits it made, such as an added route. trails d is short for it.
pnpm trails destroy model Articletrails about
Prints the versions of trails, Node and the database adapter, the application's root and environment, and the middleware stack. It is the first thing to include in a bug report.
trails db:
The database tasks: db:create, db:drop, db:migrate, db:rollback, db:seed, db:setup, db:prepare, db:reset, db:schema:load, db:schema:dump and the rest. db:prepare is the one to run on deploy: it creates the database if needed and migrates it. The Active Record Migrations guide covers them in depth.
Switching to a Different Database Later
trails db:system:change --to=postgresql rewrites config/database.ts, the driver dependency and the Docker files for another database. Data is not moved.
trails notes
Searches the code for comments annotated FIXME, OPTIMIZE or TODO and prints each with its file and line:
pnpm trails notesAnnotations
--annotations (or -a) narrows the search to the tags you name:
pnpm trails notes --annotations FIXME HACKTags
Custom tags can be registered for the whole application, so a plain trails notes finds them too, with config.annotations.registerTags.
Directories
By default notes searches app, config, db, lib and test. config.annotations.registerDirectories adds more.
Extensions
Comments are recognised in .ts, .js, .tse, .css and the other extensions trails knows. config.annotations.registerExtensions teaches it a new file extension and the comment syntax it uses.
trails routes
Prints every route: its name, HTTP verb, path pattern and the controller action it reaches. -c filters by controller and -g greps the output.
trails test
Runs the test suite, or the files and directories you name. Tests run on Vitest; trails test:system runs only the system tests.
pnpm trails test test/models/article.test.tstrails tmp:
tmp:clear empties the cache, socket and screenshot directories under tmp/; tmp:cache:clear, tmp:sockets:clear and tmp:screenshots:clear empty one each. tmp:create creates them.
Miscellaneous
trails dev:cachetoggles caching in development.trails initializerslists the initializers in the order they run.trails middlewareprints the middleware stack.trails statscounts lines of code and tests per directory.trails secretprints a new random secret.trails credentials:editopens the encrypted credentials file.trails zeitwerk:checkchecks the project's file layout against the autoloader; see Autoloading and Reloading.trails versionprints the trails version.
Custom Rake Tasks
An application's own tasks live in lib/tasks/, one or more per *.rake.ts file, and trails --help lists every task that has a description. A task file uses the rake DSL:
import { DSL } from "@blazetrails/rake";
DSL.namespace("reports", () => {
DSL.desc("Print a daily summary");
DSL.task("daily", () => {
console.log("summary");
});
});The task above runs as pnpm trails reports:daily. A task can depend on others by passing an object instead of a name, { daily: ["db:prepare"] }, and one that needs the application loaded depends on environment.