vega-frontend-monorepo/tools/utils/setup-environment.ts
botond 836c232a1c
Feat/675: Git info (#683)
* feat: add network-switcher lib

* feat: add env variables for some deployed app urls

* feat: add network processing to environment hoook

* refactor: network handling

* refactor: remove dialog from provider and add env setter

* feat: add network switcher dialog to the trading app

* refactor: add network redirect to dialog connect callback

* fix: lint

* feat: add network configuration files to static app

* feat: update environments to use config file instead of static node url

* refactor: split out network switcher utils

* refactor: split up environment hook

* fix: jsonify env variable for possible networks

* fix: add formatter file

* feat: add network loader component

* feat: add network loader to the trading app

* fix: assign correct global state to network swicther

* feat: add status modal

* feat: add network-switcher lib

* feat: add env variables for some deployed app urls

* feat: add network processing to environment hoook

* refactor: network handling

* refactor: remove dialog from provider and add env setter

* feat: add network switcher dialog to the trading app

* refactor: add network redirect to dialog connect callback

* fix: lint

* fix: jsonify env variable for possible networks

* fix: add formatter file

* fix: assign correct global state to network swicther

* fix: failing tests from UI changes

* feat: add environment validation

* feat: add runtime validation for network configs

* fix: readd node urls to envs to avoid breaking the apps for now

* chore: rename network swicther lib to environmnet

* fix: lint

* feat: add tests for config hook

* feat: add environment hook tests

* fix: lint

* fix: lint

* feat: add environment hook tests

* feat: add storage tests

* fix: formet

* feat: improve loading states

* fix: format

* fix: use router instead of window location

* fix: rearrange network loader props and components

* fix: remove FC type

* fix: env validation

* fix: untangle returns in network loader

* fix: add teardown for env and localstorage

* fix: add custom to env networks

* fix: lint

* fix: format

* fix: lint

* fix: remove env provider from simple trading app

* fix: remove failing promise hacks

* fix: some leftover format files

* fix: remove network switcher from tsconf

* fix: move Networks to libs/environment

* fix: add defaults for ether env vars

* feat: add tests for default ether env vars

* fix: remove chain id env var from web3 container

* fix: remove chain id from the environment

* fix: format

* fix: lint token

* fix: lint env

* fix: add comment to callout hack

* fix: lint token again

* fix: remove skip

* fix: move addresses to token app

* fix: improve schema validation errors and fix token app

* fix: lint

* fix: format

* fix: format

* fix: add network loaders to apps

* fix: format

* fix: remove logs

* fix: cypress process errors

* fix: change network loader hierarchy in token

* fix: remove stray console.log

* fix: revert test changes in simple trading app

* fix: prefix env vars with NX

Co-authored-by: Dexter Edwards <dexter.edwards93@gmail.com>

* fix: improve schema validation errors and fix token app

* fix: format

* fix: disable lint rules for catch block any types

* fix: format again

* fix: remove redundant process.platform injections

* fix: format

* feat: add explorer footer

* fix: format

* feat: add custom executor for next

* feat: adjust footer links and add custom executors

* fix: lint and format

* fix: format again

* fix: add explicit github feedback url

* fix: create footer lib

* feat: add footer to token app

* fix: add compiled file to ignore

* fix: make lozenge styling more flexible when no variant specified

* fix: remove schema duplication for third party executors

* fix: add missing intent variant for the lozenge

* fix: move commit hash in footer down two positions

* fix: revert breaking schema removals

* fix: format

* chore: rename footer to network-info

* fix: format

Co-authored-by: Joe <joe@vega.xyz>
Co-authored-by: Matthew Russell <mattrussell36@gmail.com>
Co-authored-by: Dexter Edwards <dexter.edwards93@gmail.com>
2022-07-06 15:53:35 +00:00

95 lines
2.6 KiB
TypeScript

import * as fs from 'fs';
import * as path from 'path';
import { execSync } from 'child_process';
import * as log from 'npmlog';
import * as dotenv from 'dotenv';
import type { ExecutorContext } from '@nrwl/devkit';
process.env['NX_GIT_COMMIT_HASH'] = execSync('git rev-parse HEAD')
.toString()
.replace(/[\r\n]/gm, '');
process.env['NX_GIT_BRANCH'] = execSync('git rev-parse --abbrev-ref HEAD')
.toString()
.replace(/[\r\n]/gm, '');
process.env['NX_GIT_ORIGIN_URL'] = execSync('git remote get-url origin')
.toString()
.replace('ssh://git@', 'https://')
.replace('.git', '')
.replace(/[\r\n]/gm, '');
const logEnvData = (
envMap: Record<string, string>,
envFiles: string[],
env?: string,
defaultEnvFile?: string,
loggerScope?: string
) => {
if (env && !envMap[env]) {
log.warn(loggerScope, `No environment called "${env}" found.`);
log.info(
loggerScope,
envFiles.length > 0
? `You can create a new environment by putting an ".env.${env}" file in your project root, or you can use the following available ones: ${envFiles.join(
', '
)}.`
: 'To get started with environments, you can create an ".env" file in your project root with the desired variables.'
);
}
if (!envMap[env]) {
log.info(
loggerScope,
defaultEnvFile
? `Using "${defaultEnvFile}" as the default project environment.`
: 'Serving the project only using the environment variables scoped to your CLI.'
);
} else {
log.info(
loggerScope,
`Using "${envMap[env]}" as the default project environment.`
);
}
};
const filenameToEnv = (filename: string) => filename.replace('.env.', '');
const getDefaultEnvFile = (envMap: Record<string, string>) => {
return envMap['local'] || envMap['.env'];
};
const getEnvFile = (env: string, envFiles: string[], loggerScope?: string) => {
const envMap = envFiles.reduce(
(acc, filename) => ({
...acc,
[filenameToEnv(filename)]: filename,
}),
{}
);
const defaultEnvFile = getDefaultEnvFile(envMap);
logEnvData(envMap, envFiles, env, defaultEnvFile, loggerScope);
return envMap[env] || defaultEnvFile;
};
export default async function setup(
env: string,
context: ExecutorContext,
loggerScope?: string
) {
const { root } = context.workspace.projects[context.projectName];
const workspacePath = path.join(context.cwd, root);
const files = await fs.promises.readdir(workspacePath);
const envFile = getEnvFile(
env,
files.filter((f) => f.startsWith('.env')),
loggerScope
);
if (env && envFile) {
dotenv.config({ path: path.join(workspacePath, envFile), override: true });
}
}