@samatawy/rules
    Preparing search index...

    Class ConfigFileReader

    Helper class to safely read workspace configurations from files and load them into a workspace. It supports reading from strings or a configuration file in JSON5 format.

    N.B. File paths should be absolute or relative to the current working directory.

    The reader will determine the type of each file based on these naming conventions and use the appropriate parsing method to load its content into the workspace.

    NB: This class relies on Node's 'fs' module for file system access, so it is intended for use in Node.js environments. In browser environments, file reading should be handled differently; this class will report errors in a browser environment.

    Hierarchy (View Summary)

    Implements

    Index

    Constructors

    Properties

    options: Partial<FileReaderOptions>

    Methods

    • This method is intended for use in the browser where File System access is not supported. In a node environment, use readFromFile() or readFromFiles().

      Parameters

      • fileContent: string

        The content of the file to parse.

      Returns ConfigFileResult

      The result of parsing, including the successfully parsed constants, functions, types, rules, and any errors encountered.

      Will throw an error if the file content cannot be parsed as valid JSON5. Errors will also be logged using the Logger.

    • Attempt to read a file from a specified path and load its content into the workspace. The reader will determine the type of the file based on its name and extension, and use the appropriate parsing method.

      Parameters

      • path: string

        the path of the file to read.

      Returns ConfigFileResult

      the result of parsing, including the successfully parsed workspaces and any errors encountered.

      Will throw an error if the file cannot be read or if its content cannot be parsed as valid JSON5. Errors will also be logged using the Logger.

    • Ensure a workspace with the specified name and options exists in the engine, then attempt to read and load the specified files into that workspace.

      Parameters

      • config: WorkspaceConfig

        The configuration for the workspace, including its name, options, and files to load.

      • mode: WorkspaceLoaderOptions

        The mode for loading the workspace, including how to handle existing workspaces and file reading options.

      Returns boolean

      true if the workspace was successfully loaded, otherwise false. Errors will be logged using the Logger.

    • Provide a node file system module to be used for reading files. This method or the equivalent loadFileSystem() must be called before attempting to read files in a Node environment.

      Parameters

      • fs: __module

        the node fs module to use.

      Returns this

      the current instance for chaining.

    • Dynamically import the Node.js file system module in a Node environment. This method or the equivalent withFS() must be called before attempting to read files in a Node environment. In a browser environment, this method will log an error since file system access is not supported.

      Returns Promise<any>

      the imported fs module if successful, otherwise undefined.

    • Validate the parsed content of the configuration file.

      Parameters

      • content: any

        the raw parsed content from the configuration file to validate.

      Returns ConfigFileResult

      the validated content.

      Will throw an error if the content is invalid. Errors will also be logged using the Logger.