xiaoju-survey/web/report.ts
luch 0ce78b62a0
[Feature]: 升级状态相关功能 (#438)
* fix: 解决readme的冲突

* feat: 修改补充单测 (#437)
2024-09-30 19:18:55 +08:00

48 lines
1.0 KiB
TypeScript

import fs from 'fs-extra'
const fsa = fs.promises
process.env.XIAOJU_SURVEY_REPORT = 'true'
const readData = async (pkg: string) => {
const id = new Date().getTime().toString()
try {
if (!fs.existsSync(pkg)) {
return {
type: 'web',
name: '',
version: '',
description: '',
id,
msg: '文件不存在'
}
}
const data = await fsa.readFile(pkg, 'utf8').catch((e) => e)
const { name, version, description } = JSON.parse(data)
return { type: 'web', name, version, description, id }
} catch (error) {
return error
}
}
const report = async () => {
if (!process.env.XIAOJU_SURVEY_REPORT) {
return
}
const res = await readData('./package.json')
// 上报
fetch &&
fetch('https://xiaojusurveysrc.didi.cn/reportSourceData', {
method: 'POST',
headers: {
Accept: 'application/json, */*',
'Content-Type': 'application/json'
},
body: JSON.stringify(res)
}).catch(() => {})
}
report()