diff --git a/README.md b/README.md index c07d6a7..4589037 100644 --- a/README.md +++ b/README.md @@ -30,5 +30,4 @@ cd to `react/resume` and run `build.sh`. If you are running in debug mode, run `yarn/npm start` ## Generate the key -Install node modules in following directories: `.`, `./scripts` -Then, cd to root and run `node scripts/generate_key.js` \ No newline at end of file +Cd to root of project and run `scripts/generate_key.py` \ No newline at end of file diff --git a/scripts/generate_key.js b/scripts/generate_key.js index be34552..6f6a87a 100644 --- a/scripts/generate_key.js +++ b/scripts/generate_key.js @@ -1,53 +1,3 @@ -const crypto = require('crypto'); -const path = require('path'); -const args = require('args-parser')(process.argv); -const fs = require('fs'); -const base64 = require('js-base64'); +const { exec } = require('child_process'); -let key = crypto.randomFillSync(Buffer.alloc(32)).toString('base64'); -let dotenv = path.resolve('.env'); - -if (args['help']) { - console.log(path.basename(__filename) + ' [--key-only] [--env|-e] [--set-key] [--dry-run] [--stdout|-s]'); - console.log('\n' + - ' --help: Display this help\n' + - ' --key-only: Generate key only and put it in stdout\n' + - ' --env -e: Specify an env file\n' + - ' --set-key: Specify your key\n' + - ' --dry-run: Don\'t write anything, just do the thing\n' + - ' --stdout -s: Don\'t write to file, write to stdout instead' - ); - process.exit(0); -} - -if (args['key-only']) { - console.log(key); - process.exit(0); -} -if (args['env'] || args['e']) { - dotenv = args['env'] ? args['env'] : args['e']; -} -if (args['set-key']) { - key = args['set-key']; -} - -const file = fs.readFileSync(dotenv).toString('utf8'); - -let lines = file.split('\n'); - -for (let i = 0; i != lines.length; i++) { - let line = lines[i]; - if (line.startsWith('APP_KEY=')) { - lines[i] = 'APP_KEY=' + key; - } -} - -const newfile = lines.join('\n'); - -const w_stdout = args['stdout'] || args['s']; - -if (!args['dry-run']) { - if (w_stdout) console.log(newfile); - else - fs.writeFileSync(dotenv, newfile); -} \ No newline at end of file +exec('generate_key.py'); \ No newline at end of file diff --git a/scripts/generate_key.py b/scripts/generate_key.py new file mode 100755 index 0000000..270a167 --- /dev/null +++ b/scripts/generate_key.py @@ -0,0 +1,24 @@ +#!/usr/bin/env python3 + +import os +import secrets +import base64 + +if (not os.path.exists('.env')): + print('No .env file found. Please create a dotenv to proceed.'); + exit(-1); + +key_bytes = secrets.token_bytes(32); +dotenv_text = ''; + +with open('.env', 'tr', encoding='utf-8') as f: + dotenv_text = f.read(); + +dotenv_lines = dotenv_text.split('\n'); +key_line = 0; + +for i, line in enumerate(dotenv_lines): + if (line.startswith('APP_KEY=')): + dotenv_lines[i] = 'APP_KEY=' + base64.b64encode(key_bytes).decode('utf-8'); + +with open('.env', 'tw', encoding='utf-8') as f: print('\n'.join(dotenv_lines), file=f) \ No newline at end of file