Skip to content

Commit

Permalink
(Windows) Fix error on joining paths due to undefined home dir
Browse files Browse the repository at this point in the history
Use more robust way of getting home directory. The code is taken from AWS
library to have parity in logic.
  • Loading branch information
rchl committed Oct 16, 2018
1 parent daa02af commit 2044785
Showing 1 changed file with 21 additions and 2 deletions.
23 changes: 21 additions & 2 deletions lib/backend.js
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down Expand Up @@ -88,15 +89,33 @@ 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)
app.set('view engine', 'ejs')
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
}

Expand Down

0 comments on commit 2044785

Please sign in to comment.