mirror of
https://gitlab.com/soapbox-pub/shakespeare.git
synced 2026-12-05 16:23:36 +00:00
134 lines
4.5 KiB
TypeScript
134 lines
4.5 KiB
TypeScript
import { join } from "path-browserify";
|
|
import type { JSRuntimeFS } from "../JSRuntime";
|
|
import type { ShellCommand, ShellCommandResult } from "./ShellCommand";
|
|
import { createSuccessResult, createErrorResult } from "./ShellCommand";
|
|
|
|
/**
|
|
* Implementation of the 'head' command
|
|
* Display the first lines of files
|
|
*/
|
|
export class HeadCommand implements ShellCommand {
|
|
name = 'head';
|
|
description = 'Display the first lines of files';
|
|
usage = 'head [-n lines] [file...]';
|
|
|
|
private fs: JSRuntimeFS;
|
|
|
|
constructor(fs: JSRuntimeFS) {
|
|
this.fs = fs;
|
|
}
|
|
|
|
async execute(args: string[], cwd: string, input?: string): Promise<ShellCommandResult> {
|
|
const { options, files } = this.parseArgs(args);
|
|
|
|
// If input is provided (from pipe), process that input
|
|
if (input !== undefined) {
|
|
const lines = input.split('\n');
|
|
const selectedLines = lines.slice(0, options.lines);
|
|
const result = selectedLines.join('\n');
|
|
return createSuccessResult(result + (result && !result.endsWith('\n') ? '\n' : ''));
|
|
}
|
|
|
|
const targetFiles = files.length > 0 ? files : ['-']; // stdin if no files
|
|
|
|
try {
|
|
const outputs: string[] = [];
|
|
|
|
for (const filePath of targetFiles) {
|
|
try {
|
|
if (filePath === '-') {
|
|
return createErrorResult(`${this.name}: reading from stdin is not supported`);
|
|
}
|
|
|
|
// Handle absolute paths
|
|
if (filePath.startsWith('/') || filePath.startsWith('\\') || /^[A-Za-z]:[\\/]/.test(filePath)) {
|
|
return createErrorResult(`${this.name}: absolute paths are not supported: ${filePath}`);
|
|
}
|
|
|
|
const absolutePath = join(cwd, filePath);
|
|
|
|
// Check if path exists and is a file
|
|
const stats = await this.fs.stat(absolutePath);
|
|
|
|
if (stats.isDirectory()) {
|
|
return createErrorResult(`${this.name}: ${filePath}: Is a directory`);
|
|
}
|
|
|
|
// Read file content
|
|
const content = await this.fs.readFile(absolutePath, 'utf8');
|
|
const lines = content.split('\n');
|
|
|
|
// Take first n lines (but don't add extra newline if file doesn't end with one)
|
|
const selectedLines = lines.slice(0, options.lines);
|
|
|
|
// If we're showing multiple files, add a header
|
|
if (targetFiles.length > 1) {
|
|
outputs.push(`==> ${filePath} <==`);
|
|
}
|
|
|
|
// Join lines and preserve original line endings
|
|
const result = selectedLines.join('\n');
|
|
outputs.push(result);
|
|
|
|
// Add separator between files (except for last file)
|
|
if (targetFiles.length > 1 && filePath !== targetFiles[targetFiles.length - 1]) {
|
|
outputs.push('');
|
|
}
|
|
|
|
} catch (error) {
|
|
if (error instanceof Error) {
|
|
if (error.message.includes('ENOENT') || error.message.includes('not found')) {
|
|
return createErrorResult(`${this.name}: ${filePath}: No such file or directory`);
|
|
} else if (error.message.includes('EACCES') || error.message.includes('permission')) {
|
|
return createErrorResult(`${this.name}: ${filePath}: Permission denied`);
|
|
} else {
|
|
return createErrorResult(`${this.name}: ${filePath}: ${error.message}`);
|
|
}
|
|
} else {
|
|
return createErrorResult(`${this.name}: ${filePath}: Unknown error`);
|
|
}
|
|
}
|
|
}
|
|
|
|
return createSuccessResult(outputs.join('\n') + (outputs.length > 0 ? '\n' : ''));
|
|
|
|
} catch (error) {
|
|
return createErrorResult(`${this.name}: ${error instanceof Error ? error.message : 'Unknown error'}`);
|
|
}
|
|
}
|
|
|
|
private parseArgs(args: string[]): { options: { lines: number }; files: string[] } {
|
|
const options = { lines: 10 }; // Default to 10 lines
|
|
const files: string[] = [];
|
|
let i = 0;
|
|
|
|
while (i < args.length) {
|
|
const arg = args[i];
|
|
|
|
if (arg === '-n') {
|
|
// Next argument should be the number of lines
|
|
i++;
|
|
if (i < args.length) {
|
|
const lineCount = parseInt(args[i], 10);
|
|
if (!isNaN(lineCount) && lineCount > 0) {
|
|
options.lines = lineCount;
|
|
}
|
|
}
|
|
} else if (arg.startsWith('-n')) {
|
|
// -n10 format
|
|
const lineCount = parseInt(arg.slice(2), 10);
|
|
if (!isNaN(lineCount) && lineCount > 0) {
|
|
options.lines = lineCount;
|
|
}
|
|
} else if (arg.startsWith('-') && arg !== '-') {
|
|
// Other options (ignore for now)
|
|
} else {
|
|
files.push(arg);
|
|
}
|
|
|
|
i++;
|
|
}
|
|
|
|
return { options, files };
|
|
}
|
|
} |