fs-classes

Handle the filesystem via classes

Usage no npm install needed!

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

README

fs-classes

Handle the file system via classes. This is an object orientated approach for the commonly used fs and fs-extra packages written in typescript. This package does also provide additional functionality like zip/unzip, filtering files with glob patterns and reading mime types of files.

Installation

npm install fs-classes

Usage

Exports

import { NodeFile, NodeDir, NodeZip } from "fs-classes";

NodeFile

Create a wrapper class for a file:

import { NodeFile } from "fs-classes";

//throws an Error if the file does not exist
const file = new NodeFile(false, "/any/path/text.txt");

//Creates the file if it does not exist
const file2 = new NodeFile(true, "/any/other/path/text.txt");

File meta data

let mimeType = file.type;  // -> text/plain
let extension = file.extension; // -> .txt
let size = file.size;  // -> size in bytes
let stats = file.stats;  // fs like stats
...

Read the file like

let str = await file.text();  //file content as string
let buffer = await file.read();  //file content as buffer
let stream = file.stream();  //provides a ReadStream of the file
...

Edit the file like

await file.write("File Content");  //overwrites the file
await file.append("additional content"); //appends to the file
...

NodeDir

Create a wrapper class for a directory:

import { NodeDir } from "fs-classes";

//throws an Error if the directory does not exist
const dir = new NodeDir(false, "/any/path/dir");

//Creates the directory if tit does not exist
const dir2 = new NodeDir(true, "/any/other/path/dir");

Read entries:

let entry = await dir.getEntry("rel/path"); //NodeFile/NodeDir
let entries = await dir.getEntries(); //list of NodeDir/NodeFile

Glob:

let matches = await dir.glob("build/**.*.js"); //List of NodeFile
let matches2 = await dir.glob("**/subDir"); //List of NodeFile/NodeDir

Add/Create entries

//Add
let newEntry = await dir.addEntry(file, {
    move: false,                //move or copy the existing file
    entryPath: "rel/path",      //relative entry path
    entryName: "new.txt",       //entry name
    overwrite: false            //overwrite existing files
});

//Create
let newEntry2 = await dir.createEntry("rel/path", {
    overwrite: false,           //overwrite existing files
    forceFile: false,           //forces the entry to be a file
    forceDir: false             //forces the entry to be a directory
});
...

NodeZip

Create a zip from an existing file/directory (using adm-zip):

let zipped = await fileOrDirectory.zip(); //returns NodeZip

Extract zip:

//returns NodeFile/NodeDir
let unzipped = zipped.unzip(destDir, {
    overwrite: false,           //overwrite existing files
    name: true,                 //name of extracted file/directory
})

//returns NodeFile/NodeDir
let extracted = zipped.extractEntry("rel/entry/path", destDir, {
    overwrite: false,           //overwrite existing files
    maintainEntry: true,        //maintain zip entry
    name: true,                 //name of extracted file/directory
});
...

NodeFile / NodeDir / NodeZip

All classes support:

await file.copy(destDir, {
    overwrite: true;
    name: "newFile";
})

await dir.move(destDir);
await zipped.rename("newName.zip", { overwrite: true });
...

Properties

NodeFile / NodeDir / NodeZip

  • deleted
  • name
  • path
  • size
  • lastModified

NodeFile

  • type
  • extension
  • alias

Methods

NodeFile / NodeDir / NodeZip

  • zip
  • rename
  • move
  • copy
  • stats
  • delete
  • equals
  • dir

NodeFile

  • read
  • arrayBuffer
  • stream
  • text
  • write
  • append

NodeDir

  • tmpDir (static)
  • getEntries
  • getEntry
  • addEntry
  • createEntry
  • glob

NodeZip

  • asFile
  • create (static)
  • unzip
  • getEntry
  • getEntries
  • addFile
  • addDir
  • deleteItem
  • extractEntry
  • rewrite