Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
37 commits
Select commit Hold shift + click to select a range
06b62a1
Adding onPoll option to operation-poller (#3046)
joehan Jan 19, 2021
6258dd5
Typescriptify functionsDeployHelper (#3059)
joehan Jan 20, 2021
ec7d079
Typescriptifying gcp.cloudfunctions (#3060)
joehan Jan 20, 2021
f583ef6
Typescriptifying functionsConfig (#3063)
joehan Jan 21, 2021
08b9d56
Typescriptifying deploymentTool (#3061)
joehan Jan 21, 2021
b4944a4
Refactoring prepare stage of functions deploy (#3067)
joehan Jan 21, 2021
3be0dca
refactoring release step of functions deploy to use typescript
joehan Jan 21, 2021
e0e703e
Adding logic to build regional deployments
joehan Jan 24, 2021
046c7d7
Implementing createDeploymentPlan
joehan Jan 26, 2021
b876523
First round of PR feedback, removing most usages of lodash
joehan Jan 28, 2021
9e0e6e9
moving function prompts into their own file
joehan Jan 28, 2021
2a3b547
seperating out a bunch of code from functionsDeployHelper
joehan Jan 28, 2021
51f2395
Resolves merge conflicts
joehan Jan 28, 2021
30cc0e9
refactoring release step of functions deploy to use typescript (#3071)
joehan Feb 1, 2021
6916000
Implements core logic of running function deploys
joehan Feb 1, 2021
3c8d4a0
Typescriptifying prepareFunctionsUpload (#3064)
joehan Feb 1, 2021
11956fa
Implementing createDeploymentPlan (#3081)
joehan Feb 1, 2021
85d0afe
adding timing and logs for deployments
joehan Feb 2, 2021
00b1989
cleaning up unused code
joehan Feb 2, 2021
397d7c4
Fixing some things that were broken while merging
joehan Feb 3, 2021
21f4906
Fixing up the order of wait and close to ensure that queue promsies a…
joehan Feb 4, 2021
3b3edbd
Format and clean up typos
joehan Feb 4, 2021
e428bcb
refactoring error handling to be cleaner
joehan Feb 5, 2021
4c8e2fb
cleaning up extera newlines
joehan Feb 8, 2021
7f48130
first round of pr fixes
joehan Feb 9, 2021
39a7e86
Readding some changes that I accidenttally wiped out during a merge
joehan Feb 9, 2021
1366955
Switching name to id where appropriate
joehan Feb 9, 2021
7513229
fixing another bug caused by functionName vs Id
joehan Feb 9, 2021
8d3d82d
Merge pull request #3107 from firebase/jh-execute-deployment-plans
joehan Feb 9, 2021
6d2260e
Refactor functions-delete (#3110)
joehan Feb 9, 2021
42e6c15
Cleaning up error reporting
joehan Feb 10, 2021
e4ce126
Merge remote-tracking branch 'public/master' into jh-functions-refactor
joehan Feb 10, 2021
12a48ea
Merge remote-tracking branch 'public/master' into jh-functions-refactor
joehan Feb 11, 2021
7cfe9d9
Implement validation for changing trigger types, and fixes from bug b…
joehan Feb 12, 2021
5eb08bd
Merge branch 'master' into jh-functions-refactor
joehan Feb 12, 2021
5ca6bbf
Merge branch 'master' into jh-functions-refactor
joehan Feb 16, 2021
344b674
fixes package.json
joehan Feb 16, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Typescriptifying functionsConfig (#3063)
  • Loading branch information
joehan authored Jan 21, 2021
commit f583ef6e23ad25eca8188c8a787ddedb8c1c5f48
188 changes: 0 additions & 188 deletions src/functionsConfig.js

This file was deleted.

197 changes: 197 additions & 0 deletions src/functionsConfig.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,197 @@
import * as _ from "lodash";
import * as clc from "cli-color";

import * as api from "./api";
import { ensure as ensureApiEnabled } from "./ensureApiEnabled";
import { FirebaseError } from "./error";
import * as getProjectId from "./getProjectId";
import * as runtimeconfig from "./gcp/runtimeconfig";

export const RESERVED_NAMESPACES = ["firebase"];

interface Id {
config: string;
variable: string;
}
function keyToIds(key: string): Id {
const keyParts = key.split(".");
const variable = keyParts.slice(1).join("/");
return {
config: keyParts[0],
variable: variable,
};
}

function setVariable(
projectId: string,
configId: string,
varPath: string,
val: string | object
): Promise<any> {
if (configId === "" || varPath === "") {
const msg = "Invalid argument, each config value must have a 2-part key (e.g. foo.bar).";
throw new FirebaseError(msg);
}
return runtimeconfig.variables.set(projectId, configId, varPath, val);
}

function isReservedNamespace(id: Id) {
return _.some(RESERVED_NAMESPACES, (reserved) => {
return id.config.toLowerCase().startsWith(reserved);
});
}

export async function ensureApi(options: any): Promise<void> {
const projectId = getProjectId(options);
return ensureApiEnabled(projectId, "runtimeconfig.googleapis.com", "runtimeconfig", true);
}

export function varNameToIds(varName: string): Id {
return {
config: varName.match(new RegExp("/configs/(.+)/variables/"))![1],
variable: varName.match(new RegExp("/variables/(.+)"))![1],
};
}

export function idsToVarName(projectId: string, configId: string, varId: string): string {
return _.join(["projects", projectId, "configs", configId, "variables", varId], "/");
}

export function getAppEngineLocation(config: any): string {
let appEngineLocation = config.locationId;
if (appEngineLocation && appEngineLocation.match(/[^\d]$/)) {
// For some regions, such as us-central1, the locationId has the trailing digit cut off
appEngineLocation = appEngineLocation + "1";
}
return appEngineLocation || "us-central1";
}

export async function getFirebaseConfig(options: any): Promise<any> {
const projectId = getProjectId(options, false);
const response = await api.request("GET", "/v1beta1/projects/" + projectId + "/adminSdkConfig", {
auth: true,
origin: api.firebaseApiOrigin,
});
return response.body;
}

// If you make changes to this function, run "node scripts/test-functions-config.js"
// to ensure that nothing broke.
export async function setVariablesRecursive(
projectId: string,
configId: string,
varPath: string,
val: string | { [key: string]: any }
): Promise<any> {
let parsed = val;
if (_.isString(val)) {
try {
// Only attempt to parse 'val' if it is a String (takes care of unparsed JSON, numbers, quoted string, etc.)
parsed = JSON.parse(val);
} catch (e) {
// 'val' is just a String
}
}
// If 'parsed' is object, call again
if (_.isPlainObject(parsed)) {
return Promise.all(
_.map(parsed, (item: any, key: string) => {
const newVarPath = varPath ? _.join([varPath, key], "/") : key;
return setVariablesRecursive(projectId, configId, newVarPath, item);
})
);
}

// 'val' wasn't more JSON, i.e. is a leaf node; set and return
return setVariable(projectId, configId, varPath, val);
}

export async function materializeConfig(configName: string, output: any): Promise<any> {
const materializeVariable = async function (varName: string) {
const variable = await runtimeconfig.variables.get(varName);
const id = exports.varNameToIds(variable.name);
const key = id.config + "." + id.variable.split("/").join(".");
_.set(output, key, variable.text);
};

const traverseVariables = async function (variables: any) {
return Promise.all(
_.map(variables, (variable) => {
return materializeVariable(variable.name);
})
);
};

const variables = await runtimeconfig.variables.list(configName);
await traverseVariables(variables);
return output;
}

export async function materializeAll(projectId: string): Promise<{ [key: string]: any }> {
const output = {};
const configs = await runtimeconfig.configs.list(projectId);
await Promise.all(
_.map(configs, (config) => {
if (config.name.match(new RegExp("configs/firebase"))) {
// ignore firebase config
return;
}
return exports.materializeConfig(config.name, output);
})
);
return output;
}

interface ParsedArg {
configId: string;
varId: string;
val?: string;
}

export function parseSetArgs(args: string[]): ParsedArg[] {
const parsed: ParsedArg[] = [];
_.forEach(args, (arg) => {
const parts = arg.split("=");
const key = parts[0];
if (parts.length < 2) {
throw new FirebaseError("Invalid argument " + clc.bold(arg) + ", must be in key=val format");
}
if (/[A-Z]/.test(key)) {
throw new FirebaseError("Invalid config name " + clc.bold(key) + ", cannot use upper case.");
}

const id = keyToIds(key);
if (isReservedNamespace(id)) {
throw new FirebaseError("Cannot set to reserved namespace " + clc.bold(id.config));
}

const val = parts.slice(1).join("="); // So that someone can have '=' within a variable value
parsed.push({
configId: id.config,
varId: id.variable,
val: val,
});
});
return parsed;
}

export function parseUnsetArgs(args: string[]): ParsedArg[] {
const parsed: ParsedArg[] = [];
let splitArgs: string[] = [];
_.forEach(args, (arg) => {
splitArgs = _.union(splitArgs, arg.split(","));
});

_.forEach(splitArgs, (key) => {
const id = keyToIds(key);
if (isReservedNamespace(id)) {
throw new FirebaseError("Cannot unset reserved namespace " + clc.bold(id.config));
}

parsed.push({
configId: id.config,
varId: id.variable,
});
});
return parsed;
}
Loading