This is the official command-line interface for Knock, a notification platform that helps developers build, send, and manage notifications.
The Knock CLI is a TypeScript-based CLI tool built with oclif that enables developers to:
- Manage notification workflows
- Handle in-app guides and message types
- Manage translations and email layouts
- Work with template partials
- Control environment branches and commits
- Push/pull resources between local filesystem and Knock
- Framework: oclif (CLI framework)
- Language: TypeScript
- Build: SWC for fast compilation
- Testing: Mocha + Chai + Sinon
- API Client: @knocklabs/mgmt for Knock API interactions
src/commands/- CLI command implementations organized by resource type (workflow, guide, partial, etc.)src/lib/- Shared library code:helpers/- Utility functions for common operationsmarshal/- Data transformation and validation logicrun-context/- Runtime context management
dist/- Compiled JavaScript outputtest/- Test files mirroring source structure
Commands follow a resource-based organization:
branch/- Branch management (create, delete, switch, merge)workflow/- Workflow operations (pull, push, validate, activate, run)guide/- In-app guide managementmessage-type/- Message type schemas for guidespartial/- Template partial managementtranslation/- Translation file handlinglayout/- Email layout managementcommit/- Commit and promotion operations
All commands extend BaseCommand from src/lib/base-command.ts which provides:
- Authentication handling
- Service token management
- Common flags (environment, branch, service-token)
- Error handling utilities
The src/lib/marshal/ directory contains logic for:
- Marshaling: Converting resources to/from local filesystem format
- Validation: Validating resource schemas
- Serialization: Formatting data for API communication
Most resources follow a standard pattern with these commands:
get- Retrieve a single resourcelist- List all resourcespull- Download from Knock to local filesystempush- Upload from local filesystem to Knockvalidate- Validate local resource filesnew- Create new resource with scaffoldingopen- Open resource in Knock dashboard
- Knock uses environment-based workflows (development → staging → production)
- Branches allow isolated development off the development environment
- Changes must be committed before promotion to subsequent environments
- Commands can authenticate via
--service-tokenflag orKNOCK_SERVICE_TOKENenv var - Alternative:
knock loginfor user-based authentication
knock.json- Project-level configuration file (created viaknock init)- Stores resource directory paths and project settings
# Install dependencies
yarn install
# Build the project
yarn build
# Run tests
yarn test
# Lint code
yarn lint
# Format code
yarn format.write
# Type check
yarn type.check
# Run all checks
yarn check- Tests use nock for HTTP mocking
- Factory helpers in
test/support/factory.tsfor creating test data - Tests follow the source structure in
test/directory
src/lib/base-command.ts- Base class all commands extendsrc/lib/resources.ts- Resource type definitionssrc/lib/api-v1.ts- Knock API client wrappersrc/lib/marshal/index.isomorphic.ts- Marshaling helpers that are runtime agnostic, and can be used in either a node.js or browser environmentsrc/commands/knock.ts- Main entry point command
- Create command file in appropriate
src/commands/subdirectory - Extend
BaseCommand - Define command flags and arguments
- Implement
run()method - Add corresponding test in
test/commands/
- Add marshal logic in
src/lib/marshal/{resource-type}/ - Create command files for standard operations (get, list, pull, push, validate)
- Update
src/lib/resources.tsif needed - Add tests
The CLI primarily interacts with Knock's Management API via the @knocklabs/mgmt package. Authentication, environment selection, and branch context are handled automatically by BaseCommand.
- This is a production CLI tool used by Knock customers
- Maintain backward compatibility with existing commands
- Follow oclif conventions for command structure and flags
- Use the existing marshal patterns for resource handling
- All commands should support
--jsonflag for machine-readable output - Error messages should be user-friendly and actionable
- Tests are required for new commands and features
- Node version: The project requires Node.js >= 20.19.0 (due to
@swc/cli). The.tool-versionsfile listsnodejs 20.9.0but this is outdated; use the latest Node 20.x via nvm (nvm install 20 && nvm use 20). - Dev entry point: Use
./bin/dev.js(not./bin/run.js) to run the CLI during development. It usests-nodewithtsconfig-paths/registerto resolve@/path aliases. The productionbin/run.jsrequires a build step and path alias resolution that SWC does not perform. - No external services needed: This is a self-contained CLI. All tests use
nockfor HTTP mocking — no Knock API access, databases, or Docker required. - Standard commands: See the "Build & Development" section above for
yarn install,yarn build,yarn test,yarn lint,yarn type.check, andyarn check. - Full check before committing: Run
yarn check(which runs lint + format.check + type.check) to validate changes before pushing.