From 01525a1d3aa5bd603b133e3b096bdbdbd1d3e3ce Mon Sep 17 00:00:00 2001 From: Paul Gschwendtner Date: Tue, 18 Apr 2017 01:06:17 +0200 Subject: [PATCH] build: move functions folder into tools/ * Moves the `functions/` folder in the root to the `tools/` directory. --- firebase.json | 3 +++ functions/package.json | 11 ----------- package.json | 5 ++--- tools/gulp/util/firebase.ts | 5 +++-- .../screenshot-test/functions}/config.json | 0 tools/screenshot-test/functions/github.ts | 9 +++------ tools/screenshot-test/functions/index.js | 17 +++++++++++++++++ tools/screenshot-test/functions/jwt-util.ts | 5 +++-- tools/screenshot-test/functions/package.json | 13 +++++++++++++ .../{index.ts => screenshot-functions.ts} | 2 +- tools/screenshot-test/functions/tsconfig.json | 7 ++----- tools/screenshot-test/functions/util/jwt.ts | 5 +---- 12 files changed, 48 insertions(+), 34 deletions(-) delete mode 100644 functions/package.json rename {functions => tools/screenshot-test/functions}/config.json (100%) create mode 100644 tools/screenshot-test/functions/index.js create mode 100644 tools/screenshot-test/functions/package.json rename tools/screenshot-test/functions/{index.ts => screenshot-functions.ts} (99%) diff --git a/firebase.json b/firebase.json index e01fb339a8d6..fff3ac5bec08 100644 --- a/firebase.json +++ b/firebase.json @@ -1,4 +1,7 @@ { + "functions": { + "source": "tools/screenshot-test/functions" + }, "hosting": { "public": "dist", "rewrites": [ diff --git a/functions/package.json b/functions/package.json deleted file mode 100644 index cd9f629113fe..000000000000 --- a/functions/package.json +++ /dev/null @@ -1,11 +0,0 @@ -{ - "name": "angular-material2-functions", - "description": "Angular Material2 screenshot test functions", - "dependencies": { - "@google-cloud/storage": "^0.8.0", - "firebase-admin": "^4.1.3", - "firebase-functions": "^0.5.2", - "jsonwebtoken": "^7.3.0", - "request": "^2.81.0" - } -} diff --git a/package.json b/package.json index 8b5a03671c6c..bb099baab50f 100644 --- a/package.json +++ b/package.json @@ -62,9 +62,8 @@ "dgeni": "^0.4.7", "dgeni-packages": "^0.16.5", "firebase": "^3.7.2", - "firebase-admin": "^4.1.2", - "firebase-functions": "^0.5.2", - "firebase-tools": "^2.2.1", + "firebase-admin": "~4.1.2", + "firebase-tools": "^3.6.1", "fs-extra": "^2.0.0", "glob": "^7.1.1", "google-closure-compiler": "^20170218.0.0", diff --git a/tools/gulp/util/firebase.ts b/tools/gulp/util/firebase.ts index 554caeeba100..1376a5566ac3 100644 --- a/tools/gulp/util/firebase.ts +++ b/tools/gulp/util/firebase.ts @@ -2,7 +2,8 @@ const firebaseAdmin = require('firebase-admin'); const firebase = require('firebase'); const gcloud = require('google-cloud'); -const config = require('../../../functions/config.json'); +// Firebase configuration for the Screenshot project. Use the config from the screenshot functions. +const screenshotFirebaseConfig = require('../../screenshot-test/functions/config.json'); /** Opens a connection to the firebase realtime database. */ export function openFirebaseDashboardDatabase() { @@ -51,6 +52,6 @@ export function decode(str: string): string { * This connection is client side connection with no credentials */ export function connectFirebaseScreenshots() { - return firebase.initializeApp(config.firebase); + return firebase.initializeApp(screenshotFirebaseConfig.firebase); } diff --git a/functions/config.json b/tools/screenshot-test/functions/config.json similarity index 100% rename from functions/config.json rename to tools/screenshot-test/functions/config.json diff --git a/tools/screenshot-test/functions/github.ts b/tools/screenshot-test/functions/github.ts index ff2caf017666..87e1fbddb6e4 100644 --- a/tools/screenshot-test/functions/github.ts +++ b/tools/screenshot-test/functions/github.ts @@ -18,14 +18,11 @@ export function updateGithubStatus(event: firebaseFunctions.Event) { return; } let result = event.data.val() == true; - let prNumber = event.params.prNumber; - return setGithubStatus(event.params.sha, - { + let {prNumber, sha} = event.params; + return setGithubStatus(sha, { result: result, name: toolName, description: `${toolName} ${result ? 'passed' : 'failed'}`, url: `http://${authDomain}/${prNumber}` - }, - repoSlug, - token); + }, repoSlug, token); } diff --git a/tools/screenshot-test/functions/index.js b/tools/screenshot-test/functions/index.js new file mode 100644 index 000000000000..6d468ed3ee1f --- /dev/null +++ b/tools/screenshot-test/functions/index.js @@ -0,0 +1,17 @@ +/** + * Entry point for the Firebase functions of the Screenshot tool. Firebase functions only support + * JavaScript files and therefore the TypeScript files needs to be transpiled. Using ts-node + * seems to be more elegant for now, because Firebase requires the `node_modules` to be copied + * to the output directory and when using TSC the `node_modules` won't be copied to the destination. + */ + +'use strict'; + +const path = require('path'); + +// Enable TypeScript compilation at runtime using ts-node. +require('ts-node').register({ + project: path.join(__dirname, 'tsconfig.json') +}); + +require('./screenshot-functions'); diff --git a/tools/screenshot-test/functions/jwt-util.ts b/tools/screenshot-test/functions/jwt-util.ts index 73571275089e..12b6fcab0379 100644 --- a/tools/screenshot-test/functions/jwt-util.ts +++ b/tools/screenshot-test/functions/jwt-util.ts @@ -13,7 +13,8 @@ const secret = firebaseFunctions.config().secret.key; * Replace '/' with '.' to get the token. */ function getSecureToken(event: firebaseFunctions.Event) { - return `${event.params.jwtHeader}.${event.params.jwtPayload}.${event.params.jwtSignature}`; + let {jwtHeader, jwtPayload, jwtSignature} = event.params; + return `${jwtHeader}.${jwtPayload}.${jwtSignature}`; } /** @@ -22,7 +23,7 @@ function getSecureToken(event: firebaseFunctions.Event) { */ export function verifySecureToken(event: firebaseFunctions.Event) { return new Promise((resolve, reject) => { - const prNumber = event.params.prNumber; + const prNumber = event.params['prNumber']; const secureToken = getSecureToken(event); return verifyJWT(secureToken, prNumber, secret, repoSlug).then(() => { diff --git a/tools/screenshot-test/functions/package.json b/tools/screenshot-test/functions/package.json new file mode 100644 index 000000000000..4246b1762ff5 --- /dev/null +++ b/tools/screenshot-test/functions/package.json @@ -0,0 +1,13 @@ +{ + "name": "material2-screenshot-functions", + "description": "Angular Material screenshot firebase functions", + "dependencies": { + "@google-cloud/storage": "^0.8.0", + "firebase-admin": "~4.1.3", + "firebase-functions": "^0.5.2", + "jsonwebtoken": "^7.3.0", + "request": "^2.81.0", + "typescript": "^2.2.2", + "ts-node": "^3.0.2" + } +} diff --git a/tools/screenshot-test/functions/index.ts b/tools/screenshot-test/functions/screenshot-functions.ts similarity index 99% rename from tools/screenshot-test/functions/index.ts rename to tools/screenshot-test/functions/screenshot-functions.ts index 977412e99af6..51cd885c50ff 100644 --- a/tools/screenshot-test/functions/index.ts +++ b/tools/screenshot-test/functions/screenshot-functions.ts @@ -46,7 +46,7 @@ import {updateGithubStatus} from './github'; * The JWT has 3 parts: header, payload and signature. These three parts are joint by '/' in path. */ -// Initailize the admin app +// Initialize the admin app firebaseAdmin.initializeApp(firebaseFunctions.config().firebase); /** The valid data types database accepts */ diff --git a/tools/screenshot-test/functions/tsconfig.json b/tools/screenshot-test/functions/tsconfig.json index cf893515ca49..8867f9e2cf5b 100644 --- a/tools/screenshot-test/functions/tsconfig.json +++ b/tools/screenshot-test/functions/tsconfig.json @@ -1,6 +1,6 @@ { "compilerOptions": { - "lib": ["es6", "es2016", "dom"], + "lib": ["es2015", "es2016", "dom"], "module": "commonjs", "moduleResolution": "node", "noEmitOnError": true, @@ -8,10 +8,7 @@ "sourceMap": true, "target": "es5", "baseUrl": "", - "outDir": "../../../functions/", - "typeRoots": [ - "../../../functions/node_modules/@types/!(node)" - ] + "outDir": "../../../dist/screenshot-functions/" }, "files": [ "./index.ts" diff --git a/tools/screenshot-test/functions/util/jwt.ts b/tools/screenshot-test/functions/util/jwt.ts index 04b43562532d..3cc97bac655a 100644 --- a/tools/screenshot-test/functions/util/jwt.ts +++ b/tools/screenshot-test/functions/util/jwt.ts @@ -1,9 +1,6 @@ import * as jwt from 'jsonwebtoken'; -export function verifyJWT(token: string, - prNumber: string, - secret: string, - repoSlug: string) { +export function verifyJWT(token: string, prNumber: string, secret: string, repoSlug: string) { return new Promise((resolve, reject) => { jwt.verify(token, secret, {issuer: 'Travis CI, GmbH'}, (err: any, payload: any) => { if (err) {