1
0
mirror of https://github.com/node-red/node-red.git synced 2023-10-10 13:36:53 +02:00

Remove all uses of fs.exists as it is deprecated

The tests still use it in places - particular localfilesystem tests,
but those tests need to be redone with sinon stubbing in place and
not rely on real fs operations.
This commit is contained in:
Nick O'Leary 2015-11-16 11:31:55 +00:00
parent e65770a53a
commit f62b7afede
7 changed files with 79 additions and 68 deletions

View File

@ -22,17 +22,21 @@ module.exports = function(RED) {
var gpioCommand = __dirname+'/nrgpio.py'; var gpioCommand = __dirname+'/nrgpio.py';
if (!fs.existsSync("/dev/ttyAMA0")) { // unlikely if not on a Pi try {
fs.statSync("/dev/ttyAMA0"); // unlikely if not on a Pi
} catch(err) {
//RED.log.info(RED._("rpi-gpio.errors.ignorenode")); //RED.log.info(RED._("rpi-gpio.errors.ignorenode"));
throw "Info : "+RED._("rpi-gpio.errors.ignorenode"); throw "Info : "+RED._("rpi-gpio.errors.ignorenode");
} }
if (!fs.existsSync("/usr/share/doc/python-rpi.gpio")) { try {
fs.statSync("/usr/share/doc/python-rpi.gpio");
} catch(err) {
RED.log.warn(RED._("rpi-gpio.errors.libnotfound")); RED.log.warn(RED._("rpi-gpio.errors.libnotfound"));
throw "Warning : "+RED._("rpi-gpio.errors.libnotfound"); throw "Warning : "+RED._("rpi-gpio.errors.libnotfound");
} }
if ( !(1 & parseInt ((fs.statSync(gpioCommand).mode & parseInt ("777", 8)).toString (8)[0]) )) { if ( !(1 & parseInt((fs.statSync(gpioCommand).mode & parseInt("777", 8)).toString(8)[0]) )) {
RED.log.error(RED._("rpi-gpio.errors.needtobeexecutable",{command:gpioCommand})); RED.log.error(RED._("rpi-gpio.errors.needtobeexecutable",{command:gpioCommand}));
throw "Error : "+RED._("rpi-gpio.errors.mustbeexecutable"); throw "Error : "+RED._("rpi-gpio.errors.mustbeexecutable");
} }

View File

