path fs: move `path.exists*` to `fs.exists*`
authorMaciej Małecki <maciej.malecki@notimplemented.org>
Sat, 21 Jan 2012 01:37:57 +0000 (02:37 +0100)
committerBen Noordhuis <info@bnoordhuis.nl>
Sat, 21 Jan 2012 13:37:14 +0000 (14:37 +0100)
`path.exists*` functions show a deprecation warning and call functions
from `fs`. They should be removed later.

test: fix references to `path.exists*` in tests

test fs: add test for `fs.exists` and `fs.existsSync`

doc: reflect moving `path.exists*` to `fs`

doc/api/fs.markdown
doc/api/path.markdown
lib/fs.js
lib/path.js
test/simple/test-fs-exists.js [new file with mode: 0644]
test/simple/test-fs-mkdir.js
test/simple/test-path.js

index b520d7f..62c40b3 100644 (file)
@@ -464,6 +464,20 @@ callback, and have some fallback logic if it is null.
          }
     });
 
+### fs.exists(p, [callback])
+
+Test whether or not the given path exists by checking with the file system.
+Then call the `callback` argument with either true or false.  Example:
+
+    fs.exists('/etc/passwd', function (exists) {
+      util.debug(exists ? "it's there" : "no passwd!");
+    });
+
+
+### fs.existsSync(p)
+
+Synchronous version of `fs.exists`.
+
 ## fs.Stats
 
 Objects returned from `fs.stat()`, `fs.lstat()` and `fs.fstat()` and their
index ba48bd2..5c33adb 100644 (file)
@@ -4,9 +4,6 @@ This module contains utilities for handling and transforming file
 paths.  Almost all these methods perform only string transformations.
 The file system is not consulted to check whether paths are valid.
 
-`path.exists` and `path.existsSync` are the exceptions, and should
-logically be found in the fs module as they do access the file system.
-
 Use `require('path')` to use this module.  The following methods are provided:
 
 ### path.normalize(p)
@@ -140,16 +137,3 @@ an empty string.  Examples:
     // returns
     ''
 
-### path.exists(p, [callback])
-
-Test whether or not the given path exists by checking with the file system.
-Then call the `callback` argument with either true or false.  Example:
-
-    path.exists('/etc/passwd', function (exists) {
-      util.debug(exists ? "it's there" : "no passwd!");
-    });
-
-
-### path.existsSync(p)
-
-Synchronous version of `path.exists`.
index 51637c1..2dc11a7 100644 (file)
--- a/lib/fs.js
+++ b/lib/fs.js
@@ -71,6 +71,21 @@ fs.Stats.prototype.isSocket = function() {
   return this._checkModeProperty(constants.S_IFSOCK);
 };
 
+fs.exists = function(path, callback) {
+  binding.stat(path, function(err, stats) {
+    if (callback) callback(err ? false : true);
+  });
+};
+
+fs.existsSync = function(path) {
+  try {
+    binding.stat(path);
+    return true;
+  } catch (e) {
+    return false;
+  }
+};
+
 fs.readFile = function(path, encoding_) {
   var encoding = typeof(encoding_) === 'string' ? encoding_ : null;
   var callback = arguments[arguments.length - 1];
index b70225b..6a744df 100644 (file)
@@ -21,6 +21,7 @@
 
 
 var isWindows = process.platform === 'win32';
+var _deprecationWarning = require('util')._deprecationWarning;
 
 
 // resolves . and .. elements in a path array with directory names there
@@ -402,19 +403,14 @@ exports.extname = function(path) {
 
 
 exports.exists = function(path, callback) {
-  process.binding('fs').stat(path, function(err, stats) {
-    if (callback) callback(err ? false : true);
-  });
+  _deprecationWarning('path', '`path.exists` is now called `fs.exists`');
+  require('fs').exists(path, callback);
 };
 
 
 exports.existsSync = function(path) {
-  try {
-    process.binding('fs').stat(path);
-    return true;
-  } catch (e) {
-    return false;
-  }
+  _deprecationWarning('path', '`path.exists` is now called `fs.exists`');
+  return require('fs').existsSync(path);
 };
 
 
diff --git a/test/simple/test-fs-exists.js b/test/simple/test-fs-exists.js
new file mode 100644 (file)
index 0000000..cf785cc
--- /dev/null
@@ -0,0 +1,42 @@
+// Copyright Joyent, Inc. and other Node contributors.
+//
+// Permission is hereby granted, free of charge, to any person obtaining a
+// copy of this software and associated documentation files (the
+// "Software"), to deal in the Software without restriction, including
+// without limitation the rights to use, copy, modify, merge, publish,
+// distribute, sublicense, and/or sell copies of the Software, and to permit
+// persons to whom the Software is furnished to do so, subject to the
+// following conditions:
+//
+// The above copyright notice and this permission notice shall be included
+// in all copies or substantial portions of the Software.
+//
+// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
+// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
+// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
+// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
+// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
+// USE OR OTHER DEALINGS IN THE SOFTWARE.
+
+var assert = require('assert');
+var fs = require('fs');
+var f = __filename;
+var exists;
+var doesNotExist;
+
+fs.exists(f, function(y) {
+  exists = y;
+});
+
+fs.exists(f + '-NO', function (y) {
+  doesNotExist = y;
+});
+
+assert(fs.existsSync(f));
+assert(!fs.existsSync(f + '-NO'));
+
+process.on('exit', function () {
+  assert.strictEqual(exists, true);
+  assert.strictEqual(doesNotExist, false);
+});
index 324c08a..9d00b1e 100644 (file)
@@ -21,7 +21,6 @@
 
 var common = require('../common');
 var assert = require('assert');
-var path = require('path');
 var fs = require('fs');
 
 function unlink(pathname) {
@@ -39,7 +38,7 @@ function unlink(pathname) {
 
   fs.mkdir(pathname, function(err) {
     assert.equal(err, null);
-    assert.equal(path.existsSync(pathname), true);
+    assert.equal(fs.existsSync(pathname), true);
     ncalls++;
   });
 
@@ -57,7 +56,7 @@ function unlink(pathname) {
 
   fs.mkdir(pathname, 511 /*=0777*/, function(err) {
     assert.equal(err, null);
-    assert.equal(path.existsSync(pathname), true);
+    assert.equal(fs.existsSync(pathname), true);
     ncalls++;
   });
 
@@ -73,7 +72,7 @@ function unlink(pathname) {
   unlink(pathname);
   fs.mkdirSync(pathname);
 
-  var exists = path.existsSync(pathname);
+  var exists = fs.existsSync(pathname);
   unlink(pathname);
 
   assert.equal(exists, true);
index e16b459..c60230a 100644 (file)
@@ -76,9 +76,6 @@ if (isWindows) {
                '\\\\unc\\share\\foo\\bar');
 }
 
-path.exists(f, function(y) { assert.equal(y, true) });
-
-assert.equal(path.existsSync(f), true);
 
 assert.equal(path.extname(''), '');
 assert.equal(path.extname('/path/to/file'), '');