A module for storing and accessing environment details across different platforms.
Go to file
2024-05-29 14:12:50 +02:00
.gitea/workflows fix(core): update 2023-10-03 18:55:16 +02:00
.vscode fix(core): update 2022-12-28 19:43:48 +01:00
test fix(core): update 2023-10-03 18:54:51 +02:00
ts fix(core): update 2023-11-09 16:37:52 +01:00
.gitignore fix(core): update 2020-06-25 22:23:10 +00:00
.npmignore update travis 2016-02-20 10:21:09 +01:00
LICENSE now has a env vars feature 2017-04-21 00:04:28 +02:00
npmextra.json update tsconfig 2024-04-14 17:32:19 +02:00
package.json update description 2024-05-29 14:12:50 +02:00
pnpm-lock.yaml fix(core): update 2023-11-09 16:20:20 +01:00
readme.hints.md update tsconfig 2024-04-14 17:32:19 +02:00
readme.md update tsconfig 2024-04-14 17:32:19 +02:00
tsconfig.json fix(core): update 2023-10-03 18:55:16 +02:00

@push.rocks/smartenv

store things about your environment and let them travel across modules

Install

To install @push.rocks/smartenv, you need Node.js and npm installed. Then, run the following command:

npm install @push.rocks/smartenv --save

Usage

@push.rocks/smartenv is a valuable utility for managing and accessing environment-specific information within your application, enabling your code to adapt to different environments, such as development, testing, and production seamlessly. Here we delve into how to utilize @push.rocks/smartenv effectively within a TypeScript project. We'll cover initializing the package, checking the execution environment, loading modules conditionally based on the environment, and accessing environment variables securely.

Getting Started

First, ensure to import Smartenv from the package:

import { Smartenv } from '@push.rocks/smartenv';

Initializing Smartenv

You can begin by creating an instance of Smartenv. This instance will be your gateway to accessing and managing the environment properties throughout your application.

const smartEnv = new Smartenv();

Checking Execution Environment

Smartenv offers straightforward methods to determine whether your code is running in a Node.js or browser environment. This can be particularly useful for isomorphic code.

// Check if running in a Node.js environment
console.log(`Is Node: ${smartEnv.isNode}`);

// Check if running in a browser environment
console.log(`Is Browser: ${smartEnv.isBrowser}`);

Loading Modules Conditionally

One of the core functionalities of @push.rocks/smartenv is to load modules based on the execution environment. This feature is immensely useful for isomorphic applications that need to run both in the browser and Node.js environments.

// Node.js module and equivalent browser module URLs
const nodeModuleName = 'example-node-module';
const browserModuleUrl = 'https://example.com/example-browser-module.js';

// Function to access the module in the browser
const getBrowserModule = () => window.exampleBrowserModule;

const module = await smartEnv.getEnvAwareModule({
  nodeModuleName: nodeModuleName,
  webUrlArg: browserModuleUrl,
  getFunction: getBrowserModule
});

console.log(module);

Accessing Environment Variables Securely

For Node.js environments, accessing environment variables is a common task. Smartenv does not directly manipulate environment variables but encourages best practices for accessing them, ensuring your application remains secure and performs optimally.

if(smartEnv.isNode) {
  console.log(`Your environment variable value: ${process.env.YOUR_ENV_VAR}`);
}

Detecting CI Environments

Detect if your application is running in a Continuous Integration (CI) environment. This can be useful for modifying behavior during testing or deployment.

console.log(`Is CI: ${smartEnv.isCI}`);

Platform-Specific Checks

Smartenv enables you to perform checks for specific operating systems when running in a Node.js environment, which can be particularly useful for tasks that depend on OS-specific features or paths.

const isMac = await smartEnv.isMacAsync();
const isWindows = await smartEnv.isWindowsAsync();
const isLinux = await smartEnv.isLinuxAsync();

console.log(`Is Mac: ${isMac}, Is Windows: ${isWindows}, Is Linux: ${isLinux}`);

Printing Environment for Debugging

To assist with debugging, Smartenv offers a method to print the current environment and some relevant information to the console. This feature is particularly useful during development or when troubleshooting environment-specific issues.

smartEnv.printEnv();

Usage within Browser

While Smartenv primarily facilitates handling different environments in server-side applications, it also provides functionalities to manage and load scripts dynamically in the browser. This allows for more flexible and environment-aware code sharing between server and client-side.

For instance, using getSafeWebModule to dynamically load a script only if it hasn't been already and then utilizing a globally available function or object that the script provides.

Conclusion

@push.rocks/smartenv is a comprehensive solution for managing environment-specific logic and configurations in your JavaScript and TypeScript projects. By leveraging its functionalities, developers can create more robust, flexible, and maintainable applications that seamlessly adapt to different execution contexts, whether in Node.js, the browser, or CI environments.

Always refer to the official documentation and source code for the most up-to-date usage examples and features.

This repository contains open-source code that is licensed under the MIT License. A copy of the MIT License can be found in the license file within this repository.

Please note: The MIT License does not grant permission to use the trade names, trademarks, service marks, or product names of the project, except as required for reasonable and customary use in describing the origin of the work and reproducing the content of the NOTICE file.

Trademarks

This project is owned and maintained by Task Venture Capital GmbH. The names and logos associated with Task Venture Capital GmbH and any related products or services are trademarks of Task Venture Capital GmbH and are not included within the scope of the MIT license granted herein. Use of these trademarks must comply with Task Venture Capital GmbH's Trademark Guidelines, and any usage must be approved in writing by Task Venture Capital GmbH.

Company Information

Task Venture Capital GmbH
Registered at District court Bremen HRB 35230 HB, Germany

For any legal inquiries or if you require further information, please contact us via email at hello@task.vc.

By using this repository, you acknowledge that you have read this section, agree to comply with its terms, and understand that the licensing of the code does not imply endorsement by Task Venture Capital GmbH of any derivative works.