实现说明
底层就是函数 tags。
环境变量 DEPLOY_ENV、PORT、DEBUG、DEPLOY_TAGS 驱动配置,一份产物适配所有环境。
使用要点
- 直接运行文件,输出会带上标签,比如
env:、port:、debug flag:——输出的每一行都能指回产生它的那次调用。
常见问题
有语言之外需要解析的导入吗?
没有。每个导入都解析到语言自带的模块。 文件在加载时定义 tags。
主签名形如 const tags = (process.env.DEPLOY_TAGS ?? '')
核心语句
const tags = (process.env.DEPLOY_TAGS ?? '').split(',').filter(Boolean);
const env = process.env.DEPLOY_ENV ?? 'development';
const port = Number(process.env.PORT ?? 3000);
代码示例
// Write a variable the way a shell or test harness would, then read it back.
process.env.DEPLOY_ENV = 'staging';
const env = process.env.DEPLOY_ENV ?? 'development';
const port = Number(process.env.PORT ?? 3000);
const debug = process.env.DEBUG === 'on';
const tags = (process.env.DEPLOY_TAGS ?? '').split(',').filter(Boolean);
console.log('env:', env);
console.log('port:', port, typeof port === 'number');
console.log('debug flag:', debug);
console.log('tags:', tags.length);
// env values are always strings, so missing vars need explicit defaults
const unset = process.env.DEFINITELY_UNSET_VAR;
console.log('unset var is undefined:', unset === undefined);