Giter VIP home page Giter VIP logo

fs-plus's Introduction

Atom and all repositories under Atom will be archived on December 15, 2022. Learn more in our official announcement

fs plus

CI

Yet another filesystem helper based on node's fs module. This library exports everything from node's fs module but with some extra helpers.

Using

npm install fs-plus
fs = require 'fs-plus'

Documentation

getHomeDirectory()

Returns the absolute path to the home directory.

absolute(relativePath)

Make the given path absolute by resolving it against the current working directory.

Params

  • String relativePath: The string representing the relative path. If the path is prefixed with '~', it will be expanded to the current user's home directory.

Return

  • String: The absolute path or the relative path if it's unable to determine its real path.

normalize(pathToNormalize)

Normalize the given path treating a leading ~ segment as referring to the home directory. This method does not query the filesystem.

Params

  • String pathToNormalize: The string containing the abnormal path. If the path is prefixed with '~', it will be expanded to the current user's home directory.

Return

  • String Returns a normalized path.

tildify(pathToTildify)

Convert an absolute path to tilde path on Linux and macOS: /Users/username/dev => ~/dev

Params

  • String pathToTildify: The string containing the full path.

Return

  • String Returns a tildified path.

getAppDataDirectory()

Get path to store application specific data.

Return

  • String Returns the absolute path or null if platform isn't supported

    • macOS: ~/Library/Application Support/
    • Windows: %AppData%
    • Linux: /var/lib

isAbsolute(pathToCheck)

Is the given path absolute?

Params

  • String pathToCheck: The relative or absolute path to check.

Return

  • Bolean Returns true if the path is absolute, false otherwise.

existsSync(pathToCheck)

Returns true if a file or folder at the specified path exists.

isDirectorySync(directoryPath)

Returns true if the given path exists and is a directory.

isDirectory(directoryPath)

Asynchronously checks that the given path exists and is a directory.

isFileSync(filePath)

Returns true if the specified path exists and is a file.

isSymbolicLinkSync(symlinkPath)

Returns true if the specified path is a symbolic link.

isSymbolicLink(symlinkPath, callback)

Calls back with true if the specified path is a symbolic link.

isExecutableSync(pathToCheck)

Returns true if the specified path is executable.

getSizeSync(pathToCheck)

Returns the size of the specified path.

listSync(rootPath, extensions)

Returns an Array with the paths of the files and directories contained within the directory path. It is not recursive.

Params

  • String rootPath: The absolute path to the directory to list.
  • Array extensions: An array of extensions to filter the results by. If none are given, none are filtered (optional).

list(rootPath, extensions)

Asynchronously lists the files and directories in the given path. The listing is not recursive.

listTreeSync(rootPath)

Get all paths under the given path.

Params

  • String rootPath The {String} path to start at.

Return

  • Array Returns an array of strings under the given path.

moveSync(source, target)

Moves the file or directory to the target synchronously.

removeSync(pathToRemove)

Removes the file or directory at the given path synchronously.

writeFileSync(filePath, content, options)

Open, write, flush, and close a file, writing the given content synchronously. It also creates the necessary parent directories.

writeFile(filePath, content, options, callback)

Open, write, flush, and close a file, writing the given content asynchronously. It also creates the necessary parent directories.

copySync(sourcePath, destinationPath)

Copies the given path recursively and synchronously.

makeTreeSync(directoryPath)

Create a directory at the specified path including any missing parent directories synchronously.

makeTree(directoryPath, callback)

Create a directory at the specified path including any missing parent directories asynchronously.

traverseTreeSync(rootPath, onFile, onDirectory)

Recursively walk the given path and execute the given functions synchronously.

Params

  • String rootPath: The string containing the directory to recurse into.
  • Function onFile: The function to execute on each file, receives a single argument the absolute path.
  • Function onDirectory: The function to execute on each directory, receives a single argument the absolute path (defaults to onFile). If this function returns a falsy value then the directory is not entered.

