1
1
mirror of https://github.com/leon-ai/leon.git synced 2024-12-20 23:31:30 +03:00
leon/scripts/generate/generate-http-api-key.js
2022-09-21 22:24:48 +08:00

77 lines
1.8 KiB
JavaScript

import dotenv from 'dotenv'
import crypto from 'crypto'
import fs from 'fs'
import { prompt } from 'inquirer'
import path from 'path'
import { LOG } from '@/helpers/log'
import { STRING } from '@/helpers/string'
dotenv.config()
/**
* Generate HTTP API key script
* save it in the .env file
*/
const generateHttpApiKey = () =>
new Promise(async (resolve, reject) => {
LOG.info('Generating the HTTP API key...')
try {
const shasum = crypto.createHash('sha1')
const str = STRING.random(11)
const dotEnvPath = path.join(process.cwd(), '.env')
const envVarKey = 'LEON_HTTP_API_KEY'
let content = fs.readFileSync(dotEnvPath, 'utf8')
shasum.update(str)
const sha1 = shasum.digest('hex')
let lines = content.split('\n')
lines = lines.map((line) => {
if (line.indexOf(`${envVarKey}=`) !== -1) {
line = `${envVarKey}=${sha1}`
}
return line
})
content = lines.join('\n')
fs.writeFileSync(dotEnvPath, content)
LOG.success('HTTP API key generated')
resolve()
} catch (e) {
LOG.error(e.message)
reject(e)
}
})
export default () =>
new Promise(async (resolve, reject) => {
try {
if (
!process.env.LEON_HTTP_API_KEY ||
process.env.LEON_HTTP_API_KEY === ''
) {
await generateHttpApiKey()
} else if (!process.env.IS_DOCKER) {
const answer = await prompt({
type: 'confirm',
name: 'generate.httpApiKey',
message: 'Do you want to regenerate the HTTP API key?',
default: false
})
if (answer.generate.httpApiKey === true) {
await generateHttpApiKey()
}
}
resolve()
} catch (e) {
reject(e)
}
})