-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathutil.js
More file actions
35 lines (29 loc) · 936 Bytes
/
util.js
File metadata and controls
35 lines (29 loc) · 936 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
import fs from 'fs'
import dotenv from 'dotenv'
export const REFRESH_TOKEN_KEY = 'REFRESH_TOKEN'
/**
* Reads the refresh token from environment variables
*/
export function getRefreshTokenFromEnv() {
return process.env[REFRESH_TOKEN_KEY] || ''
}
/**
* Writes/updates the refresh token in the .env file
*/
export function updateRefreshTokenInEnv(newRefreshToken) {
const envFilePath = '.env'
const envVariables = fs.readFileSync(envFilePath, 'utf8').split('\n')
let replaced = false
const updatedEnvVariables = envVariables.map((line) => {
if (line.startsWith(`${REFRESH_TOKEN_KEY}=`)) {
replaced = true
return `${REFRESH_TOKEN_KEY}=${newRefreshToken}`
}
return line
})
if (!replaced) {
updatedEnvVariables.push(`${REFRESH_TOKEN_KEY}=${newRefreshToken}`)
}
fs.writeFileSync(envFilePath, updatedEnvVariables.join('\n'), 'utf8')
console.log('Updated refresh token in .env file')
}