traverseTree(rootPath, onFile, onDirectory, onDone)

Public: Recursively walk the given path and execute the given functions asynchronously.

md5ForPath(pathToDigest)

Hashes the contents of the given file.

Params

  • String pathToDigest: The string containing the absolute path.

Return

  • String Returns a string containing the MD5 hexadecimal hash.

resolve(loadPaths, pathToResolve, extensions)

Finds a relative path among the given array of paths.

Params

  • Array loadPaths: An array of absolute and relative paths to search.
  • String pathToResolve The string containing the path to resolve.
  • Array extensions An array of extensions to pass to {resolveExtensions} in which case pathToResolve should not contain an extension (optional).

Return

Returns the absolute path of the file to be resolved if it's found and undefined otherwise.

resolveOnLoadPath()

Like .resolve but uses node's modules paths as the load paths to search.

resolveExtension(pathToResolve, extensions)

Finds the first file in the given path which matches the extension in the order given.

Params

  • String pathToResolve: the string containing relative or absolute path of the file in question without the extension or '.'.
  • Array extensions: the ordered array of extensions to try.

Return

Returns the absolute path of the file if it exists with any of the given extensions, otherwise it's undefined.

isCompressedExtension(ext)

Returns true for extensions associated with compressed files.

isImageExtension(ext)

Returns true for extensions associated with image files.

isPdfExtension(ext)

Returns true for extensions associated with pdf files.

isBinaryExtension(ext)

Returns true for extensions associated with binary files.

isReadmePath(readmePath)

Returns true for files named similarily to 'README'

isMarkdownExtension(ext)

Returns true for extensions associated with Markdown files.

isCaseInsensitive()

Is the filesystem case insensitive? Returns true if case insensitive, false otherwise.

isCaseSensitive()

Is the filesystem case sensitive? Returns true if case sensitive, false otherwise.

statSyncNoException(path[, options])

Calls fs.statSync, catching all exceptions raised. This method calls fs.statSyncNoException when provided by the underlying fs module (Electron < 3.0). Returns fs.Stats if the file exists, false otherwise.

lstatSyncNoException(path[, options])

Calls fs.lstatSync, catching all exceptions raised. This method calls fs.lstatSyncNoException when provided by the underlying fs module (Electron < 3.0). Returns fs.Stats if the file exists, false otherwise.

fs-plus's People

Contributors

abeestrada avatar alhadis avatar aminya avatar ashleygwilliams avatar damieng avatar darangi avatar daviwil avatar iolsen avatar ionicabizau avatar ivankravets avatar jakubzitny avatar kevinsawicki avatar lee-dohm avatar lgeiger avatar marekhrabe avatar mcolyer avatar mjansing avatar mnquintana avatar oclbdk avatar pdehaan avatar peterdavehello avatar pjeby avatar rayjanwilson avatar renz45 avatar rgbkrk avatar sadick254 avatar sberan avatar smashwilson avatar ssorallen avatar zertosh avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

fs-plus's Issues

optional Promise API

What do you think about supporting async and await (aka. a Promise-based API)? I think it would be greate since packages relying on fs-plus (e.g. atom/season) could easily use the cool ES7 async/await as well.
There is another fs extension node-fs-extra that returns Promises if no callback is provided (which I really like). Of course,node-fs-extra carries along some extra methods but those should be able to be ignored easily from what's exported by fs-plus (in case you even care).

Anyway node-fs-extra can be used as a drop in replacement for fs. So it'd be really easy for you to also support the Promise API by using node-fs-extra as a dependency in your package.json instead of the native fs module.

In case you're interested I'd be willing to create a PR.

Read and write JSON methods

Would it be useful to add methods for reading and writing JSON?

I'm currently using the read-json library for that:

var Fs = require("fs-plus");
Fs.readJSON = require("read-json");

Proxy is not defined in Node 4

