Skip to content
This repository
Browse code

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`
  • Loading branch information...
commit e10ed097cb3b506009ab28af4dec93402aa48693 1 parent 56e34c2
Maciej Małecki authored bnoordhuis committed
14 doc/api/fs.markdown
Source Rendered
@@ -464,6 +464,20 @@ callback, and have some fallback logic if it is null.
464 464 }
465 465 });
466 466
  467 +### fs.exists(p, [callback])
  468 +
  469 +Test whether or not the given path exists by checking with the file system.
  470 +Then call the `callback` argument with either true or false. Example:
  471 +
  472 + fs.exists('/etc/passwd', function (exists) {
  473 + util.debug(exists ? "it's there" : "no passwd!");
  474 + });
  475 +
  476 +
  477 +### fs.existsSync(p)
  478 +
  479 +Synchronous version of `fs.exists`.
  480 +
467 481 ## fs.Stats
468 482
469 483 Objects returned from `fs.stat()`, `fs.lstat()` and `fs.fstat()` and their
16 doc/api/path.markdown
Source Rendered
@@ -4,9 +4,6 @@ This module contains utilities for handling and transforming file
4 4 paths. Almost all these methods perform only string transformations.
5 5 The file system is not consulted to check whether paths are valid.
6 6
7   -`path.exists` and `path.existsSync` are the exceptions, and should
8   -logically be found in the fs module as they do access the file system.
9   -
10 7 Use `require('path')` to use this module. The following methods are provided:
11 8
12 9 ### path.normalize(p)
@@ -140,16 +137,3 @@ an empty string. Examples:
140 137 // returns
141 138 ''
142 139
143   -### path.exists(p, [callback])
144   -
145   -Test whether or not the given path exists by checking with the file system.
146   -Then call the `callback` argument with either true or false. Example:
147   -
148   - path.exists('/etc/passwd', function (exists) {
149   - util.debug(exists ? "it's there" : "no passwd!");
150   - });
151   -
152   -
153   -### path.existsSync(p)
154   -
155   -Synchronous version of `path.exists`.
15 lib/fs.js
@@ -71,6 +71,21 @@ fs.Stats.prototype.isSocket = function() {
71 71 return this._checkModeProperty(constants.S_IFSOCK);
72 72 };
73 73
  74 +fs.exists = function(path, callback) {
  75 + binding.stat(path, function(err, stats) {
  76 + if (callback) callback(err ? false : true);
  77 + });
  78 +};
  79 +
  80 +fs.existsSync = function(path) {
  81 + try {
  82 + binding.stat(path);
  83 + return true;
  84 + } catch (e) {
  85 + return false;
  86 + }
  87 +};
  88 +
74 89 fs.readFile = function(path, encoding_) {
75 90 var encoding = typeof(encoding_) === 'string' ? encoding_ : null;
76 91 var callback = arguments[arguments.length - 1];
14 lib/path.js
@@ -21,6 +21,7 @@
21 21
22 22
23 23 var isWindows = process.platform === 'win32';
  24 +var _deprecationWarning = require('util')._deprecationWarning;
24 25
25 26
26 27 // resolves . and .. elements in a path array with directory names there
@@ -402,19 +403,14 @@ exports.extname = function(path) {
402 403
403 404
404 405 exports.exists = function(path, callback) {
405   - process.binding('fs').stat(path, function(err, stats) {
406   - if (callback) callback(err ? false : true);
407   - });
  406 + _deprecationWarning('path', '`path.exists` is now called `fs.exists`');
  407 + require('fs').exists(path, callback);
408 408 };
409 409
410 410
411 411 exports.existsSync = function(path) {
412   - try {
413   - process.binding('fs').stat(path);
414   - return true;
415   - } catch (e) {
416   - return false;
417   - }
  412 + _deprecationWarning('path', '`path.exists` is now called `fs.exists`');
  413 + return require('fs').existsSync(path);
418 414 };
419 415
420 416
42 test/simple/test-fs-exists.js
... ... @@ -0,0 +1,42 @@
  1 +// Copyright Joyent, Inc. and other Node contributors.
  2 +//
  3 +// Permission is hereby granted, free of charge, to any person obtaining a
  4 +// copy of this software and associated documentation files (the
  5 +// "Software"), to deal in the Software without restriction, including
  6 +// without limitation the rights to use, copy, modify, merge, publish,
  7 +// distribute, sublicense, and/or sell copies of the Software, and to permit
  8 +// persons to whom the Software is furnished to do so, subject to the
  9 +// following conditions:
  10 +//
  11 +// The above copyright notice and this permission notice shall be included
  12 +// in all copies or substantial portions of the Software.
  13 +//
  14 +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  15 +// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  16 +// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
  17 +// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
  18 +// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
  19 +// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
  20 +// USE OR OTHER DEALINGS IN THE SOFTWARE.
  21 +
  22 +var assert = require('assert');
  23 +var fs = require('fs');
  24 +var f = __filename;
  25 +var exists;
  26 +var doesNotExist;
  27 +
  28 +fs.exists(f, function(y) {
  29 + exists = y;
  30 +});
  31 +
  32 +fs.exists(f + '-NO', function (y) {
  33 + doesNotExist = y;
  34 +});
  35 +
  36 +assert(fs.existsSync(f));
  37 +assert(!fs.existsSync(f + '-NO'));
  38 +
  39 +process.on('exit', function () {
  40 + assert.strictEqual(exists, true);
  41 + assert.strictEqual(doesNotExist, false);
  42 +});
7 test/simple/test-fs-mkdir.js
@@ -21,7 +21,6 @@
21 21
22 22 var common = require('../common');
23 23 var assert = require('assert');
24   -var path = require('path');
25 24 var fs = require('fs');
26 25
27 26 function unlink(pathname) {
@@ -39,7 +38,7 @@ function unlink(pathname) {
39 38
40 39 fs.mkdir(pathname, function(err) {
41 40 assert.equal(err, null);
42   - assert.equal(path.existsSync(pathname), true);
  41 + assert.equal(fs.existsSync(pathname), true);
43 42 ncalls++;
44 43 });
45 44
@@ -57,7 +56,7 @@ function unlink(pathname) {
57 56
58 57 fs.mkdir(pathname, 511 /*=0777*/, function(err) {
59 58 assert.equal(err, null);
60   - assert.equal(path.existsSync(pathname), true);
  59 + assert.equal(fs.existsSync(pathname), true);
61 60 ncalls++;
62 61 });
63 62
@@ -73,7 +72,7 @@ function unlink(pathname) {
73 72 unlink(pathname);
74 73 fs.mkdirSync(pathname);
75 74
76   - var exists = path.existsSync(pathname);
  75 + var exists = fs.existsSync(pathname);
77 76 unlink(pathname);
78 77
79 78 assert.equal(exists, true);
3  test/simple/test-path.js
@@ -76,9 +76,6 @@ if (isWindows) {
76 76 '\\\\unc\\share\\foo\\bar');
77 77 }
78 78
79   -path.exists(f, function(y) { assert.equal(y, true) });
80   -
81   -assert.equal(path.existsSync(f), true);
82 79
83 80 assert.equal(path.extname(''), '');
84 81 assert.equal(path.extname('/path/to/file'), '');

0 comments on commit e10ed09

Please sign in to comment.
Something went wrong with that request. Please try again.