Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use esbuild to bundle firebase functions rewritten in TypeScipt #539

Merged
merged 2 commits into from
Oct 14, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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