From 09c3f8365be022d0271ec80e8fc0c555cd31161e Mon Sep 17 00:00:00 2001 From: Joshua Estrin Skrzypek Date: Thu, 11 Aug 2016 01:34:11 +0300 Subject: [PATCH] Replace fs.existsSync calls with fs.statSync --- index.js | 2 ++ lib/compiler.js | 7 ++++--- lib/config-loader.js | 4 ++-- lib/utils.js | 25 +++++++++++++++++++++++++ test/utils.test.js | 31 +++++++++++++++++++++++++++++++ 5 files changed, 64 insertions(+), 5 deletions(-) create mode 100644 lib/utils.js create mode 100644 test/utils.test.js diff --git a/index.js b/index.js index e3e4975..d3e723c 100644 --- a/index.js +++ b/index.js @@ -11,6 +11,7 @@ var ConfigLoader = require('./lib/config-loader'); var compile = require('./lib/compiler'); var execute = require('./lib/executor'); var addInstructionsToBrowserify = require('./lib/bundler'); +var utils = require('./lib/utils'); /** * Initialize an application from an options object or @@ -172,4 +173,5 @@ exports.compileToBrowserify = function(options, bundler) { exports.ConfigLoader = ConfigLoader; exports.compile = compile; exports.execute = execute; +exports.utils = utils; exports.addInstructionsToBrowserify = addInstructionsToBrowserify; diff --git a/lib/compiler.js b/lib/compiler.js index cf289ba..7258e6a 100644 --- a/lib/compiler.js +++ b/lib/compiler.js @@ -9,6 +9,7 @@ var fs = require('fs'); var path = require('path'); var toposort = require('toposort'); var ConfigLoader = require('./config-loader'); +var utils = require('./utils'); var debug = require('debug')('loopback:boot:compiler'); var Module = require('module'); var _ = require('lodash'); @@ -391,7 +392,7 @@ function tryResolveAppPath(rootDir, relativePath, resolveOptions) { if (fullPath) { // This check is needed to support paths pointing to a directory - if (fs.existsSync(fullPath)) { + if (utils.fileExistsSync(fullPath)) { return fullPath; } @@ -429,7 +430,7 @@ function tryResolveAppPath(rootDir, relativePath, resolveOptions) { } }) .filter(function(candidate) { - return fs.existsSync(candidate.toString()); + return utils.fileExistsSync(candidate.toString()); }) [0]; @@ -755,7 +756,7 @@ function loadMixins(sourceFiles, options) { name = normalizeMixinName(name, options); var meta = {}; meta.name = name; - if (fs.existsSync(metafile)) { + if (utils.fileExistsSync(metafile)) { // May overwrite name, not sourceFile _.extend(meta, require(metafile)); } diff --git a/lib/config-loader.js b/lib/config-loader.js index ca922d2..1c34fab 100644 --- a/lib/config-loader.js +++ b/lib/config-loader.js @@ -4,8 +4,8 @@ // License text available at https://opensource.org/licenses/MIT var cloneDeep = require('lodash').cloneDeep; -var fs = require('fs'); var path = require('path'); +var utils = require('./utils.js'); var debug = require('debug')('loopback:boot:config-loader'); var assert = require('assert'); var g = require('strong-globalize')(); @@ -111,7 +111,7 @@ function findConfigFiles(appRootDir, env, name) { function ifExists(fileName) { var filepath = path.resolve(appRootDir, fileName); - return fs.existsSync(filepath) ? filepath : undefined; + return utils.fileExistsSync(filepath) ? filepath : undefined; } function ifExistsWithAnyExt(fileName) { diff --git a/lib/utils.js b/lib/utils.js new file mode 100644 index 0000000..49f5579 --- /dev/null +++ b/lib/utils.js @@ -0,0 +1,25 @@ +// Copyright IBM Corp. 2014,2016. All Rights Reserved. +// Node module: loopback-boot +// This file is licensed under the MIT License. +// License text available at https://opensource.org/licenses/MIT + +var fs = require('fs'); + +exports.fileExistsSync = fileExistsSync; + +/** + * Check synchronously if a filepath points to an existing file. + * Replaces calls to fs.existsSync, which is deprecated (see: + * https://github.com/nodejs/node/pull/166). + * + * @param {String} filepath The absolute path to check + * @returns {Boolean} True if the file exists + */ +function fileExistsSync(filepath) { + try { + fs.statSync(filepath); + return true; + } catch (e) { + return false; + } +} diff --git a/test/utils.test.js b/test/utils.test.js new file mode 100644 index 0000000..d7b7dc8 --- /dev/null +++ b/test/utils.test.js @@ -0,0 +1,31 @@ +// Copyright IBM Corp. 2014,2016. All Rights Reserved. +// Node module: loopback-boot +// This file is licensed under the MIT License. +// License text available at https://opensource.org/licenses/MIT + +var boot = require('../'); +var expect = require('chai').expect; +var sandbox = require('./helpers/sandbox'); +var appdir = require('./helpers/appdir'); + +describe('utils', function() { + beforeEach(sandbox.reset); + beforeEach(function(done) { + appdir.init(done); + }); + describe('fileExistsSync', function() { + it('returns false when a file does not exist', function() { + var doesNotExist = sandbox.resolve('does-not-exist.json'); + expect(boot.utils.fileExistsSync(doesNotExist)) + .to.equal(false); + }); + + it('returns true when a file does exist', function() { + var doesExist = appdir.writeConfigFileSync('does-exist.json', { + exists: true, + }); + expect(boot.utils.fileExistsSync(doesExist)) + .to.equal(true); + }); + }); +});