Skip to content

Commit

Permalink
Use esbuild to bundle firebase functions
Browse files Browse the repository at this point in the history
  • Loading branch information
SamChou19815 committed Oct 7, 2021
1 parent 08f5222 commit 17805d8
Show file tree
Hide file tree
Showing 7 changed files with 33 additions and 19 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"
}
}
29 changes: 15 additions & 14 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 Down Expand Up @@ -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<string, number>
) {
// if no colleges/programs/majors/minors for this doc, skip
if (!categories) {
return;
Expand All @@ -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<string, number>) {
if (!year) {
return;
}
Expand Down Expand Up @@ -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<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
6 changes: 2 additions & 4 deletions functions/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down

0 comments on commit 17805d8

Please sign in to comment.