firebase-tools
Version:
Command-Line Interface for Firebase
87 lines (86 loc) • 3.08 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.AppHostingYamlConfig = void 0;
const path_1 = require("path");
const utils_1 = require("../utils");
const config_1 = require("./config");
const yaml = require("yaml");
const jsYaml = require("js-yaml");
const fsutils_1 = require("../fsutils");
const error_1 = require("../error");
class AppHostingYamlConfig {
static async loadFromFile(filePath) {
var _a;
const config = new AppHostingYamlConfig();
if (!(0, fsutils_1.fileExistsSync)(filePath)) {
throw new error_1.FirebaseError("Cannot load AppHostingYamlConfig from given path, it doesn't exist");
}
const file = await (0, utils_1.readFileFromDirectory)((0, path_1.dirname)(filePath), (0, path_1.basename)(filePath));
const loadedAppHostingYaml = (_a = (await (0, utils_1.wrappedSafeLoad)(file.source))) !== null && _a !== void 0 ? _a : {};
if (loadedAppHostingYaml.env) {
const parsedEnvs = parseEnv(loadedAppHostingYaml.env);
config._environmentVariables = parsedEnvs.environmentVariables;
config._secrets = parsedEnvs.secrets;
}
return config;
}
static empty() {
return new AppHostingYamlConfig();
}
constructor() {
this._environmentVariables = new Map();
this._secrets = new Map();
}
get environmentVariables() {
return mapToArray(this._environmentVariables);
}
get secrets() {
return mapToArray(this._secrets);
}
addEnvironmentVariable(env) {
this._environmentVariables.set(env.variable, env);
}
addSecret(secret) {
this._secrets.set(secret.variable, secret);
}
clearSecrets() {
this._secrets.clear();
}
merge(other) {
for (const [key, value] of other._environmentVariables) {
this._environmentVariables.set(key, value);
}
for (const [key, value] of other._secrets) {
this._secrets.set(key, value);
}
}
async upsertFile(filePath) {
let yamlConfigToWrite = {};
if ((0, fsutils_1.fileExistsSync)(filePath)) {
const file = await (0, utils_1.readFileFromDirectory)((0, path_1.dirname)(filePath), (0, path_1.basename)(filePath));
yamlConfigToWrite = await (0, utils_1.wrappedSafeLoad)(file.source);
}
yamlConfigToWrite.env = [...this.environmentVariables, ...this.secrets];
(0, config_1.store)(filePath, yaml.parseDocument(jsYaml.dump(yamlConfigToWrite)));
}
}
exports.AppHostingYamlConfig = AppHostingYamlConfig;
function parseEnv(envs) {
const environmentVariables = new Map();
const secrets = new Map();
for (const env of envs) {
if (env.value) {
environmentVariables.set(env.variable, env);
}
if (env.secret) {
secrets.set(env.variable, env);
}
}
return {
environmentVariables,
secrets,
};
}
function mapToArray(map) {
return Array.from(map.values());
}