Skip to content

Commit 6af0f4e

Browse files
committed
feat: factor out counting into separate module
1 parent 076089f commit 6af0f4e

File tree

4 files changed

+29
-14
lines changed

4 files changed

+29
-14
lines changed

src/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { getUserPreferences } from "./prompts"
55
import { wrapInSpinner } from "./utils/spinner"
66
import { getUserPkgManager } from "./utils/getUserPkgManager"
77
import { cliOptions } from "./utils/parseCliOptions"
8+
import { count } from "./utils/count"
89

910

1011
const main = async () => {
@@ -18,6 +19,7 @@ const main = async () => {
1819
let preferences
1920
if (!ci) {
2021
preferences = await getUserPreferences()
22+
count(preferences)
2123
} else {
2224
preferences = {
2325
setProjectName: "my-sidebase-app",

src/utils/count.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import fetch from "node-fetch"
2+
import { Preferences } from "../prompts"
3+
import { cliOptions } from "./parseCliOptions"
4+
5+
/**
6+
* This method provides basic usage tracking. Note:
7+
* - we use plausible as a privacy friendly tracker,
8+
* - we only track: a) invocation count, b) what stack was selected, c) what modules were selected
9+
*
10+
* No identifiably information (such as project name, IP, user-agent) is transmitted in the process. You can opt out by passing `--nocounting` to the CLI on invocation.
11+
*/
12+
let alreadyCounted = false
13+
export const count = async (preferences: Preferences) => {
14+
if (alreadyCounted || process.env.NODE_ENV === "development" || cliOptions.nocounting) {
15+
return
16+
}
17+
18+
// We do not want to know identifiable information, only pass: Stack + selected modules
19+
const annonymizedPreferences = new URLSearchParams({ ref: preferences.setStack, utm_content: (preferences.addModules || []).join(",") || "null" })
20+
const headers = { "User-Agent": "Mozilla/5.0", "X-Forwarded-For": `2.175.${Math.floor(Math.random() * 255)}.${Math.floor(Math.random() * 255)}` }
21+
return fetch("https://plausible.io/api/event", {
22+
method: "POST",
23+
headers,
24+
body: `{"name":"cli","url":"https://cli.sidebase.io/?${annonymizedPreferences}","domain":"cli.sidebase.io"}`
25+
}).then(() => { alreadyCounted = true }).catch(() => { return })
26+
}

src/utils/getVersion.ts

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import fetch from "node-fetch"
2-
import { cliOptions } from "./parseCliOptions"
32

43
// Adapted from: https://github.com/withastro/astro/blob/2552816d5fa14a191a73179698b4b6f574a9963f/packages/create-astro/src/messages.ts#L44-L58
54
let v: string
@@ -8,18 +7,6 @@ export const getVersion = async () => {
87
return v
98
}
109

11-
if (process.env.NODE_ENV != "development" && !cliOptions.nocounting) {
12-
try {
13-
fetch("https://plausible.io/api/event", {
14-
method: "POST",
15-
headers: { "User-Agent": "Mozilla/5.0", "X-Forwarded-For": `2.175.${Math.floor(Math.random() * 255)}.${Math.floor(Math.random() * 255)}` },
16-
body: "{\"name\":\"cli\",\"url\":\"https://cli.sidebase.io/\",\"domain\":\"cli.sidebase.io\"}"
17-
})
18-
} catch (error) {
19-
// pass
20-
}
21-
}
22-
2310
let response: { version: string }
2411
try {
2512
response = await fetch("https://registry.npmjs.org/create-sidebase/latest").then(response => response.json() as unknown as { version: string } )

src/utils/parseCliOptions.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { program } from "commander"
22

33
program.option("--quick", "Quicker flow by skipping most of Diamonds interactions", false)
44
program.option("--ci", "Set CI mode: Selects the merino stack and all options, non-interactive", false)
5-
program.option("--nocounting", "Only one datapoint is collected: 'An invocation happened'. No further data or meta-data is collected. Opt out of this by setting this flag.", false)
5+
program.option("--nocounting", "Only the following annonymous data is collected: a) an invocation happened, b) what stack was selected?, c) what modules were selected?. No further identifiable or unidentifiable data or meta-data (such as project name, ...) is collected. Opt out of this by setting this flag.", false)
66
program.parse()
77

88
export const cliOptions: { quick: boolean, nocounting: boolean, ci: boolean } = program.opts()

0 commit comments

Comments
 (0)