UNPKG

realm-object-server

Version:

Realm Object Server

100 lines 4.7 kB
"use strict"; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; Object.defineProperty(exports, "__esModule", { value: true }); const fs = require("fs-extra"); const path = require("path"); const promptly = require("promptly"); const mixpanel_1 = require("./shared/mixpanel"); const uuid = require("uuid"); function validateEmail(email) { const re = /^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/; const passesTest = re.test(email); if (!passesTest) { throw new Error("Please enter a valid email"); } return email; } function checkExistingTerms(tosPath, terms) { return __awaiter(this, void 0, void 0, function* () { const jsonExists = yield fs.pathExists(tosPath); if (!jsonExists) { return; } const agreementFile = yield fs.readFile(tosPath); const agreement = JSON.parse(agreementFile.toString()); const exampleAgreement = { email: "realm@realm.io", terms: terms, agreeToLearnAboutUpdatesAndFixes: true, distinctId: "some-identifier", }; const requiredKeys = ["email", "terms", "agreeToLearnAboutUpdatesAndFixes"]; for (const key of requiredKeys) { if (!(key in agreement)) { throw new Error("Invalid format of 'realm-object-server-agreement.json'. Expected something like:\n " + JSON.stringify(exampleAgreement, null, 4) + "\nPlease delete that file and try again."); } } const agreementEmail = agreement["email"]; try { validateEmail(agreementEmail); } catch (Error) { throw new Error("Invalid email format in 'realm-object-server-agreement.json': " + agreementEmail); } if (agreement["terms"] !== terms) { throw new Error("Invalid terms URL. Must be: " + terms); } mixpanel_1.mixpanel.identify(agreement.distinctId, agreementEmail); return agreement; }); } function obtainAgreement(tosPath, terms) { return __awaiter(this, void 0, void 0, function* () { let email; let agreeToLearnAboutUpdatesAndFixes = false; if (process.env.DOCKER_DATA_PATH) { email = validateEmail(process.env.ROS_TOS_EMAIL_ADDRESS); } else { console.log("Please agree to our terms of service by signing with your email. You can read the full terms at: " + terms); email = yield promptly.prompt("Agree with your email: ", { validator: validateEmail }); agreeToLearnAboutUpdatesAndFixes = yield promptly.confirm("Would you like to receive product updates and updates about critical bug fixes? (Y/N)"); } const distinctId = uuid.v4(); const agreement = { email, terms, distinctId, agreeToLearnAboutUpdatesAndFixes }; yield fs.mkdirp(path.dirname(tosPath)); yield fs.writeFile(tosPath, JSON.stringify(agreement, null, 4), { encoding: "utf8" }); mixpanel_1.mixpanel.identify(agreement.distinctId, email); mixpanel_1.mixpanel.track("ros_agreed_to_terms", { version: (yield fs.readJson(path.join(__dirname, "../package.json"))).version, email, agreeToLearnAboutUpdatesAndFixes, }); return agreement; }); } function agreeToTerms() { return __awaiter(this, void 0, void 0, function* () { if (process.env.ROS_SKIP_PROMPTS) { return; } const terms = "https://realm.io/legal/developer-license-terms/"; const homeDir = process.env.HOME || "/"; const tosPath = process.env.DOCKER_DATA_PATH ? path.resolve(process.env.DOCKER_DATA_PATH, "realm-object-server-agreement.json") : path.resolve(homeDir, ".realm", "realm-object-server-agreement.json"); (yield checkExistingTerms(tosPath, terms)) || (yield obtainAgreement(tosPath, terms)); }); } exports.agreeToTerms = agreeToTerms; //# sourceMappingURL=termsofservice.js.map