@webdevtoday/grok-cli
Version:
A sophisticated CLI tool for interacting with xAI Grok 4, featuring conversation history, file reference, custom commands, memory system, and genetic development workflows
98 lines • 3.44 kB
JavaScript
;
/**
* File writing tool
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.WriteTool = void 0;
const base_1 = require("./base");
const fs_extra_1 = require("fs-extra");
const path_1 = require("path");
/**
* Tool for writing files to the filesystem
*/
class WriteTool extends base_1.BaseTool {
constructor() {
super('Write', 'Write content to files on the filesystem');
}
validateParams(params) {
return (typeof params['filePath'] === 'string' &&
params['filePath'].length > 0 &&
typeof params['content'] === 'string' &&
(params['encoding'] === undefined || typeof params['encoding'] === 'string') &&
(params['createDirs'] === undefined || typeof params['createDirs'] === 'boolean'));
}
async execute(params) {
const { filePath, content, encoding = 'utf-8', createDirs = true } = params;
try {
const resolvedPath = (0, path_1.resolve)(filePath);
const dirPath = (0, path_1.dirname)(resolvedPath);
// Check if file already exists
const fileExists = await (0, fs_extra_1.pathExists)(resolvedPath);
// Create parent directories if needed
if (createDirs) {
await (0, fs_extra_1.ensureDir)(dirPath);
}
// Write file content
await (0, fs_extra_1.writeFile)(resolvedPath, content, encoding);
// Calculate some metadata
const lines = content.split('\n').length;
const bytes = Buffer.byteLength(content, encoding);
return {
success: true,
output: `File written successfully: ${filePath}`,
metadata: {
filePath: resolvedPath,
bytes,
lines,
encoding,
created: !fileExists,
updated: fileExists,
},
};
}
catch (error) {
return {
success: false,
output: '',
error: error instanceof Error ? error.message : String(error),
metadata: { filePath },
};
}
}
getParameterSchema() {
return {
type: 'object',
properties: {
filePath: {
type: 'string',
description: 'Path where the file should be written',
},
content: {
type: 'string',
description: 'Content to write to the file',
},
encoding: {
type: 'string',
description: 'File encoding (default: utf-8)',
default: 'utf-8',
},
createDirs: {
type: 'boolean',
description: 'Create parent directories if they don\'t exist (default: true)',
default: true,
},
},
required: ['filePath', 'content'],
};
}
getUsageExamples() {
return [
'Write a new configuration file',
'Create a README.md file',
'Save generated code to a file',
'Write logs to a file',
];
}
}
exports.WriteTool = WriteTool;
//# sourceMappingURL=write.js.map