Files
shanxia2.0/miniprogram/scripts/remind-production-api-url.mjs
T
2026-09-12 19:42:20 +08:00

70 lines
2.4 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
/**
* 生产发版 / 编译前提醒:检查 config/env.js 的 APP_ENV。
* - 默认:仅打印醒目提醒(不中断)。
* - 严格:`node scripts/remind-production-api-url.mjs --strict`
* 若 APP_ENV 不是 production,或生产 API 仍含本机地址,则 exit 1。
*/
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 banner = () => {
const lines = [
'',
'================================================================',
' shanxia miniprogram 发版提醒',
' 部署到生产环境前,必须修改 config/env.js:',
" export const APP_ENV = 'production'",
' 生产 API:https://w.shanxiachem.com/api',
' 生产静态:https://w.shanxiachem.com/shanxia-static/',
' 微信合法域名请配置:w.shanxiachem.com',
'================================================================',
''
]
console.warn('\x1b[33m%s\x1b[0m', lines.join('\n'))
}
const readAppEnv = (raw) => {
const m = raw.match(/export\s+const\s+APP_ENV\s*=\s*['"]([^'"]+)['"]/)
return m ? m[1] : ''
}
const productionBlockLooksLocal = (raw) => {
// 只检查 production 预设块内是否误写本机地址
const m = raw.match(/production:\s*\{([\s\S]*?)\n\s*\}/)
if (!m) return false
return /127\.0\.0\.1|localhost/.test(m[1])
}
const strictCheck = () => {
const raw = fs.readFileSync(envPath, 'utf8')
const appEnv = readAppEnv(raw)
if (appEnv !== 'production') {
console.error(
`\x1b[31m[release-check]\x1b[0m config/env.js 中 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 配置检查通过。')
}
banner()
const strict = process.argv.includes('--strict')
if (strict) strictCheck()