This commit is contained in:
Martin McKeaveney 2021-03-30 11:50:49 +01:00
parent a4486fff4c
commit 7bc7fc7e7d
5 changed files with 51 additions and 45 deletions

View File

@ -1,13 +1,16 @@
const PostHog = require("posthog-node") const PostHog = require("posthog-node")
const path = require("path") const {
const fs = require("fs") BUDIBASE_POSTHOG_URL,
const os = require("os") BUDIBASE_POSTHOG_TOKEN,
const { BUDIBASE_POSTHOG_URL, BUDIBASE_POSTHOG_TOKEN, AnalyticsEvents } = require("../constants") AnalyticsEvents,
} = require("../constants")
const ConfigManager = require("../structures/ConfigManager") const ConfigManager = require("../structures/ConfigManager")
class AnalyticsClient { class AnalyticsClient {
constructor() { constructor() {
this.client = new PostHog(BUDIBASE_POSTHOG_TOKEN, { host: BUDIBASE_POSTHOG_URL }) this.client = new PostHog(BUDIBASE_POSTHOG_TOKEN, {
host: BUDIBASE_POSTHOG_URL,
})
this.configManager = new ConfigManager() this.configManager = new ConfigManager()
} }
@ -32,4 +35,4 @@ class AnalyticsClient {
} }
} }
module.exports = AnalyticsClient module.exports = AnalyticsClient

View File

@ -1,15 +1,6 @@
const Command = require("../structures/Command") const Command = require("../structures/Command")
const { CommandWords, InitTypes, BUDIBASE_POSTHOG_URL } = require("../constants") const { CommandWords } = require("../constants")
const { lookpath } = require("lookpath") const { success, error } = require("../utils")
const {
success,
error,
info,
parseEnv,
} = require("../utils")
const { confirmation } = require("../questions")
const fs = require("fs")
const axios = require("axios")
const AnalyticsClient = require("./Client") const AnalyticsClient = require("./Client")
const client = new AnalyticsClient() const client = new AnalyticsClient()
@ -18,9 +9,18 @@ async function optOut() {
try { try {
// opt them out // opt them out
client.disable() client.disable()
console.log(success("Successfully opted out of budibase analytics. You can opt in at any time by running 'budi analytics opt-in'")) console.log(
} catch (err) { success(
console.log(error("Error opting out of budibase analytics. Please try again later.", err)) "Successfully opted out of budibase analytics. You can opt in at any time by running 'budi analytics opt-in'"
)
)
} catch (err) {
console.log(
error(
"Error opting out of budibase analytics. Please try again later.",
err
)
)
} }
} }
@ -28,9 +28,15 @@ async function optIn() {
try { try {
// opt them in // opt them in
client.enable() client.enable()
console.log(success("Successfully opted in to budibase analytics. Thank you for helping us make budibase better!")) console.log(
} catch (err) { success(
console.log(error("Error opting in to budibase analytics. Please try again later.")) "Successfully opted in to budibase analytics. Thank you for helping us make budibase better!"
)
)
} catch (err) {
console.log(
error("Error opting in to budibase analytics. Please try again later.")
)
} }
} }
@ -38,23 +44,16 @@ async function status() {
try { try {
console.log(success(`Budibase analytics ${client.status()}`)) console.log(success(`Budibase analytics ${client.status()}`))
} catch (err) { } catch (err) {
console.log(error("Error fetching analytics status. Please try again later.")) console.log(
error("Error fetching analytics status. Please try again later.")
)
} }
} }
const command = new Command(`${CommandWords.ANALYTICS}`) const command = new Command(`${CommandWords.ANALYTICS}`)
.addHelp("Control the analytics you send to budibase.") .addHelp("Control the analytics you send to budibase.")
.addSubOption( .addSubOption("--optin", "Opt in to sending analytics to budibase", optIn)
"--optin", .addSubOption("--optout", "Opt out of sending analytics to budibase.", optOut)
"Opt in to sending analytics to budibase",
optIn
)
.addSubOption(
"--optout",
"Opt out of sending analytics to budibase.",
optOut
)
.addSubOption( .addSubOption(
"--status", "--status",
"Check whether you are currently opted in to budibase analytics.", "Check whether you are currently opted in to budibase analytics.",

View File

@ -12,9 +12,8 @@ exports.InitTypes = {
exports.AnalyticsEvents = { exports.AnalyticsEvents = {
OptOut: "analytics_opt_out", OptOut: "analytics_opt_out",
OptIn: "analytics_opt_in", OptIn: "analytics_opt_in",
SelfHostInit: "hosting_init" SelfHostInit: "hosting_init",
} }
exports.BUDIBASE_POSTHOG_URL = "https://posthog.budi.live" exports.BUDIBASE_POSTHOG_URL = "https://posthog.budi.live"
exports.BUDIBASE_POSTHOG_TOKEN = process.env.BUDIBASE_POSTHOG_TOKEN exports.BUDIBASE_POSTHOG_TOKEN = process.env.BUDIBASE_POSTHOG_TOKEN

View File

@ -77,8 +77,8 @@ async function init(type) {
distinctId: "cli", distinctId: "cli",
event: AnalyticsEvents.SelfHostInit, event: AnalyticsEvents.SelfHostInit,
properties: { properties: {
type type,
} },
}) })
await downloadFiles() await downloadFiles()
const config = isQuick ? makeEnv.QUICK_CONFIG : {} const config = isQuick ? makeEnv.QUICK_CONFIG : {}

View File

@ -6,16 +6,21 @@ const { error } = require("../utils")
class ConfigManager { class ConfigManager {
constructor() { constructor() {
this.path = path.join(os.homedir(), ".budibase.json") this.path = path.join(os.homedir(), ".budibase.json")
if (!fs.existsSync(this.path)) { if (!fs.existsSync(this.path)) {
fs.writeFileSync(this.path, "{}") fs.writeFileSync(this.path, "{}")
} }
} }
get config() { get config() {
try { try {
return JSON.parse(fs.readFileSync(this.path, "utf8")) return JSON.parse(fs.readFileSync(this.path, "utf8"))
} catch (err) { } catch (err) {
console.log(error(("Error parsing configuration file. Please check your .budibase.json is valid."))) console.log(
error(
"Error parsing configuration file. Please check your .budibase.json is valid."
)
)
return {}
} }
} }
@ -30,7 +35,7 @@ class ConfigManager {
setValue(key, value) { setValue(key, value) {
const updated = { const updated = {
...this.config, ...this.config,
[key]: value [key]: value,
} }
this.config = updated this.config = updated
} }
@ -42,4 +47,4 @@ class ConfigManager {
} }
} }
module.exports = ConfigManager module.exports = ConfigManager