Created
August 27, 2022 07:52
-
-
Save ZeldOcarina/94b46dd61a11a2075c664a23fcccd9ba to your computer and use it in GitHub Desktop.
A small app to build .env files programmatically
This file contains hidden or 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
const dotenv = require("dotenv"); | |
const fs = require("fs"); | |
const path = require("path"); | |
const { fetchGatsbyNodeDynamicConfigData } = require("./src/helpers/nodeHelpers"); | |
dotenv.config({ | |
path: `.env.${process.env.NODE_ENV}`, | |
}); | |
fetchGatsbyNodeDynamicConfigData({ | |
baseId: process.env.AIRTABLE_BASE_ID, | |
fields: "Value", | |
labelColumnName: "Label", | |
labelValue: "Web URL", | |
supposedStartingValue: "http", | |
expectedValueName: "Site URL", | |
}).then(siteUrl => { | |
// Append the value of siteUrl to .env file | |
const envFilePath = path.join(process.cwd(), `.env.${process.env.NODE_ENV}`); | |
const envFileContent = fs.readFileSync(envFilePath, "utf8"); | |
const newEnvFileContent = envFileContent.replace(/^SITE_URL.*$/m, `SITE_URL=${siteUrl}`); | |
fs.writeFileSync(envFilePath, newEnvFileContent, "utf8"); | |
console.log(`Site URL: ${siteUrl}`); | |
console.log(`dotenv file successfully updated!`); | |
// console.log(envFilePath); | |
// console.log(newEnvFileContent); | |
}).catch(err => { | |
console.error(err); | |
// Exit process with status 1 | |
process.exit(1); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment