path fs: move path.exists* to fs.exists*

`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`
This commit is contained in:
Maciej Małecki 2012-01-21 02:37:57 +01:00 committed by Ben Noordhuis
parent 56e34c2f81
commit e10ed097cb
7 changed files with 79 additions and 32 deletions

View 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 ## fs.Stats
Objects returned from `fs.stat()`, `fs.lstat()` and `fs.fstat()` and their Objects returned from `fs.stat()`, `fs.lstat()` and `fs.fstat()` and their

View File

@ -4,9 +4,6 @@ This module contains utilities for handling and transforming file
paths. Almost all these methods perform only string transformations. paths. Almost all these methods perform only string transformations.
The file system is not consulted to check whether paths are valid. 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: Use `require('path')` to use this module. The following methods are provided:
### path.normalize(p) ### path.normalize(p)
@ -140,16 +137,3 @@ an empty string. Examples:
// returns // 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`.

View File

@ -71,6 +71,21 @@ fs.Stats.prototype.isSocket = function() {
return this._checkModeProperty(constants.S_IFSOCK); 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_) { fs.readFile = function(path, encoding_) {
var encoding = typeof(encoding_) === 'string' ? encoding_ : null; var encoding = typeof(encoding_) === 'string' ? encoding_ : null;
var callback = arguments[arguments.length - 1]; var callback = arguments[arguments.length - 1];

View File

@ -21,6 +21,7 @@
var isWindows = process.platform === 'win32'; var isWindows = process.platform === 'win32';
var _deprecationWarning = require('util')._deprecationWarning;
// resolves . and .. elements in a path array with directory names there // resolves . and .. elements in a path array with directory names there
@ -402,19 +403,14 @@ exports.extname = function(path) {
exports.exists = function(path, callback) { exports.exists = function(path, callback) {
process.binding('fs').stat(path, function(err, stats) { _deprecationWarning('path', '`path.exists` is now called `fs.exists`');
if (callback) callback(err ? false : true); require('fs').exists(path, callback);
});
}; };
exports.existsSync = function(path) { exports.existsSync = function(path) {
try { _deprecationWarning('path', '`path.exists` is now called `fs.exists`');
process.binding('fs').stat(path); return require('fs').existsSync(path);
return true;
} catch (e) {
return false;
}
}; };

View File

@ -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);
});

View File

@ -21,7 +21,6 @@
var common = require('../common'); var common = require('../common');
var assert = require('assert'); var assert = require('assert');
var path = require('path');
var fs = require('fs'); var fs = require('fs');
function unlink(pathname) { function unlink(pathname) {
@ -39,7 +38,7 @@ function unlink(pathname) {
fs.mkdir(pathname, function(err) { fs.mkdir(pathname, function(err) {
assert.equal(err, null); assert.equal(err, null);
assert.equal(path.existsSync(pathname), true); assert.equal(fs.existsSync(pathname), true);
ncalls++; ncalls++;
}); });
@ -57,7 +56,7 @@ function unlink(pathname) {
fs.mkdir(pathname, 511 /*=0777*/, function(err) { fs.mkdir(pathname, 511 /*=0777*/, function(err) {
assert.equal(err, null); assert.equal(err, null);
assert.equal(path.existsSync(pathname), true); assert.equal(fs.existsSync(pathname), true);
ncalls++; ncalls++;
}); });
@ -73,7 +72,7 @@ function unlink(pathname) {
unlink(pathname); unlink(pathname);
fs.mkdirSync(pathname); fs.mkdirSync(pathname);
var exists = path.existsSync(pathname); var exists = fs.existsSync(pathname);
unlink(pathname); unlink(pathname);
assert.equal(exists, true); assert.equal(exists, true);

View File

@ -76,9 +76,6 @@ if (isWindows) {
'\\\\unc\\share\\foo\\bar'); '\\\\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(''), '');
assert.equal(path.extname('/path/to/file'), ''); assert.equal(path.extname('/path/to/file'), '');