Skip to content

Commit

Permalink
Merge pull request #309 from AntlerVC/rc
Browse files Browse the repository at this point in the history
Release v1.3.0
  • Loading branch information
shamsmosowi authored Mar 8, 2021
2 parents 342f5f1 + bbf9379 commit 931981b
Show file tree
Hide file tree
Showing 311 changed files with 29,834 additions and 7,353 deletions.
7 changes: 4 additions & 3 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
# dependencies
/node_modules
www/node_modules
www/.firebaserc


Firetable/node_modules
cloud_functions/functions/node_modules
Expand All @@ -22,7 +22,6 @@ cloud_functions/functions/lib
cloud_functions/functions/src/functionConfig.ts

cloud_functions/functions/firebase-credentials.json
cloud_functions/.firebaserc

# misc
.DS_Store
Expand All @@ -35,6 +34,7 @@ yarn-debug.log*
yarn-error.log*
firebase-debug.log*

*.firebaserc


# Accidental package installs to root directories
Expand All @@ -45,4 +45,5 @@ node_modules/

cloud_functions/functions/src/functionConfig.json
*.iml
.idea
.idea
*-firebase.json
5 changes: 0 additions & 5 deletions Dockerfile

This file was deleted.

31 changes: 31 additions & 0 deletions FT_build.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
steps:
- name: node:14.9.0
entrypoint: yarn
args: ["install"]
dir: "FT_functions/compiler"
- name: node:14.9.0
entrypoint: yarn
args:
- "compile"
- "${_SCHEMA_PATH}"
dir: "FT_functions/compiler"
- name: node:14.9.0
entrypoint: yarn
args: ["install"]
dir: "FT_functions/functions"
- name: node:14.9.0
entrypoint: yarn
args:
- "deployFT"
- "--project"
- "${_PROJECT_ID}"
- "--token"
- "${_FIREBASE_TOKEN}"
- "--only"
- "functions"
dir: "FT_functions/functions"

substitutions:
_PROJECT_ID: "project-id" # default value
options:
machineType: "N1_HIGHCPU_8"
66 changes: 66 additions & 0 deletions FT_functions/.gitignore
Original file line number Diff line number Diff line change
@@ -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
64 changes: 64 additions & 0 deletions FT_functions/compiler/.eslintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
module.exports = {
env: {
browser: true,
es6: true,
node: true,
},
extends: ["plugin:import/errors", "plugin:import/warnings"],
parser: "@typescript-eslint/parser",
parserOptions: {
project: "tsconfig.json",
sourceType: "module",
},
plugins: ["@typescript-eslint", "import"],
rules: {
"@typescript-eslint/adjacent-overload-signatures": "error",
"@typescript-eslint/no-empty-function": "error",
"@typescript-eslint/no-empty-interface": "warn",
"@typescript-eslint/no-floating-promises": "error",
"@typescript-eslint/no-namespace": "error",
"@typescript-eslint/no-unnecessary-type-assertion": "error",
"@typescript-eslint/prefer-for-of": "warn",
"@typescript-eslint/triple-slash-reference": "error",
"@typescript-eslint/unified-signatures": "warn",
"comma-dangle": "warn",
"constructor-super": "error",
eqeqeq: ["warn", "always"],
"import/no-deprecated": "warn",
"import/no-extraneous-dependencies": "error",
"import/no-unassigned-import": "warn",
"no-cond-assign": "error",
"no-duplicate-case": "error",
"no-duplicate-imports": "error",
"no-empty": [
"error",
{
allowEmptyCatch: true,
},
],
"no-invalid-this": "error",
"no-new-wrappers": "error",
"no-param-reassign": "error",
"no-redeclare": "error",
"no-sequences": "error",
"no-shadow": [
"error",
{
hoist: "all",
},
],
"no-throw-literal": "error",
"no-unsafe-finally": "error",
"no-unused-labels": "error",
"no-var": "warn",
"no-void": "error",
"prefer-const": "warn",
},
settings: {
jsdoc: {
tagNamePreference: {
returns: "return",
},
},
},
};
28 changes: 28 additions & 0 deletions FT_functions/compiler/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { addPackages, addSparkLib } from "./terminal";
const fs = require("fs");
import { generateConfigFromTableSchema } from "./loader";

async function asyncForEach(array: any[], callback: Function) {
for (let index = 0; index < array.length; index++) {
await callback(array[index], index, array);
}
}

