[TASK] Initial commit: basic firebase setup and part 1 almost solved (of-by-one)

This commit is contained in:
2021-12-01 09:23:36 +01:00
commit eb211e1ba3
18 changed files with 5033 additions and 0 deletions

5
.firebaserc Normal file
View File

@@ -0,0 +1,5 @@
{
"projects": {
"default": "advent-of-code-2021-911a8"
}
}

66
.gitignore vendored Normal file
View File

@@ -0,0 +1,66 @@
# Logs
logs
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*
firebase-debug.log*
firebase-debug.*.log*
# Firebase cache
.firebase/
# Firebase config
# Uncomment this if you'd like others to create their own Firebase project.
# For a team working on the same Firebase project(s), it is recommended to leave
# it commented so all members can deploy to the same project(s) in .firebaserc.
# .firebaserc
# Runtime data
pids
*.pid
*.seed
*.pid.lock
# Directory for instrumented libs generated by jscoverage/JSCover
lib-cov
# Coverage directory used by tools like istanbul
coverage
# nyc test coverage
.nyc_output
# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files)
.grunt
# Bower dependency directory (https://bower.io/)
bower_components
# node-waf configuration
.lock-wscript
# Compiled binary addons (http://nodejs.org/api/addons.html)
build/Release
# Dependency directories
node_modules/
# Optional npm cache directory
.npm
# Optional eslint cache
.eslintcache
# Optional REPL history
.node_repl_history
# Output of 'npm pack'
*.tgz
# Yarn Integrity file
.yarn-integrity
# dotenv environment variables file
.env

5
.idea/.gitignore generated vendored Normal file
View File

@@ -0,0 +1,5 @@
# Default ignored files
/shelf/
/workspace.xml
# Editor-based HTTP Client requests
/httpRequests/

12
.idea/advent-of-code-2021-firebase.iml generated Normal file
View File

@@ -0,0 +1,12 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="WEB_MODULE" version="4">
<component name="NewModuleRootManager">
<content url="file://$MODULE_DIR$">
<excludeFolder url="file://$MODULE_DIR$/temp" />
<excludeFolder url="file://$MODULE_DIR$/.tmp" />
<excludeFolder url="file://$MODULE_DIR$/tmp" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>

8
.idea/modules.xml generated Normal file
View File

@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/.idea/advent-of-code-2021-firebase.iml" filepath="$PROJECT_DIR$/.idea/advent-of-code-2021-firebase.iml" />
</modules>
</component>
</project>

6
.idea/vcs.xml generated Normal file
View File

@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="$PROJECT_DIR$" vcs="Git" />
</component>
</project>

8
firebase.json Normal file
View File

@@ -0,0 +1,8 @@
{
"functions": {
"predeploy": [
"npm --prefix \"$RESOURCE_DIR\" run lint",
"npm --prefix \"$RESOURCE_DIR\" run build"
]
}
}

31
functions/.eslintrc.js Normal file
View File

@@ -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
functions/.gitignore vendored Normal file
View File

@@ -0,0 +1,9 @@
# Compiled JavaScript files
lib/**/*.js
lib/**/*.js.map
# TypeScript v1 declaration files
typings/
# Node.js dependency directory
node_modules/

30
functions/package.json Normal file
View File

@@ -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
}

13
functions/src/day1.ts Normal file
View File

@@ -0,0 +1,13 @@
class Day1 {
static part1(input: string[]): number {
let res = 0;
for (let i = 1; i < input.length; i++) {
if (input[i] > input[i-1]) res++;
}
return res;
}
}
export default Day1;

35
functions/src/index.ts Normal file
View File

@@ -0,0 +1,35 @@
import * as functions from "firebase-functions";
import Utils from "./utils";
import Day1 from "./day1";
import {Response} 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!");
// });
interface DayResult {
part1: any;
part2: any;
}
export const day = {
1: functions.region('europe-west1').https.onRequest((request, response) => {
const input = Utils.parseInput(request);
const part1 = Day1.part1(input);
sendResponse(response, part1, 0);
}),
}
function sendResponse(response: Response, part1: any, part2: any) {
const res: DayResult = { part1, part2 };
response.send(res);
}

19
functions/src/utils.ts Normal file
View File

@@ -0,0 +1,19 @@
import {Request} from "firebase-functions/lib/common/providers/https";
class Utils {
static parseInput(request: Request): string[] {
const body = request.body;
if (typeof body === 'string') {
return body.split('\n');
} else if (body.constructor === Array) {
return body;
} else {
throw Error("Invalid request");
}
}
}
export default Utils;

View File

@@ -0,0 +1,5 @@
{
"include": [
".eslintrc.js"
]
}

15
functions/tsconfig.json Normal file
View File

@@ -0,0 +1,15 @@
{
"compilerOptions": {
"module": "commonjs",
"noImplicitReturns": true,
"noUnusedLocals": true,
"outDir": "lib",
"sourceMap": true,
"strict": true,
"target": "es2017"
},
"compileOnSave": true,
"include": [
"src"
]
}

2746
functions/yarn.lock Normal file

File diff suppressed because it is too large Load Diff

15
input/day/1-example.http Normal file
View File

@@ -0,0 +1,15 @@
POST http://localhost:5001/advent-of-code-2021-911a8/europe-west1/day-1
Content-Type: text/plain
199
200
208
210
200
207
240
269
260
263
###

2005
input/day/1.http Normal file

File diff suppressed because it is too large Load Diff