feat: wizard remediation — password mask, hooks preview, headless (IUH-M02)
Implements three wizard UX gaps identified in issue #426: AC-3: Replace plaintext password prompt in bootstrapFirstUser with a masked reader (promptMasked) that suppresses echo and requires the user to type their password twice to confirm. Min-8-chars validation is preserved and applied after both entries agree. AC-4: Add a hooks-preview stage (hooksPreviewStage) between runtimeSetupStage and skillsSelectStage in the wizard. The stage parses hooks-config.json, displays each hook event/matcher/command, and prompts the user for consent before installation. Declined → state recorded with accepted=false. Also adds `mosaic config hooks list|enable| disable` subcommands to manage installed hooks in ~/.claude/ post-install. AC-5: Add headless install path to runConfigWizard and bootstrapFirstUser gated on MOSAIC_ASSUME_YES=1 or !process.stdin.isTTY. Env-var-driven configuration with required-var validation and non-zero exit on missing or invalid inputs. Documents all env vars in packages/mosaic/README.md. Closes #426 Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -1,8 +1,74 @@
|
||||
import { spawnSync } from 'node:child_process';
|
||||
import { existsSync, readFileSync, writeFileSync } from 'node:fs';
|
||||
import { join } from 'node:path';
|
||||
import { homedir } from 'node:os';
|
||||
import type { Command } from 'commander';
|
||||
import { createConfigService } from '../config/config-service.js';
|
||||
import { DEFAULT_MOSAIC_HOME } from '../constants.js';
|
||||
|
||||
// ── Hooks management helpers ──────────────────────────────────────────────────
|
||||
|
||||
const DISABLED_PREFIX = '_disabled_';
|
||||
|
||||
/** Resolve the ~/.claude directory (allow override via CLAUDE_HOME env var). */
|
||||
function getClaudeHome(): string {
|
||||
return process.env['CLAUDE_HOME'] ?? join(homedir(), '.claude');
|
||||
}
|
||||
|
||||
interface HookEntry {
|
||||
type?: string;
|
||||
command?: string;
|
||||
args?: unknown[];
|
||||
[key: string]: unknown;
|
||||
}
|
||||
|
||||
interface HookTrigger {
|
||||
matcher?: string;
|
||||
hooks?: HookEntry[];
|
||||
}
|
||||
|
||||
interface HooksConfig {
|
||||
name?: string;
|
||||
hooks?: Record<string, HookTrigger[]>;
|
||||
[key: string]: unknown;
|
||||
}
|
||||
|
||||
function readInstalledHooksConfig(claudeHome: string): HooksConfig | null {
|
||||
const p = join(claudeHome, 'hooks-config.json');
|
||||
if (!existsSync(p)) return null;
|
||||
try {
|
||||
return JSON.parse(readFileSync(p, 'utf-8')) as HooksConfig;
|
||||
} catch {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
function writeInstalledHooksConfig(claudeHome: string, config: HooksConfig): void {
|
||||
const p = join(claudeHome, 'hooks-config.json');
|
||||
writeFileSync(p, JSON.stringify(config, null, 2) + '\n', { mode: 0o600 });
|
||||
}
|
||||
|
||||
/**
|
||||
* Collect a flat list of hook "names" for display purposes.
|
||||
* A hook name is `<EventName>/<matcher>` (e.g. `PostToolUse/Write|Edit`).
|
||||
*/
|
||||
function listHookNames(config: HooksConfig): Array<{ name: string; enabled: boolean }> {
|
||||
const results: Array<{ name: string; enabled: boolean }> = [];
|
||||
const events = config.hooks ?? {};
|
||||
|
||||
for (const [rawEvent, triggers] of Object.entries(events)) {
|
||||
const enabled = !rawEvent.startsWith(DISABLED_PREFIX);
|
||||
const event = enabled ? rawEvent : rawEvent.slice(DISABLED_PREFIX.length);
|
||||
|
||||
for (const trigger of triggers) {
|
||||
const matcher = trigger.matcher ?? '(any)';
|
||||
results.push({ name: `${event}/${matcher}`, enabled });
|
||||
}
|
||||
}
|
||||
|
||||
return results;
|
||||
}
|
||||
|
||||
/**
|
||||
* Resolve mosaicHome from the MOSAIC_HOME env var or the default constant.
|
||||
*/
|
||||
@@ -179,6 +245,138 @@ export function registerConfigCommand(program: Command): void {
|
||||
}
|
||||
});
|
||||
|
||||
// ── config hooks ────────────────────────────────────────────────────────
|
||||
|
||||
const hookCmd = cmd.command('hooks').description('Manage Mosaic hooks installed in ~/.claude/');
|
||||
|
||||
hookCmd
|
||||
.command('list')
|
||||
.description('List installed hooks and their enabled/disabled status')
|
||||
.action(() => {
|
||||
const claudeHome = getClaudeHome();
|
||||
const config = readInstalledHooksConfig(claudeHome);
|
||||
|
||||
if (!config) {
|
||||
console.log(
|
||||
`No hooks-config.json found at ${claudeHome}.\n` +
|
||||
'Run `mosaic wizard` to install hooks, or copy hooks-config.json manually.',
|
||||
);
|
||||
return;
|
||||
}
|
||||
|
||||
const entries = listHookNames(config);
|
||||
|
||||
if (entries.length === 0) {
|
||||
console.log('No hooks defined in hooks-config.json.');
|
||||
return;
|
||||
}
|
||||
|
||||
const maxName = Math.max(...entries.map((e) => e.name.length));
|
||||
const header = `${'Hook'.padEnd(maxName)} Status`;
|
||||
console.log(header);
|
||||
console.log('-'.repeat(header.length));
|
||||
|
||||
for (const { name, enabled } of entries) {
|
||||
console.log(`${name.padEnd(maxName)} ${enabled ? 'enabled' : 'disabled'}`);
|
||||
}
|
||||
});
|
||||
|
||||
hookCmd
|
||||
.command('disable <name>')
|
||||
.description('Disable a hook by name (prefix with _disabled_). Use "list" to see hook names.')
|
||||
.action((name: string) => {
|
||||
const claudeHome = getClaudeHome();
|
||||
const config = readInstalledHooksConfig(claudeHome);
|
||||
|
||||
if (!config) {
|
||||
console.error(
|
||||
`No hooks-config.json found at ${claudeHome}.\n` +
|
||||
'Nothing to disable. Run `mosaic wizard` to install hooks first.',
|
||||
);
|
||||
process.exit(1);
|
||||
}
|
||||
|
||||
const events = config.hooks ?? {};
|
||||
// Support matching by event key or by event/matcher composite
|
||||
const [targetEvent, targetMatcher] = name.split('/');
|
||||
|
||||
// Find the event key (may already have DISABLED_PREFIX)
|
||||
const existingKey = Object.keys(events).find(
|
||||
(k) =>
|
||||
k === targetEvent ||
|
||||
k === `${DISABLED_PREFIX}${targetEvent}` ||
|
||||
k.replace(DISABLED_PREFIX, '') === targetEvent,
|
||||
);
|
||||
|
||||
if (!existingKey) {
|
||||
console.error(`Hook event "${targetEvent}" not found.`);
|
||||
console.error('Run `mosaic config hooks list` to see available hooks.');
|
||||
process.exit(1);
|
||||
}
|
||||
|
||||
if (existingKey.startsWith(DISABLED_PREFIX)) {
|
||||
console.log(`Hook "${name}" is already disabled.`);
|
||||
return;
|
||||
}
|
||||
|
||||
const disabledKey = `${DISABLED_PREFIX}${existingKey}`;
|
||||
const triggers = events[existingKey];
|
||||
delete events[existingKey];
|
||||
|
||||
// If a matcher was specified, only disable that trigger
|
||||
if (targetMatcher && triggers) {
|
||||
events[disabledKey] = triggers.filter((t) => t.matcher === targetMatcher);
|
||||
events[existingKey] = triggers.filter((t) => t.matcher !== targetMatcher);
|
||||
if ((events[existingKey] ?? []).length === 0) delete events[existingKey];
|
||||
} else {
|
||||
events[disabledKey] = triggers ?? [];
|
||||
}
|
||||
|
||||
config.hooks = events;
|
||||
writeInstalledHooksConfig(claudeHome, config);
|
||||
console.log(`Hook "${name}" disabled.`);
|
||||
});
|
||||
|
||||
hookCmd
|
||||
.command('enable <name>')
|
||||
.description('Re-enable a previously disabled hook.')
|
||||
.action((name: string) => {
|
||||
const claudeHome = getClaudeHome();
|
||||
const config = readInstalledHooksConfig(claudeHome);
|
||||
|
||||
if (!config) {
|
||||
console.error(
|
||||
`No hooks-config.json found at ${claudeHome}.\n` +
|
||||
'Nothing to enable. Run `mosaic wizard` to install hooks first.',
|
||||
);
|
||||
process.exit(1);
|
||||
}
|
||||
|
||||
const events = config.hooks ?? {};
|
||||
const targetEvent = name.split('/')[0] ?? name;
|
||||
const disabledKey = `${DISABLED_PREFIX}${targetEvent}`;
|
||||
|
||||
if (!events[disabledKey]) {
|
||||
// Check if it's already enabled
|
||||
if (events[targetEvent]) {
|
||||
console.log(`Hook "${name}" is already enabled.`);
|
||||
} else {
|
||||
console.error(`Disabled hook "${name}" not found.`);
|
||||
console.error('Run `mosaic config hooks list` to see available hooks.');
|
||||
process.exit(1);
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
const triggers = events[disabledKey];
|
||||
delete events[disabledKey];
|
||||
events[targetEvent] = triggers ?? [];
|
||||
|
||||
config.hooks = events;
|
||||
writeInstalledHooksConfig(claudeHome, config);
|
||||
console.log(`Hook "${name}" enabled.`);
|
||||
});
|
||||
|
||||
// ── config path ─────────────────────────────────────────────────────────
|
||||
|
||||
cmd
|
||||
|
||||
Reference in New Issue
Block a user