generateConfigFromTableSchema(process.argv[2]).then(async () => {
const configFile = fs.readFileSync(
"../functions/src/functionConfig.ts",
"utf-8"
);
const requiredDependencies = configFile.match(
/(?<=(require\(("|'))).*?(?=("|')\))/g
);
if (requiredDependencies) {
await addPackages(requiredDependencies.map((p) => ({ name: p })));
}

const { sparksConfig } = require("../functions/src/functionConfig");
const requiredSparks = sparksConfig.map((s) => s.type);
console.log({ requiredSparks });

await asyncForEach(requiredSparks, async (s) => await addSparkLib(s));
});
153 changes: 153 additions & 0 deletions FT_functions/compiler/loader.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
const fs = require("fs");
const beautify = require("js-beautify").js;
// Initialize Firebase Admin
import * as admin from "firebase-admin";
// Initialize Firebase Admin
//const serverTimestamp = admin.firestore.FieldValue.serverTimestamp;

admin.initializeApp();
//const serviceAccount = require("./antler-vc-firebase.json");
//admin.initializeApp({ credential: admin.credential.cert(serviceAccount) });
const db = admin.firestore();

export const generateConfigFromTableSchema = async (schemaDocPath) => {
const schemaDoc = await db.doc(schemaDocPath).get();
const schemaData = schemaDoc.data();
if (!schemaData) throw new Error("no schema found");
const derivativeColumns = Object.values(schemaData.columns).filter(
(col: any) => col.type === "DERIVATIVE"
);
const derivativesConfig = `[${derivativeColumns.reduce(
(acc, currColumn: any) => {
if (
!currColumn.config.listenerFields ||
currColumn.config.listenerFields.length === 0
)
throw new Error(
`${currColumn.key} derivative is missing listener fields`
);
if (currColumn.config.listenerFields.includes(currColumn.key))
throw new Error(
`${currColumn.key} derivative has its own key as a listener field`
);
return `${acc}{\nfieldName:'${
currColumn.key
}',evaluate:async ({row,ref,db,auth,utilFns}) =>{${
currColumn.config.script
}},\nlistenerFields:[${currColumn.config.listenerFields
.map((fieldKey) => `"${fieldKey}"`)
.join(",\n")}]},\n`;
},
""
)}]`;

const initializableColumns = Object.values(
schemaData.columns
).filter((col: any) => Boolean(col.config?.defaultValue));
console.log(JSON.stringify({ initializableColumns }));
const initializeConfig = `[${initializableColumns.reduce(
(acc, currColumn: any) => {
if (currColumn.config.defaultValue.type === "static") {
return `${acc}{\nfieldName:'${currColumn.key}',
type:"${currColumn.config.defaultValue.type}",
value:${
typeof currColumn.config.defaultValue.value === "string"
? `"${currColumn.config.defaultValue.value}"`
: currColumn.config.defaultValue.value
},
},\n`;
} else if (currColumn.config.defaultValue.type === "dynamic") {
return `${acc}{\nfieldName:'${currColumn.key}',
type:"${currColumn.config.defaultValue.type}",
script:async ({row,ref,db,auth,utilFns}) =>{${currColumn.config.defaultValue.script}},
},\n`;
} else {
return `${acc}{\nfieldName:'${currColumn.key}',
type:"${currColumn.config.defaultValue.type}"
},\n`;
}
},
""
)}]`;
const documentSelectColumns = Object.values(schemaData.columns).filter(
(col: any) => col.type === "DOCUMENT_SELECT" && col.config?.trackedFields
);
const documentSelectConfig = `[${documentSelectColumns.reduce(
(acc, currColumn: any) => {
return `${acc}{\nfieldName:'${
currColumn.key
}',\ntrackedFields:[${currColumn.config.trackedFields
.map((fieldKey) => `"${fieldKey}"`)
.join(",\n")}]},\n`;
},
""
)}]`;

const sparksConfig = schemaData.sparks ? schemaData.sparks : "[]";
const collectionType = schemaDocPath.includes("subTables")
? "subCollection"
: schemaDocPath.includes("groupSchema")
? "groupCollection"
: "collection";
let collectionId = "";
let functionName = "";
let triggerPath = "";
switch (collectionType) {
case "collection":
collectionId = schemaDocPath.split("/").pop();
functionName = `"${collectionId}"`;
triggerPath = `"${collectionId}/{docId}"`;
break;
case "subCollection":
let pathParentIncrement = 0;
triggerPath =
'"' +
schemaDocPath
.replace("_FIRETABLE_/settings/schema/", "")
.replace(/subTables/g, function () {
pathParentIncrement++;
return `{parentDoc${pathParentIncrement}}`;
}) +
"/{docId}" +
'"';
functionName =
'"' +
schemaDocPath
.replace("_FIRETABLE_/settings/schema/", "")
.replace(/\/subTables\//g, "_") +
'"';
break;
case "groupCollection":
collectionId = schemaDocPath.split("/").pop();
const triggerDepth = schemaData.triggerDepth
? schemaData.triggerDepth
: 1;
triggerPath = "";
for (let i = 1; i <= triggerDepth; i++) {
triggerPath = triggerPath + `{parentCol${i}}/{parentDoc${i}}/`;
}
triggerPath = '"' + triggerPath + collectionId + "/" + "{docId}" + '"';
functionName = `"CG_${collectionId}${
triggerDepth > 1 ? `_D${triggerDepth}` : ""
}"`;
break;
default:
break;
}
const exports = {
triggerPath,
functionName: functionName.replace(/-/g, "_"),
derivativesConfig,
initializeConfig,
documentSelectConfig,
sparksConfig,
};

const fileData = Object.keys(exports).reduce((acc, currKey) => {
return `${acc}\nexport const ${currKey} = ${exports[currKey]}`;
}, ``);
fs.writeFileSync(
"../functions/src/functionConfig.ts",
beautify(fileData, { indent_size: 2 })
);
};
Loading

0 comments on commit 931981b

Please sign in to comment.