From 6d3ca89e67d92df7e93b6a85f4072205f538d1ef Mon Sep 17 00:00:00 2001 From: Bobby Date: Wed, 17 Mar 2021 16:42:18 +1100 Subject: [PATCH] add error log to sparks lib and resolve issues --- ft_build/compiler/index.ts | 24 +++++------ ft_build/compiler/terminal.ts | 70 ++++++++++++++++++++------------- ft_build/compiler/tsconfig.json | 2 +- ft_build/index.ts | 3 +- ft_build/package.json | 2 +- ft_build/tsconfig.json | 2 +- 6 files changed, 59 insertions(+), 44 deletions(-) diff --git a/ft_build/compiler/index.ts b/ft_build/compiler/index.ts index 72dddb1b..e7006fd6 100644 --- a/ft_build/compiler/index.ts +++ b/ft_build/compiler/index.ts @@ -5,12 +5,6 @@ import { commandErrorHandler } from "../utils"; const path = require("path"); import admin from "firebase-admin"; -async function asyncForEach(array: any[], callback: Function) { - for (let index = 0; index < array.length; index++) { - await callback(array[index], index, array); - } -} - export default async function generateConfig( schemaPath: string, user: admin.auth.UserRecord @@ -31,7 +25,13 @@ export default async function generateConfig( /(?<=(require\(("|'))).*?(?=("|')\))/g ); if (requiredDependencies) { - await addPackages(requiredDependencies.map((p: any) => ({ name: p }))); + const packgesAdded = await addPackages( + requiredDependencies.map((p: any) => ({ name: p })), + user + ); + if (!packgesAdded) { + return false; + } } const isFunctionConfigValid = await asyncExecute( @@ -50,10 +50,12 @@ export default async function generateConfig( const requiredSparks = sparksConfig.map((s: any) => s.type); console.log({ requiredSparks }); - await asyncForEach( - requiredSparks, - async (s: any) => await addSparkLib(s) - ); + for (const lib of requiredSparks) { + const success = await addSparkLib(lib, user); + if (!success) { + return false; + } + } return true; } diff --git a/ft_build/compiler/terminal.ts b/ft_build/compiler/terminal.ts index 475bcbe1..13f1dbd7 100644 --- a/ft_build/compiler/terminal.ts +++ b/ft_build/compiler/terminal.ts @@ -1,4 +1,6 @@ import * as child from "child_process"; +import admin from "firebase-admin"; +import { commandErrorHandler } from "../utils"; function execute(command: string, callback: any) { console.log(command); @@ -17,33 +19,45 @@ export const asyncExecute = async (command: string, callback: any) => }); }); -export const addPackages = (packages: { name: string; version?: string }[]) => - new Promise((resolve, reject) => { - //const command =`cd FT_functions/functions;yarn add ${packageName}@${version}` - const packagesString = packages.reduce((acc, currPackage) => { - return `${acc} ${currPackage.name}@${currPackage.version ?? "latest"}`; - }, ""); - if (packagesString.trim().length !== 0) { - execute("ls", function () {}); +export const addPackages = async ( + packages: { name: string; version?: string }[], + user: admin.auth.UserRecord +) => { + const packagesString = packages.reduce((acc, currPackage) => { + return `${acc} ${currPackage.name}@${currPackage.version ?? "latest"}`; + }, ""); + if (packagesString.trim().length !== 0) { + const success = await asyncExecute( + `cd build/functions;yarn add ${packagesString}`, + commandErrorHandler({ + user, + description: "Error adding packages", + }) + ); + return success; + } +}; - const command = `cd ../functions;yarn add ${packagesString}`; - console.log(command); - execute(command, function () { - resolve(true); - }); - } else resolve(false); - }); +export const addSparkLib = async ( + name: string, + user: admin.auth.UserRecord +) => { + const { dependencies } = require(`../sparksLib/${name}`); + const packages = Object.keys(dependencies).map((key) => ({ + name: key, + version: dependencies[key], + })); + let success = await addPackages(packages, user); + if (!success) { + return false; + } -export const addSparkLib = (name: string) => - new Promise(async (resolve, reject) => { - const { dependencies } = require(`../sparksLib/${name}`); - const packages = Object.keys(dependencies).map((key) => ({ - name: key, - version: dependencies[key], - })); - await addPackages(packages); - const command = `cp ../sparksLib/${name}.ts ../functions/src/sparks/${name}.ts`; - execute(command, function () { - resolve(true); - }); - }); + success = await asyncExecute( + `cp build/sparksLib/${name}.ts build/functions/src/sparks/${name}.ts`, + commandErrorHandler({ + user, + description: "Error copying sparksLib", + }) + ); + return success; +}; diff --git a/ft_build/compiler/tsconfig.json b/ft_build/compiler/tsconfig.json index 506f6fa2..6e24a6af 100644 --- a/ft_build/compiler/tsconfig.json +++ b/ft_build/compiler/tsconfig.json @@ -14,5 +14,5 @@ }, "compileOnSave": true, "include": ["src", "generateConfig.ts"], - "ignore": ["sparks","sparksLib"] + "ignore": ["sparks", "sparksLib"] } diff --git a/ft_build/index.ts b/ft_build/index.ts index 732ba5f8..50689630 100644 --- a/ft_build/index.ts +++ b/ft_build/index.ts @@ -6,8 +6,6 @@ import generateConfig from "./compiler"; import { auth } from "./firebaseConfig"; import meta from "./package.json"; import { commandErrorHandler, logErrorToDB } from "./utils"; -const fs = require("fs"); -const path = require("path"); import firebase from "firebase-admin"; const app = express(); @@ -127,6 +125,7 @@ app.post("/", jsonParser, async (req: any, res: any) => { commandErrorHandler({ user }) ); + console.log("build complete"); res.send({ success: true, }); diff --git a/ft_build/package.json b/ft_build/package.json index f741c5a6..db9baebf 100644 --- a/ft_build/package.json +++ b/ft_build/package.json @@ -6,7 +6,7 @@ "main": "index.js", "scripts": { "start": "yarn build && node build", - "build": "rm -rf build && tsc --project ./ && cp -r functions build", + "build": "rm -rf build && tsc --project ./ && cp -r functions build && cp -r sparksLib build", "deploy": "./deploy.sh" }, "engines": { diff --git a/ft_build/tsconfig.json b/ft_build/tsconfig.json index 04664c3e..98e8b6df 100644 --- a/ft_build/tsconfig.json +++ b/ft_build/tsconfig.json @@ -16,5 +16,5 @@ }, "compileOnSave": true, "exclude": ["functions", "build"], - "include": ["*.ts", "firebase.json"] + "include": ["*.ts", "firebase.json", "sparksLib"] }