@stacksjs/env 0.75.219 → 0.75.220
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/dist/cli.js +1 -1
- package/package.json +3 -3
package/dist/cli.js
CHANGED
|
@@ -20,7 +20,7 @@ Provide the private key as ${envPrivateKeyNames(options.file)[0]} in the environ
|
|
|
20
20
|
${publicKeyName}="${keypair.publicKey}"
|
|
21
21
|
${privateKeyName}="${keypair.privateKey}"
|
|
22
22
|
`;writeFileSync(keysPath,keysContent,"utf-8");const staleNames=envPublicKeyNames(options.file);for(let i=lines.length-1;i>=0;i--){const lineEntry=lines[i];if(lineEntry!==void 0&&staleNames.some((name)=>lineEntry.trim().startsWith(`${name}=`)))lines.splice(i,1)}lines.unshift(`${declaredName}="${publicKey}"`)}let finalValue=value;if(!options.plain&&publicKey)finalValue=encryptValue(value,publicKey);for(let i=0;i<lines.length;i++){const lineEntry=lines[i];if(lineEntry===void 0)continue;if(lineEntry.trim().startsWith(`${key}=`)){lines[i]=`${key}="${finalValue}"`;found=!0;break}}if(!found)lines.push(`${key}="${finalValue}"`);const output=lines.join(`
|
|
23
|
-
`);writeFileSync(envPath,output,"utf-8");return{success:!0,output:`set ${key}${options.plain?"":" with encryption"} (${options.file||".env"})`}}catch(error){return{success:!1,error:`Failed to set: ${error instanceof Error?error.message:"Unknown error"}`}}}export function getEnv(key,options={}){const cwd=options.cwd||process.cwd(),envPath=resolve(cwd,options.file||".env");if(!existsSync(envPath))return{success:!1,error:`File not found: ${envPath}`};try{let privateKey;const keysPath=resolve(cwd,options.keysFile||".env.keys"),env=((options.file||".env").split("/").pop()||"").replace(/^\.env\./,"").replace(/^\.env$/,"").toUpperCase(),privateKeyName=env?`DOTENV_PRIVATE_KEY_${env}`:"DOTENV_PRIVATE_KEY";if(existsSync(keysPath)){const keysContent=readFileSync(keysPath,"utf-8"),{parsed:keys}=parse(keysContent);privateKey=keys[privateKeyName]}if(!privateKey)privateKey=process.env[privateKeyName];const envContent=readFileSync(envPath,"utf-8"),{parsed}=parse(envContent,{privateKey});if(key&&!options.all){const value=parsed[key];if(value===void 0)return{success:!1,error:`Key not found: ${key}`};return{success:!0,output:value}}const result=options.all?{...process.env,...parsed}:parsed;let output;switch(options.format){case"shell":output=Object.entries(result).map(([k,v])=>`${k}=${v}`).join(" ");break;case"eval":output=Object.entries(result).map(([k,v])=>`${k}="${v}"`).join(`
|
|
23
|
+
`);writeFileSync(envPath,output,"utf-8");return{success:!0,output:`set ${key}${options.plain?"":" with encryption"} (${options.file||".env"})`}}catch(error){return{success:!1,error:`Failed to set: ${error instanceof Error?error.message:"Unknown error"}`}}}export function getEnv(key,options={}){const cwd=options.cwd||process.cwd(),envPath=resolve(cwd,options.file||".env");if(!existsSync(envPath))return{success:!1,error:`File not found: ${envPath}`};try{let privateKey;const keysPath=resolve(cwd,options.keysFile||".env.keys"),env=((options.file||".env").split("/").pop()||"").replace(/^\.env\./,"").replace(/^\.env$/,"").toUpperCase(),privateKeyName=env?`DOTENV_PRIVATE_KEY_${env}`:"DOTENV_PRIVATE_KEY";if(existsSync(keysPath)){const keysContent=readFileSync(keysPath,"utf-8"),{parsed:keys}=parse(keysContent);privateKey=keys[privateKeyName]}if(!privateKey)privateKey=process.env[privateKeyName];const envContent=readFileSync(envPath,"utf-8"),{parsed,skippedEncrypted}=parse(envContent,{privateKey}),unreadable=key&&!options.all?skippedEncrypted.filter((name)=>name===key):skippedEncrypted;if(unreadable.length>0)return{success:!1,error:`Could not decrypt ${unreadable.join(", ")}. Restore ${privateKeyName} before reading this environment.`};if(key&&!options.all){const value=parsed[key];if(value===void 0)return{success:!1,error:`Key not found: ${key}`};return{success:!0,output:value}}const result=options.all?{...process.env,...parsed}:parsed;let output;switch(options.format){case"shell":output=Object.entries(result).map(([k,v])=>`${k}=${v}`).join(" ");break;case"eval":output=Object.entries(result).map(([k,v])=>`${k}="${v}"`).join(`
|
|
24
24
|
`);break;case"json":default:output=options.prettyPrint?JSON.stringify(result,null,2):JSON.stringify(result);break}return{success:!0,output}}catch(error){return{success:!1,error:`Failed to get: ${error instanceof Error?error.message:"Unknown error"}`}}}export function getKeypair(keyName,options={}){const cwd=options.cwd||process.cwd(),keysPath=resolve(cwd,options.keysFile||".env.keys");if(!existsSync(keysPath))return{success:!1,error:`Keys file not found: ${keysPath}`};try{const keysContent=readFileSync(keysPath,"utf-8"),{parsed}=parse(keysContent),env=((options.file||".env").split("/").pop()||"").replace(/^\.env\./,"").replace(/^\.env$/,"").toUpperCase(),publicKeyName=env?`DOTENV_PUBLIC_KEY_${env}`:"DOTENV_PUBLIC_KEY",privateKeyName=env?`DOTENV_PRIVATE_KEY_${env}`:"DOTENV_PRIVATE_KEY";if(keyName){const value=parsed[keyName];if(!value)return{success:!1,error:`Key not found: ${keyName}`};return{success:!0,output:value}}const keypair={[publicKeyName]:parsed[publicKeyName],[privateKeyName]:parsed[privateKeyName]};return{success:!0,output:options.format==="shell"?`${publicKeyName}=${keypair[publicKeyName]} ${privateKeyName}=${keypair[privateKeyName]}`:JSON.stringify(keypair)}}catch(error){return{success:!1,error:`Failed to get keypair: ${error instanceof Error?error.message:"Unknown error"}`}}}function rotatedKeysContent(keysContent,keypair,publicKeyName,privateKeyName){const lines=keysContent.split(`
|
|
25
25
|
`);let replacedPublic=!1,replacedPrivate=!1;for(let i=0;i<lines.length;i++){const entry=lines[i];if(entry===void 0)continue;if(entry.startsWith(`${publicKeyName}=`)){lines[i]=`${publicKeyName}="${keypair.publicKey}"`;replacedPublic=!0}else if(entry.startsWith(`${privateKeyName}=`)){lines[i]=`${privateKeyName}="${keypair.privateKey}"`;replacedPrivate=!0}}if(!replacedPublic)lines.push(`${publicKeyName}="${keypair.publicKey}"`);if(!replacedPrivate)lines.push(`${privateKeyName}="${keypair.privateKey}"`);return`${lines.join(`
|
|
26
26
|
`).replace(/\n+$/,"")}
|
package/package.json
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
"name": "@stacksjs/env",
|
|
3
3
|
"type": "module",
|
|
4
4
|
"sideEffects": false,
|
|
5
|
-
"version": "0.75.
|
|
5
|
+
"version": "0.75.220",
|
|
6
6
|
"description": "Stacks env helper methods.",
|
|
7
7
|
"author": "Chris Breuer",
|
|
8
8
|
"contributors": [
|
|
@@ -56,10 +56,10 @@
|
|
|
56
56
|
"prepublishOnly": "bun run build"
|
|
57
57
|
},
|
|
58
58
|
"dependencies": {
|
|
59
|
-
"@stacksjs/path": "0.75.
|
|
59
|
+
"@stacksjs/path": "0.75.220"
|
|
60
60
|
},
|
|
61
61
|
"devDependencies": {
|
|
62
62
|
"better-dx": "^0.2.24",
|
|
63
|
-
"@stacksjs/validation": "0.75.
|
|
63
|
+
"@stacksjs/validation": "0.75.220"
|
|
64
64
|
}
|
|
65
65
|
}
|