Last active
September 25, 2019 16:55
-
-
Save devinrhode2/6bfd9f008f880dda06471a91db745202 to your computer and use it in GitHub Desktop.
attempt to try and change environment variables used in .eslintrc.js for CRA dev server without restarting the dev server
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
/* Usage: | |
const initGetEnv = require('./dev-scripts/get-env-var.js'); | |
const env = initGetEnv({ | |
useLiveEnv: typeof process.env.USE_LIVE_ENV !== 'undefined' | |
}); | |
if (env('foobar')) doSomething(); | |
// The idea is that you'd be able to change variables on the fly by editing the .env.live file. | |
// didn't work inside .eslintrc.js, could work in other contexts. | |
// for this to work a file needs to not be re-required each time it's used | |
*/ | |
/* globals process, module */ | |
/* eslint-disable security/detect-object-injection */ | |
const fs = require('fs'); | |
const convertToBool = rawVal => { | |
if (typeof rawVal === 'undefined') return false; | |
if (typeof rawVal === 'boolean') return rawVal; // good luck to you | |
if (rawVal === 'true' || rawVal === '1') return true; | |
if (rawVal === 'false' || rawVal === '0') return false; | |
if (parseInt(rawVal, 10) + '' === rawVal) { | |
return !!parseInt(rawVal, 10); | |
} | |
if (typeof rawVal !== 'string') { | |
console.log('rawVal is not a string.. rawVal:', rawVal); | |
return !!rawVal; | |
} | |
}; | |
// if not run from cli, then we don't want to change behavior. | |
module.exports = function initGetEnv({ useLiveEnv }) { | |
return useLiveEnv ? liveEnv : processEnv; | |
}; | |
const processEnv = varName => convertToBool(process.env[varName]); | |
const liveEnv = (function liveEnvClosure() { | |
// setup liveEnvVars | |
const liveEnvVars = {}; | |
try { | |
let dotEnvFileText = false; | |
let readEnvFile = false; | |
try { | |
dotEnvFileText = fs.readFileSync('./.env.live', { encoding: 'utf8' }); | |
readEnvFile = true; | |
} catch (e) { | |
console.error( | |
'missing /.env.live file. Try adding this to an .env.local file:' + | |
[ | |
'echo "ESLINT_FOR_GITLAB_CI=1\\n', | |
'IGNORE_AIRBNB=1\\n', | |
'IGNORE_ACCESSIBILITY=1\\n" | tee .env.live' | |
].join('\n') | |
); | |
} | |
if (readEnvFile) { | |
// console.log('read .env file', dotEnvFileText); | |
(dotEnvFileText.match(/[^\r\n]+/g) || []).forEach(function forEachEnvLine(line) { | |
if (!line.includes('=') || line.trim().startsWith('#') || line.trim().length < 1) | |
return; | |
const [key, value] = line.split('='); | |
liveEnvVars[key] = convertToBool(value); | |
}); | |
// console.log('liveEnvVars', liveEnvVars); | |
} | |
console.log('successfully parsed .env.live file'); | |
} catch (e) { | |
console.log('caught error (probably while trying to parse ./.env.live)', e); | |
} | |
return function liveEnvCore(varName) { | |
if (typeof process.env[varName] !== 'undefined') { | |
return processEnv(varName); | |
} else { | |
return liveEnvVars[varName]; | |
} | |
}; | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment