Skip to content

Commit

Permalink
Merge pull request #28600 from software-mansion-labs/ts-migration/env…
Browse files Browse the repository at this point in the history
…ironment-lib

[No QA] [TS migration] Migrate 'Environment' lib to TypeScript
  • Loading branch information
neil-marcellini authored Oct 30, 2023
2 parents 0df9c69 + 91dcf2b commit a2dc22f
Show file tree
Hide file tree
Showing 10 changed files with 42 additions and 48 deletions.
3 changes: 2 additions & 1 deletion src/libs/ApiUtils.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import Onyx from 'react-native-onyx';
import {ValueOf} from 'type-fest';
import CONFIG from '@src/CONFIG';
import CONST from '@src/CONST';
import ONYXKEYS from '@src/ONYXKEYS';
Expand All @@ -8,7 +9,7 @@ import * as Environment from './Environment/Environment';

// To avoid rebuilding native apps, native apps use production config for both staging and prod
// We use the async environment check because it works on all platforms
let ENV_NAME = CONST.ENVIRONMENT.PRODUCTION;
let ENV_NAME: ValueOf<typeof CONST.ENVIRONMENT> = CONST.ENVIRONMENT.PRODUCTION;
let shouldUseStagingServer = false;
Environment.getEnvironment().then((envName) => {
ENV_NAME = envName;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import lodashGet from 'lodash/get';
import Config from 'react-native-config';
import CONFIG from '@src/CONFIG';
import CONST from '@src/CONST';
Expand All @@ -20,39 +19,31 @@ const OLDDOT_ENVIRONMENT_URLS = {

/**
* Are we running the app in development?
*
* @return {boolean}
*/
function isDevelopment() {
return lodashGet(Config, 'ENVIRONMENT', CONST.ENVIRONMENT.DEV) === CONST.ENVIRONMENT.DEV;
function isDevelopment(): boolean {
return (Config?.ENVIRONMENT ?? CONST.ENVIRONMENT.DEV) === CONST.ENVIRONMENT.DEV;
}

/**
* Are we running an internal test build?
*
* @return {boolean}
*/
function isInternalTestBuild() {
return lodashGet(Config, 'ENVIRONMENT', CONST.ENVIRONMENT.DEV) === CONST.ENVIRONMENT.ADHOC && lodashGet(Config, 'PULL_REQUEST_NUMBER', '');
function isInternalTestBuild(): boolean {
return !!((Config?.ENVIRONMENT ?? CONST.ENVIRONMENT.DEV) === CONST.ENVIRONMENT.ADHOC && (Config?.PULL_REQUEST_NUMBER ?? ''));
}

/**
* Get the URL based on the environment we are in
*
* @returns {Promise}
*/
function getEnvironmentURL() {
function getEnvironmentURL(): Promise<string> {
return new Promise((resolve) => {
getEnvironment().then((environment) => resolve(ENVIRONMENT_URLS[environment]));
});
}

/**
* Get the corresponding oldDot URL based on the environment we are in
*
* @returns {Promise<string>}
*/
function getOldDotEnvironmentURL() {
function getOldDotEnvironmentURL(): Promise<string> {
return getEnvironment().then((environment) => OLDDOT_ENVIRONMENT_URLS[environment]);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,20 @@ import * as AppUpdate from '@userActions/AppUpdate';
import CONST from '@src/CONST';
import ONYXKEYS from '@src/ONYXKEYS';
import pkg from '../../../../package.json';
import IsBetaBuild from './types';

let isLastSavedBeta = false;
Onyx.connect({
key: ONYXKEYS.IS_BETA,
callback: (value) => (isLastSavedBeta = value),
callback: (value) => {
isLastSavedBeta = Boolean(value);
},
});

/**
* Check the GitHub releases to see if the current build is a beta build or production build
*
* @returns {Promise}
*/
function isBetaBuild() {
function isBetaBuild(): IsBetaBuild {
return new Promise((resolve) => {
fetch(CONST.GITHUB_RELEASE_URL)
.then((res) => res.json())
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
import {NativeModules} from 'react-native';
import IsBetaBuild from './types';

/**
* Check to see if the build is staging (TestFlight) or production
*
* @returns {Promise}
*/
function isBetaBuild() {
function isBetaBuild(): IsBetaBuild {
return new Promise((resolve) => {
NativeModules.EnvironmentChecker.isBeta().then((isBeta) => {
NativeModules.EnvironmentChecker.isBeta().then((isBeta: boolean) => {
resolve(isBeta);
});
});
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import IsBetaBuild from './types';

/**
* There's no beta build in non native
*
* @returns {Promise}
*/
function isBetaBuild() {
function isBetaBuild(): IsBetaBuild {
return Promise.resolve(false);
}

Expand Down
3 changes: 3 additions & 0 deletions src/libs/Environment/betaChecker/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
type IsBetaBuild = Promise<boolean>;

export default IsBetaBuild;
14 changes: 0 additions & 14 deletions src/libs/Environment/getEnvironment/index.js

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,28 +1,26 @@
import lodashGet from 'lodash/get';
import Config from 'react-native-config';
import betaChecker from '@libs/Environment/betaChecker';
import CONST from '@src/CONST';
import Environment from './types';

let environment = null;
let environment: Environment | null = null;

/**
* Returns a promise that resolves with the current environment string value
*
* @returns {Promise}
*/
function getEnvironment() {
function getEnvironment(): Promise<Environment> {
return new Promise((resolve) => {
// If we've already set the environment, use the current value
if (environment) {
return resolve(environment);
}

if (lodashGet(Config, 'ENVIRONMENT', CONST.ENVIRONMENT.DEV) === CONST.ENVIRONMENT.DEV) {
if ((Config?.ENVIRONMENT ?? CONST.ENVIRONMENT.DEV) === CONST.ENVIRONMENT.DEV) {
environment = CONST.ENVIRONMENT.DEV;
return resolve(environment);
}

if (lodashGet(Config, 'ENVIRONMENT', CONST.ENVIRONMENT.DEV) === CONST.ENVIRONMENT.ADHOC) {
if ((Config?.ENVIRONMENT ?? CONST.ENVIRONMENT.DEV) === CONST.ENVIRONMENT.ADHOC) {
environment = CONST.ENVIRONMENT.ADHOC;
return resolve(environment);
}
Expand Down
9 changes: 9 additions & 0 deletions src/libs/Environment/getEnvironment/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import Config from 'react-native-config';
import CONST from '@src/CONST';
import Environment from './types';

function getEnvironment(): Promise<Environment> {
return Promise.resolve((Config?.ENVIRONMENT as Environment) ?? CONST.ENVIRONMENT.DEV);
}

export default getEnvironment;
6 changes: 6 additions & 0 deletions src/libs/Environment/getEnvironment/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import {ValueOf} from 'type-fest';
import CONST from '@src/CONST';

type Environment = ValueOf<typeof CONST.ENVIRONMENT>;

export default Environment;

0 comments on commit a2dc22f

Please sign in to comment.