Documentation
Plugin
API

Plugin API

The Sec5 plugin API empowers you to extend the functionality of Sec5 to handle complex applications, intricate file formats, and custom validation needs. Plugins provide a modular and flexible approach to tailoring Sec5's behavior precisely to your project's requirements.

Using Plugins

Integrating plugins into your Sec5 workflow is straightforward. Simply import the desired plugin(s) within your Sec5 configuration file (sec5.config.js) and add them to the plugins array:

sec5.js.org
import myCustomPlugin from 'path/to/my-custom-plugin'
import anotherPlugin from 'another-plugin'
 
export default {
  plugins: [myCustomPlugin, anotherPlugin]
}

Plugin Development

Plugins are essentially JavaScript functions or objects that participate in Sec5's file checking process. Here's the general structure of a plugin function:

// biome-plugin.js (example plugin)
export default function myPlugin(config) {
  // Perform operations based on the provided config object
  // Access configuration values (e.g., `config.allowNoFormat`)
  // Perform validation or processing steps on files
}

API Reference (Arguments):

  • config: An object containing the resolved Sec5 configuration (refer to Sec5 documentation for specific configuration options). This allows plugins to access and potentially modify runtime settings.

Plugin Execution Flow:

  1. During file checking, Sec5 invokes each registered plugin function with the current configuration.
  2. The plugin receives the config object as an argument.
  3. The plugin can access and perform custom validation or processing on files based on the config and Sec5's processing context.

Error Handling and Logging:

  • Implement error handling (e.g., using try...catch blocks) within your plugins to gracefully handle unexpected situations.
  • Leverage Sec5's built-in logging functions (error and info) to provide informative messages during the checking process:
import { error, info } from 'sec5'
 
error('An error occurred while processing the file:', fileName)
info('Validation successful for file:', filePath)

Example: Custom File Type Validation

This example demonstrates a plugin that validates files based on a custom extension:

export default function customFileTypePlugin(config) {
  if (!config.customFileTypes) return // Skip if not configured
 
  const files = globSync('*.myformat', { ignore: 'node_modules' }) // Adjust glob pattern as needed
 
  files.forEach((file) => {
    // Implement your custom validation logic for files with the `.myformat` extension
    if (!isValidMyFormatFile(file)) {
      error('Invalid custom file format:', file)
    } else {
      info('Valid custom file format:', file)
    }
  })
}

Remember to replace isValidMyFormatFile with your actual validation logic specific to the .myformat file format.