path module revisited with backward compatibility and additional features
The goal of this module is to fix the current working directory issue, provide backward compatibility to the
path.normalize() function (it's terribly bugged on Windows), provide forward compatibility with the
path.delimiter aliases and all future aditions and add additional useful methods.
This module uses meta-path to guarantee the backward compatibility. See its documentation to understand the problems and the fixes.
The primary intention is to use
flavored-path as a complete replacement of the
path built-in module.
Every time you call a fs built-in function I recommend to call to the get() function:
var fs = require "fs";var path = require "flavored-path";fsexists pathget "my/path" cb;
The get() function will take care all the possible types of path (relative, absolute, network shared resource and relative from the home directory on Linux) and fix, resolve or whatever they need in order to work as expected.
npm install flavored-path
var fs = require "fs";var path = require "flavored-path";/*path.get (".") is equivalent to __dirname but get() can be used with any typeof path, e.g.: Linux home paths (~/a/b).On Linux, home paths are resolved to absolute files, so this snippet printsall the entries in the user's home path.On Windows, home paths are processed like regular relative paths, that is,get() will return something like this: <cwd>/~/, so don't use ~ on Windows.The Node.js built-in functions work the same way, for them ~ is processedlike a relative home path.*/fsreaddir pathget "~"if error return console.log error;console.log files;;
Same Node.js built-in function (path.basename()).
Same Node.js built-in function (path.delimiter).
Same Node.js built-in function (path.dirname()).
Same Node.js built-in function (path.extname()).
fsexists pathget "a" cb;
You can pass any type of path. It will be normalized, fixed, resolved, whatever modification to convert the given path to a valid path for using with any
fs built-in function.
The path is normalized and if the script is executed from a different directory from where the main script it's executed, e.g.
node a/app.js instead of
node app.js, the path is also be fixed. Example:
//app.jsvar path = require "flavored-path";console.log pathget "app.js";
$ pwd/home/gll$ node a/app.jsa/app.js
flavored-path it's used within a global module it makes no sense to try to fix a relative path because it's not needed. In this case the function only normalizes the path and returns it.
In fact, this function provides the same funcionality as the
__dirname approach but also works in third-party modules whereas
__dirname cannot be used in them.
Checks whether the path is absolute. An absolute path begins with
\ (the prefix is skipped, e.g.:
D:), this means that network paths are also absolute. The given path doesn't need to be normalized. Returns true if is absolute, false otherwise.
Checks whether the path is relative from the user's home directory. A home path path begins with
~ (cannot contain a prefix), this means that home paths are also relative. The given path doesn't need to be normalized. Returns true if is a home path, false otherwise.
Checks whether the path is a network resource (on Windows world this is called a UNC path). A network path begins with
\\\\ (the prefix is skipped, e.g.:
smb:). The given path doesn't need to be normalized. Returns true if is a network resource, false otherwise.
Checks whether the path is relative. A relative path begins with any character different from
\ (the prefix is skipped, e.g.:
D:). The given path doesn't need to be normalized. Returns true if is relative, false otherwise.
Checks whether the path is a root. A root path is equal to
\\ (the prefix is skipped, e.g.:
D:). The given path doesn't need to be normalized. Returns true if is a root path, false otherwise.
path.join(path1, path2[, ...])
Same Node.js built-in function (path.join()).
The joined path is correctly normalized.
console.log pathjoin "" "c/"; //Prints:
Same Node.js built-in function (path.normalize()).
Takes care all of the bugs found in the built-in normalize function explained in the meta-path module.
/*You can overwrite the built-in cwd function and cache the result.Now, the process.cwd() function will always return the same path ifyou move across the directories and execute a script from outside of itsdirectory, so you don't need to call to get():$ node app.js$ node dir/app.js$ node ../app.js*/processcwd =var cwd = pathrealpath ".";returnreturn cwd;;;
This is an error prone solution because you're overwritting a built-in Node.js function, and you may have other modules that may stop working properly. Use with caution.
Same Node.js built-in function (path.relative()).
path.resolve([from, [...]], to)
Same Node.js built-in function (path.resolve()).
The absolute path that is returned fixes the current working directory issue.
The Linux relative home paths are resolved to its absolute path (only on Linux). The resolve built-in function doesn't provide this functionality. The
fs built-in functions don't expand
~ to the user's home path, so when you deal with home paths you must convert it first to an absolute path:
fsexists pathresolve "~/a" cb;//Or simply://fs.exists (path.get ("~/a"), cb);
Network resource paths (paths that begin with
//) are also resolved correctly.
//app.jsconsole.log pathresolve "b" "c"; //Prints: /home/gll/a/b/cconsole.log pathresolve "s" "~/a"; //Prints: /home/gll/aconsole.log pathresolve "s" "/a" "b"; //Prints: /a/bconsole.log pathresolve "s" "//a" "b"; //Prints: //a/b
$ pwd/home/gll$ node a/app.js
Same Node.js built-in property (path.sep).