@stacksjs/env 0.75.262 → 0.75.263
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 +3 -1
- package/package.json +3 -3
package/dist/cli.js
CHANGED
|
@@ -20,7 +20,9 @@ Provide the private key as ${envPrivateKeyNames(options.file)[0]} in the environ
|
|
|
20
20
|
${publicKeyName}="${keypair.publicKey}"
|
|
21
21
|
${privateKeyName}="${keypair.privateKey}"
|
|
22
22
|
`;writePrivateKeys(keysPath,keysContent);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){assertMatchingPrivateKey(publicKey,keysPath,options.file);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"}`}}}
|
|
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"}`}}}function undecryptableMessage(unreadable,parsed,privateKey,options){const file=options.file||".env",keysFile=options.keysFile||".env.keys",[privateKeyName]=envPrivateKeyNames(options.file),values=`${unreadable.length===1?"Value":"Values"} that cannot be decrypted: ${unreadable.join(", ")}.`;if(!privateKey)return[`Cannot read ${file}: no private key for it.`,` Expected ${privateKeyName} in ${keysFile} or the environment, and found neither.`,` ${values}`].join(`
|
|
24
|
+
`);const publicKeyName=envPublicKeyNames(options.file).find((name)=>parsed[name]),declared=publicKeyName?parsed[publicKeyName]:void 0,reason=declared&&!publicKeyMatchesPrivateKey(declared,privateKey)?` The private key found for it does not match ${publicKeyName} in the file, so it belongs to a different key pair.`:" The private key found for it did not decrypt them. It may belong to a different key pair, or the values may be damaged.";return[`Cannot read ${file}: its private key does not decrypt it.`,reason,` ${values}`].join(`
|
|
25
|
+
`)}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{const privateKey=resolveEnvPrivateKey(options),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:undecryptableMessage(unreadable,parsed,privateKey,options)};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=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
26
|
`);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
27
|
`);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
28
|
`).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.263",
|
|
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.263"
|
|
60
60
|
},
|
|
61
61
|
"devDependencies": {
|
|
62
62
|
"better-dx": "^0.2.24",
|
|
63
|
-
"@stacksjs/validation": "0.75.
|
|
63
|
+
"@stacksjs/validation": "0.75.263"
|
|
64
64
|
}
|
|
65
65
|
}
|