Patterns & conventions
The conventions the luna CLI (cli/) follows, and why. Read Getting started with the CLI first if you haven't used it yet, and Add a command for the step-by-step of building one.
Generic commands over one-off scripts
The guiding idea behind the CLI is generic, reusable commands — model a command on how the AWS CLI works (aws lambda create --…, parameterised and re-runnable), not a script that does one specific thing once.
Genuinely one-off work has two escape hatches, neither of which is a bespoke command:
luna script run— runs a.js/.tsscript with full CLI context (assumed-role credentials, environment, module resolution).src/scripts/— one-off scripts kept in-repo, also run through the script runner.
cli-scripts-legacy/ is the anti-pattern this replaces — a folder of bespoke, single-use scripts predating the CLI. Don't add to it, and don't recreate its scripts as new commands here wholesale. Existing legacy scripts can still be run through the Luna CLI (see Run one-off scripts); if that doesn't work, fall back to running them directly with cd cli-scripts-legacy && node index.js <command>. New tooling belongs in cli/ as a proper command or a script under src/scripts/.
Command structure
Commands follow a three-tier layout — see Add a command and cli/STRUCTURE.md for the full walkthrough:
index.tsis Commander configuration only — name, description, arguments, options,.action()delegation. No business logic.- Business logic lives in
<subcommand>.service.ts. Keep the subcommand folder flat — no nestedutils/. - Code shared across a command's subcommands goes in
commands/<command>/lib/; code shared across commands goes insrc/utils/<domain>/, re-exported fromsrc/utils/index.ts.
Register every new top-level command in src/index.ts via program.addCommand(...).
The command lifecycle
Every invocation flows through Commander lifecycle hooks (src/utils/hooks/), wired onto the root program in src/index.ts:
preAction— resolves the target environment, then (for non-localruns) validates AWS credentials, assumes the CLI role, and creates aPENDINGaudit record.- command
.action()— your subcommand logic runs, with the assumed-role credentials already configured on the AWS SDK. postAction— updates the audit record toSUCCEEDED.errorHook— wrapsparseAsync(andSIGINT/SIGHUP); marks the audit recordCANCELLEDfor user cancellations, orFAILED(with the error message) otherwise.
Because the hooks own credential setup and audit state, commands throw on failure rather than calling process.exit() — exiting directly would skip the error hook, so a failure would never get recorded.
Environment resolution and AWS role assumption
The CLI targets one environment per run, resolved in this order:
- the
-e/--environmentflag, - the
ENVIRONMENTenvironment variable, - an interactive prompt, if neither is set.
Valid environments: local, staging, polaris, pulsar, sol, production (src/utils/environment.ts). A small allowlist of commands (ai usage track, mfa install, mfa dev) defaults to local when no environment is given, since they don't need AWS.
local short-circuits everything AWS-related — no credential check, no role assumption, no audit record — which makes it the fastest way to try a command safely.
For any other environment, the CLI needs AWS credentials for the matching account (non-prod 851725171061 for polaris/sol/pulsar/staging, 666243557741 for production), then assumes the ${environment}-iam-role-luna-cli role for you — you never assume it by hand. That role is defined per-environment in platform/infrastructure/permissions.yml.
Auditing
Almost every command run is recorded to the ${environment}-cli-audit-table DynamoDB table — one per environment. A record captures the command name, its args/options, environment, author, timestamps, and status (PENDING → SUCCEEDED / FAILED / CANCELLED). This is entirely driven by the hooks above — a command doesn't add auditing code itself.
A small allowlist opts out of auditing (COMMANDS_WITHOUT_AUDIT in src/utils/hooks/preAction.ts — currently aurora tunnel, mfa install, mfa dev, ai usage track). local runs are never audited, regardless of the allowlist.
Argument and flag conventions
- Follow the AWS CLI shape:
luna <command> <subcommand> [args] [options], with-e/--environmentas the global flag every command accepts. - Prefer named options (
-o, --output-dir,-x, --exclude) over positional arguments for anything beyond the command's primary target — seedatacopyback copy's--output-dir/--exclude/--samplefor the pattern. - Use Commander's own option parsing (
.option(...),.argument(...)) inside a command'sindex.tsrather than hand-parsingprocess.argv. - Inside a script run via
luna script run(which doesn't get Commander option parsing for its own args), use the sharedparseArgshelper (src/utils/parseArgs.ts) to turnprocess.argvinto a structured object, rather than hand-rolling flag parsing.
Error handling
Never call process.exit() inside a command's logic — throw an Error instead, so the error hook can mark the audit record FAILED (or CANCELLED for a user cancellation). Use the shared prompt helpers in src/utils/prompts/confirmActions.ts rather than hand-rolling confirmation logic — they throw the errors the hooks expect:
| Helper | What it does |
|---|---|
confirm(message) | Prompts yes/no; throws if the user declines |
confirmWithItems(message, items, label?) | Logs a list of items, then confirms |
confirmProductionEnvironment() | Confirms before proceeding against production |
terminateIfProduction(environment) | Throws immediately if the environment is production |
Versioning
cli/package.json's version is the single source of truth for luna --version. Bump it per semver on any change to the CLI.
Build and local development
The CLI runs its compiled dist/, not src/ — a command isn't runnable until it's rebuilt. yarn run build compiles once; yarn run dev watches and rebuilds on save. Both require the binary to already be linked (yarn run setup does this the first time).
