@traffical/cli 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (3) hide show
  1. package/README.md +367 -0
  2. package/dist/index.js +23343 -0
  3. package/package.json +48 -0
package/README.md ADDED
@@ -0,0 +1,367 @@
1
+ # @traffical/cli
2
+
3
+ Config-as-code CLI for Traffical - manage your feature flags and experimentation parameters in version-controlled YAML files.
4
+
5
+ ## Installation
6
+
7
+ ```bash
8
+ # Install globally
9
+ npm install -g @traffical/cli
10
+
11
+ # Or use via npx
12
+ npx @traffical/cli init
13
+ ```
14
+
15
+ ## Quick Start
16
+
17
+ ```bash
18
+ # Initialize in your project
19
+ traffical init --api-key <your-api-key>
20
+
21
+ # Push local changes to Traffical
22
+ traffical push
23
+
24
+ # Pull updates from Traffical
25
+ traffical pull
26
+
27
+ # Bidirectional sync
28
+ traffical sync
29
+ ```
30
+
31
+ ## What `init` Creates
32
+
33
+ Running `traffical init` creates a `.traffical/` directory with:
34
+
35
+ ```
36
+ .traffical/
37
+ ├── config.yaml # Main configuration file
38
+ ├── AGENTS.md # AI agent integration guide
39
+ └── templates/ # Framework-specific code templates
40
+ ├── feature-flag.tsx
41
+ ├── ab-test.tsx
42
+ └── server.ts
43
+ ```
44
+
45
+ The CLI automatically detects your framework (React, Next.js, Svelte, SvelteKit, Vue, Nuxt, Node.js) and generates appropriate templates.
46
+
47
+ ## Commands
48
+
49
+ | Command | Description |
50
+ |---------|-------------|
51
+ | `init` | Initialize Traffical in a project, creates `.traffical/` directory |
52
+ | `push` | Push local config to Traffical (validates first) |
53
+ | `pull` | Pull synced parameters from Traffical to local config |
54
+ | `sync` | Bidirectional sync (local wins policy) |
55
+ | `status` | Show current sync status |
56
+ | `import <key>` | Import dashboard parameters (supports wildcards: `ui.*`, `*.enabled`) |
57
+ | `integrate-ai-tools` | Add Traffical references to AI tool config files |
58
+
59
+ ## Sync Behavior
60
+
61
+ The CLI uses a **"local wins"** policy for the `sync` command:
62
+
63
+ 1. **Validates** your local config first (catches errors before any network calls)
64
+ 2. **Pushes** your local changes to Traffical (your edits take precedence)
65
+ 3. **Adds** new parameters from Traffical that you don't have locally
66
+ 4. **Warns** about conflicts (but your local version is used)
67
+
68
+ This matches the Git workflow where your local file is the source of truth. If you want to overwrite local changes with remote values, use `traffical pull` explicitly.
69
+
70
+ ### Example Workflow
71
+
72
+ ```bash
73
+ # Edit config locally
74
+ vim .traffical/config.yaml
75
+
76
+ # Sync: your changes are pushed, new remote params are added
77
+ traffical sync
78
+
79
+ # If you want remote values to overwrite local:
80
+ traffical pull
81
+ ```
82
+
83
+ ## Config File Format
84
+
85
+ ```yaml
86
+ # .traffical/config.yaml
87
+ version: "1.0"
88
+ project:
89
+ id: proj_xxx
90
+ orgId: org_xxx
91
+
92
+ parameters:
93
+ checkout.button.color:
94
+ type: string
95
+ default: "#FF6600"
96
+ namespace: checkout
97
+ description: Primary CTA button color
98
+
99
+ pricing.discount.enabled:
100
+ type: boolean
101
+ default: false
102
+ namespace: pricing
103
+ ```
104
+
105
+ ### Parameter Types
106
+
107
+ | Type | Default Value |
108
+ |------|---------------|
109
+ | `string` | Any string |
110
+ | `number` | Any number |
111
+ | `boolean` | `true` or `false` |
112
+ | `json` | Object or array |
113
+
114
+ ## Validation
115
+
116
+ The CLI validates your config against a JSON Schema before pushing:
117
+
118
+ - Required fields (`version`, `project.id`, `project.orgId`, `parameters`)
119
+ - Type consistency (e.g., `type: boolean` must have a boolean `default`)
120
+ - ID format (`proj_*` and `org_*` prefixes)
121
+
122
+ ```bash
123
+ # Validation happens automatically on push/sync
124
+ traffical push
125
+
126
+ # Example error output
127
+ ✗ Invalid config file
128
+
129
+ Errors:
130
+ - parameters.my_flag.default: must be boolean
131
+ ```
132
+
133
+ ## AI Tool Integration
134
+
135
+ The CLI can automatically add Traffical references to AI coding tool configuration files:
136
+
137
+ ```bash
138
+ # Scan and update AI tool files
139
+ traffical integrate-ai-tools
140
+
141
+ # Or auto-confirm without prompting
142
+ traffical integrate-ai-tools --yes
143
+ ```
144
+
145
+ Supported files:
146
+ - `CLAUDE.md` (Claude Code)
147
+ - `.cursorrules` (Cursor)
148
+ - `.github/copilot-instructions.md` (GitHub Copilot)
149
+ - `.windsurfrules` (Windsurf)
150
+
151
+ This helps AI agents understand that your project uses Traffical and how to properly use feature flags and A/B tests.
152
+
153
+ ## Global Options
154
+
155
+ ```bash
156
+ -p, --profile <name> # Use a specific profile from ~/.trafficalrc
157
+ -c, --config <path> # Path to config file (default: .traffical/config.yaml)
158
+ -b, --api-base <url> # API base URL (for self-hosted instances)
159
+ -j, --format <format> # Output format: human (default) or json
160
+ -n, --dry-run # Validate and preview changes without applying (push/sync)
161
+ ```
162
+
163
+ ## JSON Output
164
+
165
+ For scripting and CI/CD integration, use `--format json` to get machine-readable output:
166
+
167
+ ```bash
168
+ # Get status as JSON
169
+ traffical status --format json
170
+
171
+ # Example output
172
+ {
173
+ "project": { "id": "proj_xxx", "name": "My Project" },
174
+ "org": { "id": "org_xxx", "name": "My Org" },
175
+ "synced": [{ "key": "feature.enabled", "type": "boolean" }],
176
+ "dashboardOnly": [],
177
+ "localOnly": [],
178
+ "hasDrift": false
179
+ }
180
+ ```
181
+
182
+ ## Environment Variables
183
+
184
+ For CI/CD pipelines, credentials can be provided via environment variables:
185
+
186
+ | Variable | Description |
187
+ |----------|-------------|
188
+ | `TRAFFICAL_API_KEY` | API key for authentication |
189
+ | `TRAFFICAL_API_BASE` | API base URL (optional, for self-hosted) |
190
+
191
+ **Priority order** (highest to lowest):
192
+ 1. Command-line flags (`--api-key`, `--api-base`)
193
+ 2. Environment variables (`TRAFFICAL_API_KEY`, `TRAFFICAL_API_BASE`)
194
+ 3. Profile from `~/.trafficalrc`
195
+
196
+ ## Exit Codes
197
+
198
+ For scripting and CI/CD integration:
199
+
200
+ | Code | Meaning |
201
+ |------|---------|
202
+ | `0` | Success |
203
+ | `1` | Validation error (invalid config file) |
204
+ | `2` | Authentication error (invalid or missing API key) |
205
+ | `3` | Network/API error |
206
+ | `10` | Config drift detected (status command) |
207
+ | `11` | Experiment needs attention |
208
+
209
+ ## Profiles
210
+
211
+ API keys are stored in `~/.trafficalrc`:
212
+
213
+ ```yaml
214
+ default_profile: default
215
+ profiles:
216
+ default:
217
+ api_key: tk_xxx
218
+ staging:
219
+ api_key: tk_yyy
220
+ api_base: https://staging.traffical.io
221
+ ```
222
+
223
+ Use with: `traffical push --profile staging`
224
+
225
+ ## CI/CD Integration
226
+
227
+ The CLI is designed for CI/CD pipelines. Use environment variables for credentials and `--dry-run` for validation.
228
+
229
+ ### Sync on Merge to Main
230
+
231
+ **Use case:** Automatically push parameter changes to Traffical when code is merged to main. This ensures your production parameters stay in sync with your codebase.
232
+
233
+ ```yaml
234
+ # .github/workflows/traffical-sync.yml
235
+ name: Sync Traffical Config
236
+
237
+ on:
238
+ push:
239
+ branches: [main]
240
+ paths:
241
+ - '.traffical/**'
242
+
243
+ jobs:
244
+ sync:
245
+ runs-on: ubuntu-latest
246
+ steps:
247
+ - uses: actions/checkout@v4
248
+
249
+ - name: Install Traffical CLI
250
+ run: npm install -g @traffical/cli
251
+
252
+ - name: Push to Traffical
253
+ run: traffical push
254
+ env:
255
+ TRAFFICAL_API_KEY: ${{ secrets.TRAFFICAL_API_KEY }}
256
+ ```
257
+
258
+ ### Validate on Pull Request
259
+
260
+ **Use case:** Catch configuration errors before they're merged. The `--dry-run` flag validates the config and shows what would change without actually modifying anything.
261
+
262
+ ```yaml
263
+ # .github/workflows/traffical-validate.yml
264
+ name: Validate Traffical Config
265
+
266
+ on:
267
+ pull_request:
268
+ paths:
269
+ - '.traffical/**'
270
+
271
+ jobs:
272
+ validate:
273
+ runs-on: ubuntu-latest
274
+ steps:
275
+ - uses: actions/checkout@v4
276
+
277
+ - name: Install Traffical CLI
278
+ run: npm install -g @traffical/cli
279
+
280
+ - name: Validate Config (Dry Run)
281
+ run: traffical push --dry-run
282
+ env:
283
+ TRAFFICAL_API_KEY: ${{ secrets.TRAFFICAL_API_KEY }}
284
+ ```
285
+
286
+ ### Drift Detection with JSON Output
287
+
288
+ **Use case:** Detect when someone changes parameters directly in the Traffical dashboard without updating the config file. Use JSON output for easier parsing.
289
+
290
+ ```yaml
291
+ # .github/workflows/traffical-drift.yml
292
+ name: Check Traffical Drift
293
+
294
+ on:
295
+ schedule:
296
+ - cron: '0 9 * * *' # Daily at 9am UTC
297
+
298
+ jobs:
299
+ check-drift:
300
+ runs-on: ubuntu-latest
301
+ steps:
302
+ - uses: actions/checkout@v4
303
+
304
+ - name: Install Traffical CLI
305
+ run: npm install -g @traffical/cli
306
+
307
+ - name: Check for Drift
308
+ id: status
309
+ run: |
310
+ STATUS=$(traffical status --format json)
311
+ echo "$STATUS"
312
+ DRIFT=$(echo "$STATUS" | jq '.hasDrift')
313
+ echo "drift=$DRIFT" >> $GITHUB_OUTPUT
314
+ env:
315
+ TRAFFICAL_API_KEY: ${{ secrets.TRAFFICAL_API_KEY }}
316
+
317
+ - name: Create Issue on Drift
318
+ if: steps.status.outputs.drift == 'true'
319
+ uses: actions/github-script@v7
320
+ with:
321
+ script: |
322
+ github.rest.issues.create({
323
+ owner: context.repo.owner,
324
+ repo: context.repo.repo,
325
+ title: '⚠️ Traffical config drift detected',
326
+ body: 'Parameters exist locally that are not synced to Traffical.\n\nRun `traffical status` locally to see details, then run `traffical push` to sync.'
327
+ })
328
+ ```
329
+
330
+ ### Environment-Specific Deploys
331
+
332
+ **Use case:** Deploy different parameter configurations to staging and production environments, each with their own Traffical project.
333
+
334
+ ```yaml
335
+ # .github/workflows/deploy.yml
336
+ name: Deploy
337
+
338
+ on:
339
+ push:
340
+ branches: [main, staging]
341
+
342
+ jobs:
343
+ sync-traffical:
344
+ runs-on: ubuntu-latest
345
+ steps:
346
+ - uses: actions/checkout@v4
347
+
348
+ - name: Install Traffical CLI
349
+ run: npm install -g @traffical/cli
350
+
351
+ - name: Sync to Staging
352
+ if: github.ref == 'refs/heads/staging'
353
+ run: traffical push
354
+ env:
355
+ TRAFFICAL_API_KEY: ${{ secrets.TRAFFICAL_API_KEY_STAGING }}
356
+
357
+ - name: Sync to Production
358
+ if: github.ref == 'refs/heads/main'
359
+ run: traffical push
360
+ env:
361
+ TRAFFICAL_API_KEY: ${{ secrets.TRAFFICAL_API_KEY_PROD }}
362
+ ```
363
+
364
+ ## Learn More
365
+
366
+ - [Config-as-Code Documentation](https://docs.traffical.io/config-as-code)
367
+ - [Parameter Schema Reference](https://docs.traffical.io/config-as-code/schema)