fs-pro

working with files easily

Usage no npm install needed!

<script type="module">
  import fsPro from 'https://cdn.skypack.dev/fs-pro';
</script>

README

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

// the Shape class is a class that helps you manage your directory
// the Shape instance (or inst for short) is the Shape applied to a directory
// the Shape instance reference is a JS object the references the Shape instance
// the Shape constructor takes the Shape of your directory
// every key in the object passed in is an identifier for the file or dir
const shape = new Shape({
  // for adding files use Shape.File with the file name
  some_file: Shape.File("some_file.txt"),
  // for adding a directory of files use Shape.Dir with
  // the dir name and file name regex (a custom type of regex to test if the filename matches it)
  // see Shape.File doc for more information about filename regex
  some_dir: Shape.Dir("some_dir", Shape.File("test[0-9]{3}.txt|*.any")),
  // for adding a shaped folder use Shape.Dir with the directory name
  // and the shape of it
  some_shaped_dir: Shape.Dir("shaped_dir", {
    file_1: Shape.File("file_1.txt"),
    // ...
  }),
  // __rest tells Shape that any thing not mentioned
  // must follow the given shape
  __rest: Shape.File("*.txt"),
});

const shapeInstRef = shape.createShapeInst(target_dir);

shapeInstRef.some_file.write("hello world");

shapeInstRef.some_dir.createFile("test100.txt");

shapeInstRef.some_shaped_dir.file_1.write("hello world");

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);

Licence

copyright (c) AliBasicCoder 2020