Skip to content

Commit

Permalink
Use esbuild to bundle firebase functions rewritten in TypeScipt (#539)
Browse files Browse the repository at this point in the history
  • Loading branch information
SamChou19815 authored Oct 14, 2021
1 parent aed8cdc commit 460142c
Show file tree
Hide file tree
Showing 8 changed files with 543 additions and 336 deletions.
1 change: 1 addition & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@
/config/
dist/
/*.js
functions/deployment-template/index.js
2 changes: 1 addition & 1 deletion .github/workflows/ci-check.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ dist
.vscode
.firebase
requirement-generator-dist/
functions/deployment-template/index.js
.eslintcache

# local env files
Expand Down
4 changes: 4 additions & 0 deletions firebase.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,9 @@
"destination": "/index.html"
}
]
},
"functions": {
"source": "functions/deployment-template",
"runtime": "nodejs14"
}
}
9 changes: 9 additions & 0 deletions functions/deployment-template/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"name": "deployment-template",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"deploy": "firebase deploy --only functions"
}
}
32 changes: 17 additions & 15 deletions functions/index.js → functions/index.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
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();
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;
Expand All @@ -24,7 +22,8 @@ function typeToMonth(type) {
throw new Error();
}
}
function isOld(semester) {

function isOld(semester: { year: number; type: string }) {
const currentTime = new Date();
const month = currentTime.getMonth() + 1;
const year = currentTime.getFullYear();
Expand All @@ -43,7 +42,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: readonly { readonly acronym: string }[],
freqDict: Record<string, number>
) {
// if no colleges/programs/majors/minors for this doc, skip
if (!categories) {
return;
Expand All @@ -61,7 +63,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<string, number>) {
if (!year) {
return;
}
Expand Down Expand Up @@ -137,13 +139,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<string, number> = {};
const programFreq: Record<string, number> = {};
const majorFreq: Record<string, number> = {};
const minorFreq: Record<string, number> = {};

const entranceYearFreq = {};
const gradYearFreq = {};
const entranceYearFreq: Record<string, number> = {};
const gradYearFreq: Record<string, number> = {};

onboardingQuerySnapshot.forEach(doc => {
const { majors, minors, exam, colleges, gradPrograms } = doc.data();
Expand Down
Loading

0 comments on commit 460142c

Please sign in to comment.