diff --git a/.eslintignore b/.eslintignore index 4e147fe1a..72dfb1006 100644 --- a/.eslintignore +++ b/.eslintignore @@ -2,3 +2,4 @@ /config/ dist/ /*.js +functions/deployment-template/index.js diff --git a/.github/workflows/ci-check.yml b/.github/workflows/ci-check.yml index f0fd16726..7e8926d2e 100644 --- a/.github/workflows/ci-check.yml +++ b/.github/workflows/ci-check.yml @@ -11,7 +11,7 @@ jobs: with: cache: 'npm' - name: NPM Clean Install - run: npm ci + run: npm ci && cd functions && npm ci - name: Run Prettier Check run: npm run format:check - name: Run Linter diff --git a/.gitignore b/.gitignore index 4ffeb283d..561342e37 100644 --- a/.gitignore +++ b/.gitignore @@ -4,6 +4,7 @@ dist .vscode .firebase requirement-generator-dist/ +functions/deployment-template/index.js .eslintcache # local env files diff --git a/firebase.json b/firebase.json index 2c33c295f..b87ffce96 100644 --- a/firebase.json +++ b/firebase.json @@ -12,5 +12,9 @@ "destination": "/index.html" } ] + }, + "functions": { + "source": "functions/deployment-template", + "runtime": "nodejs14" } } diff --git a/functions/deployment-template/package.json b/functions/deployment-template/package.json new file mode 100644 index 000000000..7dae24f8a --- /dev/null +++ b/functions/deployment-template/package.json @@ -0,0 +1,9 @@ +{ + "name": "deployment-template", + "version": "1.0.0", + "description": "", + "main": "index.js", + "scripts": { + "deploy": "firebase deploy --only functions" + } +} diff --git a/functions/index.js b/functions/index.ts similarity index 88% rename from functions/index.js rename to functions/index.ts index 0fb59eaf4..4f7943f96 100644 --- a/functions/index.js +++ b/functions/index.ts @@ -1,7 +1,5 @@ -const functions = require('firebase-functions'); - -// The Firebase Admin SDK to access the Firebase Realtime Database. -const admin = require('firebase-admin'); +import functions from 'firebase-functions'; +import admin from 'firebase-admin'; admin.initializeApp(); const db = admin.firestore(); @@ -9,8 +7,8 @@ const usernameCollection = db.collection('user-name'); const semestersCollection = db.collection('user-semesters'); const onboardingCollection = db.collection('user-onboarding-data'); -const average = array => array.reduce((a, b) => a + b) / array.length; -function typeToMonth(type) { +const average = (array: readonly number[]) => array.reduce((a, b) => a + b) / array.length; +function typeToMonth(type: string) { switch (type) { case 'Spring': return 1; @@ -43,7 +41,10 @@ function isOld(semester) { // add all colleges/programs/majors etc. in arr to frequency dict // TODO this will not show any colleges/grad programs with 0 people. // Need to look at requirements data and add each to the map with 0 frequency to do so. -function addToFrequencyDictionary(categories, freqDict) { +function addToFrequencyDictionary( + categories: { acronym: string }[], + freqDict: Record +) { // if no colleges/programs/majors/minors for this doc, skip if (!categories) { return; @@ -61,7 +62,7 @@ function addToFrequencyDictionary(categories, freqDict) { // adds year to freqDict if not set, otherwise increments frequency by 1 // simplified version of addToFrequencyDictionary for entrance/grad year, as they // are single elements, not lists, and do not have an acronym prop -function addYearToFrequencyDictionary(year, freqDict) { +function addYearToFrequencyDictionary(year: string, freqDict: Record) { if (!year) { return; } @@ -137,13 +138,13 @@ exports.TrackUsers = functions.https.onRequest(async (req, res) => { let totalNumMinors = 0; let totalNumExams = 0; - const collegeFreq = {}; - const programFreq = {}; - const majorFreq = {}; - const minorFreq = {}; + const collegeFreq: Record = {}; + const programFreq: Record = {}; + const majorFreq: Record = {}; + const minorFreq: Record = {}; - const entranceYearFreq = {}; - const gradYearFreq = {}; + const entranceYearFreq: Record = {}; + const gradYearFreq: Record = {}; onboardingQuerySnapshot.forEach(doc => { const { majors, minors, exam, colleges, gradPrograms } = doc.data(); diff --git a/functions/package.json b/functions/package.json index 70385d360..df87785ae 100644 --- a/functions/package.json +++ b/functions/package.json @@ -4,15 +4,13 @@ "private": true, "scripts": { "lint": "eslint .", + "predeploy": "../node_modules/.bin/esbuild --bundle --platform=node --outdir=deployment-template index.ts", "serve": "firebase serve --only functions", "shell": "firebase functions:shell", "start": "npm run shell", - "deploy": "firebase deploy --only functions", + "deploy": "cd deployment-template && npm run deploy", "logs": "firebase functions:log" }, - "engines": { - "node": "12" - }, "dependencies": { "firebase-admin": "^9.5.0", "firebase-functions": "^3.13.1"