import { ILifecycleService, IWorkspaceContextService, TYPES, LifecyclePhase, IMoleculerServiceName } from '@workbench-stack/core'; import { interfaces } from 'inversify'; import { ServiceBroker } from 'moleculer'; import * as ILogger from 'bunyan'; import { readFileSync } from 'fs'; import { IProject } from '@adminide-stack/core'; // const yaml = require('js-yaml'); import * as yaml from 'js-yaml'; import { config } from '../config'; import { TYPES as GitTypes, IGitSshSettings, ICloneOptions } from '@git-stack/core'; import { IGitService } from '@git-stack/core'; import { get } from 'lodash'; import { updateProjects } from './util'; export interface IProjectConfig { projects: IProject[]; userParams: IUserGitProviderInfo; } ///temporarily adding it, but pulled from adminide-stack/core export interface IUserGitProviderInfo { [key: string]: { email: string; username: string; sshkeyPath: string; }; } export const gitContainerServiceBroker = async (_, container: interfaces.Container) => { const logger = container.get('Logger'); logger.info('PRESTART START: -- load configured repositories'); const lifecycle = container.get(TYPES.ILifecycleService); const broker = await container.get('MoleculerBroker'); broker.waitForServices(IMoleculerServiceName.WorkspaceReady).then(async () => { await lifecycle.when(LifecyclePhase.Restored); try { console.log('----WORKSPACE ____RESTORED') const projectConfig: IProjectConfig = yaml.safeLoad(readFileSync(config.WORKSPACE_PROJECT_FILE, 'utf8')); const gitService = container.get(GitTypes.IGitService); const workspaceContextService = container.get(Symbol.for('IWorkspaceContextService')); let sshkeyPath: string; if (process.env.NODE_ENV === 'development' && config.GIT_SSH_KEY_LOCATION) { sshkeyPath = config.GIT_SSH_KEY_LOCATION; } else { sshkeyPath = get(projectConfig.userParams, 'globals.sshkeyPath') as any; } const env: ICloneOptions & IGitSshSettings = { login: get(projectConfig.userParams, 'globals.username') as any, email: get(projectConfig.userParams, 'globals.email') as any, gitSSHEnabled: !!sshkeyPath, gitSSHPrivateKeyPath: sshkeyPath, // gitSSHKnownHostsPath: endpoint: '', }; console.log('----ENV____-', env) await updateProjects(config.FILE_ROOT_PATH, projectConfig, gitService, workspaceContextService, env); logger.info('PRESTART ENDS: -- load configured repositories'); } catch (err) { console.error(err); } }); };