Permalink
Browse files

Add file/directory existance checks to grunt

Add functions to simply check if a file and/or a directory exists.

Example:
var c5fs = require('libraries/fs');
var isDirectory = c5fs.isDirectory(path);
var isFile = c5fs.isFile(path);
var isFileOrDirectory = c5fs.fileExists(path);
  • Loading branch information...
1 parent 7480741 commit cbc262c373d91c8cf621858b503c70f40ef619a5 @mlocati mlocati committed Feb 19, 2014
Showing with 49 additions and 0 deletions.
  1. +49 −0 build/libraries/fs.js
View
49 build/libraries/fs.js
@@ -28,6 +28,55 @@ else {
};
}
+/** Checks whether a directory exists
+* @param {string} path
+* @return boolean
+*/
+function isDirectory(path) {
+ try {
+ return fs.lstatSync(path).isDirectory();
+ }
+ catch(e) {
+ if(e.code == 'ENOENT') {
+ return false;
+ }
+ throw e;
+ }
+}
+
+/** Checks whether a file exists
+* @param {string} path
+* @return boolean
+*/
+function isFile(path) {
+ try {
+ return fs.lstatSync(path).isFile();
+ }
+ catch(e) {
+ if(e.code == 'ENOENT') {
+ return false;
+ }
+ throw e;
+ }
+}
+
+/** Checks whether a file or directory exists
+* @param {string} path
+* @return boolean
+*/
+function fileExists(path) {
+ try {
+ fs.lstatSync(path);
+ return true;
+ }
+ catch(e) {
+ if(e.code == 'ENOENT') {
+ return false;
+ }
+ throw e;
+ }
+}
+
/** Helper function that effectively parses the directory tree (used by directoryParser).
* @param {directoryParser} main The directoryParser instance.
* @param {string} dirAbs The absolute path of the directory to parse.

0 comments on commit cbc262c

Please sign in to comment.