All files / src/functions find.command.action.ts

100% Statements 22/22
100% Branches 14/14
100% Functions 1/1
100% Lines 17/17

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36    2x       2x 12x   12x 12x 12x 10x   8x     7x 6x     3x 3x 3x   2x     4x       11x   2x  
'use strict';
 
import {listSubfoldersFilesByFolderName} from '@owservable/folders';
 
import ActionAsCommandInterface from '../interfaces/action.as.command.interface';
 
export const findCommandAction: Function = (root: string, cliCommand: string): ActionAsCommandInterface | null => {
	const actionPaths: string[] = listSubfoldersFilesByFolderName(root, 'actions');
 
	for (const actionPath of actionPaths) {
		try {
			const ActionClass: new () => ActionAsCommandInterface = require(actionPath).default;
			if (!ActionClass) continue;
 
			const actionInstance: ActionAsCommandInterface = new ActionClass();
 
			// Add signature validation
			const signature: string | undefined = actionInstance.signature();
			if (!signature || typeof signature !== 'string') continue;
 
			// Parse command from signature with validation
			const signatureParts: string[] = signature.trim().split(/\s+/);
			const actionCommand: string = signatureParts[0];
			if (!actionCommand || actionCommand.length === 0) continue;
 
			if (cliCommand === actionCommand) return actionInstance;
		} catch (error) {
			// Skip malformed or problematic action files, continue searching
			console.warn(`[@owservable/actions] Failed to load action from ${actionPath}:`, error instanceof Error ? error.message : error);
		}
	}
 
	return null;
};
export default findCommandAction;