|
|
|
|
|
|
|
const dotenv = require('dotenv'); |
|
const fs = require('fs'); |
|
const { exit } = require('process'); |
|
|
|
|
|
const originalConsoleWarn = console.warn; |
|
console.warn = () => {}; |
|
const loader = require('./loader'); |
|
console.warn = originalConsoleWarn; |
|
|
|
|
|
const apiEnvPath = loader.resolve('api/.env'); |
|
const clientEnvPath = loader.resolve('client/.env'); |
|
|
|
|
|
dotenv.config({ |
|
path: loader.resolve(apiEnvPath), |
|
}); |
|
dotenv.config({ |
|
path: loader.resolve(clientEnvPath), |
|
}); |
|
|
|
const initEnv = JSON.parse(JSON.stringify(process.env)); |
|
|
|
|
|
const rootEnvPath = loader.resolve('.env'); |
|
const devEnvPath = loader.resolve('.env.development'); |
|
const prodEnvPath = loader.resolve('.env.production'); |
|
|
|
if (fs.existsSync(rootEnvPath)) { |
|
console.error('Root env file already exists! Aborting'); |
|
exit(1); |
|
} |
|
|
|
|
|
if (!fs.existsSync(apiEnvPath)) { |
|
console.error('Api env doesn\'t exit! Did you mean to run install?'); |
|
exit(1); |
|
} |
|
if (!fs.existsSync(clientEnvPath)) { |
|
console.error('Client env doesn\'t exit! But api/.env does. Manual upgrade required'); |
|
exit(1); |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function refactorPairedEnvVar(varDev, varProd, varName) { |
|
|
|
if (initEnv[varDev] === undefined && initEnv[varProd] === undefined) { |
|
console.error(`Both ${varDev} and ${varProd} are undefined! Manual intervention required!`); |
|
} else if (initEnv[varDev] === undefined) { |
|
fs.appendFileSync(rootEnvPath, `\n${varName}=${initEnv[varProd]}`); |
|
} else if (initEnv[varProd] === undefined) { |
|
fs.appendFileSync(rootEnvPath, `\n${varName}=${initEnv[varDev]}`); |
|
} else if (initEnv[varDev] === initEnv[varProd]) { |
|
fs.appendFileSync(rootEnvPath, `\n${varName}=${initEnv[varDev]}`); |
|
} else { |
|
fs.appendFileSync(rootEnvPath, `${varName}=${initEnv[varProd]}\n`); |
|
fs.appendFileSync(devEnvPath, `${varName}=${initEnv[varDev]}\n`); |
|
} |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (fs.existsSync(apiEnvPath)) { |
|
fs.copyFileSync(apiEnvPath, rootEnvPath); |
|
fs.copyFileSync(apiEnvPath, rootEnvPath + '.api.bak'); |
|
fs.unlinkSync(apiEnvPath); |
|
} |
|
|
|
|
|
fs.appendFileSync( |
|
rootEnvPath, |
|
'\n\n##########################\n# Domain Variables:\n# Note: DOMAIN_ vars are passed to vite\n##########################\n', |
|
); |
|
refactorPairedEnvVar('CLIENT_URL_DEV', 'CLIENT_URL_PROD', 'DOMAIN_CLIENT'); |
|
refactorPairedEnvVar('SERVER_URL_DEV', 'SERVER_URL_PROD', 'DOMAIN_SERVER'); |
|
|
|
|
|
const removeEnvs = { |
|
NODE_ENV: 'remove', |
|
OPENAI_KEY: 'remove', |
|
CLIENT_URL_DEV: 'remove', |
|
CLIENT_URL_PROD: 'remove', |
|
SERVER_URL_DEV: 'remove', |
|
SERVER_URL_PROD: 'remove', |
|
JWT_SECRET_DEV: 'remove', |
|
JWT_SECRET_PROD: 'remove', |
|
VITE_APP_TITLE: 'remove', |
|
|
|
'#JWT:': 'remove', |
|
'# Add a secure secret for production if deploying to live domain.': 'remove', |
|
'# Site URLs:': 'remove', |
|
'# Don\'t forget to set Node env to development in the Server configuration section above': |
|
'remove', |
|
'# if you want to run in dev mode': 'remove', |
|
'# Change these values to domain if deploying:': 'remove', |
|
'# Set Node env to development if running in dev mode.': 'remove', |
|
}; |
|
loader.writeEnvFile(rootEnvPath, removeEnvs); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
fs.appendFileSync( |
|
rootEnvPath, |
|
'\n\n##########################\n# Secure Keys:\n##########################\n', |
|
); |
|
loader.addSecureEnvVar(rootEnvPath, 'CREDS_KEY', 32); |
|
loader.addSecureEnvVar(rootEnvPath, 'CREDS_IV', 16); |
|
loader.addSecureEnvVar(rootEnvPath, 'JWT_SECRET', 32); |
|
|
|
|
|
loader.writeEnvFile(rootEnvPath, { OPENAI_API_KEY: initEnv['OPENAI_KEY'] }); |
|
|
|
|
|
fs.appendFileSync( |
|
rootEnvPath, |
|
'\n\n##########################\n# Frontend Vite Variables:\n##########################\n', |
|
); |
|
const frontend = { |
|
APP_TITLE: initEnv['VITE_APP_TITLE'] || '"LibreChat"', |
|
ALLOW_REGISTRATION: 'true', |
|
}; |
|
loader.writeEnvFile(rootEnvPath, frontend); |
|
|
|
|
|
if (fs.existsSync(devEnvPath)) { |
|
fs.appendFileSync(devEnvPath, '\n'); |
|
} |
|
if (fs.existsSync(prodEnvPath)) { |
|
fs.appendFileSync(prodEnvPath, '\n'); |
|
} |
|
|
|
fs.copyFileSync(clientEnvPath, rootEnvPath + '.client.bak'); |
|
fs.unlinkSync(clientEnvPath); |
|
|
|
console.log('###############################################'); |
|
console.log('Upgrade completed! Please review the new .env file and make any changes as needed.'); |
|
console.log('###############################################'); |
|
|
|
|
|
if (fs.existsSync(devEnvPath)) { |
|
console.log( |
|
'NOTE: A .env.development file was created. This will take precedence over the .env file when running in dev mode.', |
|
); |
|
console.log('###############################################'); |
|
} |
|
|