UNPKG

@grouparoo/core

Version:
75 lines (74 loc) 3.32 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.ConfigUser = void 0; const fs_1 = __importDefault(require("fs")); const path_1 = __importDefault(require("path")); const pluginDetails_1 = require("../modules/pluginDetails"); const grouparooSubscription_1 = require("./grouparooSubscription"); const plugin_1 = require("../modules/plugin"); const Setting_1 = require("../models/Setting"); const runMode_1 = require("./runMode"); var ConfigUser; (function (ConfigUser) { async function localUserFilePath() { const configDir = await (0, pluginDetails_1.getConfigDir)(true); return path_1.default.join(configDir, "../.local/user.json"); } ConfigUser.localUserFilePath = localUserFilePath; async function store(content) { var _a; const previousContent = (_a = (await get())) !== null && _a !== void 0 ? _a : {}; const localFilePath = await localUserFilePath(); const localFileDir = path_1.default.dirname(localFilePath); if (!fs_1.default.existsSync(localFileDir)) { fs_1.default.mkdirSync(localFileDir, { recursive: true }); } const fileContent = { ...previousContent, ...content }; fs_1.default.writeFileSync(localFilePath, JSON.stringify(fileContent, null, 2)); } async function subscribe(email, subscribed = true) { await (0, grouparooSubscription_1.GrouparooSubscription)({ email, subscribed }); } async function storeCompanyName(company) { let setting = await Setting_1.Setting.findOne({ where: { key: "cluster-name" } }); if (setting) { setting = await plugin_1.plugin.updateSetting(setting.pluginName, setting.key, company); } return setting; } async function loadOrStoreCustomerId() { const user = await get(); if (user === null || user === void 0 ? void 0 : user.customerId) { await plugin_1.plugin.updateSetting("telemetry", "customer-id", user.customerId); } else { const setting = await plugin_1.plugin.readSetting("telemetry", "customer-id"); await store({ customerId: setting.value }); } } ConfigUser.loadOrStoreCustomerId = loadOrStoreCustomerId; async function create({ email, subscribed = true, company, }) { if ((0, runMode_1.getGrouparooRunMode)() !== "cli:config") return; await store({ email: true }); await subscribe(email, subscribed); await storeCompanyName(company); } ConfigUser.create = create; async function get() { const localFilePath = await localUserFilePath(); if (!fs_1.default.existsSync(localFilePath)) return null; const fileContent = fs_1.default.readFileSync(localFilePath).toString(); return JSON.parse(fileContent); } ConfigUser.get = get; async function isAuthenticated() { const user = await get(); return Boolean((user === null || user === void 0 ? void 0 : user.email) === true); } ConfigUser.isAuthenticated = isAuthenticated; })(ConfigUser = exports.ConfigUser || (exports.ConfigUser = {}));