@ -59,10 +59,13 @@ module.exports = {
} else { } else {
for (var p=0;p<icon_paths.length;p++) { for (var p=0;p<icon_paths.length;p++) {
var iconPath = path.join(icon_paths[p],req.params.icon); var iconPath = path.join(icon_paths[p],req.params.icon);
if (fs.existsSync(iconPath)) { try {
fs.statSync(iconPath);
res.sendFile(iconPath); res.sendFile(iconPath);
iconCache[req.params.icon] = iconPath; iconCache[req.params.icon] = iconPath;
return; return;
} catch(err) {
// iconPath doesn't exist
} }
} }
res.sendFile(defaultIcon); res.sendFile(defaultIcon);

View File

@ -28,18 +28,25 @@ var fs = require("fs");
var runtimeMetricInterval = null; var runtimeMetricInterval = null;
function init(userSettings) { function init(userSettings) {
userSettings.version = version(); userSettings.version = getVersion();
log.init(userSettings); log.init(userSettings);
settings.init(userSettings); settings.init(userSettings);
} }
function version() { var version;
var p = require(path.join(__dirname,"..","..","package.json")).version;
function getVersion() {
if (!version) {
version = require(path.join(__dirname,"..","..","package.json")).version;
/* istanbul ignore else */ /* istanbul ignore else */
if (fs.existsSync(path.join(__dirname,"..","..",".git"))) { try {
p += "-git"; fs.statSync(path.join(__dirname,"..","..",".git"));
version += "-git";
} catch(err) {
// No git directory
} }
return p; }
return version;
} }
function start() { function start() {
@ -142,7 +149,7 @@ var runtime = module.exports = {
start: start, start: start,
stop: stop, stop: stop,
version: version, version: getVersion,
log: log, log: log,
i18n: i18n, i18n: i18n,

View File

@ -40,7 +40,7 @@ function checkModulePath(folder) {
var err; var err;
var fullPath = path.resolve(folder); var fullPath = path.resolve(folder);
var packageFile = path.join(fullPath,'package.json'); var packageFile = path.join(fullPath,'package.json');
if (fs.existsSync(packageFile)) { try {
var pkg = require(packageFile); var pkg = require(packageFile);
moduleName = pkg.name; moduleName = pkg.name;
if (!pkg['node-red']) { if (!pkg['node-red']) {
@ -49,7 +49,7 @@ function checkModulePath(folder) {
err.code = 'invalid_module'; err.code = 'invalid_module';
throw err; throw err;
} }
} else { } catch(err2) {
err = new Error("Module not found"); err = new Error("Module not found");
err.code = 404; err.code = 404;
throw err; throw err;
@ -151,7 +151,10 @@ function uninstallModule(module) {
} }
var installDir = settings.userDir || process.env.NODE_RED_HOME || "."; var installDir = settings.userDir || process.env.NODE_RED_HOME || ".";
var moduleDir = path.join(installDir,"node_modules",module); var moduleDir = path.join(installDir,"node_modules",module);
if (!fs.existsSync(moduleDir)) {
try {
fs.statSync(moduleDir);
} catch(err) {
return reject(new Error(log._("server.install.uninstall-failed",{name:module}))); return reject(new Error(log._("server.install.uninstall-failed",{name:module})));
} }

View File

@ -49,16 +49,17 @@ function getLocalFile(file) {
if (isExcluded(path.basename(file))) { if (isExcluded(path.basename(file))) {
return null; return null;
} }
if (fs.existsSync(file.replace(/\.js$/,".html"))) { try {
fs.statSync(file.replace(/\.js$/,".html"));
return { return {
file: file, file: file,
module: "node-red", module: "node-red",
name: path.basename(file).replace(/^\d+-/,"").replace(/\.js$/,""), name: path.basename(file).replace(/^\d+-/,"").replace(/\.js$/,""),
version: settings.version version: settings.version
}; };
} } catch(err) {
return null; return null;
}
} }
@ -175,9 +176,11 @@ function getModuleNodeFiles(module) {
}); });
var iconDir = path.join(moduleDir,path.dirname(nodes[n]),"icons"); var iconDir = path.join(moduleDir,path.dirname(nodes[n]),"icons");
if (iconDirs.indexOf(iconDir) == -1) { if (iconDirs.indexOf(iconDir) == -1) {
if (fs.existsSync(iconDir)) { try {
fs.statSync(iconDir);
events.emit("node-icon-dir",iconDir); events.emit("node-icon-dir",iconDir);
iconDirs.push(iconDir); iconDirs.push(iconDir);
} catch(err) {
} }
} }
} }

View File

@ -128,9 +128,10 @@ var localfilesystem = {
var promises = []; var promises = [];
if (!settings.userDir) { if (!settings.userDir) {
if (fs.existsSync(fspath.join(process.env.NODE_RED_HOME,".config.json"))) { try {
fs.statSync(fspath.join(process.env.NODE_RED_HOME,".config.json"));
settings.userDir = process.env.NODE_RED_HOME; settings.userDir = process.env.NODE_RED_HOME;
} else { } catch(err) {
settings.userDir = fspath.join(process.env.HOME || process.env.HOMEPATH || process.env.USERPROFILE || process.env.NODE_RED_HOME,".node-red"); settings.userDir = fspath.join(process.env.HOME || process.env.HOMEPATH || process.env.USERPROFILE || process.env.NODE_RED_HOME,".node-red");
if (!settings.readOnly) { if (!settings.readOnly) {
promises.push(promiseDir(settings.userDir)); promises.push(promiseDir(settings.userDir));
@ -148,10 +149,11 @@ var localfilesystem = {
// Relative to cwd // Relative to cwd
flowsFullPath = fspath.join(process.cwd(),flowsFile); flowsFullPath = fspath.join(process.cwd(),flowsFile);
} else { } else {
if (fs.existsSync(fspath.join(process.cwd(),flowsFile))) { try {
fs.statSync(fspath.join(process.cwd(),flowsFile));
// Found in cwd // Found in cwd
flowsFullPath = fspath.join(process.cwd(),flowsFile); flowsFullPath = fspath.join(process.cwd(),flowsFile);
} else { } catch(err) {
// Use userDir // Use userDir
flowsFullPath = fspath.join(settings.userDir,flowsFile); flowsFullPath = fspath.join(settings.userDir,flowsFile);
} }
@ -191,15 +193,12 @@ var localfilesystem = {
return when.promise(function(resolve) { return when.promise(function(resolve) {
log.info(log._("storage.localfilesystem.user-dir",{path:settings.userDir})); log.info(log._("storage.localfilesystem.user-dir",{path:settings.userDir}));
log.info(log._("storage.localfilesystem.flows-file",{path:flowsFullPath})); log.info(log._("storage.localfilesystem.flows-file",{path:flowsFullPath}));
fs.exists(flowsFullPath, function(exists) { fs.readFile(flowsFullPath,'utf8',function(err,data) {
if (exists) { if (!err) {
resolve(nodeFn.call(fs.readFile,flowsFullPath,'utf8').then(function(data) { return resolve(JSON.parse(data));
return JSON.parse(data); }
}));
} else {
log.info(log._("storage.localfilesystem.create")); log.info(log._("storage.localfilesystem.create"));
resolve([]); resolve([]);
}
}); });
}); });
}, },
@ -209,8 +208,9 @@ var localfilesystem = {
return when.resolve(); return when.resolve();
} }
if (fs.existsSync(flowsFullPath)) { try {
fs.renameSync(flowsFullPath,flowsFileBackup); fs.renameSync(flowsFullPath,flowsFileBackup);
} catch(err) {
} }
var flowData; var flowData;
@ -225,17 +225,13 @@ var localfilesystem = {
getCredentials: function() { getCredentials: function() {
return when.promise(function(resolve) { return when.promise(function(resolve) {
fs.exists(credentialsFile, function(exists) { fs.readFile(credentialsFile,'utf8',function(err,data) {
if (exists) { if (!err) {
resolve(nodeFn.call(fs.readFile, credentialsFile, 'utf8').then(function(data) { resolve(JSON.parse(data));
return JSON.parse(data)
}));
} else { } else {
fs.exists(oldCredentialsFile, function(exists) { fs.readFile(oldCredentialsFile,'utf8',function(err,data) {
if (exists) { if (!err) {
resolve(nodeFn.call(fs.readFile, oldCredentialsFile, 'utf8').then(function(data) { resolve(JSON.parse(data));
return JSON.parse(data)
}));
} else { } else {
resolve({}); resolve({});
} }
@ -250,8 +246,9 @@ var localfilesystem = {
return when.resolve(); return when.resolve();
} }
if (fs.existsSync(credentialsFile)) { try {
fs.renameSync(credentialsFile,credentialsFileBackup); fs.renameSync(credentialsFile,credentialsFileBackup);
} catch(err) {
} }
var credentialData; var credentialData;
if (settings.flowFilePretty) { if (settings.flowFilePretty) {
@ -263,21 +260,18 @@ var localfilesystem = {
}, },
getSettings: function() { getSettings: function() {
if (fs.existsSync(globalSettingsFile)) { return when.promise(function(resolve,reject) {
return nodeFn.call(fs.readFile,globalSettingsFile,'utf8').then(function(data) { fs.readFile(globalSettingsFile,'utf8',function(err,data) {
if (data) { if (!err) {
try { try {
return JSON.parse(data); return resolve(JSON.parse(data));
} catch(err) { } catch(err2) {
log.trace("Corrupted config detected - resetting"); log.trace("Corrupted config detected - resetting");
return {};
} }
} else {
return {};
} }
}); return resolve({});
} })
return when.resolve({}); })
}, },
saveSettings: function(settings) { saveSettings: function(settings) {
if (settings.readOnly) { if (settings.readOnly) {
@ -286,21 +280,18 @@ var localfilesystem = {
return writeFile(globalSettingsFile,JSON.stringify(settings,null,1)); return writeFile(globalSettingsFile,JSON.stringify(settings,null,1));
}, },
getSessions: function() { getSessions: function() {
if (fs.existsSync(sessionsFile)) { return when.promise(function(resolve,reject) {
return nodeFn.call(fs.readFile,sessionsFile,'utf8').then(function(data) { fs.readFile(sessionsFile,'utf8',function(err,data){
if (data) { if (!err) {
try { try {
return JSON.parse(data); return resolve(JSON.parse(data));
} catch(err) { } catch(err2) {
log.trace("Corrupted sessions file - resetting"); log.trace("Corrupted sessions file - resetting");
return {};
} }
} else {
return {};
} }
resolve({});
})
}); });
}
return when.resolve({});
}, },
saveSessions: function(sessions) { saveSessions: function(sessions) {
if (settings.readOnly) { if (settings.readOnly) {

View File

@ -47,8 +47,8 @@ describe('nodes/registry/installer', function() {
registry.getModuleInfo.restore(); registry.getModuleInfo.restore();
} }
if (require('fs').existsSync.restore) { if (require('fs').statSync.restore) {
require('fs').existsSync.restore(); require('fs').statSync.restore();
} }
}); });
@ -160,7 +160,7 @@ describe('nodes/registry/installer', function() {
cb(null,"",""); cb(null,"","");
}); });
var exists = sinon.stub(fs,"existsSync", function(fn) { return true; }); sinon.stub(fs,"statSync", function(fn) { return {}; });
installer.uninstallModule("this_wont_exist").then(function(info) { installer.uninstallModule("this_wont_exist").then(function(info) {
info.should.eql(nodeInfo); info.should.eql(nodeInfo);