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
24 changes: 18 additions & 6 deletions gitnexus/src/cli/setup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,27 @@ interface SetupResult {
*/
function resolveGitnexusBin(): string | null {
try {
const cmd = process.platform === 'win32' ? 'where' : 'which';
const resolved = execFileSync(cmd, ['gitnexus'], {
const isWin = process.platform === 'win32';
const cmd = isWin ? 'where' : 'which';
const output = execFileSync(cmd, ['gitnexus'], {
encoding: 'utf-8',
timeout: 5000,
stdio: ['ignore', 'pipe', 'ignore'],
})
.split('\n')[0]
.trim();
return resolved || null;
});
const lines = output
.split('\n')
.map((l) => l.trim())
.filter(Boolean);

if (isWin) {
// On Windows, `where` returns multiple entries (e.g. the POSIX shell
// script AND the .cmd/.bat wrapper). Prefer the wrapper because
// child_process.spawn() cannot execute a shell script directly.
const cmdLine = lines.find((l) => /\.(cmd|bat)$/i.test(l));
return cmdLine || lines[0] || null;
}

return lines[0] || null;
} catch {
return null;
}
Expand Down
91 changes: 91 additions & 0 deletions gitnexus/test/unit/setup.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -185,4 +185,95 @@ describe('setupClaudeCode', () => {
args: ['-y', 'gitnexus@latest', 'mcp'],
});
});

it('picks .cmd wrapper from Windows where output (multiple lines)', async () => {
setPlatform('win32');
// `where gitnexus` on Windows returns the POSIX script first, then .cmd
execFileSyncMock.mockReturnValueOnce(
'C:\\Users\\dev\\AppData\\Roaming\\npm\\gitnexus\nC:\\Users\\dev\\AppData\\Roaming\\npm\\gitnexus.cmd\n',
);

const { setupCommand } = await import('../../src/cli/setup.js');
await setupCommand();

const raw = await fs.readFile(path.join(tempHome, '.claude.json'), 'utf-8');
const config = JSON.parse(raw);

expect(config.mcpServers.gitnexus).toEqual({
command: 'C:\\Users\\dev\\AppData\\Roaming\\npm\\gitnexus.cmd',
args: ['mcp'],
});
});

it('handles CRLF line endings from Windows where output', async () => {
setPlatform('win32');
// Windows `where` produces CRLF line endings
execFileSyncMock.mockReturnValueOnce(
'C:\\Users\\dev\\AppData\\Roaming\\npm\\gitnexus\r\nC:\\Users\\dev\\AppData\\Roaming\\npm\\gitnexus.cmd\r\n',
);

const { setupCommand } = await import('../../src/cli/setup.js');
await setupCommand();

const raw = await fs.readFile(path.join(tempHome, '.claude.json'), 'utf-8');
const config = JSON.parse(raw);

expect(config.mcpServers.gitnexus).toEqual({
command: 'C:\\Users\\dev\\AppData\\Roaming\\npm\\gitnexus.cmd',
args: ['mcp'],
});
});

it('picks .bat wrapper when .cmd is not present', async () => {
setPlatform('win32');
execFileSyncMock.mockReturnValueOnce(
'C:\\Users\\dev\\AppData\\Roaming\\npm\\gitnexus\nC:\\Users\\dev\\AppData\\Roaming\\npm\\gitnexus.bat\n',
);

const { setupCommand } = await import('../../src/cli/setup.js');
await setupCommand();

const raw = await fs.readFile(path.join(tempHome, '.claude.json'), 'utf-8');
const config = JSON.parse(raw);

expect(config.mcpServers.gitnexus).toEqual({
command: 'C:\\Users\\dev\\AppData\\Roaming\\npm\\gitnexus.bat',
args: ['mcp'],
});
});

it('handles uppercase .CMD extension (case-insensitive match)', async () => {
setPlatform('win32');
execFileSyncMock.mockReturnValueOnce(
'C:\\Users\\dev\\AppData\\Roaming\\npm\\gitnexus\nC:\\Users\\dev\\AppData\\Roaming\\npm\\gitnexus.CMD\n',
);

const { setupCommand } = await import('../../src/cli/setup.js');
await setupCommand();

const raw = await fs.readFile(path.join(tempHome, '.claude.json'), 'utf-8');
const config = JSON.parse(raw);

expect(config.mcpServers.gitnexus).toEqual({
command: 'C:\\Users\\dev\\AppData\\Roaming\\npm\\gitnexus.CMD',
args: ['mcp'],
});
});

it('falls back to first line on Windows when no .cmd/.bat wrapper found', async () => {
setPlatform('win32');
// Edge case: where returns only the POSIX script (no .cmd wrapper)
execFileSyncMock.mockReturnValueOnce('C:\\Users\\dev\\AppData\\Roaming\\npm\\gitnexus\n');

const { setupCommand } = await import('../../src/cli/setup.js');
await setupCommand();

const raw = await fs.readFile(path.join(tempHome, '.claude.json'), 'utf-8');
const config = JSON.parse(raw);

expect(config.mcpServers.gitnexus).toEqual({
command: 'C:\\Users\\dev\\AppData\\Roaming\\npm\\gitnexus',
args: ['mcp'],
});
});
});
Loading