@saasak/tool-env 1.2.0 → 1.2.1

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.
Files changed (2) hide show
  1. package/package.json +1 -1
  2. package/src/core.js +26 -23
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@saasak/tool-env",
3
3
  "license": "MIT",
4
- "version": "1.2.0",
4
+ "version": "1.2.1",
5
5
  "author": "dev@saasak.studio",
6
6
  "description": "A small util to manage environment variables for your monorepo",
7
7
  "keywords": [
package/src/core.js CHANGED
@@ -136,32 +136,35 @@ export function resolveTargetAliases(allowedEnvs) {
136
136
  * @returns {string} - Resolved secret or empty string if not provided
137
137
  */
138
138
  export function resolveSecret(secretParam) {
139
- if (secretParam) {
140
- if (secretParam === "stdin") {
141
- try {
142
- const value = fs.readFileSync(0, "utf8").trim();
143
- if (value) return value;
144
- } catch (_) {
145
- // fall through to env vars
146
- }
147
- } else if (secretParam.startsWith("file://")) {
148
- try {
149
- const value = fs.readFileSync(secretParam.slice(7), "utf8").trim();
150
- if (value) return value;
151
- } catch (_) {
152
- // fall through to env vars
153
- }
154
- } else {
155
- try {
156
- return fs.readFileSync(secretParam, "utf8").trim();
157
- } catch (_) {
158
- // Not a readable file path — treat as literal secret string
159
- return secretParam;
160
- }
139
+ const getDefault = (v = '') => v || process.env.WRENV_SECRET || process.env.TARGET_SECRET || "";
140
+ const getFromStream = (stream) => {
141
+ try {
142
+ const value = fs.readFileSync(stream, "utf8").trim();
143
+ return getDefault(value);
144
+ } catch (_) {
145
+ return getDefault();
161
146
  }
162
147
  }
148
+ const isFile = (f) => {
149
+ try {
150
+ return fs.statSync(f).isFile();
151
+ } catch (_) {
152
+ return false
153
+ }
154
+ }
155
+
156
+ if (!secretParam)
157
+ return getDefault();
158
+
159
+ if (secretParam === "stdin")
160
+ return getFromStream(0);
161
+
162
+ if (secretParam.startsWith("file://"))
163
+ return getFromStream(secretParam.slice(7));
163
164
 
164
- return process.env.WRENV_SECRET || process.env.TARGET_SECRET || "";
165
+ return isFile(secretParam)
166
+ ? getFromStream(secretParam)
167
+ : getDefault(secretParam); // Treat as literal if not a file
165
168
  }
166
169
 
167
170
  /**