-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
98 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
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 === '') { | ||
await generateHttpApiKey() | ||
} else { | ||
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) | ||
} | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import log from '@/helpers/log' | ||
|
||
import generateHttpApiKey from './generate-http-api-key' | ||
|
||
/** | ||
* Execute the generating HTTP API key script | ||
*/ | ||
(async () => { | ||
try { | ||
await generateHttpApiKey() | ||
} catch (e) { | ||
log.error(`Failed to generate the HTTP API key: ${e}`) | ||
} | ||
})() |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters