run-and-notify
v0.3.0
Published
Execute a command and send the notification results via better-notify
Maintainers
Readme
run-and-notify
run-and-notify executes a command, captures stdout and stderr, renders Handlebars templates, and sends the result through Better-Notify transports. The default configuration targets SMTP email and Slack.
Install
pnpm install
pnpm run buildRun a command by placing it after the run-and-notify options:
run-and-notify --config=config.json --stdout.format=jsonl pnpm run testConfiguration values can be overridden with dotted CLI flags such as --cwd=/repo, --stdout.format=markdown, --dry-run, --no-propagate-exit-code, or --transports.smtp.enabled=false. The command is a native yargs positional argument; options after the command are passed to the target command, and --help documents defaults from the JSON schema.
Configuration
The canonical schema is schemas/config.schema.json. config.example.json shows a complete SMTP and Slack setup.
Top-level fields:
name: human-readable automation name used in default notification titles and subjects.locale: BCP 47 locale used by date and time helpers.cwd: optional working directory for the command.dryRun: whentrue, renders and logs notification payloads without sending them.propagateExitCode: whentrue, the CLI exits with the target command status after notifications are delivered. Defaults totrue.timeoutSeconds: command timeout in seconds;0disables the timeout.showStderrIfSuccess: whenfalse, success templates skip stderr unless they explicitly ignore this field.hideCommandIfSuccess: whentrue, success templates hide command metadata, visible separators, and theOutputheading so only output content remains. Defaults tofalse.templatesDir: optional directory containing Handlebars templates and partials. When omitted, built-in templates bundled into the CLI are used.stdout: stdout parser settings.stderr: stderr parser settings.transports: Better-Notify transport settings.success: templates used when the command exits with status0.error: templates used when the command exits with a non-zero status.
stdout.format and stderr.format accept:
raw: exposes{ "format": "raw", "raw": "..." }.jsonl: parses each line as JSON and wraps invalid lines as{ "raw": "..." }.markdown: exposes{ "format": "markdown", "markdown": "..." }.html: exposes{ "format": "html", "html": "..." }.pino: parses each line as a Pino JSON log record and exposes valid objects unchanged inlines; invalid or non-object lines are wrapped as{ "raw": "..." }. Built-in templates derive display labels/colors fromlevel, usemsgas the main text, formattimeas a localized date and short time, and render non-core fields as an indented object.
Only jsonl and pino expose lines. raw, markdown, and html preserve the full stream as a single string.
transports.smtp fields:
enabled: enables SMTP email delivery.host: SMTP host.port: SMTP port, from1to65535.secure: use TLS from connection start.from: sender email address.to: one or more recipient email addresses (accepts a string or an array of strings).cc: optional CC recipient email addresses (accepts a string or an array of strings).bcc: optional BCC recipient email addresses (accepts a string or an array of strings).auth.user: optional SMTP username.auth.passEnvVar: environment variable containing the SMTP password.
The SMTP email delivery instance is scoped internally as emailSmtp/email-smtp, so future email transports can be added without overloading a generic email channel name.
transports.slack fields:
enabled: enables Slack delivery through@betternotify/slack.tokenEnvVar: environment variable containing the Slack bot token passed toslackTransport().targets: optional nonempty array of unique Slack channel IDs, names, conversation IDs, or user IDs. User IDs (U...orW...) are opened as direct messages before sending. The notification is rendered and split into blocks once, then the same message batch is sent to each target. A failure for one target does not prevent attempts to later targets, but the command fails if any target fails. When set,targetstakes precedence overdefaultChannel. CLI overrides accept repeated--transports.slack.targets=IDoptions.defaultChannel: legacy single Slack channel ID or name, used whentargetsis omitted.thread: whentrue, each target receives its own parent message, followed by replies inside that target's thread. Defaults tofalse.unfurlLinks: optional boolean forwarded as Slackunfurl_linkson every Slack payload. Omit to keep Slack's default; setfalse(typically withunfurlMedia: false) to disable classic text-link previews.unfurlMedia: optional boolean forwarded as Slackunfurl_mediaon every Slack payload. Omit to keep Slack's default; setfalse(typically withunfurlLinks: false) to disable classic media previews.
Template fields under success.email and error.email:
subject: email subject template filename.html: email HTML template filename.text: plain text email template filename.
Template fields under success.slack and error.slack:
text: optional Slack fallback text template filename. When omitted, success fallback text isconfig.name; failure fallback text isFailed: {name} (status {code}).blocks: Slack blocks template filename; it must render a JSON array. Slack fallback text uses thetexttemplate when configured, otherwise the defaults above.
By default, success and error use built-in email and Slack templates. You can override any template with config or CLI flags such as --success.email.html=custom-success.html.hbs.
Templates
Templates are loaded from templatesDir and may use partials from the same directory. If templatesDir is omitted, templates are loaded from built-ins imported into the bundled CLI. Built-in templates are registered first, so custom template directories may override any built-in by filename while still using built-in partials. The rendering context contains config, stdout, stderr, status, command, cwd, timedOut, executedAt, and dryRun.
Built-in email templates render raw as escaped preformatted text, markdown as HTML, html as provided HTML, jsonl as itemized records, and pino as level-colored log cards with extra fields indented. Built-in Slack block templates render Markdown through markdown-to-slack-blocks, split large block batches before sending, separate execution context with a divider when command metadata is visible, render raw as preformatted rich text, render markdown as Slack blocks, convert html to Markdown first, render jsonl as nested itemized records, and render pino as level/msg bullets with indented Pino fields.
Registered helpers:
dateFromISO8601,timeFromISO8601,datetimeFromISO8601,datetimeFromUnixEpochMillisecondsdateFromUnixEpoch,timeFromUnixEpoch,datetimeFromUnixEpochmarkdownToHtml,htmlToMarkdown,rawToHtmlescapeHtml,escapeMarkdown,json,jsonValue,jsonString,concat,outputToSlack,slackCodeBlock,hasOutput,hideCommandIfSuccessshellCommand,shellToken: formats command arrays and paths with space-delimited shell-friendly output, single-quoting values that contain whitespace.
Environment
Copy .env.example when developing locally. The default examples reference these variables:
SMTP_PASS: password used bytransports.smtp.auth.passEnvVar.SLACK_BOT_TOKEN: token used bytransports.slack.tokenEnvVar.
Variables loaded from the local .env file are used by run-and-notify itself, but are omitted from the target command environment so local provider secrets are not leaked to child processes by default. Variables that already exist before run-and-notify starts, such as an explicitly exported SMTP_PASS, SLACK_BOT_TOKEN, LOG_LEVEL, or any other environment variable, are still propagated to the child command.
Examples
Complete example configurations and templates live under examples/<name>/, with sample target commands under examples/apps/.
full-raw: raw stdout and stderr, including command metadata and execution timestamp.minimal: only transport configuration; uses parser, notification, and built-in template defaults.full-markdown: markdown stdout and stderr with fenced code blocks.full-html: HTML stdout and stderr, converted to markdown and wrapped in Slack code blocks.daily-digest-markdown: markdown success digest; failure renders JSONL pino stderr lines.daily-digest-html: HTML success digest; failure renders JSONL pino stderr lines.structured-jsonl-markdown: structured JSONL stdout rendered as markdown.structured-jsonl-html: structured JSONL stdout rendered as HTML.
Exit Codes
If the command succeeds and notifications are delivered, run-and-notify exits 0. If the command fails, notifications use the error templates and the CLI exits with the command status by default. Set propagateExitCode to false, pass --propagateExitCode=false, or pass --no-propagate-exit-code to return 0 after successful notification delivery even when the target command failed. If no transport is enabled, transport setup fails, or delivery through any configured transport fails, the CLI exits 1; the target command is not executed unless at least one transport is ready.
Timed out commands are terminated and reported with status 124.
--dry-run still requires enabled transport configuration, executes the target command, and renders provider payloads, but skips send() calls and logs what would have been sent.
Development
pnpm run qaThe QA gate runs Biome checks, build, tests, and typecheck.
