Compare commits
59 Commits
Author | SHA1 | Date | |
---|---|---|---|
b4897d238f | |||
df69e46697 | |||
1493430d9f | |||
78afde963b | |||
a4a633e936 | |||
27fbe00cb0 | |||
f3cc468eda | |||
b303799b8d | |||
590ed5b180 | |||
ddcf52de19 | |||
c1b3af09ef | |||
a1e2cda010 | |||
d409d3a532 | |||
b4dae84835 | |||
20e27c93d9 | |||
117dc6013c | |||
c6bc433a73 | |||
299815838c | |||
e8318cbf2f | |||
ae60053cff | |||
dca3cf1f08 | |||
eaf379b119 | |||
0074938e57 | |||
eed1c75aba | |||
6de2f36964 | |||
76e777a3ac | |||
165cd3737e | |||
2f11d1ba0f | |||
33692ff417 | |||
f408337186 | |||
bd6d574226 | |||
d5c2d167ec | |||
2af6d5a974 | |||
fdba21d60d | |||
c17ebca309 | |||
a014dcf1ab | |||
44bee7bf6f | |||
7841917058 | |||
f5a674f98d | |||
80ece7c093 | |||
39250b3d47 | |||
ed78c0becf | |||
63c103fde5 | |||
88003bde0f | |||
276a0641e8 | |||
b166269cb4 | |||
3ca1b425bf | |||
ffc07da665 | |||
be2adaf259 | |||
7aa9637826 | |||
a3c4f87741 | |||
f005f57764 | |||
6e418cdbf8 | |||
87155900e1 | |||
d24b11f737 | |||
17ebd58951 | |||
32bd229f91 | |||
4e5ddf8411 | |||
701c7c1b8b |
66
.gitea/workflows/default_nottags.yaml
Normal file
66
.gitea/workflows/default_nottags.yaml
Normal file
@ -0,0 +1,66 @@
|
||||
name: Default (not tags)
|
||||
|
||||
on:
|
||||
push:
|
||||
tags-ignore:
|
||||
- '**'
|
||||
|
||||
env:
|
||||
IMAGE: registry.gitlab.com/hosttoday/ht-docker-node:npmci
|
||||
NPMCI_COMPUTED_REPOURL: https://${{gitea.repository_owner}}:${{secrets.GITEA_TOKEN}}@gitea.lossless.digital/${{gitea.repository}}.git
|
||||
NPMCI_TOKEN_NPM: ${{secrets.NPMCI_TOKEN_NPM}}
|
||||
NPMCI_TOKEN_NPM2: ${{secrets.NPMCI_TOKEN_NPM2}}
|
||||
NPMCI_GIT_GITHUBTOKEN: ${{secrets.NPMCI_GIT_GITHUBTOKEN}}
|
||||
NPMCI_URL_CLOUDLY: ${{secrets.NPMCI_URL_CLOUDLY}}
|
||||
|
||||
jobs:
|
||||
security:
|
||||
runs-on: ubuntu-latest
|
||||
continue-on-error: true
|
||||
container:
|
||||
image: ${{ env.IMAGE }}
|
||||
|
||||
steps:
|
||||
- uses: actions/checkout@v3
|
||||
|
||||
- name: Install pnpm and npmci
|
||||
run: |
|
||||
pnpm install -g pnpm
|
||||
pnpm install -g @shipzone/npmci
|
||||
|
||||
- name: Run npm prepare
|
||||
run: npmci npm prepare
|
||||
|
||||
- name: Audit production dependencies
|
||||
run: |
|
||||
npmci command npm config set registry https://registry.npmjs.org
|
||||
npmci command pnpm audit --audit-level=high --prod
|
||||
continue-on-error: true
|
||||
|
||||
- name: Audit development dependencies
|
||||
run: |
|
||||
npmci command npm config set registry https://registry.npmjs.org
|
||||
npmci command pnpm audit --audit-level=high --dev
|
||||
continue-on-error: true
|
||||
|
||||
test:
|
||||
if: ${{ always() }}
|
||||
needs: security
|
||||
runs-on: ubuntu-latest
|
||||
container:
|
||||
image: ${{ env.IMAGE }}
|
||||
|
||||
steps:
|
||||
- uses: actions/checkout@v3
|
||||
|
||||
- name: Test stable
|
||||
run: |
|
||||
npmci node install stable
|
||||
npmci npm install
|
||||
npmci npm test
|
||||
|
||||
- name: Test build
|
||||
run: |
|
||||
npmci node install stable
|
||||
npmci npm install
|
||||
npmci npm build
|
124
.gitea/workflows/default_tags.yaml
Normal file
124
.gitea/workflows/default_tags.yaml
Normal file
@ -0,0 +1,124 @@
|
||||
name: Default (tags)
|
||||
|
||||
on:
|
||||
push:
|
||||
tags:
|
||||
- '*'
|
||||
|
||||
env:
|
||||
IMAGE: registry.gitlab.com/hosttoday/ht-docker-node:npmci
|
||||
NPMCI_COMPUTED_REPOURL: https://${{gitea.repository_owner}}:${{secrets.GITEA_TOKEN}}@gitea.lossless.digital/${{gitea.repository}}.git
|
||||
NPMCI_TOKEN_NPM: ${{secrets.NPMCI_TOKEN_NPM}}
|
||||
NPMCI_TOKEN_NPM2: ${{secrets.NPMCI_TOKEN_NPM2}}
|
||||
NPMCI_GIT_GITHUBTOKEN: ${{secrets.NPMCI_GIT_GITHUBTOKEN}}
|
||||
NPMCI_URL_CLOUDLY: ${{secrets.NPMCI_URL_CLOUDLY}}
|
||||
|
||||
jobs:
|
||||
security:
|
||||
runs-on: ubuntu-latest
|
||||
continue-on-error: true
|
||||
container:
|
||||
image: ${{ env.IMAGE }}
|
||||
|
||||
steps:
|
||||
- uses: actions/checkout@v3
|
||||
|
||||
- name: Prepare
|
||||
run: |
|
||||
pnpm install -g pnpm
|
||||
pnpm install -g @shipzone/npmci
|
||||
npmci npm prepare
|
||||
|
||||
- name: Audit production dependencies
|
||||
run: |
|
||||
npmci command npm config set registry https://registry.npmjs.org
|
||||
npmci command pnpm audit --audit-level=high --prod
|
||||
continue-on-error: true
|
||||
|
||||
- name: Audit development dependencies
|
||||
run: |
|
||||
npmci command npm config set registry https://registry.npmjs.org
|
||||
npmci command pnpm audit --audit-level=high --dev
|
||||
continue-on-error: true
|
||||
|
||||
test:
|
||||
if: ${{ always() }}
|
||||
needs: security
|
||||
runs-on: ubuntu-latest
|
||||
container:
|
||||
image: ${{ env.IMAGE }}
|
||||
|
||||
steps:
|
||||
- uses: actions/checkout@v3
|
||||
|
||||
- name: Prepare
|
||||
run: |
|
||||
pnpm install -g pnpm
|
||||
pnpm install -g @shipzone/npmci
|
||||
npmci npm prepare
|
||||
|
||||
- name: Test stable
|
||||
run: |
|
||||
npmci node install stable
|
||||
npmci npm install
|
||||
npmci npm test
|
||||
|
||||
- name: Test build
|
||||
run: |
|
||||
npmci node install stable
|
||||
npmci npm install
|
||||
npmci npm build
|
||||
|
||||
release:
|
||||
needs: test
|
||||
if: github.event_name == 'push' && startsWith(github.ref, 'refs/tags/')
|
||||
runs-on: ubuntu-latest
|
||||
container:
|
||||
image: ${{ env.IMAGE }}
|
||||
|
||||
steps:
|
||||
- uses: actions/checkout@v3
|
||||
|
||||
- name: Prepare
|
||||
run: |
|
||||
pnpm install -g pnpm
|
||||
pnpm install -g @shipzone/npmci
|
||||
npmci npm prepare
|
||||
|
||||
- name: Release
|
||||
run: |
|
||||
npmci node install stable
|
||||
npmci npm publish
|
||||
|
||||
metadata:
|
||||
needs: test
|
||||
if: github.event_name == 'push' && startsWith(github.ref, 'refs/tags/')
|
||||
runs-on: ubuntu-latest
|
||||
container:
|
||||
image: ${{ env.IMAGE }}
|
||||
continue-on-error: true
|
||||
|
||||
steps:
|
||||
- uses: actions/checkout@v3
|
||||
|
||||
- name: Prepare
|
||||
run: |
|
||||
pnpm install -g pnpm
|
||||
pnpm install -g @shipzone/npmci
|
||||
npmci npm prepare
|
||||
|
||||
- name: Code quality
|
||||
run: |
|
||||
npmci command npm install -g typescript
|
||||
npmci npm install
|
||||
|
||||
- name: Trigger
|
||||
run: npmci trigger
|
||||
|
||||
- name: Build docs and upload artifacts
|
||||
run: |
|
||||
npmci node install stable
|
||||
npmci npm install
|
||||
pnpm install -g @git.zone/tsdoc
|
||||
npmci command tsdoc
|
||||
continue-on-error: true
|
4
.gitignore
vendored
4
.gitignore
vendored
@ -15,8 +15,6 @@ node_modules/
|
||||
|
||||
# builds
|
||||
dist/
|
||||
dist_web/
|
||||
dist_serve/
|
||||
dist_ts_web/
|
||||
dist_*/
|
||||
|
||||
# custom
|
117
.gitlab-ci.yml
117
.gitlab-ci.yml
@ -1,117 +0,0 @@
|
||||
# gitzone ci_default
|
||||
image: hosttoday/ht-docker-node:npmci
|
||||
|
||||
cache:
|
||||
paths:
|
||||
- .npmci_cache/
|
||||
key: "$CI_BUILD_STAGE"
|
||||
|
||||
stages:
|
||||
- security
|
||||
- test
|
||||
- release
|
||||
- metadata
|
||||
|
||||
# ====================
|
||||
# security stage
|
||||
# ====================
|
||||
mirror:
|
||||
stage: security
|
||||
script:
|
||||
- npmci git mirror
|
||||
tags:
|
||||
- docker
|
||||
- notpriv
|
||||
|
||||
snyk:
|
||||
stage: security
|
||||
script:
|
||||
- npmci npm prepare
|
||||
- npmci command npm install -g snyk
|
||||
- npmci command npm install --ignore-scripts
|
||||
- npmci command snyk test
|
||||
tags:
|
||||
- docker
|
||||
- notpriv
|
||||
|
||||
# ====================
|
||||
# test stage
|
||||
# ====================
|
||||
|
||||
testLTS:
|
||||
stage: test
|
||||
script:
|
||||
- npmci npm prepare
|
||||
- npmci node install lts
|
||||
- npmci npm install
|
||||
- npmci npm test
|
||||
coverage: /\d+.?\d+?\%\s*coverage/
|
||||
tags:
|
||||
- docker
|
||||
- notpriv
|
||||
|
||||
testSTABLE:
|
||||
stage: test
|
||||
script:
|
||||
- npmci npm prepare
|
||||
- npmci node install stable
|
||||
- npmci npm install
|
||||
- npmci npm test
|
||||
coverage: /\d+.?\d+?\%\s*coverage/
|
||||
tags:
|
||||
- docker
|
||||
- notpriv
|
||||
|
||||
release:
|
||||
stage: release
|
||||
script:
|
||||
- npmci node install stable
|
||||
- npmci npm publish
|
||||
only:
|
||||
- tags
|
||||
tags:
|
||||
- docker
|
||||
- notpriv
|
||||
|
||||
# ====================
|
||||
# metadata stage
|
||||
# ====================
|
||||
codequality:
|
||||
stage: metadata
|
||||
allow_failure: true
|
||||
script:
|
||||
- npmci command npm install -g tslint typescript
|
||||
- npmci npm install
|
||||
- npmci command "tslint -c tslint.json ./ts/**/*.ts"
|
||||
tags:
|
||||
- docker
|
||||
- priv
|
||||
|
||||
trigger:
|
||||
stage: metadata
|
||||
script:
|
||||
- npmci trigger
|
||||
only:
|
||||
- tags
|
||||
tags:
|
||||
- docker
|
||||
- notpriv
|
||||
|
||||
pages:
|
||||
image: hosttoday/ht-docker-node:npmci
|
||||
stage: metadata
|
||||
script:
|
||||
- npmci command npm install -g @gitzone/tsdoc
|
||||
- npmci npm prepare
|
||||
- npmci npm install
|
||||
- npmci command tsdoc
|
||||
tags:
|
||||
- docker
|
||||
- notpriv
|
||||
only:
|
||||
- tags
|
||||
artifacts:
|
||||
expire_in: 1 week
|
||||
paths:
|
||||
- public
|
||||
allow_failure: true
|
11
.vscode/launch.json
vendored
Normal file
11
.vscode/launch.json
vendored
Normal file
@ -0,0 +1,11 @@
|
||||
{
|
||||
"version": "0.2.0",
|
||||
"configurations": [
|
||||
{
|
||||
"command": "npm test",
|
||||
"name": "Run npm test",
|
||||
"request": "launch",
|
||||
"type": "node-terminal"
|
||||
}
|
||||
]
|
||||
}
|
26
.vscode/settings.json
vendored
Normal file
26
.vscode/settings.json
vendored
Normal file
@ -0,0 +1,26 @@
|
||||
{
|
||||
"json.schemas": [
|
||||
{
|
||||
"fileMatch": ["/npmextra.json"],
|
||||
"schema": {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"npmci": {
|
||||
"type": "object",
|
||||
"description": "settings for npmci"
|
||||
},
|
||||
"gitzone": {
|
||||
"type": "object",
|
||||
"description": "settings for gitzone",
|
||||
"properties": {
|
||||
"projectType": {
|
||||
"type": "string",
|
||||
"enum": ["website", "element", "service", "npm", "wcc"]
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
]
|
||||
}
|
28
README.md
28
README.md
@ -1,28 +0,0 @@
|
||||
# @pushrocks/smartenv
|
||||
store things about your environment and let them travel across modules
|
||||
|
||||
## Availabililty and Links
|
||||
* [npmjs.org (npm package)](https://www.npmjs.com/package/@pushrocks/smartenv)
|
||||
* [gitlab.com (source)](https://gitlab.com/pushrocks/smartenv)
|
||||
* [github.com (source mirror)](https://github.com/pushrocks/smartenv)
|
||||
* [docs (typedoc)](https://pushrocks.gitlab.io/smartenv/)
|
||||
|
||||
## Status for master
|
||||
[](https://gitlab.com/pushrocks/smartenv/commits/master)
|
||||
[](https://gitlab.com/pushrocks/smartenv/commits/master)
|
||||
[](https://www.npmjs.com/package/@pushrocks/smartenv)
|
||||
[](https://snyk.io/test/npm/@pushrocks/smartenv)
|
||||
[](https://nodejs.org/dist/latest-v10.x/docs/api/)
|
||||
[](https://nodejs.org/dist/latest-v10.x/docs/api/)
|
||||
[](https://prettier.io/)
|
||||
|
||||
## Usage
|
||||
|
||||
Use TypeScript for best in class instellisense.
|
||||
|
||||
For further information read the linked docs at the top of this readme.
|
||||
|
||||
> MIT licensed | **©** [Lossless GmbH](https://lossless.gmbh)
|
||||
| By using this npm module you agree to our [privacy policy](https://lossless.gmbH/privacy.html)
|
||||
|
||||
[](https://maintainedby.lossless.com)
|
@ -1,20 +1,32 @@
|
||||
{
|
||||
"npmts": {
|
||||
"coverageTreshold": 60
|
||||
},
|
||||
"npmci": {
|
||||
"npmGlobalTools": [
|
||||
"npmts"
|
||||
]
|
||||
"npmGlobalTools": [],
|
||||
"npmAccessLevel": "public"
|
||||
},
|
||||
"gitzone": {
|
||||
"projectType": "npm",
|
||||
"module": {
|
||||
"githost": "gitlab.com",
|
||||
"gitscope": "pushrocks",
|
||||
"githost": "code.foss.global",
|
||||
"gitscope": "push.rocks",
|
||||
"gitrepo": "smartenv",
|
||||
"shortDescription": "store things about your environment and let them travel across modules",
|
||||
"npmPackagename": "@pushrocks/smartenv",
|
||||
"license": "MIT"
|
||||
"description": "A module for storing and accessing environment details across different platforms.",
|
||||
"npmPackagename": "@push.rocks/smartenv",
|
||||
"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"
|
||||
}
|
||||
}
|
1772
package-lock.json
generated
1772
package-lock.json
generated
File diff suppressed because it is too large
Load Diff
66
package.json
66
package.json
@ -1,50 +1,64 @@
|
||||
{
|
||||
"name": "@pushrocks/smartenv",
|
||||
"version": "4.0.5",
|
||||
"description": "store things about your environment and let them travel across modules",
|
||||
"main": "dist/index.js",
|
||||
"typings": "dist/index.d.ts",
|
||||
"name": "@push.rocks/smartenv",
|
||||
"version": "5.0.12",
|
||||
"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",
|
||||
"scripts": {
|
||||
"test": "(tstest test/ --web)",
|
||||
"build": "(tsbuild --web)",
|
||||
"testbrowser": "(npm test) && (node testbrowser.js)"
|
||||
"build": "(tsbuild --web --allowimplicitany && tsbundle npm)",
|
||||
"testbrowser": "(npm test) && (node testbrowser.js)",
|
||||
"buildDocs": "tsdoc"
|
||||
},
|
||||
"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": {
|
||||
"@pushrocks/smartparam": "^1.0.4",
|
||||
"@pushrocks/smartpromise": "^3.0.2",
|
||||
"@types/node": "^12.0.8"
|
||||
"@push.rocks/smartpromise": "^4.0.2"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@gitzone/tsbuild": "^2.1.11",
|
||||
"@gitzone/tsrun": "^1.2.6",
|
||||
"@gitzone/tstest": "^1.0.24",
|
||||
"@pushrocks/tapbundle": "^3.0.9",
|
||||
"@types/npm": "^2.0.29",
|
||||
"tslint": "^5.17.0",
|
||||
"tslint-config-prettier": "^1.18.0"
|
||||
"@git.zone/tsbuild": "^2.1.72",
|
||||
"@git.zone/tsbundle": "^2.0.15",
|
||||
"@git.zone/tsrun": "^1.2.44",
|
||||
"@git.zone/tstest": "^1.0.86",
|
||||
"@push.rocks/tapbundle": "^5.0.8",
|
||||
"@types/node": "^20.11.24",
|
||||
"@types/npm": "^7.19.3"
|
||||
},
|
||||
"private": false,
|
||||
"files": [
|
||||
"ts/*",
|
||||
"ts_web/*",
|
||||
"dist/*",
|
||||
"dist_web/*",
|
||||
"assets/*",
|
||||
"ts/**/*",
|
||||
"ts_web/**/*",
|
||||
"dist/**/*",
|
||||
"dist_*/**/*",
|
||||
"dist_ts/**/*",
|
||||
"dist_ts_web/**/*",
|
||||
"assets/**/*",
|
||||
"cli.js",
|
||||
"npmextra.json",
|
||||
"readme.md"
|
||||
],
|
||||
"browserslist": [
|
||||
"last 1 chrome versions"
|
||||
]
|
||||
}
|
||||
}
|
5729
pnpm-lock.yaml
generated
Normal file
5729
pnpm-lock.yaml
generated
Normal file
File diff suppressed because it is too large
Load Diff
1
readme.hints.md
Normal file
1
readme.hints.md
Normal file
@ -0,0 +1 @@
|
||||
|
123
readme.md
Normal file
123
readme.md
Normal file
@ -0,0 +1,123 @@
|
||||
# @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:
|
||||
|
||||
```bash
|
||||
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:
|
||||
|
||||
```typescript
|
||||
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.
|
||||
|
||||
```typescript
|
||||
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.
|
||||
|
||||
```typescript
|
||||
// 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.
|
||||
|
||||
```typescript
|
||||
// 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.
|
||||
|
||||
```typescript
|
||||
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.
|
||||
|
||||
```typescript
|
||||
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.
|
||||
|
||||
```typescript
|
||||
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.
|
||||
|
||||
```typescript
|
||||
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.
|
||||
|
||||
## 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.
|
12
test/test.ts
12
test/test.ts
@ -1,5 +1,5 @@
|
||||
import { tap, expect } from '@pushrocks/tapbundle';
|
||||
import * as smartenv from '../ts/index';
|
||||
import { tap, expect } from '@push.rocks/tapbundle';
|
||||
import * as smartenv from '../ts/index.js';
|
||||
|
||||
let testEnv: smartenv.Smartenv;
|
||||
|
||||
@ -17,20 +17,20 @@ tap.test('should get os', async () => {
|
||||
const resultWindows = await testEnv.isWindowsAsync();
|
||||
const osModule = await import('os');
|
||||
if (resultMac) {
|
||||
expect(osModule.platform()).to.equal('darwin');
|
||||
expect(osModule.platform()).toEqual('darwin');
|
||||
console.log('platform is Mac!');
|
||||
} else if (resultLinux) {
|
||||
expect(osModule.platform()).to.equal('linux');
|
||||
expect(osModule.platform()).toEqual('linux');
|
||||
console.log('platform is Linux!');
|
||||
} else {
|
||||
expect(osModule.platform()).to.equal('win32');
|
||||
expect(osModule.platform()).toEqual('win32');
|
||||
console.log('platform is Windows!');
|
||||
}
|
||||
});
|
||||
|
||||
tap.test('should state wether we are in CI', async () => {
|
||||
if (process.env.CI) {
|
||||
expect(testEnv.isCI).to.be.true;
|
||||
expect(testEnv.isCI).toBeTrue();
|
||||
}
|
||||
});
|
||||
|
||||
|
8
ts/00_commitinfo_data.ts
Normal file
8
ts/00_commitinfo_data.ts
Normal file
@ -0,0 +1,8 @@
|
||||
/**
|
||||
* autocreated commitinfo by @pushrocks/commitinfo
|
||||
*/
|
||||
export const commitinfo = {
|
||||
name: '@push.rocks/smartenv',
|
||||
version: '5.0.12',
|
||||
description: 'store things about your environment and let them travel across modules'
|
||||
}
|
@ -1 +1 @@
|
||||
export * from './smartenv.classes.smartenv';
|
||||
export * from './smartenv.classes.smartenv.js';
|
||||
|
@ -1,5 +1,5 @@
|
||||
import * as plugins from './smartenv.plugins';
|
||||
import * as interfaces from './interfaces';
|
||||
import * as plugins from './smartenv.plugins.js';
|
||||
import * as interfaces from './interfaces/index.js';
|
||||
|
||||
// interfaces
|
||||
export interface IEnvObject {
|
||||
@ -11,19 +11,80 @@ export interface IEnvObject {
|
||||
* Smartenv class that makes it easy
|
||||
*/
|
||||
export class Smartenv {
|
||||
get runtimeEnv() {
|
||||
if (typeof window !== 'undefined') {
|
||||
return 'browser';
|
||||
} else if (typeof process !== 'undefined') {
|
||||
return 'node';
|
||||
public async getEnvAwareModule(optionsArg: {
|
||||
nodeModuleName: string;
|
||||
webUrlArg: string;
|
||||
getFunction: () => any;
|
||||
}) {
|
||||
if (this.isNode) {
|
||||
const moduleResult = await this.getSafeNodeModule(optionsArg.nodeModuleName);
|
||||
return moduleResult;
|
||||
} else if (this.isBrowser) {
|
||||
const moduleResult = await this.getSafeWebModule(
|
||||
optionsArg.webUrlArg,
|
||||
optionsArg.getFunction
|
||||
);
|
||||
return moduleResult;
|
||||
} else {
|
||||
console.error('platform for loading not supported by smartenv');
|
||||
}
|
||||
}
|
||||
|
||||
get isBrowser(): boolean {
|
||||
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}`);
|
||||
return;
|
||||
}
|
||||
// tslint:disable-next-line: function-constructor
|
||||
const returnValue: T = await (new Function(`return import('${moduleNameArg}')`)() as Promise<T>);
|
||||
if (runAfterFunc) {
|
||||
await runAfterFunc(returnValue);
|
||||
}
|
||||
return returnValue;
|
||||
}
|
||||
|
||||
public loadedScripts: string[] = [];
|
||||
public async getSafeWebModule(urlArg: string, getFunctionArg: () => any) {
|
||||
if (!this.isBrowser) {
|
||||
console.error('You tried to load a web module in a wrong context');
|
||||
return;
|
||||
}
|
||||
|
||||
if (this.loadedScripts.includes(urlArg)) {
|
||||
return getFunctionArg();
|
||||
} else {
|
||||
this.loadedScripts.push(urlArg);
|
||||
}
|
||||
|
||||
const done = plugins.smartpromise.defer();
|
||||
if (globalThis.importScripts) {
|
||||
globalThis.importScripts(urlArg);
|
||||
done.resolve();
|
||||
} else {
|
||||
const script = document.createElement('script');
|
||||
script.onload = () => {
|
||||
done.resolve();
|
||||
};
|
||||
script.src = urlArg;
|
||||
document.head.appendChild(script);
|
||||
}
|
||||
await done.promise;
|
||||
return getFunctionArg();
|
||||
}
|
||||
|
||||
public get runtimeEnv() {
|
||||
if (typeof process !== 'undefined') {
|
||||
return 'node';
|
||||
} else {
|
||||
return 'browser';
|
||||
}
|
||||
}
|
||||
|
||||
public get isBrowser(): boolean {
|
||||
return !this.isNode;
|
||||
}
|
||||
|
||||
get userAgent(): string {
|
||||
public get userAgent(): string {
|
||||
if (this.isBrowser) {
|
||||
// make sure we are in Browser
|
||||
return navigator.userAgent;
|
||||
@ -32,15 +93,15 @@ export class Smartenv {
|
||||
}
|
||||
}
|
||||
|
||||
get isNode(): boolean {
|
||||
public get isNode(): boolean {
|
||||
return this.runtimeEnv === 'node';
|
||||
}
|
||||
|
||||
get nodeVersion(): string {
|
||||
public get nodeVersion(): string {
|
||||
return process.version;
|
||||
}
|
||||
|
||||
get isCI(): boolean {
|
||||
public get isCI(): boolean {
|
||||
if (this.isNode) {
|
||||
if (process.env.CI) {
|
||||
return true;
|
||||
@ -52,51 +113,40 @@ export class Smartenv {
|
||||
}
|
||||
}
|
||||
|
||||
async isMacAsync(): Promise<boolean> {
|
||||
public async isMacAsync(): Promise<boolean> {
|
||||
if (this.isNode) {
|
||||
let os = await import('os');
|
||||
const os = await this.getSafeNodeModule('os');
|
||||
return os.platform() === 'darwin';
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
async isWindowsAsync(): Promise<boolean> {
|
||||
public async isWindowsAsync(): Promise<boolean> {
|
||||
if (this.isNode) {
|
||||
let os = await import('os');
|
||||
const os = await this.getSafeNodeModule('os');
|
||||
return os.platform() === 'win32';
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
async isLinuxAsync(): Promise<boolean> {
|
||||
public async isLinuxAsync(): Promise<boolean> {
|
||||
if (this.isNode) {
|
||||
let os = await import('os');
|
||||
const os = await this.getSafeNodeModule('os');
|
||||
return os.platform() === 'linux';
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* get environment variables that fit the description
|
||||
*/
|
||||
// get envVars (regexArg: RegExp) {
|
||||
// let EnvironmentArray = []
|
||||
// // TODO: plugins.smartparam.forEachMinimatch()
|
||||
// }
|
||||
|
||||
/**
|
||||
* prints the environment to console
|
||||
*/
|
||||
async printEnv() {
|
||||
public async printEnv() {
|
||||
if (this.isNode) {
|
||||
console.log('running on NODE');
|
||||
let smartenvVersion = require('../package.json').version;
|
||||
console.log(
|
||||
'node version is ' + this.nodeVersion + ' and smartenv version is ' + smartenvVersion
|
||||
);
|
||||
console.log('node version is ' + this.nodeVersion);
|
||||
} else {
|
||||
console.log('running on BROWSER');
|
||||
console.log('browser is ' + this.userAgent);
|
||||
|
@ -1,4 +1,3 @@
|
||||
import * as smartparam from '@pushrocks/smartparam';
|
||||
import * as smartq from '@pushrocks/smartpromise';
|
||||
import * as smartpromise from '@push.rocks/smartpromise';
|
||||
|
||||
export { smartparam, smartq };
|
||||
export { smartpromise };
|
||||
|
14
tsconfig.json
Normal file
14
tsconfig.json
Normal file
@ -0,0 +1,14 @@
|
||||
{
|
||||
"compilerOptions": {
|
||||
"experimentalDecorators": true,
|
||||
"useDefineForClassFields": false,
|
||||
"target": "ES2022",
|
||||
"module": "NodeNext",
|
||||
"moduleResolution": "NodeNext",
|
||||
"esModuleInterop": true,
|
||||
"verbatimModuleSyntax": true
|
||||
},
|
||||
"exclude": [
|
||||
"dist_*/**/*.d.ts"
|
||||
]
|
||||
}
|
17
tslint.json
17
tslint.json
@ -1,17 +0,0 @@
|
||||
{
|
||||
"extends": ["tslint:latest", "tslint-config-prettier"],
|
||||
"rules": {
|
||||
"semicolon": [true, "always"],
|
||||
"no-console": false,
|
||||
"ordered-imports": false,
|
||||
"object-literal-sort-keys": false,
|
||||
"member-ordering": {
|
||||
"options":{
|
||||
"order": [
|
||||
"static-method"
|
||||
]
|
||||
}
|
||||
}
|
||||
},
|
||||
"defaultSeverity": "warning"
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user