enfsensure

Ensure file, folder, link or symlink existence in file system for node fs module


Keywords
append, chmod, create, data, ensure, dir, directory, fs, file, file system, link, mkdir, mkdirp, stream, symlink, recursive, write stream
License
CC-BY-4.0
Install
npm install enfsensure@1.0.0

Documentation

Build Status Build status Codacy Badge Donate

NPM

enfsensure

Module that add ensure functionality to node fs module

enfs stands for [E]asy [N]ode [fs]

This module is intended to work as a sub-module of enfs

Description

This module will add various method that allows the user to ensure the creation of a file, directory, link or symlink in the file system, if the parent directory of the item to be ensured don't exist it will be automatically created.

  • This module will add following methods to node fs module:
    • ensureFile
    • ensureFileSync
    • ensureDir
    • ensureDirSync
    • ensureLink
    • ensureLinkSync
    • ensureSymlink
    • ensureSymlinkSync

Usage

enfsensure

    var enfsensure = require("enfsensure");

Errors

All the methods follows the node culture.

  • Async: Every async method returns an Error in the first callback parameter
  • Sync: Every sync method throws an Error.

Additional Methods

ensureFile

  • ensureFile(path, [options], callback)

Ensures the file existence in the file system, if the file don't exist it will be created.

[options]:

  • fs (Object): an alternative fs module to use (default will be enfspatch)
  • mode (String or Number): the mode to set to the file, if the file already exists and have a different mode it will be change to the new mode
  • data (String): Data that will be written to the file, if it already exists the data will be appended if append flag is true.
  • append (Boolean): if true data will be appended to the file (Default: false)
  • encoding (String): specify the file encoding
  • dirMode (String or Number): the mode that will be set to the parent directory
  • stream (Boolean): if true this method will return a WriteStream to the ensured file
  • streamOptions (Object): Options that will be passed to the WriteStream createWriteStream()

Sync: * ensureFileSync(path,[options])

    enfsensure.ensureFile("/path/to/any/file",{ data: "contents", encoding: "utf8" },function(err){
        if(!err){
            console.log("data was written to the file");
        }
    });

Stream

    enfsensure.ensureFile("/path/to/any/file",{ stream: true, streamOptions: { autoClose: true} },function(err, stream){
        if(!err){
            console.log("stream is a WriteStream object.");
        }
    });

ensureDir

  • ensureDir(path, [options], callback)

Ensure directory existence in the file system, if directory don't exist it will be created

[options]:

  • fs (Object): an alternative fs module to use (default will be enfspatch)
  • mode (String or Number): the mode to set to the directory, if the directory already exists and is different then it will be changed to the new mode

Sync: * ensureDirSync(path,[options])

    enfsensure.ensureDir("/path/to/the/new/folder", function(err, path){
        if(!err){
            console.log("directory was created in the path: "+path);
        }
    });

ensureLink

  • ensureLink(srcPath, dstPath, [options], callback)

Ensure link creation in the file system. link

[options]:

  • fs (Object): an alternative fs module to use (default will be enfspatch)

Sync: * ensureLinkSync(srcPath, dstPath, [options])

    enfsensure.ensureLink("/srcPath", "/dstPath", function(err, dstPath){
        //do something
    });

ensureSymlink

  • ensureSymlink(target, path, [options], callback)

Ensure symlink creation in the file system. symlink

[options]:

  • fs (Object): an alternative fs module to use (default will be enfspatch)
  • type (String): symlink type (Default: file)

Sync: * ensureSymlinkSync(path,[options])

    enfsensure.ensureSymlink("./foo", "./new-port", "file", function(err, dstPath){
        //do something
    });

License

Creative Commons Attribution 4.0 International License

Copyright (c) 2016 Joao Parreira joaofrparreira@gmail.com GitHub

This work is licensed under the Creative Commons Attribution 4.0 International License. To view a copy of this license, visit CC-BY-4.0.