3.2.17 ā€¢ Published 1 year ago

extra-fs v3.2.17

Weekly downloads
19
License
BSD-3-Clause
Repository
github
Last release
1 year ago

Useful additions to inbuilt fs module. šŸ“¦ Node.js, šŸ“œ Files, šŸ“° Docs.

The file system we use today has its origins in the UNIX file system. A file is simply a chunk of data (bytes). Each file has a locally unique name and associated properties which can be grouped together in a hierarchy of directories. A directory is a list of files and other directories, and has a parent directory (except the root directory /). Given the tree-structure, a file can be uniquely identified by its full path.

Access to a file is provided by the file system though the use of a file descriptor. This can be obtained from open by providing the file path, and open mode (read/write). Once a file has been opened and necessary operations performed, such as reading it with read or writing to it with write, it should be closed with close. Reading or writing multiple blocks of a file at a time can be achieved with readv and writev. Once data is written to a file beyond it current size, it is automatically expanded. However, to reduce the size of a file (i.e., to truncate it), ftruncate is used. The additional properties attached to a file, such as access/update time, access permissions, or ownership of a file can be obtained with fstat, and updated with futimes, fchmod, and fchown respectively.

Convenience methods for accessing a file can also be used, which do not require us to go through the process of opening and closing files, and meticulously reading it or writing to it in blocks. These include readFile, writeFile, appendFile, truncate, stat, utimes. chmod and chown are applicable or directories as well. A file can be opened as a stream for reading with createReadStream and for writing with createWriteStream, and copied to another path with copyFile. Access to a file or directory can be checked with access, renamed/moved with rename, copied to another path (recursively) with cp, and removed (recursively) with rm.

Similar to opening/closing of a file, a directory can be opened (to read its contents) with opendir, and read with readdir. A new directory can be created with mkdir, and an empty directory can be removed with rmdir (a non-empty directory can be recursively removed with rm). A temporary directory (with a unique random suffix) can be created with mkdtemp. Changes to a file or dierctory can be observed with watch.

The file system also provides symbolic links and hard links which simply point to an existing file or directory. Symbolic (or soft) links point to a file or directory through its path, while hard links point directly to the file. Therefore renaming/moving or removing the original file makes symbolic links dangling (pointing to non-existent file, and thus will not work) but hard links continue to work (think of them as shared pointers to a memory block). A hard link can be created with link, and a symbolic link (also called symlink) with symlink. Note that symlinks are basically files containing the path of target file or directory, and this path can be read with readlink. The full path of a symlink can be resolved with realpath. Hard links point directly to files, and thus do not have a "target" path. The additional properties attached to a symlink, such as access/update time, or ownership of the symlink can be obtained with lstat, and updated with lutimes, and lchown respectively. The access permissions of a symlink cannot be changed.

This package provides async versions of functions (in addition to the existing sync and callback-based functions) in the inbuilt fs module, exposed as *Async() from the fs.promises namespace. They can be used with Promise-based asynchronous programming using the await keyword. In addition, callback-based functions, such as readFile, also behave as async functions when a callback is not provided. The idea behind using *Async() function names is to provide a flat module.

In addition, convenience functions such as readFileText, writeFileText, readJson and writeJson are included. For performing file/directory existence check async exists, assertExists, and assertNotExists can be used. Cleanup of one-item outer directories (which are usually created upon extracting a compressed file) can be performed with dehuskdir. This package previously included which(), which is now instead suitably included in extra-child-process package.

Stability: Experimental.

const xfs = require('extra-fs');


// 1. Read file text.
async function example1() {
  var text = xfs.readFileTextSync('.npmignore');
  var text = await xfs.readFileText('.npmignore');
  // ā†’ # Source only
  // ā†’ .gitmodules
  // ā†’ .github/
  // ā†’ .docs/
  // ā†’ ...
}
example1();


// 2. Read JSON file.
async function example2() {
  var json = xfs.readJsonSync('package.json');
  var json = await xfs.readJson('package.json');
  // ā†’ {
  // ā†’   name: 'extra-fs',
  // ā†’   version: '3.0.27',
  // ā†’   description: 'Useful additions to inbuilt fs module.',
  // ā†’   ...
  // ā†’ }
}
example2();


// 3. Assert that a file exists.
async function example3() {
  if (!(await xfs.exists('LICENSE'))) throw 'May I see you license sir?';
  await xfs.assertExists('LICENSE');
}
example3();


// 4. Get contents of a directory.
async function example4() {
  var contents = xfs.readdirSync('src');
  var contents = await xfs.readdir('src');
  // ā†’ [ 'index.ts' ]
}
example4();

Index

