t-comm
Version:
专业、稳定、纯粹的工具库
32 lines (31 loc) • 1.17 kB
TypeScript
/**
* 获取文件中所有环境变量的键值对映射
* 支持解析 KEY=VALUE 格式的环境变量文件,忽略以 # 开头的注释行
* @param {string} filepath 文件路径,如果文件不存在则将参数作为字符串直接解析
* @returns {Record<string, any>} 环境变量的键值对映射
* @example
* ```ts
* // 解析 .env 文件
* const envMap = getEnvVariableMap('.env.local');
* // { NODE_ENV: 'production', API_URL: 'https://api.example.com' }
*
* // 直接解析字符串
* const envMap2 = getEnvVariableMap('KEY1=value1\nKEY2=value2');
* // { KEY1: 'value1', KEY2: 'value2' }
* ```
*/
export declare function getEnvVariableMap(filepath: string): Record<string, any>;
/**
* 读取文件中环境变量的值,支持:
* - NPM_TOKEN=xxx
* - NPM_TOKEN = xxx
* @param {string} key 环境变量的key
* @param {string} filepath 保存环境变量的文件路径
* @returns {string} 环境变量的值
* @example
* ```ts
* const token = readEnvVariable('NPM_TOKEN', '.env.local');
* console.log(token); // 'npm_xxxxxxxxxxxxxxxx'
* ```
*/
export declare function readEnvVariable(key: string, filepath: string): string;