umami/scripts/telemetry.js

57 lines
1.1 KiB
JavaScript
Raw Normal View History

2022-03-17 05:50:24 +01:00
const fs = require('fs-extra');
const path = require('path');
const os = require('os');
const isCI = require('is-ci');
const pkg = require('../package.json');
const dest = path.resolve(__dirname, '../.next/cache/umami.json');
2023-07-19 13:06:49 +02:00
const url = 'https://api.umami.is/v1/telemetry';
2022-03-17 05:50:24 +01:00
2022-03-19 06:39:39 +01:00
async function sendTelemetry(action) {
2022-03-17 05:50:24 +01:00
let json = {};
try {
json = await fs.readJSON(dest);
} catch {
// Ignore
}
2022-04-04 23:36:42 +02:00
try {
await fs.writeJSON(dest, { version: pkg.version });
} catch {
// Ignore
}
2022-03-19 06:39:39 +01:00
const { default: isDocker } = await import('is-docker');
const { default: fetch } = await import('node-fetch');
const payload = {
action,
version: pkg.version,
node: process.version,
platform: os.platform(),
arch: os.arch(),
os: `${os.type()} (${os.version()})`,
2023-07-19 13:06:49 +02:00
isDocker: isDocker(),
isCi: isCI,
prevVersion: json.version,
2022-03-19 06:39:39 +01:00
};
2022-04-05 00:14:26 +02:00
try {
await fetch(url, {
method: 'post',
cache: 'no-cache',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify(payload),
});
} catch {
// Ignore
}
2022-03-17 05:50:24 +01:00
}
2022-03-19 05:57:58 +01:00
module.exports = {
sendTelemetry,
};