Skip to content

Commit

Permalink
feat: add update command and enhance error handling
Browse files Browse the repository at this point in the history
- Introduced a new "test:update" script in package.json for testing updates.
- Updated the updatePackage function to use 'npm install -g' instead of 'npx' for installing updates.
- Improved error handling to provide detailed output for both stdout and stderr during the update process.
- Adjusted unit tests to reflect changes in the update command and error handling.

Co-Authored-By: Michael Latman <[email protected]>
  • Loading branch information
michaellatman committed Dec 15, 2024
1 parent 50ee23a commit 981076e
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 6 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
"test:list": "node --loader ts-node/esm src/index.ts list",
"test:install": "node --loader ts-node/esm src/index.ts install",
"test:installed": "node --loader ts-node/esm src/index.ts installed",
"test:update": "node --loader ts-node/esm src/index.ts update",
"extract": "node --loader ts-node/esm src/extractors/modelcontextprotocol-extractor.ts",
"test:uninstall": "node --loader ts-node/esm src/index.ts uninstall",
"version:patch": "npm version patch",
Expand Down
7 changes: 5 additions & 2 deletions src/__tests__/auto-update.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ await jest.unstable_mockModule('fs', () => ({
const { updatePackage } = await import('../auto-update.js');

// Helper to create exec result
const createExecResult = (stdout: string): ExecResult => ({ stdout, stderr: '' });
const createExecResult = (stdout: string, stderr: string = ''): ExecResult => ({ stdout, stderr });

describe('updatePackage', () => {
beforeEach(() => {
Expand All @@ -73,7 +73,7 @@ describe('updatePackage', () => {
await updatePackage();

expect(mockExecPromise).toHaveBeenNthCalledWith(1, 'npm show @michaellatman/mcp-get version');
expect(mockExecPromise).toHaveBeenNthCalledWith(2, 'npx --yes @michaellatman/mcp-get@latest');
expect(mockExecPromise).toHaveBeenNthCalledWith(2, 'npm install -g @michaellatman/mcp-get@latest');

expect(console.log).toHaveBeenNthCalledWith(1,
'\nA new version of mcp-get is available: 1.0.50 (current: 1.0.48)'
Expand All @@ -82,6 +82,9 @@ describe('updatePackage', () => {
'Installing update...'
);
expect(console.log).toHaveBeenNthCalledWith(3,
'success'
);
expect(console.log).toHaveBeenNthCalledWith(4,
'✓ Update complete\n'
);
});
Expand Down
22 changes: 18 additions & 4 deletions src/auto-update.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,17 +29,31 @@ export async function updatePackage(silent: boolean = false): Promise<void> {
}

try {
await execAsync('npx --yes @michaellatman/mcp-get@latest');
if (!silent) console.log(chalk.green('✓ Update complete\n'));
const { stdout, stderr } = await execAsync('npm install -g @michaellatman/mcp-get@latest');
if (!silent) {
if (stdout) console.log(stdout);
if (stderr) console.error(chalk.yellow('Update process output:'), stderr);
console.log(chalk.green('✓ Update complete\n'));
}
} catch (installError: any) {
if (!silent) console.error(chalk.red('Failed to install update:'), installError.message);
if (!silent) {
console.error(chalk.red('Failed to install update:'), installError.message);
if (installError.stdout) console.log('stdout:', installError.stdout);
if (installError.stderr) console.error('stderr:', installError.stderr);
console.error(chalk.yellow('Try running the update manually with sudo:'));
console.error(chalk.cyan(' sudo npm install -g @michaellatman/mcp-get@latest'));
}
return;
}
} else {
if (!silent) console.log(chalk.green('✓ mcp-get is already up to date\n'));
}
} catch (error: any) {
if (!silent) console.error(chalk.red('Failed to check for updates:'), error.message);
if (!silent) {
console.error(chalk.red('Failed to check for updates:'), error.message);
if (error.stdout) console.log('stdout:', error.stdout);
if (error.stderr) console.error('stderr:', error.stderr);
}
return;
}
}
Expand Down

0 comments on commit 981076e

Please sign in to comment.