-
-
Notifications
You must be signed in to change notification settings - Fork 87
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
🔨 chore: simple telemetry (optional) (#581)
- Loading branch information
Showing
17 changed files
with
824 additions
and
534 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
# @snailycad/telemetry | ||
|
||
TODO |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
{ | ||
"name": "@snailycad/telemetry", | ||
"version": "1.0.0-beta.79", | ||
"main": "./dist/index.js", | ||
"scripts": { | ||
"copy-env": "node ../../scripts/copy-env.mjs --telemetry", | ||
"build": "tsup", | ||
"typecheck": "tsc --noEmit" | ||
}, | ||
"dependencies": { | ||
"@snailycad/utils": "1.0.0-beta.79", | ||
"axios": "^0.26.1" | ||
}, | ||
"devDependencies": { | ||
"@types/node": "^17.0.23", | ||
"tslib": "^2.3.1", | ||
"tsup": "^5.12.1", | ||
"typescript": "^4.6.3" | ||
}, | ||
"tsup": { | ||
"entry": [ | ||
"src/**/*.ts" | ||
], | ||
"dts": true, | ||
"bundle": false, | ||
"platform": "node", | ||
"target": "node16", | ||
"silent": true, | ||
"minify": true | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
interface TelemetryCache { | ||
cadVersion: string | null; | ||
node: string | null; | ||
yarn: string | null; | ||
npm: string | null; | ||
} | ||
|
||
const cache: Partial<TelemetryCache> = {}; | ||
|
||
export function set(key: keyof TelemetryCache, value: string) { | ||
cache[key] = value; | ||
return value; | ||
} | ||
|
||
export function get(key: keyof TelemetryCache) { | ||
return cache[key]; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
import process from "node:process"; | ||
import axios from "axios"; | ||
import os from "node:os"; | ||
import { execSync } from "node:child_process"; | ||
import { get, set } from "./cache"; | ||
import { getCADVersion } from "@snailycad/utils/version"; | ||
|
||
const TELEMETRY_ENABLED = process.env.TELEMETRY_ENABLED === "true"; | ||
const REPORT_URL = "https://snailycad-telemetry.caspertheghost.workers.dev/"; | ||
|
||
interface ErrorReport { | ||
name: string; | ||
message: string; | ||
stack?: string; | ||
} | ||
|
||
export async function sendErrorReport(errorReport: ErrorReport) { | ||
if (!TELEMETRY_ENABLED) return; | ||
|
||
const [yarn, node, npm, cadVersion] = await Promise.all([ | ||
getBinaryVersions("yarn"), | ||
getBinaryVersions("node"), | ||
getBinaryVersions("npm"), | ||
getCADVersion(), | ||
]); | ||
|
||
const data = { | ||
yarn, | ||
npm, | ||
node, | ||
cadVersion, | ||
platform: os.platform(), | ||
os: os.platform(), | ||
stack: `\`\`\`${errorReport.stack || null}\`\`\``, | ||
message: `\`\`\`${errorReport.message || null}\`\`\``, | ||
name: errorReport.name || "Unknown Error", | ||
}; | ||
|
||
try { | ||
await axios({ | ||
url: REPORT_URL, | ||
method: "POST", | ||
data: JSON.stringify(data), | ||
}); | ||
} catch (e: any) { | ||
if (process.env.NODE_ENV === "development") { | ||
console.log(e.response); | ||
console.log(e.response.data.errors); | ||
} | ||
} | ||
} | ||
|
||
async function getBinaryVersions(command: "yarn" | "node" | "npm") { | ||
const cache = get(command); | ||
|
||
if (cache) { | ||
return cache; | ||
} | ||
|
||
try { | ||
const out = execSync(`${command} -v`, { encoding: "utf-8" }).toString().trim(); | ||
set(command, out); | ||
return out; | ||
} catch { | ||
return null; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
{ | ||
"extends": "../../tsconfig.base.json", | ||
"compilerOptions": { | ||
"outDir": "./dist", | ||
"target": "ESNext", | ||
"module": "CommonJS", | ||
"moduleResolution": "node", | ||
"declaration": true, | ||
"skipLibCheck": true, | ||
"importHelpers": true, | ||
"esModuleInterop": true | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import { resolve } from "node:path"; | ||
import { readFile } from "node:fs/promises"; | ||
import process from "node:process"; | ||
|
||
let versionCache: string; | ||
export async function getCADVersion(): Promise<string | null> { | ||
const packageJsonPath = resolve(process.cwd(), "package.json"); | ||
const packageJson = await readFile(packageJsonPath, "utf-8").catch(() => null); | ||
if (!packageJson) return null; | ||
|
||
const json = JSON.parse(packageJson); | ||
|
||
versionCache ??= json.version; | ||
|
||
return versionCache; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
import { expect, test } from "vitest"; | ||
import { getCADVersion } from "../src/version"; | ||
|
||
test("Should return the CAD version", async () => { | ||
expect(await getCADVersion()).toBeTypeOf("string"); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.