Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
81 changes: 81 additions & 0 deletions src/cli/mm.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4149,6 +4149,87 @@ describe('main', () => {
process.argv = origArgv;
});

it.each([
'launch --help',
'launch -h',
'launch --platform ios --help',
'stop --help',
'cleanup --shutdown -h',
'serve --background --help',
'click --help',
'status -h',
])('prints help for `mm %s` without running it', async (command) => {
const { readDaemonState } = await import('../server/daemon-state.js');
const { spawn } = await import('node:child_process');
const fetchSpy = vi.spyOn(globalThis, 'fetch');

const origArgv = process.argv;
process.argv = ['node', 'mm', ...command.split(' ')];

try {
await expect(main()).rejects.toThrowError('process.exit');
expect(exitSpy).toHaveBeenCalledWith(0);
expect(stdoutSpy).toHaveBeenCalledWith(expect.stringContaining('Usage:'));
expect(stderrSpy).not.toHaveBeenCalled();
expect(fs.realpath).not.toHaveBeenCalled();
expect(readDaemonState).not.toHaveBeenCalled();
expect(spawn).not.toHaveBeenCalled();
expect(fetchSpy).not.toHaveBeenCalled();
} finally {
process.argv = origArgv;
}
});

it('types --help as text when it is not the first argument', async () => {
const { readDaemonState, isDaemonAlive, isDaemonVersionMatch } =
await import('../server/daemon-state.js');
vi.mocked(readDaemonState).mockResolvedValueOnce({
port: 3000,
pid: 123,
nonce: 'abc',
startedAt: '2024-01-01',
version: '1.0.0',
subPorts: {},
});
vi.mocked(isDaemonAlive).mockResolvedValueOnce(true);
vi.mocked(isDaemonVersionMatch).mockReturnValueOnce(true);
vi.spyOn(globalThis, 'fetch').mockResolvedValue({
ok: true,
json: async () => ({ ok: true, result: {} }),
} as Response);

const origArgv = process.argv;
process.argv = ['node', 'mm', 'type', 'e5', '--help'];

try {
await main();
} finally {
process.argv = origArgv;
}

expect(globalThis.fetch).toHaveBeenCalledWith(
'http://127.0.0.1:3000/tool/type',
expect.objectContaining({
body: JSON.stringify({ a11yRef: 'e5', text: '--help' }),
}),
);
});

it('rejects an unknown command before its --help flag', async () => {
const origArgv = process.argv;
process.argv = ['node', 'mm', 'frobnicate', '--help'];

try {
await expect(main()).rejects.toThrowError('process.exit');
expectFailure(expect.stringContaining("unknown command 'frobnicate'"), 2);
expect(stdoutSpy).not.toHaveBeenCalledWith(
expect.stringContaining('Usage:'),
);
} finally {
process.argv = origArgv;
}
});

it('routes stop command to handleStop', async () => {
const { readDaemonState } = await import('../server/daemon-state.js');
vi.mocked(readDaemonState).mockResolvedValueOnce(null);
Expand Down
39 changes: 33 additions & 6 deletions src/cli/mm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,31 @@ function isKnownCommand(
);
}

/**
* Whether the argument is `--help` or `-h`.
*
* @param arg - A CLI argument.
* @returns True for a help flag.
*/
function isHelpFlag(arg: string | undefined): boolean {
return arg === '--help' || arg === '-h';
}

/**
* Whether `mm <command> <args>` asks for help: a help flag right after the
* command, or anywhere for a lifecycle command. Other commands take free
* text, so `mm type e5 --help` types `--help`.
*
* @param command - The command.
* @param args - The arguments after the command.
* @returns True when the command must print help and not run.
*/
function isHelpRequest(command: string, args: string[]): boolean {
return (LIFECYCLE_COMMANDS as readonly string[]).includes(command)
? args.some(isHelpFlag)
: isHelpFlag(args[0]);
}

const DAEMON_POLL_INTERVAL_MS = 200;
const DAEMON_START_TIMEOUT_MS = 30_000;
const DAEMON_POLL_MAX_ATTEMPTS =
Expand Down Expand Up @@ -495,23 +520,25 @@ export async function main(): Promise<void> {
resolvePretty(pretty, process.env.MM_PRETTY, process.stdout.isTTY),
);

if (
remainingArgs.length === 0 ||
remainingArgs[0] === '--help' ||
remainingArgs[0] === '-h'
) {
if (remainingArgs.length === 0 || isHelpFlag(remainingArgs[0])) {
printHelp();
process.exit(0);
}

const worktreeRoot = await resolveWorktreeRoot(projectPath);
const args = remainingArgs;
const command = args[0];

if (!isKnownCommand(command)) {
failUsage(`unknown command '${command}'. Run 'mm --help' for usage.`);
}

if (isHelpRequest(command, args.slice(1))) {
printHelp();
process.exit(0);
}

const worktreeRoot = await resolveWorktreeRoot(projectPath);

if (command === 'serve') {
const background = args.includes('--background');
await handleServe(worktreeRoot, background);
Expand Down
Loading