aahook
Version:
A CLI tool that displays ASCII art when commands succeed or fail
124 lines • 3.32 kB
JavaScript
Object.defineProperty(exports, "__esModule", { value: true });
exports.executeCommand = executeCommand;
exports.runWithHook = runWithHook;
exports.findHook = findHook;
exports.determineArtFile = determineArtFile;
const child_process_1 = require("child_process");
const config_1 = require("./config");
const display_1 = require("./display");
/**
* Execute command and capture result
*/
function executeCommand(args) {
const command = args.join(' ');
const result = {
command: command,
exitCode: 0,
stdout: '',
stderr: ''
};
try {
// Execute command and capture output
const output = (0, child_process_1.execSync)(command, {
encoding: 'utf8',
stdio: 'pipe',
shell: true,
windowsHide: true
});
result.stdout = output;
result.exitCode = 0;
// Display the command output
if (output) {
process.stdout.write(output);
}
}
catch (error) {
// Command failed
result.exitCode = error.status || 1;
result.stderr = error.stderr || '';
// Display error output
if (error.stdout) {
process.stdout.write(error.stdout);
}
if (error.stderr) {
process.stderr.write(error.stderr);
}
}
return result;
}
/**
* Run command with hook support
*/
function runWithHook(args) {
let config = null;
// Try to load config
try {
config = (0, config_1.loadConfig)();
}
catch (error) {
// Config not found, just run the command without hooks
console.error(error.message);
const result = executeCommand(args);
process.exit(result.exitCode);
return;
}
// Execute the command
const result = executeCommand(args);
// Find matching hook
const command = args.join(' ');
const hook = findHook(config, command);
if (hook) {
// Determine which art to show
const artFile = determineArtFile(hook, result.exitCode);
if (artFile) {
(0, display_1.showArt)(artFile);
}
}
// Exit with the same code as the command
process.exit(result.exitCode);
}
/**
* Find hook configuration for command
*/
function findHook(config, command) {
if (!config || !config.hooks) {
return null;
}
// First, try exact match
if (config.hooks[command]) {
return config.hooks[command];
}
// Then, try prefix match (for commands with arguments)
for (const [pattern, hook] of Object.entries(config.hooks)) {
// Skip wildcard for now
if (pattern === '*')
continue;
// Check if command starts with the pattern
if (command.startsWith(pattern + ' ') || command === pattern) {
return hook;
}
}
// Finally, check for wildcard
if (config.hooks['*']) {
return config.hooks['*'];
}
return null;
}
/**
* Determine which art file to display
*/
function determineArtFile(hook, exitCode) {
if (!hook) {
return null;
}
if (exitCode === 0) {
// Success
return hook.success || null;
}
else {
// Error
return hook.error || null;
}
}
//# sourceMappingURL=execute.js.map
;