|
2 | 2 | cbapicategory:
|
3 | 3 | - name: createFile
|
4 | 4 | link: /docs/api/apiaccess/fs/createFile
|
5 |
| - description: 'Creates a new file with the specified name and content.' |
| 5 | + description: 'Creates a new file with the specified name and content. Supports creating files with complex content including code definitions.' |
6 | 6 | - name: createFolder
|
7 | 7 | link: /docs/api/apiaccess/fs/createFolder
|
8 |
| - description: 'Creates a new folder at the specified location.' |
| 8 | + description: 'Creates a new folder at the specified location. Useful for organizing project structures and temporary workspaces.' |
9 | 9 | - name: readFile
|
10 | 10 | link: /docs/api/apiaccess/fs/readFile
|
11 |
| - description: 'Reads the contents of a file and returns it as a string.' |
| 11 | + description: 'Reads the contents of a file and returns it as a string. Works with various file types including text, code, and configuration files.' |
12 | 12 | - name: updateFile
|
13 | 13 | link: /docs/api/apiaccess/fs/updateFile
|
14 |
| - description: 'Updates an existing file with new content.' |
| 14 | + description: 'Updates an existing file with new content. Requires separate filename and path parameters.' |
| 15 | + - name: writeToFile |
| 16 | + link: /docs/api/apiaccess/fs/writeToFile |
| 17 | + description: 'Creates or updates a file and writes data to it. Uses a single relative path parameter and overwrites existing content.' |
15 | 18 | - name: deleteFile
|
16 | 19 | link: /docs/api/apiaccess/fs/deleteFile
|
17 |
| - description: 'Deletes a specified file from the filesystem.' |
| 20 | + description: 'Deletes a specified file from the filesystem. Includes proper error handling and cleanup workflows.' |
18 | 21 | - name: deleteFolder
|
19 | 22 | link: /docs/api/apiaccess/fs/deleteFolder
|
20 |
| - description: 'Deletes a specified folder and its contents.' |
| 23 | + description: 'Deletes a specified folder and its contents. Requires the folder to be empty before deletion.' |
21 | 24 | - name: listFile
|
22 | 25 | link: /docs/api/apiaccess/fs/listFile
|
23 |
| - description: 'Lists all files in the specified directory.' |
| 26 | + description: 'Lists all files in the specified directory. Supports recursive listing and enhanced file information modes.' |
24 | 27 | - name: listCodeDefinitionNames
|
25 | 28 | link: /docs/api/apiaccess/fs/listCodeDefinitionNames
|
26 |
| - description: 'Lists all code definition names within a project.' |
| 29 | + description: 'Extracts and lists all code definition names (functions, classes, methods) from source files within a project.' |
27 | 30 | - name: searchFiles
|
28 | 31 | link: /docs/api/apiaccess/fs/searchFiles
|
29 |
| - description: 'Searches for files matching a specific pattern or criteria.' |
30 |
| - - name: writeToFile |
31 |
| - link: /docs/api/apiaccess/fs/writeToFile |
32 |
| - description: 'Creates or updates a file and writes data to it.' |
| 32 | + description: 'Searches for files matching a regex pattern within file contents. Supports advanced regex patterns and file type filtering.' |
33 | 33 | ---
|
34 | 34 |
|
35 | 35 | # fs
|
36 |
| -<CBAPICategory /> |
| 36 | + |
| 37 | +The `fs` module provides comprehensive file system operations for CodeboltJS. It includes methods for creating, reading, updating, and deleting files and folders, as well as advanced features like code definition extraction and content searching. |
| 38 | + |
| 39 | +<CBAPICategory /> |
| 40 | + |
| 41 | +## Key Features |
| 42 | + |
| 43 | +### File Operations |
| 44 | +- **File Creation**: Create files with simple or complex content, including code definitions |
| 45 | +- **File Reading**: Read various file types (text, code, configuration files) |
| 46 | +- **File Updates**: Two methods available - `updateFile` (separate name/path) and `writeToFile` (single path) |
| 47 | +- **File Deletion**: Safe deletion with proper error handling and cleanup workflows |
| 48 | + |
| 49 | +### Folder Operations |
| 50 | +- **Folder Creation**: Create single folders or complex directory structures |
| 51 | +- **Folder Deletion**: Remove folders with proper cleanup procedures |
| 52 | +- **Directory Listing**: List files with options for recursive scanning and enhanced information |
| 53 | + |
| 54 | +### Advanced Features |
| 55 | +- **Code Analysis**: Extract function names, class names, and other code definitions from source files |
| 56 | +- **Content Search**: Search within file contents using regex patterns with file type filtering |
| 57 | +- **Batch Operations**: Perform operations on multiple files or folders efficiently |
| 58 | + |
| 59 | +## Common Workflows |
| 60 | + |
| 61 | +### Complete File Lifecycle |
| 62 | +```js |
| 63 | +// Create → Read → Update → Delete |
| 64 | +await codebolt.fs.createFile('example.txt', 'Initial content', '.'); |
| 65 | +const content = await codebolt.fs.readFile('./example.txt'); |
| 66 | +await codebolt.fs.updateFile('example.txt', '.', 'Updated content'); |
| 67 | +await codebolt.fs.deleteFile('example.txt', '.'); |
| 68 | +``` |
| 69 | + |
| 70 | +### Project Structure Creation |
| 71 | +```js |
| 72 | +// Create organized project structure |
| 73 | +await codebolt.fs.createFolder('my-project', '.'); |
| 74 | +await codebolt.fs.createFolder('src', './my-project'); |
| 75 | +await codebolt.fs.createFolder('tests', './my-project'); |
| 76 | +await codebolt.fs.createFile('index.js', 'console.log("Hello");', './my-project/src'); |
| 77 | +``` |
| 78 | + |
| 79 | +### Code Analysis Workflow |
| 80 | +```js |
| 81 | +// Create code file and extract definitions |
| 82 | +const jsCode = ` |
| 83 | +class MyClass { |
| 84 | + method() { return 'test'; } |
| 85 | +} |
| 86 | +function myFunction() { return 'hello'; } |
| 87 | +`; |
| 88 | +await codebolt.fs.createFile('code.js', jsCode, '.'); |
| 89 | +const definitions = await codebolt.fs.listCodeDefinitionNames('.'); |
| 90 | +``` |
| 91 | + |
| 92 | +## Error Handling |
| 93 | + |
| 94 | +All fs methods return promises and should be used with proper error handling: |
| 95 | + |
| 96 | +```js |
| 97 | +try { |
| 98 | + const result = await codebolt.fs.createFile('test.txt', 'content', '.'); |
| 99 | + if (result.success) { |
| 100 | + console.log('✅ Operation successful'); |
| 101 | + } else { |
| 102 | + console.log('❌ Operation failed:', result.message); |
| 103 | + } |
| 104 | +} catch (error) { |
| 105 | + console.error('Error:', error.message); |
| 106 | +} |
| 107 | +``` |
0 commit comments