Options
All
  • Public
  • Public/Protected
  • All
Menu

Class Shape<T>

Type parameters

Hierarchy

  • Shape

Index

Constructors

Properties

Methods

Constructors

constructor

  • the Shape class is a class that helps you create folder with a certain shape (hierarchy) or test if a folder have a certain shape (hierarchy) example 1:

    imagine that you want to test if a folder

    (1) has a folder named "js_ts_files" which only contains js or ts files

    (2) has a folder named "text_folders" which only contains folder with contains text files

    (3) has an empty folder named "empty_folder"

    (4) everything else in the folder must be a json file

    import { Shape, __rest } from "fs-pro";
    // (1) create the shape
    const shape = new Shape({
       // a key to reference in the code (if you need)
       js_ts_files:
          // Shape.Dir means this should be a folder named "js_ts_files"
          Shape.Dir(
             // the name of the folder in the file system
             "js_ts_files",
             // the pattern of the files (you could also use regexs)
             Shape.Pattern("*.js|*.ts")
          ),
       // passing an oject this folder must folder this shape (hierarchy)
       text_folders: Shape.Dir("text_folders", {
          // __rest means anything else in the folder
          //  and because we set __rest here as an object it means that
          //  __rest is a folder
          [__rest]: {
            [__rest]: Shape.Pattern("*.txt")
         }
       }),
       // this regex doesn't match anything except an empty string
       // so it wont match any file name so the folder must be empty
       empty_folder: Shape.Dir("empty_folder", Shape.Pattern(/$^/)),
       // __rest means any thing else (not mentioned)
       // Shape.Pattern means it must be a file following
       // the given pattern
       [__rest]: Shape.Pattern("*.json")
    });
    
    // (2) test if a folder follows it
    // this will throw an error if the folder doesn't match the shape
    shape.validate("/path/to/folder", true);
    // or if you want more details
    console.log(shape.validate("/path/to/folder"));
    // this will not throw an error even if the folder doesn't match the shape
    // but it will log an array of errors saying where the folder doesn't match
    // the shape

    example 2:

    imagine that you want to create a

    (1) folder named "something" that has a file named "hi.txt" and contains "hello world"

    (2) you want to log the path of everything created

    // (1) create the shape
    const shape = new Shape({
      something: {
        hi: Shape.File("hi.txt", "hello world")
      }
    });
    
    // (2) use createShapeInst
    shape.createShapeInst("/path/to/folder", {
      // gets called when a file or folder is created
      onCreate(fileOrFolder) {
        console.log(fileOrFolder.path);
      }
    });
    deprecated

    since v3.8.0 please use __rest symbol instead

    Parameters

    Returns Shape

  • the Shape class is a class that helps you create folder with a certain shape (hierarchy) or test if a folder have a certain shape (hierarchy) example 1:

    imagine that you want to test if a folder

    (1) has a folder named "js_ts_files" which only contains js or ts files

    (2) has a folder named "text_folders" which only contains folder with contains text files

    (3) has an empty folder named "empty_folder"

    (4) everything else in the folder must be a json file

    import { Shape, __rest } from "fs-pro";
    // (1) create the shape
    const shape = new Shape({
       // a key to reference in the code (if you need)
       js_ts_files:
          // Shape.Dir means this should be a folder named "js_ts_files"
          Shape.Dir(
             // the name of the folder in the file system
             "js_ts_files",
             // the pattern of the files (you could also use regexs)
             Shape.Pattern("*.js|*.ts")
          ),
       // passing an oject this folder must folder this shape (hierarchy)
       text_folders: Shape.Dir("text_folders", {
          // __rest means anything else in the folder
          //  and because we set __rest here as an object it means that
          //  __rest is a folder
          [__rest]: {
            [__rest]: Shape.Pattern("*.txt")
         }
       }),
       // this regex doesn't match anything except an empty string
       // so it wont match any file name so the folder must be empty
       empty_folder: Shape.Dir("empty_folder", Shape.Pattern(/$^/)),
       // __rest means any thing else (not mentioned)
       // Shape.Pattern means it must be a file following
       // the given pattern
       [__rest]: Shape.Pattern("*.json")
    });
    
    // (2) test if a folder follows it
    // this will throw an error if the folder doesn't match the shape
    shape.validate("/path/to/folder", true);
    // or if you want more details
    console.log(shape.validate("/path/to/folder"));
    // this will not throw an error even if the folder doesn't match the shape
    // but it will log an array of errors saying where the folder doesn't match
    // the shape

    example 2:

    imagine that you want to create a

    (1) folder named "something" that has a file named "hi.txt" and contains "hello world"

    (2) you want to log the path of everything created

    // (1) create the shape
    const shape = new Shape({
      something: {
        hi: Shape.File("hi.txt", "hello world")
      }
    });
    
    // (2) use createShapeInst
    shape.createShapeInst("/path/to/folder", {
      // gets called when a file or folder is created
      onCreate(fileOrFolder) {
        console.log(fileOrFolder.path);
      }
    });

    Parameters

    • shape: T

    Returns Shape

Properties

shapeObj

shapeObj: T

Methods

createShapeInst

  • createShapeInst(path: string, eventsListeners?: createEvents): {} & { __dir: Dir }
  • use this method to create a folder with matches the shape example for eventsListeners

    const shape = new Shape({ ... });
    
    shape.createShapeInst("/path/to/folder", {
      onCreate(fileOrFolder) {
        console.log(`CREATED: ${fileOrFolder.path}`);
      }
    })

    Parameters

    • path: string

      the path you want to create

    • Optional eventsListeners: createEvents

      listen to some event

    Returns {} & { __dir: Dir }

validate

  • validate(path: string, crash?: undefined | false | true): { arr: fsProErr[]; push: (err?: fsProErr | errArr) => void }

Static Dir

  • Dir(name: string, fileType: ShapeFilePattern): ShapeDir
  • Dir(name: string, fileType: ShapeFile): ShapeDir
  • Dir<K>(name: string, shapeObj: K): K & { __computed: string }
  • Dir<K>(name: string, shapeObj: K): K & { __computed: string }

Static File

  • File(base: string, defaultContent?: string | BufferType, validator?: ShapeFile["validator"]): ShapeFile
  • use this method for adding files to your shape

    see Shape.constructor docs for more details Shape.constructor

    Parameters

    • base: string

      the base (name with extension) of the file

    • Optional defaultContent: string | BufferType

      file default content (content will be added on creation)

    • Optional validator: ShapeFile["validator"]

      a function used to validate the file

    Returns ShapeFile

Static Pattern

  • Pattern(pattern: string | RegExp, defaultContent?: string | BufferType, validator?: ShapeFilePattern["validator"]): ShapeFilePattern
  • use this method for adding file patterns to your shape see Shape.constructor docs for more details Shape.constructor NOTE: if you use string as pattern it will be converted to a regex like this:

    1. convert any "*" to ".*" (unless there's a backslash before it)
    2. it splits the regex by "|" and test every one separately (unless there's a backslash before it)

    Parameters

    • pattern: string | RegExp

      regex or string

    • Optional defaultContent: string | BufferType

      if you want files to be created with some content

    • Optional validator: ShapeFilePattern["validator"]

      if you want to validate the content of the file

    Returns ShapeFilePattern

Generated using TypeDoc