Added telemetry script.

This commit is contained in:
Mike Cao 2022-03-16 21:50:24 -07:00
parent 3717b0e888
commit 45e4bfe3a9
6 changed files with 162 additions and 31 deletions

View file

@ -1,4 +1,4 @@
const fs = require('fs');
const fs = require('fs-extra');
const path = require('path');
const https = require('https');
const chalk = require('chalk');
@ -16,11 +16,9 @@ const asyncForEach = async (array, callback) => {
}
};
if (!fs.existsSync(dest)) {
fs.mkdirSync(dest);
}
const download = async files => {
await fs.ensureDir(dest);
await asyncForEach(files, async file => {
const locale = file.replace('-', '_').replace('.json', '');

View file

@ -1,4 +1,4 @@
const fs = require('fs');
const fs = require('fs-extra');
const path = require('path');
const https = require('https');
const chalk = require('chalk');
@ -16,11 +16,9 @@ const asyncForEach = async (array, callback) => {
}
};
if (!fs.existsSync(dest)) {
fs.mkdirSync(dest);
}
const download = async files => {
await fs.ensureDir(dest);
await asyncForEach(files, async file => {
const locale = file.replace('-', '_').replace('.json', '');

View file

@ -1,4 +1,4 @@
const fs = require('fs');
const fs = require('fs-extra');
const path = require('path');
const del = require('del');
const prettier = require('prettier');
@ -14,22 +14,24 @@ if (removed.length) {
console.log(removed.map(n => `${n} ${chalk.redBright('✗')}`).join('\n'));
}
if (!fs.existsSync(dest)) {
fs.mkdirSync(dest);
async function run() {
await fs.ensureDir(dest);
files.forEach(file => {
const lang = require(`../lang/${file}`);
const keys = Object.keys(lang).sort();
const formatted = keys.reduce((obj, key) => {
obj[key] = { defaultMessage: lang[key] };
return obj;
}, {});
const json = prettier.format(JSON.stringify(formatted), { parser: 'json' });
fs.writeFileSync(path.resolve(dest, file), json);
console.log(path.resolve(src, file), chalk.greenBright('->'), path.resolve(dest, file));
});
}
files.forEach(file => {
const lang = require(`../lang/${file}`);
const keys = Object.keys(lang).sort();
const formatted = keys.reduce((obj, key) => {
obj[key] = { defaultMessage: lang[key] };
return obj;
}, {});
const json = prettier.format(JSON.stringify(formatted), { parser: 'json' });
fs.writeFileSync(path.resolve(dest, file), json);
console.log(path.resolve(src, file), chalk.greenBright('->'), path.resolve(dest, file));
});
run();

59
scripts/telemetry.js Normal file
View file

@ -0,0 +1,59 @@
require('dotenv').config();
const fs = require('fs-extra');
const path = require('path');
const os = require('os');
const retry = require('async-retry');
const isCI = require('is-ci');
const pkg = require('../package.json');
const dest = path.resolve(__dirname, '../.next/cache/umami.json');
const url = 'https://telemetry.umami.is/api/collect';
async function run() {
await fs.ensureFile(dest);
let json = {};
try {
json = await fs.readJSON(dest);
} catch {
// Ignore
}
if (json.version !== pkg.version) {
const { default: isDocker } = await import('is-docker');
const { default: fetch } = await import('node-fetch');
await fs.writeJSON(dest, { version: pkg.version });
const payload = {
umami: pkg.version,
node: process.version,
platform: os.platform(),
arch: os.arch(),
os: os.version(),
isDocker: isDocker(),
isCI,
};
await retry(
async () => {
const res = await fetch(url, {
method: 'post',
cache: 'no-cache',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify(payload),
});
console.log(res);
},
{ minTimeout: 500, retries: 1, factor: 1 },
).catch(() => {});
}
}
if (!process.env.TELEMETRY_DISABLED) {
run();
}