@quark-fw/plugin-env 0.1.26 → 0.1.27
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/README.md +31 -0
- package/package.json +4 -4
- package/src/helpers.ts +4 -1
package/README.md
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
# @quark-fw/plugin-env
|
|
2
|
+
|
|
3
|
+
`.env` plugin for Quark apps: every environment variable is an atom (`quark.env.get("KEY")`), the `.env` file in the working directory is loaded on init and re-read when it changes (chokidar), and every change emits `env:changed`.
|
|
4
|
+
|
|
5
|
+
## Install
|
|
6
|
+
|
|
7
|
+
```sh
|
|
8
|
+
npm i @quark-fw/plugin-env
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
Peer: `@quark-fw/core`.
|
|
12
|
+
|
|
13
|
+
## Usage
|
|
14
|
+
|
|
15
|
+
```ts
|
|
16
|
+
import "@quark-fw/plugin-env"; // provides quark.env, watches ./.env
|
|
17
|
+
|
|
18
|
+
quark.env.get("FEATURE_FLAG").value; // Atom<string | undefined>, initial value from process.env
|
|
19
|
+
quark.env.has("REDIS_URL"); // true if seen by the plugin or present in process.env
|
|
20
|
+
const unsubscribe = quark.env.subscribe("FEATURE_FLAG", (value) => {
|
|
21
|
+
/* reacts to .env edits at runtime */
|
|
22
|
+
});
|
|
23
|
+
quark.env.set("FEATURE_FLAG", "on"); // updates the atom and emits env:changed
|
|
24
|
+
quark.ee.value.on("env:changed", (key, value) => {});
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
How it works: atoms are created lazily per key with `process.env[key]` as the initial value. On init the plugin parses `.env` at `process.cwd()` (`KEY=VALUE` lines, `#` comments; the value is everything after the first `=`, trimmed, quotes are not stripped) and calls `quark.env.set` for each line, then watches the file (`awaitWriteFinish`) and reloads it on change. `set()` writes the atom only — `process.env` is not modified, so code that reads `process.env` directly keeps the startup value. The watcher is closed in `onDestroy`.
|
|
28
|
+
|
|
29
|
+
Env: no fixed names — the plugin reads `process.env[<key>]` for whatever key is requested, plus the `./.env` file.
|
|
30
|
+
|
|
31
|
+
Part of the [Quark framework](https://www.npmjs.com/package/@quark-fw/core).
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@quark-fw/plugin-env",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.27",
|
|
4
4
|
"main": "src/index.ts",
|
|
5
5
|
"types": "src/index.ts",
|
|
6
6
|
"type": "module",
|
|
@@ -21,11 +21,11 @@
|
|
|
21
21
|
"access": "public"
|
|
22
22
|
},
|
|
23
23
|
"dependencies": {
|
|
24
|
-
"@quark-fw/atom": "0.1.
|
|
25
|
-
"@quark-fw/plugin": "0.1.
|
|
24
|
+
"@quark-fw/atom": "0.1.27",
|
|
25
|
+
"@quark-fw/plugin": "0.1.27",
|
|
26
26
|
"chokidar": "^5.0.0"
|
|
27
27
|
},
|
|
28
28
|
"peerDependencies": {
|
|
29
|
-
"@quark-fw/core": "0.1.
|
|
29
|
+
"@quark-fw/core": "0.1.27"
|
|
30
30
|
}
|
|
31
31
|
}
|
package/src/helpers.ts
CHANGED
|
@@ -1,6 +1,9 @@
|
|
|
1
1
|
// Хелперы env-плагина: загрузка .env и наблюдение за его изменениями.
|
|
2
2
|
import chokidar, { FSWatcher } from "chokidar";
|
|
3
3
|
import fs from "node:fs";
|
|
4
|
+
import { log as rootLog } from "@quark-fw/core/src/log.js";
|
|
5
|
+
|
|
6
|
+
const log = rootLog.child({ scope: "env" });
|
|
4
7
|
|
|
5
8
|
export function loadEnvFile(quark: Quark, path: string) {
|
|
6
9
|
const content = fs.readFileSync(path, "utf8");
|
|
@@ -30,7 +33,7 @@ export function watchEnv(quark: Quark, envPath = ".env") {
|
|
|
30
33
|
});
|
|
31
34
|
|
|
32
35
|
watcher.on("change", () => {
|
|
33
|
-
|
|
36
|
+
log.info(".env file changed");
|
|
34
37
|
loadEnvFile(quark, envPath);
|
|
35
38
|
});
|
|
36
39
|
}
|