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';
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"));
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"));
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}));
throw "Error : "+RED._("rpi-gpio.errors.mustbeexecutable");
}

View File

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

View File

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

View File

@ -40,7 +40,7 @@ function checkModulePath(folder) {
var err;
var fullPath = path.resolve(folder);
var packageFile = path.join(fullPath,'package.json');
if (fs.existsSync(packageFile)) {
try {
var pkg = require(packageFile);
moduleName = pkg.name;
if (!pkg['node-red']) {
@ -49,7 +49,7 @@ function checkModulePath(folder) {
err.code = 'invalid_module';
throw err;
}
} else {
} catch(err2) {
err = new Error("Module not found");
err.code = 404;
throw err;
@ -151,7 +151,10 @@ function uninstallModule(module) {
}
var installDir = settings.userDir || process.env.NODE_RED_HOME || ".";
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})));
}

View File

@ -49,16 +49,17 @@ function getLocalFile(file) {
if (isExcluded(path.basename(file))) {
return null;
}
if (fs.existsSync(file.replace(/\.js$/,".html"))) {
try {
fs.statSync(file.replace(/\.js$/,".html"));
return {
file: file,
module: "node-red",
name: path.basename(file).replace(/^\d+-/,"").replace(/\.js$/,""),
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");
if (iconDirs.indexOf(iconDir) == -1) {
if (fs.existsSync(iconDir)) {
try {
fs.statSync(iconDir);
events.emit("node-icon-dir",iconDir);
iconDirs.push(iconDir);
} catch(err) {
}
}
}

View File

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

View File

@ -47,8 +47,8 @@ describe('nodes/registry/installer', function() {
registry.getModuleInfo.restore();
}
if (require('fs').existsSync.restore) {
require('fs').existsSync.restore();
if (require('fs').statSync.restore) {
require('fs').statSync.restore();
}
});
@ -160,7 +160,7 @@ describe('nodes/registry/installer', function() {
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) {
info.should.eql(nodeInfo);