Compare commits
14 Commits
Author | SHA1 | Date | |
---|---|---|---|
|
ce338e27ea | ||
b4897d238f | |||
df69e46697 | |||
1493430d9f | |||
78afde963b | |||
a4a633e936 | |||
27fbe00cb0 | |||
f3cc468eda | |||
b303799b8d | |||
590ed5b180 | |||
ddcf52de19 | |||
c1b3af09ef | |||
a1e2cda010 | |||
d409d3a532 |
8
changelog.md
Normal file
8
changelog.md
Normal file
@@ -0,0 +1,8 @@
|
||||
# Changelog
|
||||
|
||||
## [5.0.13] - 2025-07-28
|
||||
|
||||
### Fixed
|
||||
- Updated readme.md with comprehensive documentation including API reference, core features, and usage examples
|
||||
- Added readme.hints.md with architecture and implementation details for future reference
|
||||
- Improved documentation structure and clarity
|
@@ -6,12 +6,27 @@
|
||||
"gitzone": {
|
||||
"projectType": "npm",
|
||||
"module": {
|
||||
"githost": "gitlab.com",
|
||||
"githost": "code.foss.global",
|
||||
"gitscope": "push.rocks",
|
||||
"gitrepo": "smartenv",
|
||||
"description": "store things about your environment and let them travel across modules",
|
||||
"description": "A module for storing and accessing environment details across different platforms.",
|
||||
"npmPackagename": "@push.rocks/smartenv",
|
||||
"license": "MIT"
|
||||
"license": "MIT",
|
||||
"keywords": [
|
||||
"environment detection",
|
||||
"cross-platform",
|
||||
"node.js",
|
||||
"browser",
|
||||
"module loading",
|
||||
"CI detection",
|
||||
"OS detection",
|
||||
"runtime environment",
|
||||
"typescript",
|
||||
"async"
|
||||
]
|
||||
}
|
||||
},
|
||||
"tsdoc": {
|
||||
"legal": "\n## License and Legal Information\n\nThis repository contains open-source code that is licensed under the MIT License. A copy of the MIT License can be found in the [license](license) file within this repository. \n\n**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.\n\n### Trademarks\n\nThis 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.\n\n### Company Information\n\nTask Venture Capital GmbH \nRegistered at District court Bremen HRB 35230 HB, Germany\n\nFor any legal inquiries or if you require further information, please contact us via email at hello@task.vc.\n\nBy 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.\n"
|
||||
}
|
||||
}
|
32
package.json
32
package.json
@@ -1,7 +1,7 @@
|
||||
{
|
||||
"name": "@push.rocks/smartenv",
|
||||
"version": "5.0.8",
|
||||
"description": "store things about your environment and let them travel across modules",
|
||||
"version": "5.0.13",
|
||||
"description": "A module for storing and accessing environment details across different platforms.",
|
||||
"main": "dist_ts/index.js",
|
||||
"typings": "dist_ts/index.d.ts",
|
||||
"type": "module",
|
||||
@@ -13,28 +13,35 @@
|
||||
},
|
||||
"repository": {
|
||||
"type": "git",
|
||||
"url": "https://gitlab.com/pushrocks/smartenv.git"
|
||||
"url": "https://code.foss.global/push.rocks/smartenv.git"
|
||||
},
|
||||
"keywords": [
|
||||
"environment"
|
||||
"environment detection",
|
||||
"cross-platform",
|
||||
"node.js",
|
||||
"browser",
|
||||
"module loading",
|
||||
"CI detection",
|
||||
"OS detection",
|
||||
"runtime environment",
|
||||
"typescript",
|
||||
"async"
|
||||
],
|
||||
"author": "Lossless GmbH <hello@lossless.support> (https://lossless.com)",
|
||||
"license": "MIT",
|
||||
"bugs": {
|
||||
"url": "https://gitlab.com/pushrocks/smartenv/issues"
|
||||
},
|
||||
"homepage": "https://gitlab.com/pushrocks/smartenv",
|
||||
"homepage": "https://code.foss.global/push.rocks/smartenv",
|
||||
"dependencies": {
|
||||
"@push.rocks/smartpromise": "^4.0.2"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@git.zone/tsbuild": "^2.1.66",
|
||||
"@git.zone/tsbundle": "^2.0.8",
|
||||
"@git.zone/tsbuild": "^2.1.72",
|
||||
"@git.zone/tsbundle": "^2.0.15",
|
||||
"@git.zone/tsrun": "^1.2.44",
|
||||
"@git.zone/tstest": "^1.0.77",
|
||||
"@push.rocks/tapbundle": "^5.0.8",
|
||||
"@types/node": "^20.8.2",
|
||||
"@types/npm": "^7.19.1"
|
||||
"@git.zone/tstest": "^2.3.2",
|
||||
"@types/node": "^22.0.0"
|
||||
},
|
||||
"private": false,
|
||||
"files": [
|
||||
@@ -51,5 +58,6 @@
|
||||
],
|
||||
"browserslist": [
|
||||
"last 1 chrome versions"
|
||||
]
|
||||
],
|
||||
"packageManager": "pnpm@10.11.0+sha512.6540583f41cc5f628eb3d9773ecee802f4f9ef9923cc45b69890fb47991d4b092964694ec3a4f738a420c918a333062c8b925d312f42e4f0c263eb603551f977"
|
||||
}
|
||||
|
11886
pnpm-lock.yaml
generated
11886
pnpm-lock.yaml
generated
File diff suppressed because it is too large
Load Diff
23
readme.hints.md
Normal file
23
readme.hints.md
Normal file
@@ -0,0 +1,23 @@
|
||||
# SmartEnv Hints
|
||||
|
||||
## Architecture Overview
|
||||
- Single main class `Smartenv` that provides all functionality
|
||||
- Uses dependency injection pattern with plugins imported from smartenv.plugins.ts
|
||||
- Utilizes @push.rocks/smartpromise for async operations
|
||||
|
||||
## Key Implementation Details
|
||||
- Runtime detection based on checking if `process` is defined
|
||||
- Dynamic module loading using Function constructor for Node.js modules
|
||||
- Script tag injection for browser module loading with duplicate prevention
|
||||
- OS detection uses the native Node.js 'os' module loaded dynamically
|
||||
|
||||
## Testing Approach
|
||||
- Tests use @git.zone/tstest with tap-based testing
|
||||
- Test file demonstrates OS detection and CI environment detection
|
||||
- Tests can run in both Node.js and browser environments
|
||||
|
||||
## Important Notes
|
||||
- The `getSafeNodeModule` uses dynamic import via Function constructor to avoid bundler issues
|
||||
- Browser module loading tracks loaded scripts to prevent duplicate loads
|
||||
- All OS detection methods are async and return false in browser environments
|
||||
- The package is isomorphic and designed for use in both Node.js and browser contexts
|
271
readme.md
271
readme.md
@@ -1,37 +1,256 @@
|
||||
# @push.rocks/smartenv
|
||||
store things about your environment and let them travel across modules
|
||||
|
||||
## Availabililty and Links
|
||||
* [npmjs.org (npm package)](https://www.npmjs.com/package/@push.rocks/smartenv)
|
||||
* [gitlab.com (source)](https://gitlab.com/push.rocks/smartenv)
|
||||
* [github.com (source mirror)](https://github.com/push.rocks/smartenv)
|
||||
* [docs (typedoc)](https://push.rocks.gitlab.io/smartenv/)
|
||||
A cross-platform TypeScript library for detecting and managing runtime environments. It provides comprehensive environment detection capabilities and safe module loading for both Node.js and browser contexts.
|
||||
|
||||
## Status for master
|
||||
## Install
|
||||
|
||||
Status Category | Status Badge
|
||||
-- | --
|
||||
GitLab Pipelines | [](https://lossless.cloud)
|
||||
GitLab Pipline Test Coverage | [](https://lossless.cloud)
|
||||
npm | [](https://lossless.cloud)
|
||||
Snyk | [](https://lossless.cloud)
|
||||
TypeScript Support | [](https://lossless.cloud)
|
||||
node Support | [](https://nodejs.org/dist/latest-v10.x/docs/api/)
|
||||
Code Style | [](https://lossless.cloud)
|
||||
PackagePhobia (total standalone install weight) | [](https://lossless.cloud)
|
||||
PackagePhobia (package size on registry) | [](https://lossless.cloud)
|
||||
BundlePhobia (total size when bundled) | [](https://lossless.cloud)
|
||||
To install `@push.rocks/smartenv`, you need Node.js and pnpm installed. Then, run the following command:
|
||||
|
||||
```bash
|
||||
pnpm install @push.rocks/smartenv --save
|
||||
```
|
||||
|
||||
## Usage
|
||||
|
||||
Use TypeScript for best in class instellisense.
|
||||
`@push.rocks/smartenv` is a powerful utility for managing and accessing environment-specific information within your application. It enables your code to adapt seamlessly to different environments such as development, testing, and production.
|
||||
|
||||
## Contribution
|
||||
### Getting Started
|
||||
|
||||
We are always happy for code contributions. If you are not the code contributing type that is ok. Still, maintaining Open Source repositories takes considerable time and thought. If you like the quality of what we do and our modules are useful to you we would appreciate a little monthly contribution: You can [contribute one time](https://lossless.link/contribute-onetime) or [contribute monthly](https://lossless.link/contribute). :)
|
||||
First, import the `Smartenv` class from the package:
|
||||
|
||||
For further information read the linked docs at the top of this readme.
|
||||
```typescript
|
||||
import { Smartenv } from '@push.rocks/smartenv';
|
||||
```
|
||||
|
||||
## Legal
|
||||
> MIT licensed | **©** [Task Venture Capital GmbH](https://task.vc)
|
||||
| By using this npm module you agree to our [privacy policy](https://lossless.gmbH/privacy)
|
||||
### Initializing Smartenv
|
||||
|
||||
Create an instance of `Smartenv` to access all environment detection and module loading features:
|
||||
|
||||
```typescript
|
||||
const smartEnv = new Smartenv();
|
||||
```
|
||||
|
||||
## Core Features
|
||||
|
||||
- **Runtime Environment Detection**: Instantly detect whether your code is running in Node.js or browser
|
||||
- **Operating System Detection**: Identify Mac, Windows, or Linux platforms in Node.js environments
|
||||
- **CI Environment Detection**: Detect if running in a continuous integration environment
|
||||
- **Safe Module Loading**: Load modules conditionally based on the runtime environment
|
||||
- **Browser Information**: Access user agent information in browser contexts
|
||||
- **Node.js Version**: Get the current Node.js version when running in Node.js
|
||||
|
||||
## API Reference
|
||||
|
||||
### Environment Detection
|
||||
|
||||
#### `isNode: boolean`
|
||||
Returns `true` if running in a Node.js environment.
|
||||
|
||||
```typescript
|
||||
if (smartEnv.isNode) {
|
||||
console.log('Running in Node.js');
|
||||
}
|
||||
```
|
||||
|
||||
#### `isBrowser: boolean`
|
||||
Returns `true` if running in a browser environment.
|
||||
|
||||
```typescript
|
||||
if (smartEnv.isBrowser) {
|
||||
console.log('Running in browser');
|
||||
}
|
||||
```
|
||||
|
||||
#### `runtimeEnv: string`
|
||||
Returns the runtime environment as a string ('node' or 'browser').
|
||||
|
||||
```typescript
|
||||
console.log(`Runtime: ${smartEnv.runtimeEnv}`);
|
||||
```
|
||||
|
||||
#### `isCI: boolean`
|
||||
Returns `true` if running in a CI environment (checks for CI environment variable).
|
||||
|
||||
```typescript
|
||||
if (smartEnv.isCI) {
|
||||
console.log('Running in CI environment');
|
||||
}
|
||||
```
|
||||
|
||||
### Platform Detection (Node.js only)
|
||||
|
||||
#### `isMacAsync(): Promise<boolean>`
|
||||
Asynchronously checks if running on macOS.
|
||||
|
||||
```typescript
|
||||
const isMac = await smartEnv.isMacAsync();
|
||||
if (isMac) {
|
||||
console.log('Running on macOS');
|
||||
}
|
||||
```
|
||||
|
||||
#### `isWindowsAsync(): Promise<boolean>`
|
||||
Asynchronously checks if running on Windows.
|
||||
|
||||
```typescript
|
||||
const isWindows = await smartEnv.isWindowsAsync();
|
||||
if (isWindows) {
|
||||
console.log('Running on Windows');
|
||||
}
|
||||
```
|
||||
|
||||
#### `isLinuxAsync(): Promise<boolean>`
|
||||
Asynchronously checks if running on Linux.
|
||||
|
||||
```typescript
|
||||
const isLinux = await smartEnv.isLinuxAsync();
|
||||
if (isLinux) {
|
||||
console.log('Running on Linux');
|
||||
}
|
||||
```
|
||||
|
||||
### Runtime Information
|
||||
|
||||
#### `nodeVersion: string`
|
||||
Returns the Node.js version (only available in Node.js environment).
|
||||
|
||||
```typescript
|
||||
if (smartEnv.isNode) {
|
||||
console.log(`Node.js version: ${smartEnv.nodeVersion}`);
|
||||
}
|
||||
```
|
||||
|
||||
#### `userAgent: string`
|
||||
Returns the browser user agent string (only available in browser environment).
|
||||
|
||||
```typescript
|
||||
if (smartEnv.isBrowser) {
|
||||
console.log(`Browser: ${smartEnv.userAgent}`);
|
||||
}
|
||||
```
|
||||
|
||||
### Module Loading
|
||||
|
||||
#### `getEnvAwareModule(options)`
|
||||
Loads a module appropriate for the current environment. In Node.js, it uses dynamic import; in browsers, it loads a script via URL.
|
||||
|
||||
```typescript
|
||||
const module = await smartEnv.getEnvAwareModule({
|
||||
nodeModuleName: 'node-fetch',
|
||||
webUrlArg: 'https://unpkg.com/whatwg-fetch@3.6.2/dist/fetch.umd.js',
|
||||
getFunction: () => window.fetch
|
||||
});
|
||||
```
|
||||
|
||||
#### `getSafeNodeModule<T>(moduleName, runAfterFunc?)`
|
||||
Safely loads a Node.js module with error handling. Only works in Node.js environment.
|
||||
|
||||
```typescript
|
||||
const fs = await smartEnv.getSafeNodeModule('fs');
|
||||
if (fs) {
|
||||
// Use fs module
|
||||
}
|
||||
|
||||
// With post-load function
|
||||
const express = await smartEnv.getSafeNodeModule('express', async (mod) => {
|
||||
console.log('Express loaded successfully');
|
||||
});
|
||||
```
|
||||
|
||||
#### `getSafeWebModule(url, getFunction)`
|
||||
Safely loads a web module via script tag. Only works in browser environment. Prevents duplicate loading of the same script.
|
||||
|
||||
```typescript
|
||||
const jQuery = await smartEnv.getSafeWebModule(
|
||||
'https://code.jquery.com/jquery-3.6.0.min.js',
|
||||
() => window.jQuery
|
||||
);
|
||||
```
|
||||
|
||||
### Debugging
|
||||
|
||||
#### `printEnv()`
|
||||
Prints the current environment information to the console for debugging purposes.
|
||||
|
||||
```typescript
|
||||
await smartEnv.printEnv();
|
||||
// Output in Node.js: "running on NODE" + version
|
||||
// Output in browser: "running on BROWSER" + user agent
|
||||
```
|
||||
|
||||
## Common Use Cases
|
||||
|
||||
### 1. Isomorphic Module Loading
|
||||
|
||||
```typescript
|
||||
// Define environment-specific implementations
|
||||
const cryptoModule = await smartEnv.getEnvAwareModule({
|
||||
nodeModuleName: 'crypto',
|
||||
webUrlArg: 'https://unpkg.com/crypto-js@4.1.1/crypto-js.js',
|
||||
getFunction: () => window.CryptoJS
|
||||
});
|
||||
```
|
||||
|
||||
### 2. Platform-Specific Operations
|
||||
|
||||
```typescript
|
||||
if (smartEnv.isNode) {
|
||||
const os = await smartEnv.getSafeNodeModule('os');
|
||||
console.log(`Home directory: ${os.homedir()}`);
|
||||
} else {
|
||||
console.log('Browser environment - no filesystem access');
|
||||
}
|
||||
```
|
||||
|
||||
### 3. CI/CD Pipeline Detection
|
||||
|
||||
```typescript
|
||||
if (smartEnv.isCI) {
|
||||
// Run extended tests or different build configuration
|
||||
console.log('Running in CI - enabling extended test suite');
|
||||
} else {
|
||||
console.log('Local development environment');
|
||||
}
|
||||
```
|
||||
|
||||
### 4. Dynamic Script Loading in Browser
|
||||
|
||||
```typescript
|
||||
if (smartEnv.isBrowser) {
|
||||
// Load analytics only in browser
|
||||
await smartEnv.getSafeWebModule(
|
||||
'https://www.google-analytics.com/analytics.js',
|
||||
() => window.ga
|
||||
);
|
||||
}
|
||||
```
|
||||
|
||||
## TypeScript Support
|
||||
|
||||
The package is written in TypeScript and provides full type definitions. The main type export is:
|
||||
|
||||
```typescript
|
||||
export interface IEnvObject {
|
||||
name: string;
|
||||
value: string;
|
||||
}
|
||||
```
|
||||
|
||||
## License and Legal Information
|
||||
|
||||
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](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.
|
||||
|
@@ -1,4 +1,4 @@
|
||||
import { tap, expect } from '@push.rocks/tapbundle';
|
||||
import { tap, expect } from '@git.zone/tstest/tapbundle';
|
||||
import * as smartenv from '../ts/index.js';
|
||||
|
||||
let testEnv: smartenv.Smartenv;
|
||||
|
@@ -3,6 +3,6 @@
|
||||
*/
|
||||
export const commitinfo = {
|
||||
name: '@push.rocks/smartenv',
|
||||
version: '5.0.8',
|
||||
version: '5.0.12',
|
||||
description: 'store things about your environment and let them travel across modules'
|
||||
}
|
||||
|
@@ -30,13 +30,17 @@ export class Smartenv {
|
||||
}
|
||||
}
|
||||
|
||||
public async getSafeNodeModule<T = any>(moduleNameArg: string): Promise<T> {
|
||||
public async getSafeNodeModule<T = any>(moduleNameArg: string, runAfterFunc?: (moduleArg: T) => Promise<any>): Promise<T> {
|
||||
if (!this.isNode) {
|
||||
console.error(`You tried to load a node module in a wrong context: ${moduleNameArg}`);
|
||||
console.error(`You tried to load a node module in a wrong context: ${moduleNameArg}. This does not throw.`);
|
||||
return;
|
||||
}
|
||||
// tslint:disable-next-line: function-constructor
|
||||
return new Function(`return import('${moduleNameArg}')`)() as Promise<T>;
|
||||
const returnValue: T = await (new Function(`return import('${moduleNameArg}')`)() as Promise<T>);
|
||||
if (runAfterFunc) {
|
||||
await runAfterFunc(returnValue);
|
||||
}
|
||||
return returnValue;
|
||||
}
|
||||
|
||||
public loadedScripts: string[] = [];
|
||||
|
Reference in New Issue
Block a user