diff --git a/lib/compiler.js b/lib/compiler.js index cf289ba..ff29782 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.fileExists(fullPath)) { return fullPath; } @@ -429,7 +430,7 @@ function tryResolveAppPath(rootDir, relativePath, resolveOptions) { } }) .filter(function(candidate) { - return fs.existsSync(candidate.toString()); + return utils.fileExists(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.fileExists(metafile)) { // May overwrite name, not sourceFile _.extend(meta, require(metafile)); } diff --git a/lib/config-loader.js b/lib/config-loader.js index ca922d2..e4479e1 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.fileExists(filepath) ? filepath : undefined; } function ifExistsWithAnyExt(fileName) { diff --git a/lib/utils.js b/lib/utils.js new file mode 100644 index 0000000..b33091b --- /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.fileExists = fileExists; + +/** + * 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 fileExists(filepath) { + try { + fs.statSync(filepath); + return true; + } catch (e) { + return false; + } +}