Self-hosted, Self-Improving, Federated Everything App — dev management, agent orchestration, persistent mind, digital twin, second brain, and creative pipeline from a single dashboard over Tailscale to port the user into the OS
JavaScript ai-agentsdashboarddeveloper-toolsdigital-twinquantified-selfsecond-brainself-hostedtailscale
refactor([extract-layout-doc-io-helper-from-migrations]): extract readLayoutsDoc/writeLayoutsDoc helper
Adam Eivy9035e2dc0c9e
diff --git a/scripts/migrations/029-seed-network-exposure-widget.js b/scripts/migrations/029-seed-network-exposure-widget.jsindex 1564bb97c1..8dbef489c5 100644--- a/scripts/migrations/029-seed-network-exposure-widget.js+++ b/scripts/migrations/029-seed-network-exposure-widget.js@@ -21,8 +21,7 @@* come back.*/-import { readFile, writeFile } from 'fs/promises';-import { join } from 'path';+import { readLayoutsDoc, writeLayoutsDoc } from './_lib.js';const WIDGET_ID = 'network-exposure';const WIDGET_W = 3;@@ -75,23 +74,9 @@ function applyToLayout(layout) {export default {async up({ rootDir }) {- const path = join(rootDir, 'data', 'dashboard-layouts.json');- const raw = await readFile(path, 'utf-8').catch((err) => {- if (err.code === 'ENOENT') return null;- throw err;- });- if (raw == null) {- console.log(`📦 migration 029: no dashboard-layouts.json yet — fresh install will seed from defaults.`);- return { updated: 0, reason: 'no-state' };- }- let doc;- try { doc = JSON.parse(raw); } catch {- console.log(`📦 migration 029: dashboard-layouts.json unreadable — skipping.`);- return { updated: 0, reason: 'unreadable' };- }- if (!doc || !Array.isArray(doc.layouts)) {- return { updated: 0, reason: 'no-layouts-array' };- }+ const result = await readLayoutsDoc({ rootDir, label: 'migration 029' });+ if (!result.ok) return { updated: 0, reason: result.reason };+ const { doc, path } = result;let touched = 0;for (const layout of doc.layouts) {@@ -104,7 +89,7 @@ export default {return { updated: 0, reason: 'already-applied' };}- await writeFile(path, JSON.stringify(doc, null, 2));+ await writeLayoutsDoc(path, doc);console.log(`📦 migration 029: seeded network-exposure widget into ${touched} built-in layout(s).`);return { updated: touched };},diff --git a/scripts/migrations/030-seed-intent-dashboard-layouts.js b/scripts/migrations/030-seed-intent-dashboard-layouts.jsindex 5e6a6e1dcd..f878617cfd 100644--- a/scripts/migrations/030-seed-intent-dashboard-layouts.js+++ b/scripts/migrations/030-seed-intent-dashboard-layouts.js@@ -20,29 +20,14 @@* re-seed it. This matches the behavior of the fresh-install path.*/-import { readFile, writeFile } from 'fs/promises';-import { join } from 'path';import { INTENT_LAYOUTS } from '../../server/services/dashboardLayouts.js';+import { readLayoutsDoc, writeLayoutsDoc } from './_lib.js';export default {async up({ rootDir }) {- const path = join(rootDir, 'data', 'dashboard-layouts.json');- const raw = await readFile(path, 'utf-8').catch((err) => {- if (err.code === 'ENOENT') return null;- throw err;- });- if (raw == null) {- console.log(`📦 migration 030: no dashboard-layouts.json yet — fresh install will seed from defaults.`);- return { updated: 0, reason: 'no-state' };- }- let doc;- try { doc = JSON.parse(raw); } catch {- console.log(`📦 migration 030: dashboard-layouts.json unreadable — skipping.`);- return { updated: 0, reason: 'unreadable' };- }- if (!doc || !Array.isArray(doc.layouts)) {- return { updated: 0, reason: 'no-layouts-array' };- }+ const result = await readLayoutsDoc({ rootDir, label: 'migration 030' });+ if (!result.ok) return { updated: 0, reason: result.reason };+ const { doc, path } = result;const existingIds = new Set(doc.layouts.map((l) => l?.id).filter(Boolean));const toAdd = INTENT_LAYOUTS.filter((l) => !existingIds.has(l.id));@@ -52,7 +37,7 @@ export default {}doc.layouts.push(...toAdd.map((l) => ({ ...l, builtIn: true })));- await writeFile(path, JSON.stringify(doc, null, 2));+ await writeLayoutsDoc(path, doc);console.log(`📦 migration 030: seeded ${toAdd.length} intent layout(s) (${toAdd.map((l) => l.id).join(', ')}).`);return { updated: toAdd.length };},diff --git a/scripts/migrations/033-seed-quick-image-widget.js b/scripts/migrations/033-seed-quick-image-widget.jsindex f8fcdfab60..df7c0151ea 100644--- a/scripts/migrations/033-seed-quick-image-widget.js+++ b/scripts/migrations/033-seed-quick-image-widget.js@@ -13,8 +13,7 @@* already present and skip.*/-import { readFile, writeFile } from 'fs/promises';-import { join } from 'path';+import { readLayoutsDoc, writeLayoutsDoc } from './_lib.js';const WIDGET_ID = 'quick-image';const WIDGET_W = 3;@@ -83,23 +82,9 @@ function applyToLayout(layout) {export default {async up({ rootDir }) {- const path = join(rootDir, 'data', 'dashboard-layouts.json');- const raw = await readFile(path, 'utf-8').catch((err) => {- if (err.code === 'ENOENT') return null;- throw err;- });- if (raw == null) {- console.log(`📦 migration 033: no dashboard-layouts.json yet — fresh install will seed from defaults.`);- return { updated: 0, reason: 'no-state' };- }- let doc;- try { doc = JSON.parse(raw); } catch {- console.log(`📦 migration 033: dashboard-layouts.json unreadable — skipping.`);- return { updated: 0, reason: 'unreadable' };- }- if (!doc || !Array.isArray(doc.layouts)) {- return { updated: 0, reason: 'no-layouts-array' };- }+ const result = await readLayoutsDoc({ rootDir, label: 'migration 033' });+ if (!result.ok) return { updated: 0, reason: result.reason };+ const { doc, path } = result;let touched = 0;for (const layout of doc.layouts) {@@ -112,7 +97,7 @@ export default {return { updated: 0, reason: 'already-applied' };}- await writeFile(path, JSON.stringify(doc, null, 2));+ await writeLayoutsDoc(path, doc);console.log(`📦 migration 033: seeded quick-image widget into ${touched} built-in layout(s).`);return { updated: touched };},diff --git a/scripts/migrations/_lib.js b/scripts/migrations/_lib.jsindex c7d134b240..7587cb3330 100644--- a/scripts/migrations/_lib.js+++ b/scripts/migrations/_lib.js@@ -1,9 +1,13 @@/**- * Shared scaffolding for hash-driven prompt-replace migrations.+ * Shared scaffolding for migrations. Two families live here:*- * Every prompt-replace migration from 003 onward uses- * `makePromptReplaceMigration` to collapse onto ~50 lines (hash table + label- * + customized-skip hint).+ * 1. Hash-driven prompt-replace migrations — every one from 003 onward uses+ * `makePromptReplaceMigration` to collapse onto ~50 lines (hash table ++ * label + customized-skip hint).+ * 2. Dashboard-layout seeding migrations — `readLayoutsDoc` /+ * `writeLayoutsDoc` collapse the read → JSON.parse → `Array.isArray`+ * guard → write shell shared by every migration that mutates built-in+ * layouts in `data/dashboard-layouts.json` (029, 030, 033, …).** The runner (`scripts/run-migrations.js`) explicitly skips `_`-prefixed* files so this module is never imported as a migration.@@ -179,3 +183,46 @@ export function makePromptReplaceMigration({return { applyMigration, up };}++/**+ * Read + parse + guard `data/dashboard-layouts.json` for a layout-seeding+ * migration. Collapses the preamble every such migration repeats: resolve the+ * path, read the file (absent → fresh install, nothing to do), JSON-parse it+ * (unreadable → skip), and verify `doc.layouts` is an array.+ *+ * Returns a discriminated result:+ * - `{ ok: false, reason: 'no-state' | 'unreadable' | 'no-layouts-array', path }`+ * — the caller short-circuits with `return { updated: 0, reason: result.reason }`.+ * - `{ ok: true, doc, path }` — mutate `doc.layouts` in place, then persist+ * with `writeLayoutsDoc(path, doc)`.+ *+ * `label` is the migration's human tag (e.g. `'migration 029'`); it keeps the+ * no-state / unreadable log lines per-migration identifiable.+ */+export async function readLayoutsDoc({ rootDir, label }) {+ const path = join(rootDir, 'data', 'dashboard-layouts.json');+ const raw = await readFile(path, 'utf-8').catch((err) => {+ if (err.code === 'ENOENT') return null;+ throw err;+ });+ if (raw == null) {+ console.log(`📦 ${label}: no dashboard-layouts.json yet — fresh install will seed from defaults.`);+ return { ok: false, reason: 'no-state', path };+ }+ let doc;+ try {+ doc = JSON.parse(raw);+ } catch {+ console.log(`📦 ${label}: dashboard-layouts.json unreadable — skipping.`);+ return { ok: false, reason: 'unreadable', path };+ }+ if (!doc || !Array.isArray(doc.layouts)) {+ return { ok: false, reason: 'no-layouts-array', path };+ }+ return { ok: true, doc, path };+}++/** Persist a layouts doc with the canonical 2-space indentation. */+export async function writeLayoutsDoc(path, doc) {+ await writeFile(path, JSON.stringify(doc, null, 2));+}diff --git a/scripts/migrations/_lib.test.js b/scripts/migrations/_lib.test.jsindex df50e8c544..c7c1983ab9 100644--- a/scripts/migrations/_lib.test.js+++ b/scripts/migrations/_lib.test.js@@ -13,7 +13,7 @@ import { mkdtempSync, rmSync, writeFileSync, readFileSync, mkdirSync, existsSyncimport { tmpdir } from 'os';import { join } from 'path';-import { applyPromptReplaceMigration, md5 } from './_lib.js';+import { applyPromptReplaceMigration, md5, readLayoutsDoc, writeLayoutsDoc } from './_lib.js';const FILENAME = 'pipeline-fake.md';const BODY_OLD = '# OLD\n';@@ -108,3 +108,62 @@ describe('applyPromptReplaceMigration opt-ins', () => {});});});++describe('readLayoutsDoc / writeLayoutsDoc', () => {+ let rootDir;+ let dataDir;+ let layoutsPath;++ beforeEach(() => {+ rootDir = mkdtempSync(join(tmpdir(), 'migration-layouts-'));+ dataDir = join(rootDir, 'data');+ mkdirSync(dataDir, { recursive: true });+ layoutsPath = join(dataDir, 'dashboard-layouts.json');+ });++ afterEach(() => {+ rmSync(rootDir, { recursive: true, force: true });+ });++ it('reports no-state when the file is absent', async () => {+ const result = await readLayoutsDoc({ rootDir, label: 'migration test' });+ expect(result.ok).toBe(false);+ expect(result.reason).toBe('no-state');+ expect(result.path).toBe(layoutsPath);+ });++ it('reports unreadable for malformed JSON', async () => {+ writeFileSync(layoutsPath, 'not json');+ const result = await readLayoutsDoc({ rootDir, label: 'migration test' });+ expect(result.ok).toBe(false);+ expect(result.reason).toBe('unreadable');+ });++ it('reports no-layouts-array when the layouts key is missing or non-array', async () => {+ writeFileSync(layoutsPath, JSON.stringify({ activeLayoutId: 'default' }));+ expect((await readLayoutsDoc({ rootDir, label: 'x' })).reason).toBe('no-layouts-array');+ writeFileSync(layoutsPath, JSON.stringify({ layouts: 'nope' }));+ expect((await readLayoutsDoc({ rootDir, label: 'x' })).reason).toBe('no-layouts-array');+ writeFileSync(layoutsPath, 'null');+ expect((await readLayoutsDoc({ rootDir, label: 'x' })).reason).toBe('no-layouts-array');+ });++ it('returns the parsed doc + path when valid', async () => {+ const doc = { activeLayoutId: 'default', layouts: [{ id: 'default', widgets: [] }] };+ writeFileSync(layoutsPath, JSON.stringify(doc));+ const result = await readLayoutsDoc({ rootDir, label: 'migration test' });+ expect(result.ok).toBe(true);+ expect(result.path).toBe(layoutsPath);+ expect(result.doc).toEqual(doc);+ });++ it('round-trips through writeLayoutsDoc with 2-space indentation', async () => {+ const doc = { activeLayoutId: 'default', layouts: [{ id: 'default', widgets: ['cos'] }] };+ await writeLayoutsDoc(layoutsPath, doc);+ const raw = readFileSync(layoutsPath, 'utf-8');+ expect(raw).toBe(JSON.stringify(doc, null, 2));+ const reread = await readLayoutsDoc({ rootDir, label: 'migration test' });+ expect(reread.ok).toBe(true);+ expect(reread.doc).toEqual(doc);+ });+});