Skip to main content
Deno 2 is finally here 🎉️
Learn more

fs pro

A small package to manage your files and folders easily

nest badge Codacy Badge npm npm npm bundle size GitHub Workflow Status

see the API documentation here


Table of Contents

Features

  • works on both node and deno
  • you don’t have to get the path of the file or directory every single time you want to do something with it
  • Strong typed and documented in the code
  • provides a method to parse json files .json()
  • object will be automatically be stringified to json when you use the write method
  • have a file structure system see Shape
  • will delete the whole dir when you use the delete method
  • provide advanced watching methods see Dir.watch method and File.watch method
  • you could add any method you like on any class you like via plugins see how to create plugins and addPlugin

Installation

via npm:

npm i fs-pro

via yarn:

yarn add fs-pro

via deno.land

import * as fsPro from "http://deno.land/x/fs_pro@version/mod.ts";

via nest.land

import * as fsPro from "https://x.nest.land/fs-pro@version/mod.ts";

Usage

import { File, Dir, Shape } from "fs-pro";

// creating a file object
const file = new File(__dirname, "hello_world.txt");
// creating a directory object
const dir = new Dir(__dirname, "some_dir");

file
  // writing to the file
  .write("hello ");
  // append content to the file
  .append("world");

console.log(file.read().toString()) // => "hello world"
// and there's much more in the docs

dir
  // create the directory (in the actual hard disk aka file system)
  .create()
  // creating a directory in the directory
  .createDir("sub_dir");
  // creating a file in the directory (this create a file object)
  .createFile("text.txt")
// and there's much more in the docs

// imagine that you want to check if a folder
// 1. has an folder named "something"
// 2. has an folder named "js_ts_files" that only contains js and ts files
// 3. has a text file name "hello world.txt" and by default this file contains "hello world"
// 4. any thing else in this folder must be a txt file
// you can do that easily with Shape

// (1) Create the Shape
const shape = new Shape({
  // Shape.File means that hello_world is a file
  // the second arg is the default content
  hello_world: Shape.File("hello world.txt", "hello world"),
  // Shape.Dir means that "something" is a folder
  // Shape.Pattern tells the types of files that can be in the folder
  // (like, .txt, .js, ...etc) here *.* means any thing
  something: Shape.Dir("something", Shape.Pattern("*.*")),
  // Notice js_ts_files is the name that you can reference in your code
  // but "js_ts_files" passed in Shape.Dir in the actual name in the files system
  js_ts_files: Shape.Dir("js_ts_files", Shape.Pattern("*.js|*.ts")),
  // __rest means any thing else in the folder
  __rest: Shape.Pattern("*.txt")
});

// (2) Check the folder
// returns an array of errors
const errs = shape.validate(folder_path);

if (errs.length === 0) console.log("no errors");

// or..
// when you pass true shape.validate will throw an error
// if the folder doesn't match the shape
shape.validate(folder_path, true);

Api

Creating plugins

import { Plugin } from "fs-pro/types";
import { addPlugin } from "fs-pro";

const myPlugin: Plugin = {
  name: "your-plugin-name",
  required: [anyRequiredPlugin] // optional
  plugin: [
    {
      methodName: "myMethod",
      className: "File", // could be the name of any class in the library (File or Dir or Shape)
      isStatic: false, // if true the method you add will be static
      func(...myArgs: any[]){
        // your code...
      }
    }
  ]
}

export default myPlugin

Using Plugins

import { addPlugin } from "fs-pro";
import myPlugin from "./my-plugin";

addPlugin(myPlugin);

Change Log

see our Change Log

we don’t leave our change log here cause it’s quite long

License

Copyright (c) AliBasicCoder 2020