createFile
codebolt.fs.createFile(fileName: string, source: string, filePath: string): CreateFileResponse
Creates a new file with the specified content at the given path.
Parameters
Name | Type | Description |
---|---|---|
fileName | string | The name of the file to create. |
source | string | The source content to write into the file. |
filePath | string | The path where the file should be created. |
Examples
Basic File Creation
// Create a simple text file
const result = await codebolt.fs.createFile(
'example.txt',
'This is the content of the file.',
'/home/user/documents'
);
console.log('File created:', result);
Creating Files with Complex Content
// Create a JavaScript file with code content
const jsCodeContent = `
// Sample JavaScript file with various code definitions
class TestClass {
constructor(name) {
this.name = name;
}
testMethod() {
return 'Hello from ' + this.name;
}
static staticMethod() {
return 'Static method called';
}
}
function testFunction(param1, param2) {
return param1 + param2;
}
const arrowFunction = (x, y) => x * y;
module.exports = { TestClass, testFunction, arrowFunction };
`;
const createResult = await codebolt.fs.createFile(
'test-code-definitions.js',
jsCodeContent,
'.'
);
console.log('JavaScript file created:', createResult);
Error Handling
try {
const result = await codebolt.fs.createFile(
'test-file.txt',
'This is a test file created by CodeboltJS',
'.'
);
if (result.success) {
console.log('✅ File created successfully:', result.fileName);
console.log('Message:', result.message);
} else {
console.log('❌ File creation failed:', result.message);
}
} catch (error) {
console.error('Error creating file:', error.message);
}
Creating Files in Current Directory
// Create a file in the current working directory
const result = await codebolt.fs.createFile(
'fs-test-file.txt',
'This is a test file created by CodeboltJS FS test',
'.'
);
console.log('File created in current directory:', result);
// Let's assume you want to create a file named example.txt in the /home/user/documents directory with some content.
codebolt.fs.createFile('example.txt', 'This is the content of the file.', '/home/user/documents');