Skip to content

Commit

Permalink
feat: deploy
Browse files Browse the repository at this point in the history
  • Loading branch information
Derek Burgman committed Jan 22, 2022
1 parent 5e3c81c commit 30a654c
Show file tree
Hide file tree
Showing 18 changed files with 1,209 additions and 312 deletions.
7 changes: 7 additions & 0 deletions .firebase/hosting.ZGlzdC9wYWNrYWdlcy9kZW1v.cache
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
3rdpartylicenses.txt,1642813839869,61a8419025987138586daeed338926b25a20c2eecb50ba2599342a8331de3dbf
favicon.ico,1642813840360,6be3a6e89960570770439ecf45643daf2ac471a280e6e50694d57b7b852d8b38
index.html,1642813840371,b8209ec76a05e4a745fabb9ff42de48d064c1ac390f7d0f65c891e18f9220b52
main.2b2dd65c704efa8a.js,1642813839869,594db57ef9e1c3a8ee13f3720e72e14652a07de01df6c011cf1bad43fbc8fd21
polyfills.f14b3c32b995584a.js,1642813839869,158a0900596ec1f407ba4a7569375d237c292e091f99c40da691295c2b0ac239
runtime.b6ab0b2c82468c69.js,1642813839869,8f45b672d343425c9c73195559d68a1c7a31092fb851ef6b643fa5c458295d90
styles.ef46db3751d8e999.css,1642813839869,f1b651238a58fe290baec6c5e32f3bdb1943dd2bd582f02569231f7a757c7837
5 changes: 5 additions & 0 deletions .firebaserc
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"projects": {
"default": "dereekb-components"
}
}
154 changes: 154 additions & 0 deletions NOTES.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@
# Notes
Set of notes relevant to setting up an Nx environment, similar to this one.

# Using Nx
## Creating a NodeJs Library
Be sure to include the `--buildable` and `--publishable` flags if relevant.

## Creating an Angular Library

Example: `nx generate @nrwl/angular:library --name=dbx-firebase --buildable --publishable --importPath @dereekb/dbx-firebase`