PropertyDescription
openOpen a file.
closeClose a file.
readRead data from a file.
writeWrite data to a file.
readvRead an array of buffers from file.
writevWrite an array of buffers to file.
ftruncateShorten (truncate) a file.
futimesChange the file system timestamps of a file.
fstatGet information about a file.
fchmodSet the permissions of a file.
fchownSet the owner of a file.
...
linkCreate a hard link to a file or directory.
symlinkCreate a symbolic link to a file or directory.
readlinkRead the contents of a symbolic link.
realpathGet canonical pathname by resolving ., ..
lutimesChange the file system timestamps of an object.
lstatGet information about a file, without dereferencing symbolic links.
lchownSet the owner of a symbolic link.
...
readFileRead the entire contents of a file.
writeFileWrite data to the file, replace if it already exists.
appendFileAppend data to a file, create if it does not exist.
truncateShorten (truncate) a file.
unlinkRemove a file or symbolic link.
utimesChange the file system timestamps of an object.
statGet file status.
copyFileCopy source file to destination, overwite if exists.
readFileTextRead file text with Unix EOL.
writeFileTextWrite file text with system EOL.
readJsonRead JSON file as value.
writeJsonWrite object to JSON file.
watchFileWatch for changes on filename.
unwatchFileStop watching for changes on filename.
watchWatch for changes on filename, where filename is either a file or a directory.
createReadStreamCreate a readable stream with 64kb highWaterMark.
createWriteStreamCreate a writeable stream from a desired start position.
...
mkdirCreate a directory.
mkdtempCreate a unique temporary directory.
opendirOpen a directory.
readdirOpen a directory.
rmdirRemove a directory.
dehuskdirRemove outer one-item directories.
...
accessTest a user's permissions for the file or directory.
chmodChange the permissions of a file.
chownChange owner and group of a file.
renameRename/move a file or directory.
cpCopy source directory to destination, overwite if exists.
rmRemove a file or directory.
existsCheck if file or directory exists.
assertExistsAssert that a file or directory exists.
assertNotExistsAssert that a file or directory does not exist.

References

npm.io ORG DOI

extrafsaccessAsyncappendFileAsyncchmodAsyncchownAsynccopyFileAsynccpAsynclchownAsynclinkAsynclstatAsynclutimesAsyncmkdirAsyncmkdtempAsyncopenAsyncopendirAsyncreadFileAsyncreaddirAsyncreadlinkAsyncrealpathAsyncrenameAsyncrmAsyncrmdirAsyncstatAsyncsymlinkAsynctruncateAsyncunlinkAsyncutimesAsyncwriteFileAsyncconstantspromisesrealpathSyncDirDirentFsErrorReadStreamStatsWriteStreamBigIntOptionsBigIntStatsCopyOptionsFSWatcherMakeDirectoryOptionsObjectEncodingOptionsOpenDirOptionsReadAsyncOptionsReadResultReadSyncOptionsReadVResultRmDirOptionsRmOptionsStatOptionsStatSyncFnStatSyncOptionsStatWatcherStatsBaseWatchFileOptionsWatchOptionsWriteResultWriteVResultBufferEncodingOptionDehuskDirCallbackEncodingOptionExistsCallbackMakeDirectoryCallbackMakeDirectoryTemporaryCallbackModeNoParamCallbackOpenCallbackOpenDirCallbackOpenModePathLikePathOrFileDescriptorReadCallbackReadDirCallbackReadDirOptionsReadFileCallbackReadFileTextCallbackReadJsonCallbackReadPositionReadVCallbackReadlinkCallbackRealpathCallbackStatCallbackSymlinkTypeTimeLikeWatchEventTypeWatchListenerWriteCallbackWriteFileOptionsWriteVCallbackaccessaccessSyncappendFileappendFileSyncassertExistsassertExistsAsyncassertExistsSyncassertNotExistsassertNotExistsAsyncassertNotExistsSyncchmodchmodSyncchownchownSyncclosecloseAsynccloseSynccopyFilecopyFileSynccpcpSynccreateReadStreamcreateWriteStreamdehuskdirdehuskdirAsyncdehuskdirSyncexistsexistsAsyncexistsSyncfchmodfchmodAsyncfchmodSyncfchownfchownAsyncfchownSyncfdatasyncfdatasyncAsyncfdatasyncSyncfstatfstatAsyncfstatSyncfsyncfsyncAsyncfsyncSyncftruncateftruncateAsyncftruncateSyncfutimesfutimesAsyncfutimesSynclchownlchownSynclinklinkSynclstatlstatSynclutimeslutimesSyncmkdirmkdirSyncmkdtempmkdtempSyncopenopenSyncopendiropendirSyncreadreadAsyncreadFilereadFileSyncreadFileTextreadFileTextAsyncreadFileTextSyncreadJsonreadJsonAsyncreadJsonSyncreadSyncreaddirreaddirSyncreadlinkreadlinkSyncreadvreadvAsyncreadvSyncrealpathrenamerenameSyncrmrmSyncrmdirrmdirSyncstatstatSyncsymlinksymlinkSynctruncatetruncateSyncunlinkunlinkSyncunwatchFileutimesutimesSyncwatchwatchFilewritewriteAsyncwriteFilewriteFileSyncwriteFileTextwriteFileTextAsyncwriteFileTextSyncwriteJsonwriteJsonAsyncwriteJsonSyncwriteSyncwritevwritevAsyncwritevSync
3.2.17

