|
| 1 | +#!/usr/bin/env dart |
| 2 | +/* |
| 3 | + * Copyright 2025 LiveKit |
| 4 | + * |
| 5 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | + * you may not use this file except in compliance with the License. |
| 7 | + * You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | + |
| 18 | +import 'dart:io'; |
| 19 | + |
| 20 | +/// Creates a new change entry in the .changes directory. |
| 21 | +/// |
| 22 | +/// Usage: |
| 23 | +/// dart scripts/create_change.dart |
| 24 | +/// dart scripts/create_change.dart --level patch --type fixed --name my-fix --description "Fix something" |
| 25 | +/// |
| 26 | +/// Interactive mode (no args) will prompt for each field with arrow-key selection. |
| 27 | +/// CLI mode allows passing all fields as arguments. |
| 28 | +/// |
| 29 | +/// Change file format: |
| 30 | +/// level type="kind" "description" |
| 31 | +/// |
| 32 | +/// Examples: |
| 33 | +/// patch type="fixed" "Fix audio frame generation when publishing" |
| 34 | +/// minor type="added" "Add support for custom audio processing" |
| 35 | +/// major type="changed" "Breaking: Rename Room.connect() to Room.join()" |
| 36 | +
|
| 37 | +// ANSI escape codes |
| 38 | +const _esc = '\x1B['; |
| 39 | +const _reset = '\x1B[0m'; |
| 40 | +const _bold = '\x1B[1m'; |
| 41 | +const _dim = '\x1B[2m'; |
| 42 | +const _green = '\x1B[32m'; |
| 43 | +const _cyan = '\x1B[36m'; |
| 44 | +const _hideCursor = '\x1B[?25l'; |
| 45 | +const _showCursor = '\x1B[?25h'; |
| 46 | + |
| 47 | +const levels = ['patch', 'minor', 'major']; |
| 48 | +const levelDescriptions = { |
| 49 | + 'patch': 'Bug fixes, no API changes', |
| 50 | + 'minor': 'New features, backwards compatible', |
| 51 | + 'major': 'Breaking changes', |
| 52 | +}; |
| 53 | + |
| 54 | +const types = [ |
| 55 | + 'added', |
| 56 | + 'changed', |
| 57 | + 'fixed', |
| 58 | + 'refactor', |
| 59 | + 'performance', |
| 60 | + 'security', |
| 61 | + 'deprecated', |
| 62 | + 'removed', |
| 63 | + 'docs', |
| 64 | +]; |
| 65 | + |
| 66 | +/// Read a single raw keypress (handles arrow keys as 3-byte escape sequences). |
| 67 | +List<int> readKey() { |
| 68 | + stdin.echoMode = false; |
| 69 | + stdin.lineMode = false; |
| 70 | + try { |
| 71 | + final first = stdin.readByteSync(); |
| 72 | + if (first == 27) { |
| 73 | + // Escape sequence |
| 74 | + final second = stdin.readByteSync(); |
| 75 | + if (second == 91) { |
| 76 | + final third = stdin.readByteSync(); |
| 77 | + return [27, 91, third]; |
| 78 | + } |
| 79 | + return [27, second]; |
| 80 | + } |
| 81 | + return [first]; |
| 82 | + } finally { |
| 83 | + stdin.lineMode = true; |
| 84 | + stdin.echoMode = true; |
| 85 | + } |
| 86 | +} |
| 87 | + |
| 88 | +/// Interactive picker with arrow keys. Returns selected index. |
| 89 | +String pick(String label, List<String> options, {Map<String, String>? descriptions}) { |
| 90 | + var selected = 0; |
| 91 | + |
| 92 | + void render() { |
| 93 | + stdout.write(_hideCursor); |
| 94 | + for (var i = 0; i < options.length; i++) { |
| 95 | + stdout.write('${_esc}2K'); // Clear entire line |
| 96 | + if (i == selected) { |
| 97 | + final desc = descriptions?[options[i]]; |
| 98 | + final suffix = desc != null ? ' $_dim- $desc$_reset' : ''; |
| 99 | + stdout.writeln(' $_cyan>$_reset $_bold${options[i]}$_reset$suffix'); |
| 100 | + } else { |
| 101 | + stdout.writeln(' ${options[i]}'); |
| 102 | + } |
| 103 | + } |
| 104 | + } |
| 105 | + |
| 106 | + stdout.writeln('$_bold$_green?$_reset $_bold$label$_reset ${_dim}(arrow keys, enter to confirm)$_reset'); |
| 107 | + render(); |
| 108 | + |
| 109 | + while (true) { |
| 110 | + final key = readKey(); |
| 111 | + |
| 112 | + if (key.length == 3 && key[0] == 27 && key[1] == 91) { |
| 113 | + if (key[2] == 65) { |
| 114 | + // Up arrow |
| 115 | + selected = (selected - 1) % options.length; |
| 116 | + if (selected < 0) selected = options.length - 1; |
| 117 | + } else if (key[2] == 66) { |
| 118 | + // Down arrow |
| 119 | + selected = (selected + 1) % options.length; |
| 120 | + } |
| 121 | + } else if (key.length == 1 && (key[0] == 10 || key[0] == 13)) { |
| 122 | + // Enter |
| 123 | + stdout.write(_showCursor); |
| 124 | + // Clear the list and show selection |
| 125 | + stdout.write('${_esc}${options.length}A'); // Move up |
| 126 | + stdout.write('${_esc}0J'); // Clear from cursor down |
| 127 | + stdout.writeln(' $_green>${_reset} $_bold${options[selected]}$_reset'); |
| 128 | + return options[selected]; |
| 129 | + } else if (key.length == 1 && (key[0] == 3 || key[0] == 27)) { |
| 130 | + // Ctrl+C or Escape |
| 131 | + stdout.write(_showCursor); |
| 132 | + exit(0); |
| 133 | + } |
| 134 | + |
| 135 | + // Redraw |
| 136 | + stdout.write('${_esc}${options.length}A'); // Move up to start of list |
| 137 | + render(); |
| 138 | + } |
| 139 | +} |
| 140 | + |
| 141 | +/// Interactive text input with prompt. |
| 142 | +String input(String label) { |
| 143 | + stdout.write('$_bold$_green?$_reset $_bold$label$_reset '); |
| 144 | + final value = stdin.readLineSync()?.trim() ?? ''; |
| 145 | + if (value.isEmpty) { |
| 146 | + stderr.writeln('Input required.'); |
| 147 | + exit(1); |
| 148 | + } |
| 149 | + return value; |
| 150 | +} |
| 151 | + |
| 152 | +String slugify(String text) { |
| 153 | + return text.toLowerCase().replaceAll(RegExp(r'[^a-z0-9]+'), '-').replaceAll(RegExp(r'^-|-$'), ''); |
| 154 | +} |
| 155 | + |
| 156 | +void main(List<String> args) { |
| 157 | + final changesDir = Directory('.changes'); |
| 158 | + if (!changesDir.existsSync()) { |
| 159 | + changesDir.createSync(); |
| 160 | + } |
| 161 | + |
| 162 | + String? level; |
| 163 | + String? type; |
| 164 | + String? name; |
| 165 | + String? description; |
| 166 | + |
| 167 | + // Parse CLI args |
| 168 | + for (var i = 0; i < args.length; i++) { |
| 169 | + switch (args[i]) { |
| 170 | + case '--level': |
| 171 | + case '-l': |
| 172 | + level = args[++i]; |
| 173 | + case '--type': |
| 174 | + case '-t': |
| 175 | + type = args[++i]; |
| 176 | + case '--name': |
| 177 | + case '-n': |
| 178 | + name = args[++i]; |
| 179 | + case '--description': |
| 180 | + case '-d': |
| 181 | + description = args[++i]; |
| 182 | + case '--help': |
| 183 | + case '-h': |
| 184 | + stdout.writeln('Usage: dart scripts/create_change.dart [options]'); |
| 185 | + stdout.writeln(); |
| 186 | + stdout.writeln('Options:'); |
| 187 | + stdout.writeln(' -l, --level Version bump level (${levels.join(', ')})'); |
| 188 | + stdout.writeln(' -t, --type Change type (${types.join(', ')})'); |
| 189 | + stdout.writeln(' -n, --name File name slug (e.g. fix-svc-dynacast)'); |
| 190 | + stdout.writeln(' -d, --description Change description'); |
| 191 | + stdout.writeln(' -h, --help Show this help'); |
| 192 | + exit(0); |
| 193 | + } |
| 194 | + } |
| 195 | + |
| 196 | + // Interactive prompts for missing fields |
| 197 | + level ??= pick('Level:', levels, descriptions: levelDescriptions); |
| 198 | + type ??= pick('Type:', types); |
| 199 | + name ??= slugify(input('Name (slug):')); |
| 200 | + description ??= input('Description:'); |
| 201 | + |
| 202 | + // Validate |
| 203 | + if (!levels.contains(level)) { |
| 204 | + stderr.writeln('Invalid level: $level'); |
| 205 | + exit(1); |
| 206 | + } |
| 207 | + if (!types.contains(type)) { |
| 208 | + stderr.writeln('Invalid type: $type'); |
| 209 | + exit(1); |
| 210 | + } |
| 211 | + |
| 212 | + final content = '$level type="$type" "$description"\n'; |
| 213 | + final file = File('.changes/$name'); |
| 214 | + |
| 215 | + if (file.existsSync()) { |
| 216 | + stderr.writeln('Change file already exists: .changes/$name'); |
| 217 | + exit(1); |
| 218 | + } |
| 219 | + |
| 220 | + file.writeAsStringSync(content); |
| 221 | + stdout.writeln(); |
| 222 | + stdout.writeln('$_green\u2713$_reset Created $_bold.changes/$name$_reset'); |
| 223 | + stdout.writeln(' ${_dim}${content.trim()}$_reset'); |
| 224 | +} |
0 commit comments