CLI tool for Flags SDK to generate TypeScript type definitions
npm install @hauses/flags-cliCLI tool for Flags SDK to generate TypeScript type definitions from your feature flags.
š Website: flags.hauses.dev
``bash`
npm install -D @hauses/flags-clior
yarn add -D @hauses/flags-clior
pnpm add -D @hauses/flags-clior
bun add -D @hauses/flags-cli
- šÆ Type-safe - Generate TypeScript definitions for your flags
- ā” Fast - Quick flag fetching and type generation
- š Secure - Multiple ways to provide API keys
- š Simple - One command to sync your flags
- š CI-friendly - Works great in CI/CD pipelines
`json`
{
"scripts": {
"flags:pull": "flags-cli pull --env VITE_FLAGS_KEY --out src/flags.d.ts"
}
}
`bash`
npm run flags:pull
This generates a flags.d.ts file with type definitions for all your flags.
`bash`
flags-cli pull [options]
- -k, --key - Publishable key (supports $VARIABLE syntax)-e, --env
- - Environment variable name containing the key-o, --out
- - Output file path (default: ./flags.d.ts)-V, --version
- - Output the version number-h, --help
- - Display help information
`bash`
flags-cli pull --env VITE_FLAGS_KEY
This reads the key from the VITE_FLAGS_KEY environment variable.
`bash`
flags-cli pull --key $VITE_FLAGS_KEY
This also reads from the VITE_FLAGS_KEY environment variable.
`bash`
flags-cli pull --key pk_123456789
ā ļø Not recommended for production - Use environment variables instead.
`bash`
flags-cli pull --env FLAGS_KEY --out types/flags.d.ts
`json`
{
"scripts": {
"flags:dev": "flags-cli pull --env VITE_FLAGS_DEV_KEY --out src/flags.d.ts",
"flags:prod": "flags-cli pull --env VITE_FLAGS_PROD_KEY --out src/flags.d.ts"
}
}
The CLI generates a TypeScript declaration file that extends the SDK types:
`typescript
import "react-sdk";
declare module "react-sdk" {
export interface FlagValues {
"dark-mode": boolean;
"new-dashboard": boolean;
"beta-features": boolean;
"experimental-ui": boolean;
}
}
`
This provides autocomplete and type checking in your IDE:
`tsx
import { useFlags } from '@hauses/react-sdk';
// ā
TypeScript knows about these flags
useFlags('dark-mode');
useFlags('new-dashboard');
// ā TypeScript error - flag doesn't exist
useFlags('unknown-flag');
`
`json`
{
"scripts": {
"flags:pull": "flags-cli pull --env VITE_FLAGS_KEY"
}
}
`env`
VITE_FLAGS_KEY=pk_your_key_here
`json`
{
"scripts": {
"flags:pull": "flags-cli pull --env REACT_APP_FLAGS_KEY"
}
}
`env`
REACT_APP_FLAGS_KEY=pk_your_key_here
`json`
{
"scripts": {
"flags:pull": "flags-cli pull --env NEXT_PUBLIC_FLAGS_KEY"
}
}
`env`
NEXT_PUBLIC_FLAGS_KEY=pk_your_key_here
`json`
{
"scripts": {
"flags:pull": "flags-cli pull --env VITE_FLAGS_KEY"
}
}
`yaml
name: Update Flag Types
on:
schedule:
- cron: '0 /6 ' # Every 6 hours
workflow_dispatch:
jobs:
update-flags:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/setup-node@v3
with:
node-version: '18'
- run: npm install
- run: npm run flags:pull
env:
VITE_FLAGS_KEY: ${{ secrets.FLAGS_KEY }}
- name: Create Pull Request
uses: peter-evans/create-pull-request@v5
with:
commit-message: 'chore: update flag types'
title: 'Update feature flag types'
branch: update-flags
`
`yaml`
update-flags:
stage: update
script:
- npm install
- npm run flags:pull
artifacts:
paths:
- src/flags.d.ts
only:
- schedules
`json`
{
"husky": {
"hooks": {
"pre-commit": "npm run flags:pull && git add src/flags.d.ts"
}
}
}
Go to flags.hauses.dev and create your flags.
`bash`
npm run flags:pull
`tsx`
const myFlag = useFlags('my-new-flag'); // Autocomplete works!
`bash`
git add src/flags.d.ts
git commit -m "chore: update flag types"
`json`
{
"scripts": {
"prebuild": "npm run flags:pull",
"build": "vite build"
}
}
`bashā
Good
flags-cli pull --env VITE_FLAGS_KEY
$3
If you prefer to generate types locally only:
`gitignore
.gitignore
src/flags.d.ts
`Or commit it for team consistency:
`bash
Keep flags.d.ts in version control
git add src/flags.d.ts
`$3
Automate flag type updates with scheduled workflows.
Troubleshooting
$3
`bash
ā Error: Environment variable VITE_FLAGS_KEY is not set.
`Solution: Make sure the environment variable exists:
`bash
echo $VITE_FLAGS_KEY
`$3
`bash
ā Error: Publishable Key is missing.
`Solution: Provide the key using one of these methods:
`bash
flags-cli pull --key pk_123
flags-cli pull --env FLAGS_KEY
flags-cli pull --key $FLAGS_KEY
`$3
`bash
ā Error generating types: Failed to fetch flags: 401 Unauthorized
`Solution: Check that your publishable key is valid and has the correct permissions.
$3
`bash
Warning: No flags found.
ā
Successfully generated types at ./flags.d.ts
Found 0 flags:
`Solution: Create some flags in your dashboard at flags.hauses.dev.
API
$3
Fetches flags from the API and generates TypeScript definitions.
`bash
flags-cli pull [options]
`Options:
| Option | Alias | Description | Required | Default |
|--------|-------|-------------|----------|---------|
|
--key | -k | Publishable key | Yes* | - |
| --env | -e | Environment variable name | Yes* | - |
| --out | -o | Output file path | No | ./flags.d.ts |\* Either
--key or --env is requiredVersion
Check the CLI version:
`bash
flags-cli --version
`Help
Display help information:
`bash
flags-cli --help
flags-cli pull --help
``- @hauses/flags-core - Core SDK
- @hauses/react-sdk - React SDK
MIT
- š§ Email: jordi.casas004@gmail.com
- š Website: flags.hauses.dev
- š Documentation: flags.hauses.dev/docs
- š Issues: GitHub Issues
---
Part of the Flags SDK monorepo.