Skip to content

Commit

Permalink
fix: made saveAndReplaceDotEnvCredentials pass
Browse files Browse the repository at this point in the history
  • Loading branch information
shazron committed Aug 24, 2020
1 parent ee3390f commit 1218657
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 42 deletions.
63 changes: 23 additions & 40 deletions lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -366,51 +366,34 @@ async function runOpenWhiskJar (jarFile, runtimeConfigFile, apihost, waitInitTim
}
}

// function saveAndReplaceDotEnvCredentials (dotenvFile, saveFile, apihost, namespace, auth) {
// if (fs.existsSync(saveFile)) throw new Error(`cannot save .env, please make sure to restore and delete ${saveFile}`) // todo make saveFile relative
// fs.moveSync(dotenvFile, saveFile)
// // Only override needed env vars and preserve other vars in .env
// const env = dotenv.parse(fs.readFileSync(saveFile))
// const newCredentials = {
// AIO_RUNTIME_NAMESPACE: namespace,
// AIO_RUNTIME_AUTH: auth,
// AIO_RUNTIME_APIHOST: apihost
// }

// // remove old keys (match by normalized key name)
// for (const key in env) {
// const match = key.match(/^AIO_(.+)/i)
// if (match) {
// for (const newCredential in newCredentials) {
// if (newCredential.toLowerCase() === key.toLowerCase()) {
// delete env[key]
// }
// }
// }
// }

// // set the new keys
// for (const key in newCredentials) {
// env[key] = newCredentials[key]
// }

// const envContent = Object.keys(env).reduce((content, k) => content + `${k}=${env[k]}\n`, '')

// fs.writeFileSync(dotenvFile, envContent)
// }

function saveAndReplaceDotEnvCredentials (dotenvFile, saveFile, apihost, namespace, auth) {
if (fs.existsSync(saveFile)) throw new Error(`cannot save .env, please make sure to restore and delete ${saveFile}`) // todo make saveFile relative
fs.moveSync(dotenvFile, saveFile)
// Only override needed env vars and preserve other vars in .env
const env = dotenv.parse(fs.readFileSync(saveFile))
env.AIO_RUNTIME_APIHOST = apihost
env.AIO_RUNTIME_AUTH = auth
env.AIO_RUNTIME_NAMESPACE = namespace
// existing AIO__ vars might override above AIO_ vars
delete env.AIO__RUNTIME_AUTH
delete env.AIO__RUNTIME_NAMESPACE
delete env.AIO__RUNTIME_APIHOST
const newCredentials = {
AIO_RUNTIME_NAMESPACE: namespace,
AIO_RUNTIME_AUTH: auth,
AIO_RUNTIME_APIHOST: apihost
}

// remove old keys (match by normalized key name)
for (const key in env) {
const match = key.match(/^AIO_(.+)/i)
if (match) {
for (const newCredential in newCredentials) {
if (newCredential.toLowerCase() === key.toLowerCase()) {
delete env[key]
}
}
}
}

// set the new keys
for (const key in newCredentials) {
env[key] = newCredentials[key]
}

const envContent = Object.keys(env).reduce((content, k) => content + `${k}=${env[k]}\n`, '')

fs.writeFileSync(dotenvFile, envContent)
Expand Down
4 changes: 2 additions & 2 deletions test/__fixtures__/app-env/my-env.expected
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
AIO_RUNTIME_APIHOST=local-apihost
AIO_RUNTIME_NAMESPACE=local-namespace
AIO_RUNTIME_AUTH=local-auth
AIO_RUNTIME_NAMESPACE=local-namespace
AIO_RUNTIME_APIHOST=local-apihost

0 comments on commit 1218657

Please sign in to comment.