Skip to content

Commit

Permalink
Merge pull request #2 from flotiq/feature/25530-flotiq-setup-saves-co…
Browse files Browse the repository at this point in the history
…mments-in-env-files

#25530 flotiq setup saves comments in env files
  • Loading branch information
CiotkaCierpienia authored Nov 7, 2024
2 parents 849e8c0 + 6d58aff commit 4e67091
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 23 deletions.
30 changes: 23 additions & 7 deletions commands/setup.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,15 +69,31 @@ async function setup(authUrl, logger, roKey, rwKey, noStore) {
}

// Save the token to .env file
if(roKey) {
saveTokenToEnv("GATSBY_FLOTIQ_API_KEY", api_key, ".env", logger);
saveTokenToEnv("FLOTIQ_API_KEY", api_key, ".env", logger);
saveTokenToEnv("GATSBY_FLOTIQ_API_KEY", api_key, ".env.development", logger);
saveTokenToEnv("FLOTIQ_API_KEY", api_key, ".env.development", logger);
if (roKey) {
saveTokenToEnv(
"GATSBY_FLOTIQ_API_KEY",
api_key,
[".env", ".env.development"],
logger,
"Your Flotiq Api key, allows application to download data from Your Flotiq account"
);

saveTokenToEnv(
"FLOTIQ_API_KEY",
api_key,
[".env", ".env.development"],
logger,
"Your Flotiq Api key, allows application to download data from Your Flotiq account"
);
}
if (rwKey) {
saveTokenToEnv("FLOTIQ_RW_API_KEY", api_key_rw, ".env", logger);
saveTokenToEnv("FLOTIQ_RW_API_KEY", api_key_rw, ".env.development", logger);
saveTokenToEnv(
"FLOTIQ_RW_API_KEY",
api_key_rw,
[".env", ".env.development"],
logger,
"Your Flotiq Read and Write Api key, allows application to download or modify data from Your Flotiq account"
);
}

logger.log(chalk.bgWhite.hex("#0083FC").inverse("Your .env files have been adjusted with your Flotiq API keys. You can close this terminal."));
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
"axios": "^1.6.8",
"chalk": "^4.1.1",
"dotenv": "^16.4.5",
"dotenv-flow": "^4.1.0",
"esm": "^3.2.25",
"express": "^4.19.2",
"open": "^8.4.2",
Expand Down
47 changes: 32 additions & 15 deletions src/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ const fs = require("fs");
const dotenv = require("dotenv");
const chalk = require("chalk");
const open = require('open');
const dotenvFlow = require('dotenv-flow');

/**
* This function opens the Flotiq login screen and requests a redirect
Expand All @@ -25,25 +26,41 @@ function loginRedirect(authUrl, port, roKey, rwKey) {
open(url.toString());
}

function saveTokenToEnv(key, value, file, logger) {
const envFilePath = path.resolve(process.cwd(), file);
function saveTokenToEnv(key, value, files, logger, comment = '') {
files.forEach((file) => {
const envFilePath = path.resolve(process.cwd(), file);

// Check if .env file exists, if not, create it
if (!fs.existsSync(envFilePath)) {
fs.writeFileSync(envFilePath, "", {encoding: "utf8"});
//logger.log(chalk.magenta(".env file created"));
}

const envConfig = dotenvFlow.parse(envFilePath);

let envKey = ``;
if (comment) {
envKey += `\n# ${comment}`
}
envKey += `\n${key}=${value}\n`

// Check if .env file exists, if not, create it
if (!fs.existsSync(envFilePath)) {
fs.writeFileSync(envFilePath, "", {encoding: "utf8"});
//logger.log(chalk.magenta(".env file created"));
}

const envConfig = dotenv.parse(fs.readFileSync(envFilePath));
// env variable exists but is empty
if (!envConfig[key] && typeof envConfig[key] !== "undefined") {
const fileContent = fs.readFileSync(envFilePath, {encoding: "utf8"});
const updatedContent = fileContent.replace(`${key}=`, '');

// Update or add the key-value pair in the env file
envConfig[key] = value;
const newEnvContent = Object.keys(envConfig)
.map((k) => `${k}=${envConfig[k]}`)
.join("\n");
fs.writeFileSync(envFilePath, updatedContent);
fs.appendFileSync(envFilePath, envKey);
} else if (typeof envConfig[key] === "undefined") {
fs.appendFileSync(envFilePath, envKey);
} else if (envConfig[key] && typeof envConfig[key] !== "undefined") {
logger.log(chalk.yellow(`${key} already exists in`), chalk.yellow(file));
return;
}

fs.writeFileSync(envFilePath, newEnvContent);
logger.log(chalk.green(`${key} updated in`), chalk.yellow(file));
logger.log(chalk.green(`${key} updated in`), chalk.yellow(file));
});
}

module.exports = {
Expand Down
9 changes: 8 additions & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,14 @@ destroy@1.2.0:
resolved "https://registry.npmjs.org/destroy/-/destroy-1.2.0.tgz"
integrity sha512-2sJGJTaXIIaR1w4iJSNoN0hnMY7Gpc/n8D4qSCJw8QqFWXf7cuAgnEHxBpweaVcPevC2l3KpjYCx3NypQQgaJg==

dotenv@^16.4.5:
dotenv-flow@^4.1.0:
version "4.1.0"
resolved "https://registry.yarnpkg.com/dotenv-flow/-/dotenv-flow-4.1.0.tgz#a78e79dcaf08a48200192eb5ed1e5af11062b861"
integrity sha512-0cwP9jpQBQfyHwvE0cRhraZMkdV45TQedA8AAUZMsFzvmLcQyc1HPv+oX0OOYwLFjIlvgVepQ+WuQHbqDaHJZg==
dependencies:
dotenv "^16.0.0"

dotenv@^16.0.0, dotenv@^16.4.5:
version "16.4.5"
resolved "https://registry.npmjs.org/dotenv/-/dotenv-16.4.5.tgz"
integrity sha512-ZmdL2rui+eB2YwhsWzjInR8LldtZHGDoQ1ugH85ppHKwpUHL7j7rN0Ti9NCnGiQbhaZ11FpR+7ao1dNsmduNUg==
Expand Down

0 comments on commit 4e67091

Please sign in to comment.