fix: resolve all TypeScript type errors across codebase for Deno strict mode

Comprehensive type safety improvements across all CLI handlers and daemon:

**Error handling type fixes:**
- Add 'error instanceof Error' checks before accessing error.message throughout
- Fix all error/retryError/stdError/upsError type assertions
- Replace direct error.message with proper type guards

**Switch case improvements:**
- Wrap case block declarations in braces to satisfy deno-lint
- Fix no-case-declarations warnings in CLI command handlers

**Null/undefined safety:**
- Add checks for config.snmp and config.thresholds before access
- Fix IUpsStatus lastStatusChange to handle undefined with default value
- Add proper null checks in legacy configuration paths

**Type annotations:**
- Add explicit type annotations to lambda parameters (groupId, updateAvailable, etc.)
- Add TUpsModel type cast for 'cyberpower' default
- Import and use INupstConfig type where needed

**Parameter type fixes:**
- Fix implicit 'any' type errors in array callbacks
- Add type annotations to filter/find/map parameters

Files modified:
- ts/cli.ts: config.snmp/thresholds null checks, unused error variable fixes
- ts/cli/group-handler.ts: 4 error.message fixes + 2 parameter type annotations
- ts/cli/service-handler.ts: 3 error.message fixes
- ts/cli/ups-handler.ts: 5 error.message fixes + config checks + TUpsModel import
- ts/daemon.ts: 8 error.message fixes + IUpsStatus lastStatusChange fix + updateAvailable type
- ts/nupst.ts: 1 error.message fix
- ts/systemd.ts: 5 error.message fixes + parameter type annotation

All tests passing (3/3 SNMP tests + 10/10 logger tests)
Type check: ✓ No errors
This commit is contained in:
2025-10-18 21:07:57 +00:00
parent 9ccbbbdc37
commit c2d39cc19a
7 changed files with 112 additions and 86 deletions

View File

@@ -2,6 +2,8 @@ import { execSync } from "node:child_process";
import { Nupst } from '../nupst.ts';
import { logger } from '../logger.ts';
import * as helpers from '../helpers/index.ts';
import type { TUpsModel } from '../snmp/types.ts';
import type { INupstConfig } from '../daemon.ts';
/**
* Class for handling UPS-related CLI commands
@@ -46,7 +48,7 @@ export class UpsHandler {
rl.close();
}
} catch (error) {
logger.error(`Add UPS error: ${error.message}`);
logger.error(`Add UPS error: ${error instanceof Error ? error.message : String(error)}`);
}
}
@@ -105,7 +107,7 @@ export class UpsHandler {
community: 'public',
version: 1,
timeout: 5000,
upsModel: 'cyberpower'
upsModel: 'cyberpower' as TUpsModel
},
thresholds: {
battery: 60,
@@ -135,7 +137,7 @@ export class UpsHandler {
config.upsDevices.push(newUps);
// Save the configuration
await this.nupst.getDaemon().saveConfig(config);
await this.nupst.getDaemon().saveConfig(config as INupstConfig);
this.displayUpsConfigSummary(newUps);
@@ -177,7 +179,7 @@ export class UpsHandler {
rl.close();
}
} catch (error) {
logger.error(`Edit UPS error: ${error.message}`);
logger.error(`Edit UPS error: ${error instanceof Error ? error.message : String(error)}`);
}
}
@@ -212,6 +214,10 @@ export class UpsHandler {
// Convert old format to new format if needed
if (!config.upsDevices) {
// Initialize with the current config as the first UPS
if (!config.snmp || !config.thresholds) {
logger.error('Legacy configuration is missing required SNMP or threshold settings');
return;
}
config.upsDevices = [{
id: 'default',
name: 'Default UPS',
@@ -348,7 +354,7 @@ export class UpsHandler {
// Check if service is running and restart it if needed
await this.restartServiceIfRunning();
} catch (error) {
logger.error(`Failed to delete UPS: ${error.message}`);
logger.error(`Failed to delete UPS: ${error instanceof Error ? error.message : String(error)}`);
}
}
@@ -378,6 +384,12 @@ export class UpsHandler {
const boxWidth = 45;
logger.logBoxTitle('UPS Devices', boxWidth);
logger.logBoxLine('Legacy single-UPS configuration detected.');
if (!config.snmp || !config.thresholds) {
logger.logBoxLine('');
logger.logBoxLine('Error: Configuration missing SNMP or threshold settings');
logger.logBoxEnd();
return;
}
logger.logBoxLine('');
logger.logBoxLine('Default UPS:');
logger.logBoxLine(` Host: ${config.snmp.host}:${config.snmp.port}`);
@@ -416,7 +428,7 @@ export class UpsHandler {
logger.logBoxEnd();
} catch (error) {
logger.error(`Failed to list UPS devices: ${error.message}`);
logger.error(`Failed to list UPS devices: ${error instanceof Error ? error.message : String(error)}`);
}
}
@@ -465,7 +477,7 @@ export class UpsHandler {
await this.testConnection(config);
}
} catch (error) {
logger.error(`Test failed: ${error.message}`);
logger.error(`Test failed: ${error instanceof Error ? error.message : String(error)}`);
}
}
@@ -568,7 +580,7 @@ export class UpsHandler {
} catch (error) {
const errorBoxWidth = 45;
logger.logBoxTitle(`Connection Failed: ${upsName}`, errorBoxWidth);
logger.logBoxLine(`Error: ${error.message}`);
logger.logBoxLine(`Error: ${error instanceof Error ? error.message : String(error)}`);
logger.logBoxEnd();
logger.log("\nPlease check your settings and run 'nupst edit' to reconfigure this UPS.");
}
@@ -937,7 +949,7 @@ export class UpsHandler {
const errorBoxWidth = 45;
logger.log('');
logger.logBoxTitle('Connection Failed!', errorBoxWidth);
logger.logBoxLine(`Error: ${error.message}`);
logger.logBoxLine(`Error: ${error instanceof Error ? error.message : String(error)}`);
logger.logBoxEnd();
logger.log('\nPlease check your settings and try again.');
}
@@ -972,7 +984,7 @@ export class UpsHandler {
logger.logBoxLine(' sudo systemctl restart nupst.service');
}
} catch (error) {
logger.logBoxLine(`Error restarting service: ${error.message}`);
logger.logBoxLine(`Error restarting service: ${error instanceof Error ? error.message : String(error)}`);
logger.logBoxLine('You may need to restart the service manually:');
logger.logBoxLine(' sudo systemctl restart nupst.service');
}