env-safe-check 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.github/workflows/npm-publish.yml +38 -0
- package/README.md +93 -0
- package/package.json +18 -0
- package/scripts/patch-extensions.cjs +32 -0
- package/scripts/patch-extensions.js +38 -0
- package/src/index.ts +1 -0
- package/src/validate.ts +20 -0
- package/tsconfig.json +46 -0
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
# This workflow will run tests using node and then publish a package to GitHub Packages when a release is created
|
|
2
|
+
# For more information see: https://docs.github.com/en/actions/publishing-packages/publishing-nodejs-packages
|
|
3
|
+
|
|
4
|
+
name: Node.js Package
|
|
5
|
+
|
|
6
|
+
on:
|
|
7
|
+
release:
|
|
8
|
+
types: [created]
|
|
9
|
+
workflow_dispatch: {}
|
|
10
|
+
|
|
11
|
+
jobs:
|
|
12
|
+
build:
|
|
13
|
+
# Only run automatically for the `main` branch or when a release is created.
|
|
14
|
+
if: github.ref == 'refs/heads/main' || github.event_name == 'release' || github.event_name == 'workflow_dispatch'
|
|
15
|
+
runs-on: ubuntu-latest
|
|
16
|
+
steps:
|
|
17
|
+
- uses: actions/checkout@v4
|
|
18
|
+
- uses: actions/setup-node@v4
|
|
19
|
+
with:
|
|
20
|
+
node-version: 20
|
|
21
|
+
- run: npm ci
|
|
22
|
+
- run: npm test
|
|
23
|
+
|
|
24
|
+
publish-npm:
|
|
25
|
+
needs: build
|
|
26
|
+
# Only publish when the run is for `main` or a release (keeps publishes scoped to main).
|
|
27
|
+
if: github.ref == 'refs/heads/main' || github.event_name == 'release' || github.event_name == 'workflow_dispatch'
|
|
28
|
+
runs-on: ubuntu-latest
|
|
29
|
+
steps:
|
|
30
|
+
- uses: actions/checkout@v4
|
|
31
|
+
- uses: actions/setup-node@v4
|
|
32
|
+
with:
|
|
33
|
+
node-version: 20
|
|
34
|
+
registry-url: https://registry.npmjs.org/
|
|
35
|
+
- run: npm ci
|
|
36
|
+
- run: npm publish
|
|
37
|
+
env:
|
|
38
|
+
NODE_AUTH_TOKEN: ${{secrets.NPM_TOKEN}}
|
package/README.md
ADDED
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
# env-safe-check
|
|
2
|
+
|
|
3
|
+
Reliable, minimal utility to validate required environment variables at runtime.
|
|
4
|
+
|
|
5
|
+
This tiny library helps Node and TypeScript projects fail fast with a clear
|
|
6
|
+
error message when required environment variables are missing or empty.
|
|
7
|
+
|
|
8
|
+
**Highlights**
|
|
9
|
+
|
|
10
|
+
- Zero runtime dependencies
|
|
11
|
+
- Tiny API: a single `validateEnv()` function
|
|
12
|
+
- Works in TypeScript and JavaScript projects (ESM)
|
|
13
|
+
- Safe defaults for CI and production (exits with non-zero code on missing vars)
|
|
14
|
+
|
|
15
|
+
## Installation
|
|
16
|
+
|
|
17
|
+
Install from npm (devs using this repo can also build locally):
|
|
18
|
+
|
|
19
|
+
```bash
|
|
20
|
+
npm install env-safe-check
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
For local development (this repository):
|
|
24
|
+
|
|
25
|
+
```bash
|
|
26
|
+
npm install
|
|
27
|
+
npm run build
|
|
28
|
+
```
|
|
29
|
+
|
|
30
|
+
## Quick usage
|
|
31
|
+
|
|
32
|
+
TypeScript (recommended):
|
|
33
|
+
|
|
34
|
+
```ts
|
|
35
|
+
import { validateEnv } from 'env-safe-check';
|
|
36
|
+
|
|
37
|
+
// throw and exit if any required env var is missing or empty
|
|
38
|
+
validateEnv(['DATABASE_URL', 'API_KEY']);
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
JavaScript (ESM):
|
|
42
|
+
|
|
43
|
+
```js
|
|
44
|
+
import { validateEnv } from 'env-safe-check';
|
|
45
|
+
|
|
46
|
+
validateEnv(['DATABASE_URL', 'API_KEY']);
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
When any required variable is missing the library prints a friendly list
|
|
50
|
+
and exits the process with code `1`.
|
|
51
|
+
|
|
52
|
+
## API
|
|
53
|
+
|
|
54
|
+
- `validateEnv(required: string[]): void`
|
|
55
|
+
- `required` — array of environment variable names to verify.
|
|
56
|
+
- Throws/terminates the process (with a console error) when any are
|
|
57
|
+
missing or empty.
|
|
58
|
+
|
|
59
|
+
## Project specifics (for contributors / package authors)
|
|
60
|
+
|
|
61
|
+
- Source files are TypeScript in `src/`.
|
|
62
|
+
- Build emits ESM JavaScript into `dist/`.
|
|
63
|
+
- Source imports are written without `.js` extensions for ergonomics in
|
|
64
|
+
TypeScript; the build process rewrites emitted imports to include `.js`
|
|
65
|
+
so the output is runnable under Node ESM (`node >= 12` with ESM support).
|
|
66
|
+
|
|
67
|
+
Commands:
|
|
68
|
+
|
|
69
|
+
```bash
|
|
70
|
+
# Build and patch emitted imports
|
|
71
|
+
npm run build
|
|
72
|
+
|
|
73
|
+
# Run TypeScript type-check only
|
|
74
|
+
npx tsc --noEmit
|
|
75
|
+
```
|
|
76
|
+
|
|
77
|
+
## Publishing
|
|
78
|
+
|
|
79
|
+
1. Bump the package version in `package.json`.
|
|
80
|
+
2. Run `npm run build` to produce `dist/`.
|
|
81
|
+
3. Verify the `main`/`exports` fields point to built files (if applicable).
|
|
82
|
+
4. `npm publish --access public`
|
|
83
|
+
|
|
84
|
+
## Contributing
|
|
85
|
+
|
|
86
|
+
Contributions are welcome. Open issues for bugs or small feature requests.
|
|
87
|
+
|
|
88
|
+
Please run the tests (if added) and ensure linting/type-checks pass before
|
|
89
|
+
submitting a PR.
|
|
90
|
+
|
|
91
|
+
## License
|
|
92
|
+
|
|
93
|
+
MIT
|
package/package.json
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "env-safe-check",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"type": "module",
|
|
5
|
+
"main": "index.js",
|
|
6
|
+
"scripts": {
|
|
7
|
+
"test": "echo \"hello world\"",
|
|
8
|
+
"build": "tsc && node ./scripts/patch-extensions.cjs"
|
|
9
|
+
},
|
|
10
|
+
"keywords": [],
|
|
11
|
+
"author": "",
|
|
12
|
+
"license": "ISC",
|
|
13
|
+
"description": "",
|
|
14
|
+
"devDependencies": {
|
|
15
|
+
"typescript": "^5.9.3",
|
|
16
|
+
"@types/node": "^20.6.5"
|
|
17
|
+
}
|
|
18
|
+
}
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
const fs = require('fs');
|
|
2
|
+
const path = require('path');
|
|
3
|
+
|
|
4
|
+
function walk(dir) {
|
|
5
|
+
const entries = fs.readdirSync(dir, { withFileTypes: true });
|
|
6
|
+
for (const entry of entries) {
|
|
7
|
+
const full = path.join(dir, entry.name);
|
|
8
|
+
if (entry.isDirectory()) walk(full);
|
|
9
|
+
else if (entry.isFile() && full.endsWith('.js')) patchFile(full);
|
|
10
|
+
}
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
function patchFile(file) {
|
|
14
|
+
let src = fs.readFileSync(file, 'utf8');
|
|
15
|
+
src = src.replace(/(from\s+|import\()(["'])(\.\/[^"'\)]+?)\2/g, (m, p1, q, p2) => {
|
|
16
|
+
if (/\.[a-zA-Z0-9]+$/.test(p2)) return m;
|
|
17
|
+
return `${p1}${q}${p2}.js${q}`;
|
|
18
|
+
});
|
|
19
|
+
src = src.replace(/(export\s+[^;]*?from\s+)(["'])(\.\/[^"']+?)\2/g, (m, p1, q, p2) => {
|
|
20
|
+
if (/\.[a-zA-Z0-9]+$/.test(p2)) return m;
|
|
21
|
+
return `${p1}${q}${p2}.js${q}`;
|
|
22
|
+
});
|
|
23
|
+
fs.writeFileSync(file, src, 'utf8');
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
const dist = path.join(__dirname, '..', 'dist');
|
|
27
|
+
if (!fs.existsSync(dist)) {
|
|
28
|
+
console.error('dist directory not found; run tsc first');
|
|
29
|
+
process.exit(1);
|
|
30
|
+
}
|
|
31
|
+
walk(dist);
|
|
32
|
+
console.log('Patched imports to include .js extensions in', dist);
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
const fs = require('fs');
|
|
2
|
+
const path = require('path');
|
|
3
|
+
|
|
4
|
+
function walk(dir) {
|
|
5
|
+
const entries = fs.readdirSync(dir, { withFileTypes: true });
|
|
6
|
+
for (const entry of entries) {
|
|
7
|
+
const full = path.join(dir, entry.name);
|
|
8
|
+
if (entry.isDirectory()) walk(full);
|
|
9
|
+
else if (entry.isFile() && full.endsWith('.js')) patchFile(full);
|
|
10
|
+
}
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
function patchFile(file) {
|
|
14
|
+
let src = fs.readFileSync(file, 'utf8');
|
|
15
|
+
// Replace import/export specifiers that are relative and have no extension
|
|
16
|
+
// Examples: import {x} from "./foo" -> ./foo.js
|
|
17
|
+
// export * from './bar' -> ./bar.js
|
|
18
|
+
src = src.replace(/(from\s+|import\()(["'])(\.\/[^"'\)]+?)\2/g, (m, p1, q, p2) => {
|
|
19
|
+
// p2 is like ./module or ../module/sub
|
|
20
|
+
// If it already ends with an extension, leave it
|
|
21
|
+
if (/\.[a-zA-Z0-9]+$/.test(p2)) return m;
|
|
22
|
+
return `${p1}${q}${p2}.js${q}`;
|
|
23
|
+
});
|
|
24
|
+
// also handle export ... from '...'
|
|
25
|
+
src = src.replace(/(export\s+[^;]*?from\s+)(["'])(\.\/[^"']+?)\2/g, (m, p1, q, p2) => {
|
|
26
|
+
if (/\.[a-zA-Z0-9]+$/.test(p2)) return m;
|
|
27
|
+
return `${p1}${q}${p2}.js${q}`;
|
|
28
|
+
});
|
|
29
|
+
fs.writeFileSync(file, src, 'utf8');
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
const dist = path.join(__dirname, '..', 'dist');
|
|
33
|
+
if (!fs.existsSync(dist)) {
|
|
34
|
+
console.error('dist directory not found; run tsc first');
|
|
35
|
+
process.exit(1);
|
|
36
|
+
}
|
|
37
|
+
walk(dist);
|
|
38
|
+
console.log('Patched imports to include .js extensions in', dist);
|
package/src/index.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { validateEnv } from "./validate";
|
package/src/validate.ts
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
export function validateEnv(required: string[]): void {
|
|
2
|
+
const missing = required.filter((key) => {
|
|
3
|
+
const value = process.env[key];
|
|
4
|
+
return value === undefined || value.trim() === "";
|
|
5
|
+
});
|
|
6
|
+
|
|
7
|
+
if (missing.length > 0) {
|
|
8
|
+
console.error("❌ Missing required environment variables:\n");
|
|
9
|
+
|
|
10
|
+
missing.forEach((key) => {
|
|
11
|
+
console.error(`- ${key}`);
|
|
12
|
+
});
|
|
13
|
+
|
|
14
|
+
console.error(
|
|
15
|
+
"\nPlease define them in your .env file or environment config."
|
|
16
|
+
);
|
|
17
|
+
|
|
18
|
+
process.exit(1);
|
|
19
|
+
}
|
|
20
|
+
}
|
package/tsconfig.json
ADDED
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
{
|
|
2
|
+
// Visit https://aka.ms/tsconfig to read more about this file
|
|
3
|
+
"compilerOptions": {
|
|
4
|
+
// File Layout
|
|
5
|
+
// "rootDir": "./src",
|
|
6
|
+
// "outDir": "./dist",
|
|
7
|
+
|
|
8
|
+
// Environment Settings
|
|
9
|
+
// See also https://aka.ms/tsconfig/module
|
|
10
|
+
"module": "esnext",
|
|
11
|
+
"moduleResolution": "node",
|
|
12
|
+
"target": "esnext",
|
|
13
|
+
"outDir": "./dist",
|
|
14
|
+
"types": ["node"],
|
|
15
|
+
// For nodejs:
|
|
16
|
+
// "lib": ["esnext"],
|
|
17
|
+
// "types": ["node"],
|
|
18
|
+
// and npm install -D @types/node
|
|
19
|
+
|
|
20
|
+
// Other Outputs
|
|
21
|
+
"sourceMap": true,
|
|
22
|
+
"declaration": true,
|
|
23
|
+
"declarationMap": true,
|
|
24
|
+
|
|
25
|
+
// Stricter Typechecking Options
|
|
26
|
+
"noUncheckedIndexedAccess": true,
|
|
27
|
+
"exactOptionalPropertyTypes": true,
|
|
28
|
+
|
|
29
|
+
// Style Options
|
|
30
|
+
// "noImplicitReturns": true,
|
|
31
|
+
// "noImplicitOverride": true,
|
|
32
|
+
// "noUnusedLocals": true,
|
|
33
|
+
// "noUnusedParameters": true,
|
|
34
|
+
// "noFallthroughCasesInSwitch": true,
|
|
35
|
+
// "noPropertyAccessFromIndexSignature": true,
|
|
36
|
+
|
|
37
|
+
// Recommended Options
|
|
38
|
+
"strict": true,
|
|
39
|
+
"jsx": "react-jsx",
|
|
40
|
+
"verbatimModuleSyntax": true,
|
|
41
|
+
"isolatedModules": true,
|
|
42
|
+
"noUncheckedSideEffectImports": true,
|
|
43
|
+
"moduleDetection": "force",
|
|
44
|
+
"skipLibCheck": true,
|
|
45
|
+
}
|
|
46
|
+
}
|