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);
}
});
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}`);
}
})
the path you want to create
listen to some event
use this method to check if a folder follows the shape
see Shape.constructor docs for more details Shape.constructor
the path to check
if true will throw an error if the folder doesn't match the shape
use this method for adding folder to you shape
see Shape.constructor docs for more details Shape.constructor
name of the folder
use this method for adding files to your shape
see Shape.constructor docs for more details Shape.constructor
the base (name with extension) of the file
file default content (content will be added on creation)
a function used to validate the file
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:
regex or string
if you want files to be created with some content
if you want to validate the content of the file
Generated using TypeDoc
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); } });
since v3.8.0 please use __rest symbol instead