Skip to content

How to Create a Tool in ToolX

Introduction to Tool Creation

Creating custom tools in ToolX allows for tailored solutions to specific file processing needs. Tools in ToolX have a defined structure and key methods that handle the processing logic.

Understanding onEveryFile and onBody

  • onEveryFile: This method is invoked for each file that the tool processes. It is where you can define the actions performed on individual files.
  • onBody: This method is called after all files have been processed by onEveryFile. It is used for final processing steps or to compile results.

The Significance of next()

  • The next() function is crucial in managing the asynchronous flow of file processing. It ensures that the tool moves to the next file or step only after the current operation is complete.
  • Proper use of next() is essential for maintaining order and efficiency in the processing pipeline.

Defining File Extensions (exts)

  • Specifying file extensions allows you to define which file types the tool will process. For instance, an image processing tool might only handle .jpg and .png files.
  • This feature adds a layer of flexibility and control in tool customization.

Code Example with Explanations

Below is an example of a custom ToolX tool with detailed comments:

javascript
class CustomTool {
    // Constructor for initializing the tool
    constructor(options) {
        this.options = options;
        // Define the file extensions this tool will process
        this.exts = ['jpg', 'png'];
    }

    // Method called on before all files have been processed
    async onStart(pathIn, pathOut, options) {
        // File processing logic here
        // ...
    }


    // Method called for each file
    async onEveryFile(next, { file, options }) {
        // File processing logic here
        // ...

        // Call next() to move to the next file or processing step
        next(file);
    }

    // Method called after all files have been processed
    async onBody(next, { files, options }) {
        // Final processing logic or compilation of results
        // ...

        // Finalize the processing
        next();
    }
}

export default CustomTool;

This example illustrates the basic structure and key methods of a ToolX tool. The comments provide insights into each part of the tool's functionality.

Conclusion

Understanding the architecture and flow of ToolX tools is vital for creating effective custom solutions. This guide aims to provide a foundational understanding, empowering developers to build their own tools within the ToolX ecosystem.

Released primarily under the MIT license.