Negligent Pachinko Machine
Have opinions about JavaScript? We want to hear them. Take the 2018 JavaScript Ecosystem Survey »

wFiles

0.6.430 • Public • Published

wFiles Build Status Build status

Collection of classes to abstract files systems. Many interfaces provide files, but not called as file systems and treated differently. For example server-side gives access to local files and browser-side HTTP/HTTPS protocol gives access to files as well, but in the very different way, it does the first. This problem forces a developer to break fundamental programming principle DRY and make code written to solve a problem not applicable to the same problem, on another platform/technology.

Files treats any file-system-like interface as files system. Files combines all files available to the application into the single namespace where each file has unique Path/URI, so that operating with several files on different files systems is not what user of the module should worry about. If Files does not have an adapter for your files system you may design it providing a short list of stupid methods fulfilling completely or partly good defined API and get access to all sophisticated general algorithms on files for free. Who said files is only externals? Files makes possible to treat internals of a program as files system(s). Use the module to keep DRY.

Files manipulation library of middle level. Module offers several implementations of single interface, called ( FileProvider ) to perform file operations in the same manner with different sources/destinations.

Avaible operations:

  • File read/write operations.
  • Creating read/write steams.
  • Operations with file stats object.
  • File create,delete,rename,copy operations.
  • Making soft/hard links.

Installation

npm install wFiles

FileProvider.HardDrive

Allows files manipulations on local drive.

Declaration
var _ = wTools;
var provider = _.FileProvider.HardDrive();

FileProvider.Extract

Allows file manipulations on filesTree - object based on some folders/files tree, where folders are nested objects with same depth level as in real folder and contains some files that are properties with corresponding names and file content as a values.

Structure example:
var tree =
{
 "some_dir" :
 {
   'some_file.txt' : "content",
   'empty_dir' : {}
 }
}
Declaration
var _ = wTools;
var provider = _.FileProvider.Extract({ filesTree : tree });

Usage:

Example #1
/*Read file synchronously*/
var data = provider.fileRead
({
  pathFile : 'my_file',
  sync : 1
});
console.log( data );
 
/*Read file asynchronously*/
provider.fileRead({ pathFile : 'my_file' })
.got( function( err, data )
{
  if( err )
  throw err;
  console.log( data );
});
Example #2
/*Write to file synchronously*/
provider.fileWrite
({
  pathFile : 'my_file',
  data : 'some data'
})
 
/*Write to file asynchronously*/
provider.fileWrite
({
  pathFile : 'my_file',
  data : 'some data',
  sync : 0
})
.got( function( err )
{
  if( err )
  throw err;
  console.log( 'Success' );
});
Example #3
/*Create dir synchronously*/
provider.directoryMake( 'my_dir' );
 
/*Create dir asynchronously*/
provider.directoryMake
({
   pathFile : 'a',
   sync : 0
 })
.got( function( err )
{
  if( err )
  throw err;
  /*some code after dir creation*/
});
Example #4
/*Getting file stats object sync*/
/*error throwing is disabled by default, use throwing : 1 to turn on*/
var stats = provider.fileStat( 'my_dir' );
if( stats )
console.log( stats );
 
/*async*/
provider.fileStat
({
   pathFile : 'my_dir',
   throwing : 1,
   sync : 0
})
.got( function( err, stats )
{
  if( err )
  throw err;
  console.log( stats );
});
Example #5
/*Copy file sync*/
provider.fileCopy
({
  pathDst : 'my_file2',
  pathSrc : 'my_file'
});
 

install

npm i wFiles

Downloadsweekly downloads

28,254

version

0.6.430

license

MIT

homepage

github.com

repository

Gitgithub

last publish

collaborators

  • avatar
Report a vulnerability