Merge pull request #204 from jskrzypek/replace-exists-sync

Replace fs.existsSync calls with wrapped fs.statSync

Close #204
This commit is contained in:
Miroslav Bajtoš 2016-09-05 14:45:20 +02:00
commit 53112957d9
5 changed files with 64 additions and 5 deletions

View File

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

View File

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

View File

@ -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) {

25
lib/utils.js Normal file
View File

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

31
test/utils.test.js Normal file
View File

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