watson-developer-cloud
Version:
Client library to use the IBM Watson Services and AlchemyAPI
51 lines • 1.87 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
var dotenv = require("dotenv");
var fs = require("fs");
var os = require("os");
var path = require("path");
var filename = 'ibm-credentials.env';
function readCredentialsFile() {
// first look for an env variable called IBM_CREDENTIALS_FILE
// it should be the path to the file
var givenFilepath = process.env['IBM_CREDENTIALS_FILE'] || '';
var homeDir = constructFilepath(os.homedir());
var workingDir = constructFilepath(process.cwd());
var filepathToUse;
if (givenFilepath) {
if (fileExistsAtPath(givenFilepath)) {
// see if user gave a path to a file named something other than `ibm-credentials.env`
filepathToUse = givenFilepath;
}
else if (fileExistsAtPath(constructFilepath(givenFilepath))) {
// see if user gave a path to the directory where file is located
filepathToUse = constructFilepath(givenFilepath);
}
}
else if (fileExistsAtPath(homeDir)) {
filepathToUse = homeDir;
}
else if (fileExistsAtPath(workingDir)) {
filepathToUse = workingDir;
}
else {
// file does not exist anywhere, will not be used
return {};
}
var credsFile = fs.readFileSync(filepathToUse);
return dotenv.parse(credsFile);
}
exports.readCredentialsFile = readCredentialsFile;
function fileExistsAtPath(filepath) {
return fs.existsSync(filepath) && fs.lstatSync(filepath).isFile();
}
exports.fileExistsAtPath = fileExistsAtPath;
function constructFilepath(filepath) {
// ensure filepath includes the filename
if (!filepath.endsWith(filename)) {
filepath = path.join(filepath, filename);
}
return filepath;
}
exports.constructFilepath = constructFilepath;
//# sourceMappingURL=read-credentials-file.js.map