1 year ago

3.2.16

1 year ago

3.2.13

1 year ago

3.2.12

1 year ago

3.2.15

1 year ago

3.2.14

1 year ago

3.2.11

2 years ago

3.0.89

2 years ago

3.0.87

2 years ago

3.0.88

2 years ago

3.0.81

2 years ago

3.0.82

2 years ago

3.0.80

2 years ago

3.0.85

2 years ago

3.0.86

2 years ago

3.0.83

2 years ago

3.0.84

2 years ago

3.2.2

2 years ago

3.2.1

2 years ago

3.2.0

2 years ago

3.2.6

2 years ago

3.2.5

2 years ago

3.2.4

2 years ago

3.2.3

2 years ago

3.0.92

2 years ago

3.0.90

2 years ago

3.0.91

2 years ago

3.0.67

2 years ago

3.0.68

2 years ago

3.0.65

2 years ago

3.0.66

2 years ago

3.0.69

2 years ago

3.0.60

2 years ago

3.0.63

2 years ago

3.0.64

2 years ago

3.0.61

2 years ago

3.0.62

2 years ago

3.2.9

2 years ago

3.2.8

2 years ago

3.2.7

2 years ago

3.1.3

2 years ago

3.0.78

2 years ago

3.1.2

2 years ago

3.0.79

2 years ago

3.1.1

2 years ago

3.0.76

2 years ago

3.1.0

2 years ago

3.0.77

2 years ago

3.1.4

2 years ago

3.0.70

2 years ago

3.0.71

2 years ago

3.0.74

2 years ago

3.0.75

2 years ago

3.0.72

2 years ago

3.2.10

2 years ago

3.0.73

2 years ago

3.0.45

2 years ago

3.0.46

2 years ago

3.0.43

2 years ago

3.0.44

2 years ago

3.0.49

2 years ago

3.0.47

2 years ago

3.0.48

2 years ago

3.0.41

2 years ago

3.0.42

2 years ago

3.0.40

2 years ago

3.0.56

2 years ago

3.0.57

2 years ago

3.0.54

2 years ago

3.0.55

2 years ago

3.0.58

2 years ago

3.0.59

2 years ago

3.0.52

2 years ago

3.0.53

2 years ago

3.0.50

2 years ago

3.0.51

2 years ago

3.0.34

2 years ago

3.0.35

2 years ago

3.0.32

2 years ago

3.0.33

2 years ago

3.0.38

2 years ago

3.0.39

2 years ago

3.0.36

2 years ago

3.0.37

2 years ago

3.0.17

2 years ago

3.0.23

2 years ago

3.0.24

2 years ago

3.0.21

2 years ago

3.0.22

2 years ago

3.0.27

2 years ago

3.0.28

2 years ago

3.0.25

2 years ago

3.0.26

2 years ago

3.0.20

2 years ago

3.0.18

2 years ago

3.0.19

2 years ago

3.0.30

2 years ago

3.0.31

2 years ago

3.0.29

2 years ago

3.0.12

2 years ago

3.0.4

2 years ago

3.0.13

2 years ago

3.0.3

2 years ago

3.0.10

2 years ago

3.0.2

2 years ago

3.0.11

2 years ago

3.0.1

2 years ago

3.0.16

2 years ago

3.0.8

2 years ago

3.0.7

2 years ago

3.0.14

2 years ago

3.0.6

2 years ago

3.0.15

2 years ago

3.0.5

2 years ago

3.0.0

2 years ago

3.0.9

2 years ago

2.0.5

3 years ago

2.0.4

4 years ago

2.0.3

4 years ago

2.0.2

4 years ago

2.0.1

4 years ago

2.0.0

4 years ago

1.3.14

4 years ago

1.3.13

4 years ago

1.3.12

4 years ago

1.3.11

4 years ago

1.3.10

4 years ago

1.3.9

4 years ago

1.3.8

4 years ago

1.3.7

4 years ago

1.3.6

4 years ago

1.3.5

4 years ago

1.3.4

4 years ago

1.3.3

4 years ago

1.3.2

4 years ago

1.3.1

4 years ago

1.3.0

4 years ago

1.2.10

4 years ago

1.2.11

4 years ago

1.2.9

4 years ago

1.2.0

4 years ago

1.2.8

4 years ago

1.2.7

4 years ago

1.2.6

4 years ago

1.2.5

4 years ago

1.2.4

4 years ago

1.2.3

4 years ago

1.2.2

4 years ago

1.2.1

4 years ago

1.1.3

5 years ago

1.1.2

5 years ago

1.1.1

5 years ago

1.1.0

5 years ago

1.0.3

5 years ago

1.0.2

5 years ago

1.0.1

5 years ago

1.0.0

5 years ago

0.1.0

5 years ago

0.0.8

5 years ago

0.0.7

5 years ago

0.0.6

5 years ago

0.0.5

5 years ago

0.0.4

5 years ago

0.0.3

5 years ago

0.0.2

5 years ago

0.0.1

5 years ago