@gylautorun/dev-proxy-cookie 1.0.0-beta.1 → 1.0.0

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.
@@ -1,155 +0,0 @@
1
- /**
2
- * 环境检测工具函数
3
- * 用于智能判断当前是否为生产构建环境
4
- */
5
-
6
- /**
7
- * 判断环境变量值是否表示生产环境
8
- */
9
- export function isProductionValue(value: string): boolean {
10
- const productionValues = [
11
- 'production', // 生产环境
12
- 'prod', // 生产环境
13
- 'prd', // 生产环境
14
- 'release', // 发布环境
15
- 'staging', // 预发布环境
16
- 'uat', // 预发布环境
17
- ];
18
- return productionValues.includes(value.toLowerCase().trim());
19
- }
20
-
21
- /**
22
- * 智能检测当前是否为生产构建环境
23
- * 通过多种方式综合判断,避免依赖单一环境变量
24
- *
25
- * @param customEnvs 用户自定义的环境变量名称列表
26
- * @param debug 是否输出调试日志
27
- * @param loggerPrefix 日志前缀,用于区分不同模块
28
- * @returns 是否为生产环境
29
- */
30
- export function detectProductionEnvironment(
31
- customEnvs: string[] = [],
32
- debug: boolean = false,
33
- loggerPrefix: string = '[env-detector]'
34
- ): boolean {
35
- const env = process.env;
36
-
37
- // 1. 检查自定义环境变量
38
- if (customEnvs.length > 0) {
39
- for (const envName of customEnvs) {
40
- const envValue = env[envName];
41
- if (envValue && isProductionValue(envValue)) {
42
- if (debug) {
43
- console.log(`${loggerPrefix} Detected production via custom env: ${envName}=${envValue}`);
44
- }
45
- return true;
46
- }
47
- }
48
- }
49
-
50
- // 2. 检查常见的环境变量
51
- const commonEnvNames = [
52
- 'NODE_ENV',
53
- 'BUILD_MODE',
54
- 'VUE_APP_ENV',
55
- 'VITE_NODE_ENV',
56
- 'WEBPACK_MODE',
57
- 'CI_ENV',
58
- 'APP_ENV',
59
- 'ENV',
60
- 'DEPLOY_ENV',
61
- 'RUN_MODE',
62
- ];
63
-
64
- for (const envName of commonEnvNames) {
65
- const envValue = env[envName];
66
- if (envValue && isProductionValue(envValue)) {
67
- if (debug) {
68
- console.log(`${loggerPrefix} Detected production via env: ${envName}=${envValue}`);
69
- }
70
- return true;
71
- }
72
- }
73
-
74
- // 3. 检查 CI/CD 环境标识
75
- if (env.CI === 'true' || env.CI === '1' || env.CI === 'yes') {
76
- if (debug) {
77
- console.log(`${loggerPrefix} Detected production via CI env`);
78
- }
79
- return true;
80
- }
81
-
82
- // 4. 检查 npm 生命周期事件(构建命令)
83
- if (env.npm_lifecycle_event) {
84
- const lifecycleEvent = env.npm_lifecycle_event.toLowerCase();
85
- if (lifecycleEvent.includes('build') ||
86
- lifecycleEvent.includes('prod') ||
87
- lifecycleEvent.includes('prd') ||
88
- lifecycleEvent.includes('release')) {
89
- if (debug) {
90
- console.log(`${loggerPrefix} Detected production via lifecycle event: ${env.npm_lifecycle_event}`);
91
- }
92
- return true;
93
- }
94
- }
95
-
96
- // 5. 检查进程参数(构建工具通常会传入特定参数)
97
- const processArgs = process.argv.join('').toLowerCase();
98
- if (processArgs.includes('build') ||
99
- processArgs.includes('production') ||
100
- processArgs.includes('--mode=production') ||
101
- processArgs.includes('--prod') ||
102
- processArgs.includes('--release')) {
103
- if (debug) {
104
- console.log(`${loggerPrefix} Detected production via process arguments`);
105
- }
106
- return true;
107
- }
108
-
109
- // 默认认为是开发环境
110
- return false;
111
- }
112
-
113
- /**
114
- * 判断是否应该启用文件监听
115
- *
116
- * 注意:此函数仅在 isDev 参数未设置时调用,用于智能判断环境
117
- * 如果用户已通过 isDev 参数明确指定环境,则不会调用此函数
118
- *
119
- * @param watch 用户设置的 watch 选项
120
- * @param customEnvs 用户自定义的环境变量列表
121
- * @param debug 是否输出调试日志
122
- * @param loggerPrefix 日志前缀
123
- * @returns 是否应该启用监听
124
- */
125
- export function shouldEnableWatch(
126
- watch: boolean | 'auto',
127
- customEnvs: string[] = [],
128
- debug: boolean = false,
129
- loggerPrefix: string = '[env-detector]'
130
- ): boolean {
131
- // 用户显式设置为 true 或 false,直接返回
132
- if (typeof watch === 'boolean') {
133
- if (debug && !watch) {
134
- console.log(`${loggerPrefix} Watch disabled by user setting`);
135
- }
136
- return watch;
137
- }
138
-
139
- // 'auto' 模式:智能检测环境
140
- const isProduction = detectProductionEnvironment(customEnvs, debug, loggerPrefix);
141
-
142
- if (isProduction) {
143
- if (debug) {
144
- console.log(`${loggerPrefix} Auto-detected production mode - disabling watch`);
145
- }
146
- return false;
147
- }
148
-
149
- // 开发环境:启用监听
150
- if (debug) {
151
- console.log(`${loggerPrefix} Auto-detected development mode - enabling watch`);
152
- }
153
-
154
- return true;
155
- }