52 lines
2.0 KiB
JavaScript
52 lines
2.0 KiB
JavaScript
/**
|
||
* 生产发版 / 编译前提醒:检查 config/api.js 是否仍指向本机调试地址。
|
||
* - 默认:仅打印醒目提醒(不中断)。
|
||
* - 严格:`node scripts/remind-production-api-url.mjs --strict` 若仍检测到活动行中的本机地址则 exit 1(供 CI)。
|
||
*/
|
||
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 apiPath = path.join(root, 'config', 'api.js')
|
||
|
||
const banner = () => {
|
||
const lines = [
|
||
'',
|
||
'================================================================',
|
||
' shanxia miniprogram 发版提醒',
|
||
' 部署到生产环境前,必须核对并修改 config/api.js:',
|
||
' - 常量 DEV_BASE_URL',
|
||
' - 微信小程序条件编译块 (#ifdef MP-WEIXIN) 内的 devBaseUrl',
|
||
' 二者须与生产网关 / API 根路径一致(勿保留 127.0.0.1 / localhost)。',
|
||
' 详见项目 README「生产部署与 API 基址」一节。',
|
||
'================================================================',
|
||
''
|
||
]
|
||
console.warn('\x1b[33m%s\x1b[0m', lines.join('\n'))
|
||
}
|
||
|
||
const looksLikeLocalApi = (line) => {
|
||
const t = line.trim()
|
||
if (!t || t.startsWith('//') || t.startsWith('*')) return false
|
||
if (!/(DEV_BASE_URL|devBaseUrl)/.test(t)) return false
|
||
return /127\.0\.0\.1|localhost/.test(t)
|
||
}
|
||
|
||
const strictCheck = () => {
|
||
const raw = fs.readFileSync(apiPath, 'utf8')
|
||
const hit = raw.split(/\r?\n/).some(looksLikeLocalApi)
|
||
if (hit) {
|
||
console.error(
|
||
'\x1b[31m[release-check]\x1b[0m config/api.js 中仍存在指向本机的 DEV_BASE_URL 或 devBaseUrl,请先改为生产地址后再发版(或去掉 --strict)。'
|
||
)
|
||
process.exit(1)
|
||
}
|
||
console.warn('\x1b[32m[release-check]\x1b[0m 未发现活动行中的本机 API 地址(已通过 --strict)。')
|
||
}
|
||
|
||
banner()
|
||
const strict = process.argv.includes('--strict')
|
||
if (strict) strictCheck()
|