Skip to content

Command line for CI

Published  · Updated by Claude

The same checks and formatter run outside the editor, from npm, with no gems to install.

Terminal window
npm install --save-dev @x-ror/ruby-templates
Terminal window
npx ruby-templates check app/views

check prints the diagnostics (errors and warnings) of ERB, Haml, Slim, Jbuilder and Builder templates, one per line with the file, line and column:

app/views/users/index.html.erb:4:3: error: unexpected end-of-input, assuming it is closing the parent top level context
app/views/users/index.html.erb:3:5: warning: `<img>` without `alt`: the alt attribute must be specified (HTML 4.8.4.4.1)

It exits with 1 if there are any, or if a .ruby-templates.toml has a problem (printed the same way, once per file). The checks are the ones described in Errors as you type; the command line checker reads ERB as Erubi does, so erb-percent % lines are not modelled.

Terminal window
npx ruby-templates format --check app/views   # files that would change; exit 1 if any
npx ruby-templates format --write app/views   # format them

Without --check or --write, the result goes to standard output. A file whose structure is in doubt is left as it is, with the reason on standard error. See Formatting for what the formatter changes.

Option What it does
--check Lists the files that would change; exits with 1 if any
--write Writes the formatted files
--verbose Also prints the settings, and the .editorconfig and .ruby-templates.toml files used
--align-comments Moves the lines inside ERB comments with the line the comment opens on; it can only turn this on, over format.align_comments in the file
--stdin-filepath <path> Formats the template read from standard input as the file at <path>, for editors
Terminal window
npx ruby-templates format --stdin-filepath show.html.erb < show.html.erb   # from stdin, for editors
npx ruby-templates format --verbose --check app/views   # also the settings and .editorconfig files used

Both commands read the .ruby-templates.toml files above each template, as the editor does: rules turned off, files excluded, [erb] format.enabled = false, format.align_comments, extra_engines. The rules reference describes the file. format --verbose also prints each file read and its problems; an excluded file is reported as left unchanged: excluded by <file>.

Pass files or folders. Folders are walked for template files, skipping hidden folders and node_modules.

Code Meaning
0 OK
1 Diagnostics (errors or warnings), a problem in a .ruby-templates.toml, or files that would change
2 Usage or I/O error, a formatter bug, or (for check) a named file that isn’t a template

With @x-ror/ruby-templates in your package.json dev dependencies, add steps like these to a job that has checked out the repository:

- run: npm ci
- run: npx ruby-templates check app/views
- run: npx ruby-templates format --check app/views

Each step fails the job when the command exits with a non-zero code.