Hi, I am currently learning Typescript, for me the best way seems to be porting an already known app written in python to Typescript.
But as always while learning new languages there are some specific implementation details.
Given following class which is some kind of proxy vor environment variables. What it basically does is defering the resoltion of the given envrionment variable until the value is requested.
class EnvProxy:
def __init__(self, key: str) -> None:
self._key = key
def __get__(self, obj: Any, objtype: Any = None) -> str:
if os.getenv("CI"): # when running within a gitlab ci pipeline
return os.environ[self._key]
# indicate that we are not running within a pipeline by
# returning an empty string
if self._key == "CI":
return ""
# in the case we are not running within a pipeline ($CI is empty)
# for all other variables we return a dummy value which
# explicitly describe this state
return os.getenv(self._key, "notRunningInAPipeline")
To implement the functionality into other classes I am using this EnvProxy class and assign an instance to a variable.
class PredefinedVariables:
CI: EnvProxy = EnvProxy("CI")
"""
Mark that job is executed in CI environment.
Added in GitLab all
Available in GitLab Runner 0.4
Raises:
KeyError: If environment variable not available.
"""
As soon as I am using this CI variable inside my app the key the __get__() method is invoked and returns the environment variable value.
if PredefinedVariables.CI:
print("Running in CI")
How can I achieve the same behavior in TS?
Regards!