From 204478540e5042cbbe3d028e0eebe35a8f822d48 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rafa=C5=82=20Ch=C5=82odnicki?= Date: Tue, 16 Oct 2018 10:27:28 +0200 Subject: [PATCH] (Windows) Fix error on joining paths due to undefined home dir Use more robust way of getting home directory. The code is taken from AWS library to have parity in logic. --- lib/backend.js | 23 +++++++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) diff --git a/lib/backend.js b/lib/backend.js index 27c9032..40347a5 100644 --- a/lib/backend.js +++ b/lib/backend.js @@ -3,6 +3,7 @@ const _ = require('lodash') const {promisify} = require('es6-promisify') const path = require('path') const fs = require('fs') +const os = require('os') const errorhandler = require('errorhandler') const { extractKey, extractKeysForItems, parseKey } = require('./util') const bodyParser = require('body-parser') @@ -88,6 +89,22 @@ const createAwsConfig = (AWS) => { return dynamoConfig } +function getHomeDir() { + const env = process.env + const home = env.HOME || env.USERPROFILE + || (env.HOMEPATH ? ((env.HOMEDRIVE || 'C:/') + env.HOMEPATH) : null) + + if (home) { + return home + } + + if (typeof os.homedir === 'function') { + return os.homedir() + } + + return null +} + exports.createServer = (dynamodb, docClient) => { const app = express() app.set('json spaces', 2) @@ -95,8 +112,10 @@ exports.createServer = (dynamodb, docClient) => { app.set('views', path.resolve(__dirname, '..', 'views')) if (!dynamodb || !docClient) { - if (fs.existsSync(path.join(process.env.HOME, '.aws', 'credentials')) && - fs.existsSync(path.join(process.env.HOME, '.aws', 'config'))) { + const homeDir = getHomeDir() + + if (homeDir && fs.existsSync(path.join(homeDir, '.aws', 'credentials')) && + fs.existsSync(path.join(homeDir, '.aws', 'config'))) { process.env.AWS_SDK_LOAD_CONFIG = 1 }