UNPKG

@newos/cli

Version:
50 lines 2.24 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); /// <reference lib="dom" /> - Necessary for firebase types. const app_1 = __importDefault(require("firebase/app")); require("firebase/auth"); require("firebase/firestore"); const FIREBASE_CONFIG = { apiKey: 'AIzaSyAZoGB2p26UejODezZPmczgwehI6xlSKPs', authDomain: 'cli-telemetry.firebaseapp.com', databaseURL: 'https://cli-telemetry.firebaseio.com', projectId: 'cli-telemetry', storageBucket: '', messagingSenderId: '449985678611', appId: '1:449985678611:web:88b411adc68e6521e19ee6', }; process.once('message', async function ({ uuid, commandData, userEnvironment }) { const unixWeek = Math.floor(Date.now() / (1000 * 60 * 60 * 24 * 7)); // Initialize Firebase and anonymously authenticate const app = app_1.default.initializeApp(FIREBASE_CONFIG); try { const db = app.firestore(); const { FieldValue } = app_1.default.firestore; await app.auth().signInAnonymously(); // create a new command document for the current uuid await db.runTransaction(async (tx) => { const dbSnapshot = await tx.get(db.doc(`users/${uuid}`)); let incrementalId; // if the current user document exists, retreive the latest command id and create a new command document. // otherwise, create a document for the user and set the id to 0. if (dbSnapshot.exists) { incrementalId = dbSnapshot.get('latestId') + 1; await tx.update(db.doc(`users/${uuid}`), { latestId: FieldValue.increment(1) }); } else { incrementalId = 0; await tx.set(db.doc(`users/${uuid}`), { latestId: 0 }); } await tx.set(db.collection(`users/${uuid}/commands`).doc(), Object.assign(Object.assign({}, commandData), { userEnvironment, unixWeek, id: incrementalId })); }); } finally { // close all connections await app.delete(); } }); //# sourceMappingURL=send-to-firebase.js.map