# Setting up Firebase for Nx
These steps were use for setting up firebase for the demo components. Inspiration [from here](https://itnext.io/nx-nest-firebase-the-dream-616e8ee71920).

Setting up our workspace to work with Firebase is fairly straightforward. Start by making sure Firebase is [installed globally](https://firebase.google.com/docs/functions/get-started):

> `npm install -g firebase-tools@latest `
Go ahead and also already set up your project on Firebase.

https://console.firebase.google.com/

## Setup
Start by initializing firebase in the root directory:

> `firebase init`
Skip installing node_modules.

Go ahead and delete the created `public` and `functions` directories. We will update the firebase configuration to deploy from the `dist` folders, as configure the build steps.

## Webapp
Create an angular project using ng generate.

Example: `nx generate @nrwl/angular:app --name=demo`

The angular project is now setup properly. When built, its output will go to the `dist/packages/demo` folder.

### Configuring Firebase.json
We just need to update the folder firebase pulls from to instead pull from the webapp's dist folder. Update `firebase.json` by changing the `hosting` section's `public` value to be `dist/packages/demo`.

It should look like this:

```
"hosting": {
"public": "dist/packages/demo",
"ignore": [
"firebase.json",
"**/.*",
"**/node_modules/**"
]
}
```

### Configuring project.json
We can add functions to our `project.json` for `demo` to call build and the firebase functions for deploying to hosting.

Find `targets` in your `project.json`, and add the following targets:

```
"deploy-dist-to-hosting": {
"builder": "@nrwl/workspace:run-commands",
"options": {
"command": "firebase deploy --only hosting"
}
}
```

This will deploy whatever content is configured for `public` in `firebase.json`, which we configured above to be `dist/packages/demo`.

Add the section below to add an action that calls build before calling deploy.

```
"deploy": {
"builder": "@nrwl/workspace:run-commands",
"options": {
"commands": [
{
"command": "nx build demo"
},
{
"command": "nx deploy-dist-to-hosting demo"
}
],
"parallel": false
}
}
```

Now we can deploy our hosting with this command:

> `nx deploy demo`
## NestJs
We will be creating a NestJS project that will be deployed to Google's functions cloud.

Create a NestJS project using the following:

Example: `nx generate @nrwl/nest:application demo-api`

### Setting Up NestJS for Firebase Functions Using onRequest()
Copy the contents of `packages/demo-api/src/main.ts`. This will instruct your app to direct all https requests to

More details here:
https://firebase.google.com/docs/functions/http-events

Alternatively, if you do not want to use onRequest but want to use NestJS, you can use NestJS as a Standalone App (see next section).

### Settings Up NestJS For Firebase Functions (Standalone App)
You can alternatively use NestJS as a standalone app, and response to requests like this:

```
const bootstrap = async (expressInstance: Express) => {
const app = await NestFactory.create(AppModule, expressInstance);
await app.init();
return app;
};
const main = bootstrap(server);
export const subscriptions = functions
.pubsub
.topic('cron-topic')
.onPublish((context, message) => main.then(app => {
return app.get(SubscribeService).initDailyCharges(context, message));
});
```

https://docs.nestjs.com/standalone-applications

https://stackoverflow.com/questions/53307541/firebase-handle-cloud-events-within-nestjs-framework


### Configuring Firebase.json
We just need to update the folder firebase pulls from to instead pull from the webapp's dist folder. Update `firebase.json` by changing the `functions` section's `source` value to be `dist/packages/demo-api`. Also add `runtime` and set it to `nodejs16`.

It should look like this:

```
"functions": {
"source": "dist/packages/demo-api",
"runtime": "nodejs16",
"ignore": [
"firebase.json",
"**/.*",
"**/node_modules/**"
]
},
```

### Emulators

TODO
7 changes: 7 additions & 0 deletions database.rules.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
/* Visit https://firebase.google.com/docs/database/security to learn more about security rules. */
"rules": {
".read": false,
".write": false
}
}
3 changes: 3 additions & 0 deletions deploy-demo-api.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/bin/bash
# Builds and Deploys demo-api to Firebase Functions
nx deploy demo-api
3 changes: 3 additions & 0 deletions deploy-demo.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/bin/bash
# Builds and Deploys demo to Firebase Hosting
nx deploy demo
45 changes: 45 additions & 0 deletions firebase.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
{
"hosting": {
"public": "dist/packages/demo",
"ignore": [
"firebase.json",
"**/.*",
"**/node_modules/**"
]
},
"functions": {
"source": "dist/packages/demo-api",
"runtime": "nodejs16",
"ignore": [
"firebase.json",
"**/.*",
"**/node_modules/**"
]
},
"database": {
"rules": "database.rules.json"
},
"storage": {
"rules": "storage.rules"
},
"emulators": {
"auth": {
"port": 9099
},
"functions": {
"port": 5001
},
"database": {
"port": 9000
},
"pubsub": {
"port": 8085
},
"storage": {
"port": 9199
},
"ui": {
"enabled": true
}
}
}
31 changes: 31 additions & 0 deletions functions/.eslintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
module.exports = {
root: true,
env: {
es6: true,
node: true,
},
extends: [
"eslint:recommended",
"plugin:import/errors",
"plugin:import/warnings",
"plugin:import/typescript",
"google",
"plugin:@typescript-eslint/recommended",
],
parser: "@typescript-eslint/parser",
parserOptions: {
project: ["tsconfig.json", "tsconfig.dev.json"],
sourceType: "module",
},
ignorePatterns: [
"/lib/**/*", // Ignore built files.
],
plugins: [
"@typescript-eslint",
"import",
],
rules: {
"quotes": ["error", "double"],
"import/no-unresolved": 0,
},
};
9 changes: 9 additions & 0 deletions functions/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# Compiled JavaScript files
lib/**/*.js
lib/**/*.js.map

# TypeScript v1 declaration files
typings/

# Node.js dependency directory
node_modules/
30 changes: 30 additions & 0 deletions functions/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
{
"name": "functions",
"scripts": {
"lint": "eslint --ext .js,.ts .",
"build": "tsc",
"serve": "npm run build && firebase emulators:start --only functions",
"shell": "npm run build && firebase functions:shell",
"start": "npm run shell",
"deploy": "firebase deploy --only functions",
"logs": "firebase functions:log"
},
"engines": {
"node": "16"
},
"main": "lib/index.js",
"dependencies": {
"firebase-admin": "^9.8.0",
"firebase-functions": "^3.14.1"
},
"devDependencies": {
"@typescript-eslint/eslint-plugin": "^3.9.1",
"@typescript-eslint/parser": "^3.8.0",
"eslint": "^7.6.0",
"eslint-config-google": "^0.14.0",
"eslint-plugin-import": "^2.22.0",
"firebase-functions-test": "^0.2.0",
"typescript": "^3.8.0"
},
"private": true
}
9 changes: 9 additions & 0 deletions functions/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import * as functions from "firebase-functions";

// // Start writing Firebase Functions
// // https://firebase.google.com/docs/functions/typescript
//
// export const helloWorld = functions.https.onRequest((request, response) => {
// functions.logger.info("Hello logs!", {structuredData: true});
// response.send("Hello from Firebase!");
// });
5 changes: 5 additions & 0 deletions functions/tsconfig.dev.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"include": [
".eslintrc.js"
]
}
15 changes: 15 additions & 0 deletions functions/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"compilerOptions": {
"module": "commonjs",
"noImplicitReturns": true,
"noUnusedLocals": true,
"outDir": "lib",
"sourceMap": true,
"strict": true,
"target": "es2017"
},
"compileOnSave": true,
"include": [
"src"
]
}
Loading

0 comments on commit 30a654c

Please sign in to comment.