npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

cli-fs

v1.0.4

Published

Utility functions for working with the filesystem

Downloads

38,311

Readme

File System

Utility functions for working with the file system.

The primary purpose of this module is to allow for expressions like test(1) in order to test file existence, file type etc.

Install

npm install cli-fs

Test

Note that rather than mocking lots of files (and file descriptors) these tests run against standard system files, which means if you are not running a UNIX variant (and likely OSX only) these tests may well fail. It is also quite possible that they only work on this machine as permissions can get out of sync, however, effort has been made to use pretty standard values, see the files list to see the paths used for testing.

npm test

Expressions

This module is designed to support the majority of file tests as described by man test it does not support complex expressions such as -gt or != which require two operands, this is better achieved with pure javascript.

Use this module when you just want to answer questions like is this file a directory? without all the error handling actually testing that condition implies.

Supported expressions:

  • -b: True if file exists and is a block special file.
  • -c: True if file exists and is a character special file.
  • -d: True if file exists and is a directory.
  • -e: True if file exists (regardless of type).
  • -f: True if file exists and is a regular file.
  • -n: True if the length of string is nonzero.
  • -p: True if file is a named pipe (FIFO).
  • -r: True if file exists and is readable ([1]).
  • -s: True if file exists and has a size greater than zero.
  • -t: True if the file descriptor number is open and associated with a terminal.
  • -w: True if the file exists and is writable ([1]).
  • -x: True if file exists and is executable. True indicates only that the execute flag is on. If file is a directory, true indicates that file can be searched.
  • -z: True if the length of string is zero.
  • -L: True if the file exists and is a symbolic link.
  • -S: Ture if file exists and is a socket.

API

All functions support asynchronous and synchronous operation, specifying a callback function implies asynchronous behaviour.

test(expr, value, [callback])

Test an expression.

  • expr: The expression to test.
  • value: The value to test.
  • callback: An optional callback function, forces an asynchronous test.

Returns a boolean indicating whether the test passed or throws an error if the expression is not supported.

readable(path, [callback])

Determine if a file is readable, this method opens the file for reading using the r flag and immediately closes the file [1].

  • path: The file system path.
  • callback: An optional callback function, forces an asynchronous test.

Returns a boolean indicating whether the test passed.

writable(path, [callback])

Determine if a file is writable, this method opens the file for reading using the r+ flag and immediately closes the file. Because the r+ flag is used this method also tests that the file is readable [1].

  • path: The file system path.
  • callback: An optional callback function, forces an asynchronous test.

Returns a boolean indicating whether the test passed.

executable(path, [callback])

Determine if a file is has the executable bit set. This method does not ensure that attempting to execute the file will not result in an EPERM error, use with caution.

  • path: The file system path.
  • callback: An optional callback function, forces an asynchronous test.

Returns a boolean indicating whether the test passed.

supported

Array of the supported test expression characters.

home()

Attempt to resolve the user's home directory using the environment variables HOME, HOMEPATH, USERPROFILE.

Returns the user's home directory or the empty string if none of the environment variables are set.

Caveats

  1. The only reliable way to test whether a file is readable or writable is to attempt to open and close the file, hence these tests incur a performance overhead. If you need to perform these tests with a lot of files it is better deferred until you actually need to read or write to the files.

License

Everything is MIT. Read the license if you feel inclined.