|
| 1 | +# CLI Output Management System |
| 2 | + |
| 3 | +This system automatically captures and caches CLI command outputs in MDX files, converting them to SVG format for consistent rendering. |
| 4 | + |
| 5 | +## Setup |
| 6 | + |
| 7 | +1. Configure `ansi-run.json` with your example project path and starting commit hash: |
| 8 | + |
| 9 | +```json |
| 10 | +{ |
| 11 | + "exampleProjectPath": "/path/to/example/project", |
| 12 | + "startingHash": "initial-commit-hash", |
| 13 | + "config": { |
| 14 | + "cacheDir": "content/cache", |
| 15 | + "outputFormat": "svg" |
| 16 | + } |
| 17 | +} |
| 18 | +``` |
| 19 | + |
| 20 | +2. Install dependencies: |
| 21 | +```bash |
| 22 | +pnpm install |
| 23 | +``` |
| 24 | + |
| 25 | +## Usage |
| 26 | + |
| 27 | +### Adding CLI Commands to MDX |
| 28 | + |
| 29 | +Use special `cli` code blocks in your MDX files: |
| 30 | + |
| 31 | +```mdx |
| 32 | +Here's how to check status: |
| 33 | + |
| 34 | +```cli |
| 35 | +but status |
| 36 | +``` |
| 37 | + |
| 38 | +The output will be automatically captured and cached. |
| 39 | +``` |
| 40 | + |
| 41 | +### Restore Commands |
| 42 | + |
| 43 | +To restore to a specific state before running commands, add a restore comment: |
| 44 | + |
| 45 | +```mdx |
| 46 | +{/* restore [commit-hash] */} |
| 47 | + |
| 48 | +```cli |
| 49 | +but status |
| 50 | +``` |
| 51 | + |
| 52 | +This will run `but restore [commit-hash]` before executing the cli command. |
| 53 | +``` |
| 54 | + |
| 55 | +### Updating CLI Outputs |
| 56 | + |
| 57 | +Run the update script to process all MDX files and update CLI outputs: |
| 58 | + |
| 59 | +```bash |
| 60 | +pnpm update-cli |
| 61 | +``` |
| 62 | + |
| 63 | +This will: |
| 64 | +- Read your `ansi-run.json` configuration |
| 65 | +- Change to your example project directory |
| 66 | +- Restore to the starting hash |
| 67 | +- Process all MDX files in `content/docs/` |
| 68 | +- Execute CLI commands and capture outputs |
| 69 | +- Convert outputs to SVG using ansi2html |
| 70 | +- Cache outputs in `content/cache/[hash].svg` |
| 71 | +- Update MDX files with hash references: ```cli [hash] |
| 72 | +- Report any changes detected |
| 73 | + |
| 74 | +### How It Works |
| 75 | + |
| 76 | +1. **Processing**: The script finds all ````cli` blocks in MDX files |
| 77 | +2. **Execution**: Commands are run in your configured example project |
| 78 | +3. **Caching**: Output is converted to SVG and stored with a content hash |
| 79 | +4. **Updates**: MDX blocks are updated with hash references |
| 80 | +5. **Rendering**: The CliBlock component renders cached SVGs or shows placeholders |
| 81 | + |
| 82 | +### File Structure |
| 83 | + |
| 84 | +``` |
| 85 | +├── ansi-run.json # Configuration |
| 86 | +├── content/ |
| 87 | +│ ├── cache/ # Cached SVG outputs |
| 88 | +│ │ ├── abc123def456.svg |
| 89 | +│ │ └── def789ghi012.svg |
| 90 | +│ └── docs/ # MDX documentation files |
| 91 | +│ └── commands/ |
| 92 | +│ └── status.mdx |
| 93 | +├── scripts/ |
| 94 | +│ └── update-cli-outputs.js # Main processing script |
| 95 | +└── app/ |
| 96 | + └── components/ |
| 97 | + ├── CliBlock.tsx # Rendering component |
| 98 | + └── remark-cli.ts # MDX transformer |
| 99 | +``` |
| 100 | + |
| 101 | +### Example Workflow |
| 102 | + |
| 103 | +1. Create a new MDX file with CLI commands: |
| 104 | +```mdx |
| 105 | +# Status Command |
| 106 | + |
| 107 | +Check your workspace status: |
| 108 | + |
| 109 | +```cli |
| 110 | +but status |
| 111 | +``` |
| 112 | +``` |
| 113 | + |
| 114 | +2. Run the update script: |
| 115 | +```bash |
| 116 | +pnpm update-cli |
| 117 | +``` |
| 118 | + |
| 119 | +3. The script will show output like: |
| 120 | +``` |
| 121 | +Processing: content/docs/commands/status.mdx |
| 122 | +Found CLI command: but status |
| 123 | +New CLI block found: but status |
| 124 | +Updated: content/docs/commands/status.mdx |
| 125 | +``` |
| 126 | + |
| 127 | +4. Your MDX file is now updated: |
| 128 | +```mdx |
| 129 | +# Status Command |
| 130 | + |
| 131 | +Check your workspace status: |
| 132 | + |
| 133 | +```cli [abc123def456] |
| 134 | +but status |
| 135 | +``` |
| 136 | +``` |
| 137 | + |
| 138 | +5. When rendered, users see the actual command output in SVG format. |
| 139 | + |
| 140 | +## Troubleshooting |
| 141 | + |
| 142 | +- **Missing outputs**: Run `pnpm update-cli` to generate missing cache files |
| 143 | +- **Outdated outputs**: The script will detect hash changes and notify you |
| 144 | +- **Command failures**: Failed commands will still be cached to show error output |
| 145 | +- **Path issues**: Ensure your `ansi-run.json` paths are absolute and correct |
| 146 | + |
| 147 | +### Updating CLI Outputs |
| 148 | + |
| 149 | +Run the update script to process all MDX files and update CLI outputs: |
| 150 | + |
| 151 | +```bash |
| 152 | +export CLICOLOR_FORCE=1 |
| 153 | +export GIT_AUTHOR_DATE="2020-09-09 09:06:03 +0800" |
| 154 | +export GIT_COMMITTER_DATE="2020-10-09 09:06:03 +0800" |
| 155 | +pnpm update-cli |
| 156 | +``` |
| 157 | + |
| 158 | +## Commands |
| 159 | + |
| 160 | +The command "man pages" are copied from `../gitbutler/cli-docs` so that changes to the commands docs can be included in changes with the code. |
| 161 | + |
| 162 | +To update the command man-pages, you can run ./scripts/sync-commands.sh |
| 163 | + |
| 164 | + |
0 commit comments