The new export using Proxy in 2.10.0 breaks in Node 4 since Proxy is not available. I would consider this a breaking change, but at the very least, please specify engines in the package.json.

traverseTree should work when a regular file is passed in

Robustness enhancement.
Should work when passed the path to a regular (non-directory) file:

fs.traverseTreeSync('/home/vagrant/projs/api-core-js/package.json', function(path) { console.log(path);},function(path) { console.log(path);} );
undefined

Works fine when given a directory.

fs.traverseTreeSync('/home/vagrant/projs/api-core-js/', function(path) { console.log(path);},function(path) { console.log(path);} );
/home/vagrant/projs/api-core-js/.git
/home/vagrant/projs/api-core-js/.gitignore
/home/vagrant/projs/api-core-js/.ib_meta
/home/vagrant/projs/api-core-js/Makefile
/home/vagrant/projs/api-core-js/docker
/home/vagrant/projs/api-core-js/lib
/home/vagrant/projs/api-core-js/log
/home/vagrant/projs/api-core-js/node_modules
/home/vagrant/projs/api-core-js/package.json
/home/vagrant/projs/api-core-js/scratch
/home/vagrant/projs/api-core-js/test
undefined

getAppDataDirectory questions/ potential for getDotFileDirectory

I've got some confusion regarding getAppDataDirectory, and also a question about storing application dotfiles, which might be related.

My confusion is as such:
In windows, %AppData% is user specific, as is ~/Library/Application Support in mac (and anything in ~, obviously). Correct me if I'm wrong, but /var/lib is global across users, no?
Either way, the docs read to me as if it's meant to be global. If i'm wrong about /var/lib, the docs might want a little love to clarify a bit, IMO. If I'm right, then things should be normalized to be all global or all user-specific.

Also, potentially related is the idea of a getDotFileDirectory method, for getting the location to store dotfiles, eg the .atom directory in the case of atom, or .myAppRC, etc. %AppData% is the appropriate place on windows, but the places ingetAppDataDirectoryseem wrong for mac/linux. I'm not sure if this really is the intended purpose ofgetAppDataDirectory`, or if they're better off side by side. This would be pretty simple to add, and I'd be happy to take care of it, if it's welcome.

getDotFileDirectory()

Get path to store application dotfiles for the user.

Return

  • String Returns the absolute path or null if platform isn't supported
    • Mac: ~
    • Win: %AppData%
    • Linux: ~

Those values could be worked out a bit more, like checking for env.HOME and env.USERNAME``env.LOGNAME etc, but thats the gist of it.

resolve - loadPaths argument is not properly handled

fs-plus.coffee

# loadPaths - An {Array} of absolute and relative paths to search.

...

resolve: (args...) ->
    extensions = args.pop() if _.isArray(_.last(args))
    pathToResolve = args.pop()?.toString()
    loadPaths = args

when loadPaths is assigned args, args is an array containing the rest of the parameters - excluding extensions and pathToResolve. args will unexpectedly be a nested array (it should not be) and the resolve method will break because of this. As I have seen implementations using a single string as argument, I propose that loadPaths should be able to be a string or an array;

loadPaths = if _.isArray(_.last(args)) args.pop() else args 

example showing when it breaks: https://runkit.com/58679334ff226c001410a427/586793351fc1d20015993407

crash on calling list

image

Here is my code, i have most recent version at this date (27/7/2015)
"name": "fs-plus",
"version": "2.8.1",

    var path = fs.normalize(command["path"]);
    console.log(path); // shows d:\github\
    var res = fs.list(path);
    console.log(res);//show undefined
    event.sender.send('command-res',res);

Make ability to traverse a tree by post-order method

traverseTree and traverseTreeSync use pre-order by default, which means that onDirectory would be called with parent directory before the children.

Sometimes, it needs to be post-order which let the callbacks above executes with the children before the parent. (e.g. deleting tree).

So I suggest either:

  • Create traverseTreePostOrder and its corresponding sync version
  • Add one more parameter to these functions

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.