adding abstraction for config manager
This commit is contained in:
parent
865a9673ba
commit
b5c32f8488
|
@ -0,0 +1,35 @@
|
|||
const PostHog = require("posthog-node")
|
||||
const path = require("path")
|
||||
const fs = require("fs")
|
||||
const os = require("os")
|
||||
const { BUDIBASE_POSTHOG_URL, BUDIBASE_POSTHOG_TOKEN, AnalyticsEvents } = require("../constants")
|
||||
const ConfigManager = require("../structures/ConfigManager")
|
||||
|
||||
class AnalyticsClient {
|
||||
constructor() {
|
||||
this.client = new PostHog(BUDIBASE_POSTHOG_TOKEN, { host: BUDIBASE_POSTHOG_URL })
|
||||
this.configManager = new ConfigManager()
|
||||
}
|
||||
|
||||
capture(event) {
|
||||
if (this.manager.config.analyticsDisabled) return
|
||||
|
||||
this.client.capture(event)
|
||||
}
|
||||
|
||||
enable() {
|
||||
this.configManager.removeKey("analyticsDisabled")
|
||||
this.client.capture({ event: AnalyticsEvents.OptIn, distinctId: "cli" })
|
||||
}
|
||||
|
||||
disable() {
|
||||
this.client.capture({ event: AnalyticsEvents.OptOut, distinctId: "cli" })
|
||||
this.configManager.setValue("analyticsDisabled", true)
|
||||
}
|
||||
|
||||
status() {
|
||||
return this.configManager.config.analyticsDisabled ? "disabled" : "enabled"
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = AnalyticsClient
|
|
@ -1,71 +1,26 @@
|
|||
const PostHog = require("posthog-node")
|
||||
const Command = require("../structures/Command")
|
||||
const { CommandWords, InitTypes } = require("../constants")
|
||||
const { CommandWords, InitTypes, BUDIBASE_POSTHOG_URL } = require("../constants")
|
||||
const { lookpath } = require("lookpath")
|
||||
const {
|
||||
downloadFile,
|
||||
logErrorToFile,
|
||||
success,
|
||||
error,
|
||||
info,
|
||||
parseEnv,
|
||||
} = require("../utils")
|
||||
const { confirmation } = require("../questions")
|
||||
const fs = require("fs")
|
||||
// const makeEnv = require("./makeEnv")
|
||||
const axios = require("axios")
|
||||
|
||||
const Events = {
|
||||
OptOut: "opt_out",
|
||||
OptIn: "opt_in",
|
||||
}
|
||||
|
||||
class AnalyticsClient {
|
||||
constructor() {
|
||||
this.client = new PostHog("Oeq9KzIpZYaNsXIvHw5QTZWNpfiG_EOjAOpjTyAiitY", { host: "https://posthog.budi.live" })
|
||||
}
|
||||
|
||||
capture() {
|
||||
if (!enabled) return
|
||||
|
||||
this.client.capture({
|
||||
event: Events.OptOut
|
||||
})
|
||||
}
|
||||
|
||||
enable() {
|
||||
this.disabled = false
|
||||
}
|
||||
|
||||
disable() {
|
||||
this.disabled = true
|
||||
}
|
||||
|
||||
status() {
|
||||
return this.disabled ? "disabled" : "enabled"
|
||||
}
|
||||
}
|
||||
|
||||
class BudibaseConfig {
|
||||
constructor(config) {
|
||||
this.config = config
|
||||
}
|
||||
|
||||
write() {
|
||||
// fs.wri
|
||||
}
|
||||
}
|
||||
const AnalyticsClient = require("./Client")
|
||||
|
||||
const client = new AnalyticsClient()
|
||||
|
||||
|
||||
async function optOut() {
|
||||
client.capture({ event: Events.OptOut })
|
||||
client.disable()
|
||||
try {
|
||||
// opt them out
|
||||
client.disable()
|
||||
console.log(success("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."))
|
||||
console.log(error("Error opting out of budibase analytics. Please try again later.", err))
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -73,8 +28,7 @@ async function optIn() {
|
|||
try {
|
||||
// opt them in
|
||||
client.enable()
|
||||
client.capture({ event: Events.OptIn })
|
||||
console.log(success("Successfully opted in of budibase analytics. Thank you for helping us make budibase better!"))
|
||||
console.log(success("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."))
|
||||
}
|
||||
|
|
|
@ -8,3 +8,13 @@ exports.InitTypes = {
|
|||
QUICK: "quick",
|
||||
DIGITAL_OCEAN: "do",
|
||||
}
|
||||
|
||||
exports.AnalyticsEvents = {
|
||||
OptOut: "analytics_opt_out",
|
||||
OptIn: "analytics_opt_in",
|
||||
SelfHostInit: "hosting_init"
|
||||
}
|
||||
|
||||
|
||||
exports.BUDIBASE_POSTHOG_URL = "https://posthog.budi.live"
|
||||
exports.BUDIBASE_POSTHOG_TOKEN = process.env.BUDIBASE_POSTHOG_TOKEN
|
|
@ -1,5 +1,5 @@
|
|||
const Command = require("../structures/Command")
|
||||
const { CommandWords, InitTypes } = require("../constants")
|
||||
const { CommandWords, InitTypes, AnalyticsEvents } = require("../constants")
|
||||
const { lookpath } = require("lookpath")
|
||||
const {
|
||||
downloadFile,
|
||||
|
@ -13,6 +13,7 @@ const fs = require("fs")
|
|||
const compose = require("docker-compose")
|
||||
const makeEnv = require("./makeEnv")
|
||||
const axios = require("axios")
|
||||
const AnalyticsClient = require("../analytics/Client")
|
||||
|
||||
const BUDIBASE_SERVICES = ["app-service", "worker-service"]
|
||||
const ERROR_FILE = "docker-error.log"
|
||||
|
@ -22,6 +23,8 @@ const FILE_URLS = [
|
|||
]
|
||||
const DO_USER_DATA_URL = "http://169.254.169.254/metadata/v1/user-data"
|
||||
|
||||
const client = new AnalyticsClient()
|
||||
|
||||
async function downloadFiles() {
|
||||
const promises = []
|
||||
for (let url of FILE_URLS) {
|
||||
|
@ -70,6 +73,12 @@ async function init(type) {
|
|||
return
|
||||
}
|
||||
}
|
||||
client.capture({
|
||||
event: AnalyticsEvents.SelfHostInit,
|
||||
properties: {
|
||||
type
|
||||
}
|
||||
})
|
||||
await downloadFiles()
|
||||
const config = isQuick ? makeEnv.QUICK_CONFIG : {}
|
||||
if (type === InitTypes.DIGITAL_OCEAN) {
|
||||
|
|
|
@ -0,0 +1,45 @@
|
|||
const fs = require("fs")
|
||||
const path = require("path")
|
||||
const os = require("os")
|
||||
const { error } = require("../utils")
|
||||
|
||||
class ConfigManager {
|
||||
constructor() {
|
||||
this.path = path.join(os.homedir(), ".budibase.json")
|
||||
if (!fs.existsSync(this.path)) {
|
||||
fs.writeFileSync(this.path, "{}")
|
||||
}
|
||||
}
|
||||
|
||||
get config() {
|
||||
try {
|
||||
return JSON.parse(fs.readFileSync(this.path, "utf8"))
|
||||
} catch (err) {
|
||||
console.log(error(("Error parsing configuration file. Please check your .budibase.json is valid.")))
|
||||
}
|
||||
}
|
||||
|
||||
set config(json) {
|
||||
fs.writeFileSync(this.path, JSON.stringify(json))
|
||||
}
|
||||
|
||||
getValue(key) {
|
||||
return this.config[key]
|
||||
}
|
||||
|
||||
setValue(key, value) {
|
||||
const updated = {
|
||||
...this.config,
|
||||
[key]: value
|
||||
}
|
||||
this.config = updated
|
||||
}
|
||||
|
||||
removeKey(key) {
|
||||
const updated = { ...this.config }
|
||||
delete updated[key]
|
||||
this.config = updated
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = ConfigManager
|
|
@ -56,3 +56,11 @@ exports.parseEnv = env => {
|
|||
}
|
||||
return result
|
||||
}
|
||||
|
||||
exports.getConfigValue = (key, value) => {
|
||||
|
||||
}
|
||||
|
||||
exports.setConfigValue = (key, value) => {
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue