mirror of
https://github.com/kingomarnajjar/flex-template-web.git
synced 2026-07-26 06:47:17 +10:00
Save mandatory env variables to .env file
This commit is contained in:
parent
04a16fe8fe
commit
7706bcd8f0
1 changed files with 74 additions and 0 deletions
|
|
@ -1,8 +1,70 @@
|
|||
const inquirer = require('inquirer');
|
||||
const fs = require('fs');
|
||||
const readline = require('readline');
|
||||
const chalk = require('chalk');
|
||||
|
||||
const mandatoryVariables = [
|
||||
{
|
||||
type: 'input',
|
||||
name: 'REACT_APP_SHARETRIBE_SDK_CLIENT_ID',
|
||||
message: `What is your Flex client id?`,
|
||||
},
|
||||
{
|
||||
type: 'input',
|
||||
name: 'REACT_APP_STRIPE_PUBLISHABLE_KEY',
|
||||
message: `What is your Stripe publishable key?`,
|
||||
},
|
||||
{
|
||||
type: 'input',
|
||||
name: 'REACT_APP_MAPBOX_ACCESS_TOKEN',
|
||||
message: `What is your Mapbox access token?`,
|
||||
},
|
||||
];
|
||||
|
||||
|
||||
const updateEnvFile = data => {
|
||||
let content = '';
|
||||
data.map(line => {
|
||||
content = content + line.toString();
|
||||
});
|
||||
|
||||
fs.writeFileSync('./.env', content);
|
||||
};
|
||||
|
||||
const checkIfSameLine = (answers, line) => {
|
||||
let foundKey;
|
||||
if (answers) {
|
||||
Object.keys(answers).map(key => {
|
||||
if (line.includes(key)) {
|
||||
foundKey = key;
|
||||
}
|
||||
});
|
||||
}
|
||||
return foundKey;
|
||||
};
|
||||
|
||||
const readLines = answers => {
|
||||
return new Promise((resolve, reject) => {
|
||||
const rl = readline.createInterface({
|
||||
input: require('fs').createReadStream('./.env')
|
||||
});
|
||||
|
||||
const data = [];
|
||||
rl.on('line', function (line) {
|
||||
const key = checkIfSameLine(answers, line);
|
||||
if (key) {
|
||||
data.push(`${key}=${answers[key]}\n`);
|
||||
} else {
|
||||
data.push(`${line}\n`);
|
||||
}
|
||||
});
|
||||
|
||||
rl.on('close', () => {
|
||||
resolve(data);
|
||||
});
|
||||
});
|
||||
};
|
||||
|
||||
const createEnvFile = () => {
|
||||
fs.copyFileSync('./.env-template', './.env', err => {
|
||||
if (err) throw err;
|
||||
|
|
@ -21,6 +83,18 @@ if (fs.existsSync(`./.env`)) {
|
|||
|
||||
createEnvFile();
|
||||
|
||||
console.log(chalk.bold('Mandatory variables'));
|
||||
inquirer
|
||||
.prompt(mandatoryVariables)
|
||||
.then(answers => {
|
||||
return readLines(answers);
|
||||
})
|
||||
.then(data => {
|
||||
updateEnvFile(data);
|
||||
})
|
||||
.catch(err => {
|
||||
console.log(chalk.red(`An error occurred due to: ${err.message}`));
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue