-
Notifications
You must be signed in to change notification settings - Fork 91
feat(console): render arduino-cli colour and collapse progress redraws #1002
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
5b878d3
feat(console): render arduino-cli colour and collapse progress redraws
thiagoralves 2dfbb55
fix(console): keep a progress redraw on one visual line
thiagoralves 38f0f78
Revert "fix(console): keep a progress redraw on one visual line"
thiagoralves d301457
Merge development into feat/console-terminal-output
thiagoralves 1f06b1f
fix(review): CRLF handling, escape stripping, YAML guards, atomic write
thiagoralves File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
159 changes: 159 additions & 0 deletions
159
src/backend/editor/services/user-service/data/__tests__/arduino-cli-config.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,159 @@ | ||
| import { parse } from 'yaml' | ||
|
|
||
| import { reconcileArduinoCliConfig } from '../arduino-cli-config' | ||
| import { ARDUINO_DATA } from '../types' | ||
|
|
||
| /** | ||
| * The config every install created before this change: two board-manager URLs | ||
| * and the colour suppression the console no longer needs. | ||
| */ | ||
| const LEGACY_CONFIG = ` | ||
| board_manager: | ||
| additional_urls: | ||
| - https://arduino.esp8266.com/stable/package_esp8266com_index.json | ||
| - https://espressif.github.io/arduino-esp32/package_esp32_index.json | ||
| output: | ||
| no_color: true | ||
| ` | ||
|
|
||
| function isRecord(value: unknown): value is Record<string, unknown> { | ||
| return typeof value === 'object' && value !== null && !Array.isArray(value) | ||
| } | ||
|
|
||
| /** Board-manager URLs in a YAML document, without asserting its shape. */ | ||
| function urlsOf(yaml: string): string[] { | ||
| const parsed: unknown = parse(yaml) | ||
| if (!isRecord(parsed) || !isRecord(parsed.board_manager)) return [] | ||
| const urls = parsed.board_manager.additional_urls | ||
| return Array.isArray(urls) ? urls.filter((url): url is string => typeof url === 'string') : [] | ||
| } | ||
|
|
||
| /** | ||
| * The reconciled config, failing the test if nothing changed. | ||
| * | ||
| * `reconcileArduinoCliConfig` returns `null` for "already up to date", which | ||
| * is a real outcome worth asserting on separately -- so a test that expects a | ||
| * rewrite says so here rather than casting the null away and failing later | ||
| * with a confusing message. | ||
| */ | ||
| function requireUpdated(result: string | null): string { | ||
| if (result === null) throw new Error('expected the config to be rewritten, but it needed no changes') | ||
| return result | ||
| } | ||
|
|
||
| describe('reconcileArduinoCliConfig', () => { | ||
| // --------------------------------------------------------------------- | ||
| // The upgrade path that matters: an install that already has no_color. | ||
| // --------------------------------------------------------------------- | ||
| it('drops output.no_color from a legacy config', () => { | ||
| const updated = reconcileArduinoCliConfig(LEGACY_CONFIG, ARDUINO_DATA) | ||
| expect(updated).not.toBeNull() | ||
| expect(updated).not.toContain('no_color') | ||
| // The whole `output` map existed only to hold it. | ||
| expect(updated).not.toContain('output:') | ||
| }) | ||
|
|
||
| it('backfills the board manager URLs the legacy config never received', () => { | ||
| const updated = reconcileArduinoCliConfig(LEGACY_CONFIG, ARDUINO_DATA) | ||
| const result = urlsOf(requireUpdated(updated)) | ||
| for (const url of urlsOf(ARDUINO_DATA)) expect(result).toContain(url) | ||
| }) | ||
|
|
||
| it('produces a config that still parses as valid YAML', () => { | ||
| const updated = requireUpdated(reconcileArduinoCliConfig(LEGACY_CONFIG, ARDUINO_DATA)) | ||
| expect(() => parse(updated)).not.toThrow() | ||
| expect(parse(updated)).toMatchObject({ board_manager: { additional_urls: expect.any(Array) } }) | ||
| }) | ||
|
|
||
| it('is idempotent — a second pass reports nothing left to do', () => { | ||
| const once = requireUpdated(reconcileArduinoCliConfig(LEGACY_CONFIG, ARDUINO_DATA)) | ||
| expect(reconcileArduinoCliConfig(once, ARDUINO_DATA)).toBeNull() | ||
| }) | ||
|
|
||
| // --------------------------------------------------------------------- | ||
| // Don't destroy what the user put there. | ||
| // --------------------------------------------------------------------- | ||
| it('keeps user-added URLs that the editor does not ship', () => { | ||
| const withCustom = ` | ||
| board_manager: | ||
| additional_urls: | ||
| - https://arduino.esp8266.com/stable/package_esp8266com_index.json | ||
| - https://example.com/package_mine_index.json | ||
| output: | ||
| no_color: true | ||
| ` | ||
| const updated = requireUpdated(reconcileArduinoCliConfig(withCustom, ARDUINO_DATA)) | ||
| const result = urlsOf(updated) | ||
| expect(result).toContain('https://example.com/package_mine_index.json') | ||
| // ...and the shipped ones still get added alongside it. | ||
| for (const url of urlsOf(ARDUINO_DATA)) expect(result).toContain(url) | ||
| }) | ||
|
|
||
| it('preserves unrelated settings and comments', () => { | ||
| const withExtras = `# my notes\nlogging:\n level: debug\n${LEGACY_CONFIG}` | ||
| const updated = requireUpdated(reconcileArduinoCliConfig(withExtras, ARDUINO_DATA)) | ||
| expect(updated).toContain('# my notes') | ||
| expect(parse(updated)).toMatchObject({ logging: { level: 'debug' } }) | ||
| }) | ||
|
|
||
| it('keeps an output map that still holds other keys', () => { | ||
| const withOtherOutput = `board_manager:\n additional_urls: []\noutput:\n no_color: true\n format: json\n` | ||
| const updated = requireUpdated(reconcileArduinoCliConfig(withOtherOutput, ARDUINO_DATA)) | ||
| expect(updated).not.toContain('no_color') | ||
| expect(parse(updated)).toMatchObject({ output: { format: 'json' } }) | ||
| }) | ||
|
|
||
| // --------------------------------------------------------------------- | ||
| // No-op and failure cases. | ||
| // --------------------------------------------------------------------- | ||
| it('returns null when the file already matches what we ship', () => { | ||
| expect(reconcileArduinoCliConfig(ARDUINO_DATA, ARDUINO_DATA)).toBeNull() | ||
| }) | ||
|
|
||
| it('installs the shipped URL list when the key is missing entirely', () => { | ||
| const updated = requireUpdated(reconcileArduinoCliConfig('output:\n no_color: true\n', ARDUINO_DATA)) | ||
| expect(urlsOf(updated)).toEqual(urlsOf(ARDUINO_DATA)) | ||
| }) | ||
|
|
||
| // A hand-edited config can be parseable but structurally odd. yaml's nested | ||
| // `*In()` helpers throw on a scalar parent ("Expected YAML collection at | ||
| // board_manager"), which would abort the whole reconciliation and silently | ||
| // leave the user un-migrated. | ||
| it('does not throw when board_manager is a scalar', () => { | ||
| expect(() => reconcileArduinoCliConfig('board_manager: 5\n', ARDUINO_DATA)).not.toThrow() | ||
| }) | ||
|
|
||
| it('does not throw when output is a scalar, and still backfills URLs', () => { | ||
| const updated = reconcileArduinoCliConfig('output: "text"\n', ARDUINO_DATA) | ||
| expect(updated).not.toBeNull() | ||
| for (const url of urlsOf(ARDUINO_DATA)) expect(urlsOf(requireUpdated(updated))).toContain(url) | ||
| }) | ||
|
|
||
| it('leaves a scalar board_manager untouched rather than guessing', () => { | ||
| // Nothing safe to merge into a scalar: leave it for the user to fix. | ||
| const updated = reconcileArduinoCliConfig('board_manager: 5\noutput:\n no_color: true\n', ARDUINO_DATA) | ||
| // The no_color retirement still happens; the URL backfill is skipped. | ||
| expect(updated).not.toContain('no_color') | ||
| expect(updated).toContain('board_manager: 5') | ||
| }) | ||
|
|
||
| it('does not throw when additional_urls is a scalar', () => { | ||
| expect(() => reconcileArduinoCliConfig('board_manager:\n additional_urls: 7\n', ARDUINO_DATA)).not.toThrow() | ||
| }) | ||
|
|
||
| it('leaves an unparseable config alone rather than clobbering it', () => { | ||
| expect(reconcileArduinoCliConfig('board_manager: [oops\n : :\n', ARDUINO_DATA)).toBeNull() | ||
| }) | ||
| }) | ||
|
|
||
| describe('ARDUINO_DATA', () => { | ||
| it('no longer ships the obsolete colour suppression', () => { | ||
| // The console renders SGR colour now; forcing it off would make the | ||
| // renderer dead code on every fresh install. | ||
| expect(ARDUINO_DATA).not.toContain('no_color') | ||
| }) | ||
|
|
||
| it('is valid YAML with a non-empty board manager list', () => { | ||
| expect(urlsOf(ARDUINO_DATA).length).toBeGreaterThan(0) | ||
| }) | ||
| }) |
92 changes: 92 additions & 0 deletions
92
src/backend/editor/services/user-service/data/arduino-cli-config.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,92 @@ | ||
| /** | ||
| * Reconcile an existing `arduino-cli.yaml` with the one the editor ships. | ||
| * | ||
| * The config used to be written once with `{ flag: 'wx' }` and skipped | ||
| * forever after, so anything added to `ARDUINO_DATA` later never reached an | ||
| * existing install — the only fix was deleting the file by hand. This brings | ||
| * a stale file up to date in place. | ||
| * | ||
| * Two rules, and deliberately only two: | ||
| * | ||
| * - **Add missing board-manager URLs.** Never remove one: users add their own | ||
| * vendor indexes here, and VPP-declared indexes arrive at compile time. | ||
| * - **Drop `output.no_color`.** The editor forced it on to stop raw `ESC[92m` | ||
| * bytes appearing in the console. The console now renders SGR colour | ||
| * itself, so the suppression is obsolete; leaving it behind would silently | ||
| * keep colour off on every machine that has ever launched an older build. | ||
| * | ||
| * Everything else is left exactly as the user left it, comments and ordering | ||
| * included — hence the `Document` API for the existing file rather than a | ||
| * parse/serialise round-trip through plain objects. | ||
| */ | ||
|
|
||
| import { isMap, isSeq, parse, parseDocument } from 'yaml' | ||
|
|
||
| const BOARD_MANAGER_URLS_PATH = ['board_manager', 'additional_urls'] | ||
|
|
||
| function isRecord(value: unknown): value is Record<string, unknown> { | ||
| return typeof value === 'object' && value !== null && !Array.isArray(value) | ||
| } | ||
|
|
||
| /** Board-manager URLs declared by the shipped template (which we author). */ | ||
| function shippedBoardManagerUrls(shipped: string): string[] { | ||
| const parsed: unknown = parse(shipped) | ||
| if (!isRecord(parsed)) return [] | ||
|
|
||
| const boardManager = parsed.board_manager | ||
| if (!isRecord(boardManager)) return [] | ||
|
|
||
| const urls = boardManager.additional_urls | ||
| return Array.isArray(urls) ? urls.filter((url): url is string => typeof url === 'string') : [] | ||
| } | ||
|
|
||
| /** | ||
| * Return the updated file contents, or `null` when nothing needed changing. | ||
| * | ||
| * Also returns `null` when `existing` is unparseable — a broken config is the | ||
| * user's to fix, and rewriting it would discard whatever they were editing. | ||
| */ | ||
| export function reconcileArduinoCliConfig(existing: string, shipped: string): string | null { | ||
| const doc = parseDocument(existing) | ||
| if (doc.errors.length > 0) return null | ||
|
|
||
| let changed = false | ||
|
|
||
| // Nested `*In()` calls walk the tree and throw ("Expected YAML collection | ||
| // at board_manager") if a parent turns out to be a scalar. A hand-edited | ||
| // config can absolutely be parseable-but-odd, and throwing here would abort | ||
| // the whole reconciliation — leaving the user with no migration and only a | ||
| // console error to explain it. So fetch each parent and check it first. | ||
| const boardManager = doc.get('board_manager') | ||
| const output = doc.get('output') | ||
|
|
||
| // 1. Board-manager URLs — union, never subtract. | ||
| const shippedUrls = shippedBoardManagerUrls(shipped) | ||
| if (shippedUrls.length > 0 && (boardManager === undefined || boardManager === null || isMap(boardManager))) { | ||
| const current = isMap(boardManager) ? boardManager.get('additional_urls') : undefined | ||
| const present = new Set<string>(isSeq(current) ? current.toJSON().map(String) : []) | ||
| const missing = shippedUrls.filter((url) => !present.has(url)) | ||
|
|
||
| if (missing.length > 0) { | ||
| if (isSeq(current)) { | ||
| for (const url of missing) current.add(url) | ||
| } else { | ||
| // No `additional_urls` key, or it is not a list — install the shipped | ||
| // set wholesale rather than guessing at a merge. | ||
| doc.setIn(BOARD_MANAGER_URLS_PATH, shippedUrls) | ||
| } | ||
| changed = true | ||
| } | ||
| } | ||
|
|
||
| // 2. Retire the obsolete colour suppression. | ||
| if (isMap(output) && output.has('no_color')) { | ||
| output.delete('no_color') | ||
| changed = true | ||
|
|
||
| // Don't leave an empty `output:` behind once its only key is gone. | ||
| if (output.items.length === 0) doc.delete('output') | ||
| } | ||
|
|
||
| return changed ? String(doc) : null | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.