@bscotch/stitch
Version:
Stitch: The GameMaker Studio 2 Asset Pipeline Development Kit.
45 lines • 1.71 kB
JavaScript
/**
* @file Gather environment variables. Look for a local .env file,
* and a global (in user home) .?stitch.env file.
*/
import fs from 'fs-extra';
import os from 'os';
import path from 'path';
/** Only return those requested. (Use 'as const' to flag input as readonly ) */
export function loadEnvironmentVariables(varNames) {
// In lowest-to-highest precedence, in case
// there are multiple env files.
const possibleEnvDirs = [os.homedir(), process.cwd()];
const possibleEnvFileNames = ['.env', '.stitch.env', 'stitch.env'];
const env = {};
for (const dir of possibleEnvDirs) {
for (const file of possibleEnvFileNames) {
try {
const filepath = path.join(dir, file);
const contents = fs.readFileSync(filepath, 'utf-8');
const lines = contents.split(/[\r\n]+/).filter((x) => x);
for (const line of lines) {
const [, varName, value] = (line
.match(/^([^=]+)=(.+)$/)
?.map((x) => x.trim())
.filter((x) => x) || []);
if (varName && varNames.includes(varName) && value) {
env[varName] = value;
}
}
}
catch (err) {
if (err?.code != 'ENOENT') {
console.log(err);
}
}
}
}
return env;
}
/** Load env var GITHUB_PERSONAL_ACCESS_TOKEN if it exists */
export function getGithubAccessToken() {
return loadEnvironmentVariables(['GITHUB_PERSONAL_ACCESS_TOKEN'])
.GITHUB_PERSONAL_ACCESS_TOKEN;
}
//# sourceMappingURL=env.js.map