@samatawy/rules
    Preparing search index...

    Class WorkspaceFilesReader

    Helper class to safely read rules, functions, types, and constants from files and load them into a workspace. It supports reading from strings, individual files, or entire folders, and can handle different file formats such as markdown, general, constants, rules, types, and functions files. The reader can be configured to either accept all valid components while logging errors for invalid ones, or to reject the entire file if any component is invalid.

    N.B. Declarations do not need to be in order, although that is still recommended.

    N.B. This is a transactional safe reader. If you provide a workspace and select the option accept: 'all', then that workplace will not be affected if any errors are encountered.

    Files should be named according to their content, with the following conventions:

    • Constants files should include '.constants' in their name (e.g. 'my.constants.txt').
    • Functions files should include '.functions' in their name (e.g. 'my.functions.txt').
    • Types files should include '.types' in their name (e.g. 'my.types.txt').
    • Rules files should include '.rules' in their name (e.g. 'my.rules.txt').
    • Markdown files should have a '.md' extension (e.g. 'my.logic.md').
    • General (mixed) files can have any name (e.g. 'my.logic.txt').

    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

    • Create a new reader instance for loading rules, functions, types, and constants into the provided workspace.

      N.B. After creation you must either call withFS() or loadFileSystem() to provide access to the file system before attempting to read files in a Node environment.

      Parameters

      • workspace: Workspace

        The workspace into which the components will be loaded.

      • accept: "all" | "partial" = 'all'

        Determines whether to accept all valid components while logging errors for invalid ones ('partial') or to reject the entire file if any component is invalid ('all').

      Returns WorkspaceFilesReader

    Properties

    options: Partial<FileReaderOptions>
    ruleParser: RuleParser
    functionParser: FunctionParser
    typeParser: TypeParser

    Methods

    • Attempt to read all files from a specified folder path and load their content into the workspace. N.B. If a folder contains subfolders, the reader will attempt to read files from those subfolders as well. If a file path is accidentally provided instead of a folder path, the reader will attempt to read that individual file.

      If a folder path is included with the individual file paths, the reader will attempt to read files from that folder as well. The reader will determine the type of each file based on its name and extension, and use the appropriate parsing method. If any file fails to load and the accept option is set to 'partial', the reader will continue loading the remaining files while logging errors for the failed ones. If the accept option is set to 'all', the reader will stop loading further files upon encountering an error in any file.

      Parameters

      • folderPath: string

        the path of the folder containing the files to read.

      Returns boolean

      true if components were registered successfully (according to 'all' or 'partial' option), otherwise false.

    • Attempt to read all given files and load their contents into the workspace. If a folder path is included with the individual file paths, the reader will attempt to read files from that folder as well.

      The reader will determine the type of each file based on its name and extension, and use the appropriate parsing method. If any file fails to load and the accept option is set to 'partial', the reader will continue loading the remaining files while logging errors for the failed ones. If the accept option is set to 'all', the reader will stop loading further files upon encountering an error in any file.

      Parameters

      • paths: string[]

        an array of file paths to read.

      Returns boolean

      true if components were registered successfully (according to 'all' or 'partial' option), otherwise false.

    • Attempt to read a file from a specified path and load its content into the workspace. If a folder path is included instead of a file path, the reader will fail silently and return false. 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 boolean

      true if components were registered successfully (according to 'all' or 'partial' option), otherwise false.

    • 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.