style: configure deno fmt to use single quotes
All checks were successful
CI / Build All Platforms (Tag/Main only) (push) Has been skipped
CI / Type Check & Lint (push) Successful in 6s
CI / Build Test (Current Platform) (push) Successful in 6s

- Add singleQuote: true to deno.json fmt configuration
- Reformat all files with single quotes using deno fmt
This commit is contained in:
2025-10-19 13:14:18 +00:00
parent b935087d50
commit 071ded9c41
24 changed files with 1094 additions and 672 deletions

View File

@@ -35,10 +35,10 @@ export class GroupHandler {
logger.logBoxEnd();
return;
}
// Get current configuration
const config = this.nupst.getDaemon().getConfig();
// Check if multi-UPS config
if (!config.groups || !Array.isArray(config.groups)) {
// Legacy or missing groups configuration
@@ -49,11 +49,11 @@ export class GroupHandler {
logger.logBoxEnd();
return;
}
// Display group list
const boxWidth = 60;
logger.logBoxTitle('UPS Groups', boxWidth);
if (config.groups.length === 0) {
logger.logBoxLine('No UPS groups configured.');
logger.logBoxLine('Use "nupst group add" to add a UPS group.');
@@ -62,27 +62,29 @@ export class GroupHandler {
logger.logBoxLine('');
logger.logBoxLine('ID | Name | Mode | UPS Devices');
logger.logBoxLine('-----------+----------------------+--------------+----------------');
for (const group of config.groups) {
const id = group.id.padEnd(10, ' ').substring(0, 10);
const name = (group.name || '').padEnd(20, ' ').substring(0, 20);
const mode = (group.mode || 'unknown').padEnd(12, ' ').substring(0, 12);
// Count UPS devices in this group
const upsInGroup = config.upsDevices.filter(ups => ups.groups.includes(group.id));
const upsInGroup = config.upsDevices.filter((ups) => ups.groups.includes(group.id));
const upsCount = upsInGroup.length;
const upsNames = upsInGroup.map(ups => ups.name).join(', ');
const upsNames = upsInGroup.map((ups) => ups.name).join(', ');
logger.logBoxLine(`${id} | ${name} | ${mode} | ${upsCount > 0 ? upsNames : 'None'}`);
}
}
logger.logBoxEnd();
} catch (error) {
logger.error(`Failed to list UPS groups: ${error instanceof Error ? error.message : String(error)}`);
logger.error(
`Failed to list UPS groups: ${error instanceof Error ? error.message : String(error)}`,
);
}
}
/**
* Add a new UPS group
*/
@@ -110,54 +112,56 @@ export class GroupHandler {
try {
await this.nupst.getDaemon().loadConfig();
} catch (error) {
logger.error('No configuration found. Please run "nupst setup" first to create a configuration.');
logger.error(
'No configuration found. Please run "nupst setup" first to create a configuration.',
);
return;
}
// Get current configuration
const config = this.nupst.getDaemon().getConfig();
// Initialize groups array if not exists
if (!config.groups) {
config.groups = [];
}
// Check if upsDevices is initialized
if (!config.upsDevices) {
config.upsDevices = [];
}
logger.log('\nNUPST Add Group');
logger.log('==============\n');
logger.log('This will guide you through creating a new UPS group.\n');
// Generate a new unique group ID
const groupId = helpers.shortId();
// Get group name
const name = await prompt('Group Name: ');
// Get group mode
const modeInput = await prompt('Group Mode (redundant/nonRedundant) [redundant]: ');
const mode = modeInput.toLowerCase() === 'nonredundant' ? 'nonRedundant' : 'redundant';
// Get optional description
const description = await prompt('Group Description (optional): ');
// Create the new group
const newGroup: IGroupConfig = {
id: groupId,
name: name || `Group-${groupId}`,
mode,
description: description || undefined
description: description || undefined,
};
// Add the group to the configuration
config.groups.push(newGroup);
// Save the configuration
await this.nupst.getDaemon().saveConfig(config);
// Display summary
const boxWidth = 45;
logger.logBoxTitle('Group Created', boxWidth);
@@ -168,21 +172,23 @@ export class GroupHandler {
logger.logBoxLine(`Description: ${newGroup.description}`);
}
logger.logBoxEnd();
// Check if there are UPS devices to assign to this group
if (config.upsDevices.length > 0) {
const assignUps = await prompt('Would you like to assign UPS devices to this group now? (y/N): ');
const assignUps = await prompt(
'Would you like to assign UPS devices to this group now? (y/N): ',
);
if (assignUps.toLowerCase() === 'y') {
await this.assignUpsToGroup(newGroup.id, config, prompt);
// Save again after assigning UPS devices
await this.nupst.getDaemon().saveConfig(config);
}
}
// Check if service is running and restart it if needed
this.nupst.getUpsHandler().restartServiceIfRunning();
logger.log('\nGroup setup complete!');
} finally {
rl.close();
@@ -191,7 +197,7 @@ export class GroupHandler {
logger.error(`Add group error: ${error instanceof Error ? error.message : String(error)}`);
}
}
/**
* Edit an existing UPS group
* @param groupId ID of the group to edit
@@ -220,57 +226,61 @@ export class GroupHandler {
try {
await this.nupst.getDaemon().loadConfig();
} catch (error) {
logger.error('No configuration found. Please run "nupst setup" first to create a configuration.');
logger.error(
'No configuration found. Please run "nupst setup" first to create a configuration.',
);
return;
}
// Get current configuration
const config = this.nupst.getDaemon().getConfig();
// Check if groups are initialized
if (!config.groups || !Array.isArray(config.groups)) {
logger.error('No groups configured. Please run "nupst group add" first to create a group.');
logger.error(
'No groups configured. Please run "nupst group add" first to create a group.',
);
return;
}
// Find the group to edit
const groupIndex = config.groups.findIndex(group => group.id === groupId);
const groupIndex = config.groups.findIndex((group) => group.id === groupId);
if (groupIndex === -1) {
logger.error(`Group with ID "${groupId}" not found.`);
return;
}
const group = config.groups[groupIndex];
logger.log(`\nNUPST Edit Group: ${group.name} (${group.id})`);
logger.log('==============================================\n');
// Edit group name
const newName = await prompt(`Group Name [${group.name}]: `);
if (newName.trim()) {
group.name = newName;
}
// Edit group mode
const currentMode = group.mode || 'redundant';
const modeInput = await prompt(`Group Mode (redundant/nonRedundant) [${currentMode}]: `);
if (modeInput.trim()) {
group.mode = modeInput.toLowerCase() === 'nonredundant' ? 'nonRedundant' : 'redundant';
}
// Edit description
const currentDesc = group.description || '';
const newDesc = await prompt(`Group Description [${currentDesc}]: `);
if (newDesc.trim() || newDesc === '') {
group.description = newDesc.trim() || undefined;
}
// Update the group in the configuration
config.groups[groupIndex] = group;
// Save the configuration
await this.nupst.getDaemon().saveConfig(config);
// Display summary
const boxWidth = 45;
logger.logBoxTitle('Group Updated', boxWidth);
@@ -281,19 +291,21 @@ export class GroupHandler {
logger.logBoxLine(`Description: ${group.description}`);
}
logger.logBoxEnd();
// Edit UPS assignments if requested
const editAssignments = await prompt('Would you like to edit UPS assignments for this group? (y/N): ');
const editAssignments = await prompt(
'Would you like to edit UPS assignments for this group? (y/N): ',
);
if (editAssignments.toLowerCase() === 'y') {
await this.assignUpsToGroup(group.id, config, prompt);
// Save again after editing assignments
await this.nupst.getDaemon().saveConfig(config);
}
// Check if service is running and restart it if needed
this.nupst.getUpsHandler().restartServiceIfRunning();
logger.log('\nGroup edit complete!');
} finally {
rl.close();
@@ -302,7 +314,7 @@ export class GroupHandler {
logger.error(`Edit group error: ${error instanceof Error ? error.message : String(error)}`);
}
}
/**
* Delete an existing UPS group
* @param groupId ID of the group to delete
@@ -313,48 +325,53 @@ export class GroupHandler {
try {
await this.nupst.getDaemon().loadConfig();
} catch (error) {
logger.error('No configuration found. Please run "nupst setup" first to create a configuration.');
logger.error(
'No configuration found. Please run "nupst setup" first to create a configuration.',
);
return;
}
// Get current configuration
const config = this.nupst.getDaemon().getConfig();
// Check if groups are initialized
if (!config.groups || !Array.isArray(config.groups)) {
logger.error('No groups configured.');
return;
}
// Find the group to delete
const groupIndex = config.groups.findIndex(group => group.id === groupId);
const groupIndex = config.groups.findIndex((group) => group.id === groupId);
if (groupIndex === -1) {
logger.error(`Group with ID "${groupId}" not found.`);
return;
}
const groupToDelete = config.groups[groupIndex];
// Get confirmation before deleting
const readline = await import('node:readline');
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
});
const confirm = await new Promise<string>(resolve => {
rl.question(`Are you sure you want to delete group "${groupToDelete.name}" (${groupId})? [y/N]: `, answer => {
resolve(answer.toLowerCase());
});
const confirm = await new Promise<string>((resolve) => {
rl.question(
`Are you sure you want to delete group "${groupToDelete.name}" (${groupId})? [y/N]: `,
(answer) => {
resolve(answer.toLowerCase());
},
);
});
rl.close();
if (confirm !== 'y' && confirm !== 'yes') {
logger.log('Deletion cancelled.');
return;
}
// Remove this group from all UPS device group assignments
if (config.upsDevices && Array.isArray(config.upsDevices)) {
for (const ups of config.upsDevices) {
@@ -364,19 +381,21 @@ export class GroupHandler {
}
}
}
// Remove the group from the array
config.groups.splice(groupIndex, 1);
// Save the configuration
await this.nupst.getDaemon().saveConfig(config);
logger.log(`Group "${groupToDelete.name}" (${groupId}) has been deleted.`);
// Check if service is running and restart it if needed
this.nupst.getUpsHandler().restartServiceIfRunning();
} catch (error) {
logger.error(`Failed to delete group: ${error instanceof Error ? error.message : String(error)}`);
logger.error(
`Failed to delete group: ${error instanceof Error ? error.message : String(error)}`,
);
}
}
@@ -389,18 +408,18 @@ export class GroupHandler {
public async assignUpsToGroups(
ups: any,
groups: any[],
prompt: (question: string) => Promise<string>
prompt: (question: string) => Promise<string>,
): Promise<void> {
// Initialize groups array if it doesn't exist
if (!ups.groups) {
ups.groups = [];
}
// Show current group assignments
logger.log('\nCurrent Group Assignments:');
if (ups.groups && ups.groups.length > 0) {
for (const groupId of ups.groups) {
const group = groups.find(g => g.id === groupId);
const group = groups.find((g) => g.id === groupId);
if (group) {
logger.log(`- ${group.name} (${group.id})`);
} else {
@@ -410,52 +429,56 @@ export class GroupHandler {
} else {
logger.log('- None');
}
// Show available groups
logger.log('\nAvailable Groups:');
if (groups.length === 0) {
logger.log('- No groups available. Use "nupst group add" to create groups.');
return;
}
for (let i = 0; i < groups.length; i++) {
const group = groups[i];
const assigned = ups.groups && ups.groups.includes(group.id);
logger.log(`${i + 1}) ${group.name} (${group.id}) [${assigned ? 'Assigned' : 'Not Assigned'}]`);
logger.log(
`${i + 1}) ${group.name} (${group.id}) [${assigned ? 'Assigned' : 'Not Assigned'}]`,
);
}
// Prompt for group selection
const selection = await prompt('\nSelect groups to assign/unassign (comma-separated numbers, or "clear" to remove all): ');
const selection = await prompt(
'\nSelect groups to assign/unassign (comma-separated numbers, or "clear" to remove all): ',
);
if (selection.toLowerCase() === 'clear') {
// Clear all group assignments
ups.groups = [];
logger.log('All group assignments cleared.');
return;
}
if (!selection.trim()) {
// No change if empty input
return;
}
// Process selections
const selections = selection.split(',').map(s => s.trim());
const selections = selection.split(',').map((s) => s.trim());
for (const sel of selections) {
const index = parseInt(sel, 10) - 1;
if (isNaN(index) || index < 0 || index >= groups.length) {
logger.error(`Invalid selection: ${sel}`);
continue;
}
const group = groups[index];
// Initialize groups array if needed (should already be done above)
if (!ups.groups) {
ups.groups = [];
}
// Toggle assignment
const groupIndex = ups.groups.indexOf(group.id);
if (groupIndex === -1) {
@@ -469,7 +492,7 @@ export class GroupHandler {
}
}
}
/**
* Assign UPS devices to a specific group
* @param groupId Group ID to assign UPS devices to
@@ -479,22 +502,24 @@ export class GroupHandler {
public async assignUpsToGroup(
groupId: string,
config: any,
prompt: (question: string) => Promise<string>
prompt: (question: string) => Promise<string>,
): Promise<void> {
if (!config.upsDevices || config.upsDevices.length === 0) {
logger.log('No UPS devices available. Use "nupst add" to add UPS devices.');
return;
}
const group = config.groups.find((g: { id: string }) => g.id === groupId);
if (!group) {
logger.error(`Group with ID "${groupId}" not found.`);
return;
}
// Show current assignments
logger.log(`\nUPS devices in group "${group.name}" (${group.id}):`);
const upsInGroup = config.upsDevices.filter((ups: { groups?: string[] }) => ups.groups && ups.groups.includes(groupId));
const upsInGroup = config.upsDevices.filter((ups: { groups?: string[] }) =>
ups.groups && ups.groups.includes(groupId)
);
if (upsInGroup.length === 0) {
logger.log('- None');
} else {
@@ -502,7 +527,7 @@ export class GroupHandler {
logger.log(`- ${ups.name} (${ups.id})`);
}
}
// Show all UPS devices
logger.log('\nAvailable UPS devices:');
for (let i = 0; i < config.upsDevices.length; i++) {
@@ -510,10 +535,12 @@ export class GroupHandler {
const assigned = ups.groups && ups.groups.includes(groupId);
logger.log(`${i + 1}) ${ups.name} (${ups.id}) [${assigned ? 'Assigned' : 'Not Assigned'}]`);
}
// Prompt for UPS selection
const selection = await prompt('\nSelect UPS devices to assign/unassign (comma-separated numbers, or "clear" to remove all): ');
const selection = await prompt(
'\nSelect UPS devices to assign/unassign (comma-separated numbers, or "clear" to remove all): ',
);
if (selection.toLowerCase() === 'clear') {
// Clear all UPS from this group
for (const ups of config.upsDevices) {
@@ -527,29 +554,29 @@ export class GroupHandler {
logger.log(`All UPS devices removed from group "${group.name}".`);
return;
}
if (!selection.trim()) {
// No change if empty input
return;
}
// Process selections
const selections = selection.split(',').map(s => s.trim());
const selections = selection.split(',').map((s) => s.trim());
for (const sel of selections) {
const index = parseInt(sel, 10) - 1;
if (isNaN(index) || index < 0 || index >= config.upsDevices.length) {
logger.error(`Invalid selection: ${sel}`);
continue;
}
const ups = config.upsDevices[index];
// Initialize groups array if needed
if (!ups.groups) {
ups.groups = [];
}
// Toggle assignment
const groupIndex = ups.groups.indexOf(groupId);
if (groupIndex === -1) {
@@ -563,4 +590,4 @@ export class GroupHandler {
}
}
}
}
}