Port phase 1 to TypeScript

Move the standalone GameSense scale lighting to TypeScript ahead of the
Max for Live work, so step 3 gets type declarations at the seam.

Build emits CommonJS at ES2020 into dist/, because Node for Max loads CJS
on a Node version we do not control. bin/apex7-scale.js becomes a plain-JS
launcher so `node bin/apex7-scale.js` keeps working; the CLI itself moves
to src/cli.ts.

Two modules make previously implicit structure explicit:

  - src/protocol.ts, the GameSense wire types, so the transport and the
    payload construction agree on shapes neither of them owns
  - src/errors.ts, since `catch (err)` binds `unknown` under strict

Typing surfaced a few real fixes:

  - ScaleLighting.started was a public field the CLI set by hand to make
    --off work without start(); that is now release(), with started
    private behind isStarted
  - readAddress() trusted JSON.parse output; the address is now checked
    for being a non-empty string
  - res.statusCode is number|undefined, so the old `>= 200` comparison
    coerced silently; a missing status now rejects
  - parseIntervals claimed to dedupe and sort, and never did (comment
    corrected; pitchClassesFor is what dedupes)

Behavior is otherwise unchanged: all 18 tests pass and --once, --off,
--demo, --key-test, the resident stdin loop and the error paths were
verified against tools/fake-gamesense.ts.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This commit is contained in:
khannurien
2026-08-12 13:30:53 +00:00
parent 425eb86327
commit 3b02612461
18 changed files with 1550 additions and 837 deletions

View File

@@ -1,37 +1,35 @@
'use strict';
import assert from 'assert';
const assert = require('assert');
const {
parseRoot,
parseScale,
parseIntervals,
pitchClassesFor,
keysFor,
noteName,
import { splitAddress } from '../src/gamesense';
import { ALL_KEYS, HID } from '../src/hid';
import {
BACKGROUND_FRAME_KEY,
BACKGROUND_KEYS,
buildBlackFrame,
buildFrame,
buildHandlers,
parseColor,
} from '../src/lighting';
import {
NOTE_KEYS,
NOTE_KEY_HIDS,
SCALES,
} = require('../src/scale');
const {
parseColor,
buildHandlers,
buildFrame,
buildBlackFrame,
BACKGROUND_KEYS,
BACKGROUND_FRAME_KEY,
} = require('../src/lighting');
const { splitAddress } = require('../src/gamesense');
const { HID, ALL_KEYS } = require('../src/hid');
keysFor,
noteName,
parseIntervals,
parseRoot,
parseScale,
pitchClassesFor,
} from '../src/scale';
let passed = 0;
function test(name, fn) {
function test(name: string, fn: () => void): void {
try {
fn();
passed++;
console.log(` ok ${name}`);
} catch (err) {
console.error(` FAIL ${name}\n ${err.message}`);
console.error(` FAIL ${name}\n ${err instanceof Error ? err.message : err}`);
process.exitCode = 1;
}
}
@@ -68,7 +66,7 @@ test('raw interval lists parse', () => {
test('C major lights the seven white keys', () => {
const pcs = pitchClassesFor(0, SCALES.major);
assert.deepStrictEqual(pcs, [0, 2, 4, 5, 7, 9, 11]);
assert.deepStrictEqual(pcs.map(noteName), ['C', 'D', 'E', 'F', 'G', 'A', 'B']);
assert.deepStrictEqual(pcs.map((pc) => noteName(pc)), ['C', 'D', 'E', 'F', 'G', 'A', 'B']);
// Home row plus the octave key.
assert.deepStrictEqual(keysFor(pcs), ['a', 's', 'd', 'f', 'g', 'h', 'j', 'k']);
});
@@ -76,7 +74,10 @@ test('C major lights the seven white keys', () => {
test('F# major wraps around the octave correctly', () => {
const pcs = pitchClassesFor(6, SCALES.major);
assert.deepStrictEqual(pcs, [1, 3, 6, 8, 10, 11, 5].sort((a, b) => a - b));
assert.deepStrictEqual(pcs.map(noteName).sort(), ['A#', 'B', 'C#', 'D#', 'F', 'F#', 'G#']);
assert.deepStrictEqual(
pcs.map((pc) => noteName(pc)).sort(),
['A#', 'B', 'C#', 'D#', 'F', 'F#', 'G#']
);
});
test('A minor is the same pitch classes as C major', () => {
@@ -107,7 +108,7 @@ test('note keys match the Ableton layout table in the brief', () => {
});
test('HID codes match the USB HID keyboard page', () => {
const expected = {
const expected: Record<string, number> = {
a: 0x04, w: 0x1a, s: 0x16, e: 0x08, d: 0x07, f: 0x09, t: 0x17,
g: 0x0a, y: 0x1c, h: 0x0b, u: 0x18, j: 0x0d, k: 0x0e,
};