| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485 |
- /**
- * 生产发版 / 编译前提醒:检查 config/env.js 的 APP_ENV。
- * - 默认:打印当前环境 + 醒目提醒(不中断)。
- * - 严格:`node scripts/remind-production-api-url.mjs --strict`
- * 若 APP_ENV 不是 production,或生产 API 仍含本机地址,则 exit 1。
- *
- * 上传微信前务必执行:npm run release:check-api
- */
- import fs from 'node:fs'
- import path from 'node:path'
- import { fileURLToPath } from 'node:url'
- const __dirname = path.dirname(fileURLToPath(import.meta.url))
- const root = path.resolve(__dirname, '..')
- const envPath = path.join(root, 'config', 'env.js')
- const readAppEnv = (raw) => {
- // 只认行首 export,避免命中注释里的示例「APP_ENV = 'production'」
- const m = raw.match(/^export\s+const\s+APP_ENV\s*=\s*['"]([^'"]+)['"]/m)
- return m ? m[1] : ''
- }
- const productionBlockLooksLocal = (raw) => {
- const m = raw.match(/production:\s*\{([\s\S]*?)\n\s*\}/)
- if (!m) return false
- return /127\.0\.0\.1|localhost/.test(m[1])
- }
- const raw = fs.readFileSync(envPath, 'utf8')
- const appEnv = readAppEnv(raw) || '?'
- const color =
- appEnv === 'production' ? '\x1b[32m' : appEnv === 'local' ? '\x1b[31m' : '\x1b[33m'
- const reset = '\x1b[0m'
- console.log(
- [
- '',
- '╔════════════════════════════════════════════════════════════╗',
- `║ 当前小程序 APP_ENV = ${appEnv.padEnd(36)}║`,
- appEnv === 'production'
- ? '║ ✅ 生产配置 ║'
- : '║ ⚠️ 非生产!正式上传微信前请改为 production ║',
- '║ 文件:config/env.js ║',
- '║ 检查:npm run release:check-api ║',
- '╚════════════════════════════════════════════════════════════╝',
- ''
- ].join('\n').replace(/^/gm, color) + reset
- )
- if (appEnv !== 'production') {
- console.warn(
- '\x1b[33m%s\x1b[0m',
- [
- '发版步骤:',
- " 1. 修改 config/env.js → export const APP_ENV = 'production'",
- ' 2. npm run release:check-api(必须通过)',
- ' 3. HBuilderX:发行 → 小程序-微信 → 再上传',
- ''
- ].join('\n')
- )
- }
- const strict = process.argv.includes('--strict')
- if (strict) {
- if (appEnv !== 'production') {
- console.error(
- `\x1b[31m[release-check]\x1b[0m APP_ENV='${appEnv}',发版须改为 'production'。`
- )
- process.exit(1)
- }
- if (productionBlockLooksLocal(raw)) {
- console.error(
- '\x1b[31m[release-check]\x1b[0m production 预设中仍含 127.0.0.1 / localhost,请先修正。'
- )
- process.exit(1)
- }
- if (!/https:\/\/w\.shanxiachem\.com\/api/.test(raw)) {
- console.error(
- '\x1b[31m[release-check]\x1b[0m 未在 env.js 中发现生产 API https://w.shanxiachem.com/api。'
- )
- process.exit(1)
- }
- console.warn('\x1b[32m[release-check]\x1b[0m APP_ENV=production,生产 API 配置检查通过。')
- }
|