2024-01-16 09:36:35 +00:00
|
|
|
import fs from 'fs-extra';
|
|
|
|
import path from 'path';
|
|
|
|
import toml from 'toml';
|
|
|
|
import debug from 'debug';
|
|
|
|
|
2024-01-17 11:29:59 +00:00
|
|
|
import { Project } from './entity/Project';
|
2024-01-18 08:36:49 +00:00
|
|
|
import { ProjectMember } from './entity/ProjectMember';
|
|
|
|
import { Deployment } from './entity/Deployment';
|
|
|
|
import { EnvironmentVariable } from './entity/EnvironmentVariable';
|
2024-01-17 11:29:59 +00:00
|
|
|
|
2024-01-16 09:36:35 +00:00
|
|
|
const log = debug('snowball:utils');
|
|
|
|
|
|
|
|
export const getConfig = async <ConfigType>(
|
|
|
|
configFile: string
|
|
|
|
): Promise<ConfigType> => {
|
|
|
|
const configFilePath = path.resolve(configFile);
|
|
|
|
const fileExists = await fs.pathExists(configFilePath);
|
|
|
|
if (!fileExists) {
|
|
|
|
throw new Error(`Config file not found: ${configFilePath}`);
|
|
|
|
}
|
|
|
|
|
|
|
|
const config = toml.parse(await fs.readFile(configFilePath, 'utf8'));
|
|
|
|
log('config', JSON.stringify(config, null, 2));
|
|
|
|
|
|
|
|
return config;
|
|
|
|
};
|
2024-01-17 11:29:59 +00:00
|
|
|
|
2024-01-18 08:36:49 +00:00
|
|
|
export const projectToGqlType = (dbProject: Project, projectMembers: ProjectMember[], environmentVariables: EnvironmentVariable[]): any => {
|
2024-01-17 11:29:59 +00:00
|
|
|
return {
|
|
|
|
id: dbProject.id,
|
|
|
|
owner: dbProject.owner,
|
|
|
|
name: dbProject.name,
|
|
|
|
repository: dbProject.repository,
|
|
|
|
prodBranch: dbProject.prodBranch,
|
|
|
|
description: dbProject.description,
|
|
|
|
template: dbProject.template,
|
|
|
|
framework: dbProject.framework,
|
|
|
|
webhooks: dbProject.webhooks,
|
2024-01-18 08:36:49 +00:00
|
|
|
members: projectMembers,
|
|
|
|
environmentVariables: environmentVariables,
|
2024-01-17 11:29:59 +00:00
|
|
|
createdAt: dbProject.createdAt,
|
|
|
|
updatedAt: dbProject.updatedAt
|
|
|
|
};
|
|
|
|
};
|
2024-01-18 08:36:49 +00:00
|
|
|
|
|
|
|
// TODO: Add domain field to deployment
|
|
|
|
export const deploymentToGqlType = (dbDeployment: Deployment): any => {
|
|
|
|
return {
|
|
|
|
id: dbDeployment.id,
|
|
|
|
domain: dbDeployment.domain,
|
|
|
|
branch: dbDeployment.branch,
|
|
|
|
commitHash: dbDeployment.commitHash,
|
|
|
|
title: dbDeployment.title,
|
|
|
|
environment: dbDeployment.environment,
|
|
|
|
isCurrent: dbDeployment.isCurrent,
|
|
|
|
status: dbDeployment.status,
|
|
|
|
createdAt: dbDeployment.createdAt,
|
|
|
|
updatedAt: dbDeployment.updatedAt
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
export const projectMemberToGqlType = (dbProjectMember: ProjectMember): any => {
|
|
|
|
return {
|
|
|
|
id: dbProjectMember.id,
|
|
|
|
member: dbProjectMember.member,
|
|
|
|
permissions: dbProjectMember.permissions,
|
|
|
|
createdAt: dbProjectMember.createdAt,
|
|
|
|
updatedAt: dbProjectMember.updatedAt
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
export const environmentVariableToGqlType = (dbEnvironmentVariable: EnvironmentVariable): any => {
|
|
|
|
return {
|
|
|
|
id: dbEnvironmentVariable.id,
|
|
|
|
environments: dbEnvironmentVariable.environments,
|
|
|
|
key: dbEnvironmentVariable.key,
|
|
|
|
value: dbEnvironmentVariable.value,
|
|
|
|
createdAt: dbEnvironmentVariable.createdAt,
|
|
|
|
updatedAt: dbEnvironmentVariable.updatedAt
|
|
|
|
};
|
|
|
|
};
|
2024-01-23 09:31:33 +00:00
|
|
|
|
|
|
|
export const isUserOwner = (userId: string, projectOwnerId: string): boolean => {
|
|
|
|
return userId === projectOwnerId;
|
|
|
|
};
|