import { parse, stringify } from 'yaml'; import fs from 'fs'; import path from 'path'; import os from 'os'; import { globSync } from 'glob'; import chalk from 'chalk'; import { IGlobalConfig } from './@types/IGlobalConfig'; // const HexaSyncClusterHost = "hexasync.com"; const HEXASYNC_HOME = path.join(os.homedir(), 'hexasync'); export const HEXASYNC_GLOBAL_CONFIG = path.join( os.homedir(), 'hexasync/globalConfig', ); export const HEXASYNC_CONNECTOR_CONFIG = path.join( os.homedir(), 'hexasync/connectorConfig', ); export function getSingleSignOnUrl(cluster: string): string { return `sso.${cluster}`; } export function getAppUrl(cluster: string): string { return `app.${cluster}`; } export function getGlobalConfig(): IGlobalConfig { if (!fs.existsSync(HEXASYNC_HOME)) { fs.mkdirSync(HEXASYNC_HOME); } if (!fs.existsSync(HEXASYNC_GLOBAL_CONFIG)) { fs.writeFileSync(HEXASYNC_GLOBAL_CONFIG, 'clusters: {}'); } let cfg = parse( fs.readFileSync(HEXASYNC_GLOBAL_CONFIG, 'utf-8').toString(), ) as IGlobalConfig; return cfg; } export function readUser() { let cfg = getGlobalConfig(); return cfg['clusters'][cfg.currentCluster].user; } export function useCluster(cluster: string, data: any) { let cfg = getGlobalConfig(); cfg.clusters = cfg.clusters || {}; if (!!data) { cfg.clusters[cluster] = data; } cfg.currentCluster = cluster; fs.writeFileSync( HEXASYNC_GLOBAL_CONFIG, stringify(cfg, { keepSourceTokens: true, lineWidth: 0 }), ); } export function useProfile(folder: string, options: any) { let cfg = getGlobalConfig(); let projectPath = path.resolve(folder); if (!fs.existsSync(projectPath)) { console.error( `${chalk.red('✗')} There is no such directory ${chalk.red(projectPath)}`, ); return false; } if (!fs.lstatSync(projectPath).isDirectory()) { console.error( `${chalk.red('✗')} The path ${chalk.red(projectPath)} is not a directory`, ); return false; } // 1. Find if there is a main.yaml file in the folder const mainYaml = path.join(projectPath, 'main.yaml'); if (fs.existsSync(mainYaml)) { // 2. Move to the parent folder of projectPath projectPath = path.dirname(projectPath); console.log( `${chalk.green('✓')} Found project ${chalk.green(projectPath)}`, ); } // 3. Find if there is only one main.yaml file in the **/main.yaml using fast-glob const mainYamls = globSync(`${projectPath}/**/main.yaml`, { windowsPathsNoEscape: true, }); if (mainYamls.length === 0) { console.error( `${chalk.red('✗')} There is no main.yaml file in the project folder`, ); return false; } if (mainYamls.length !== 1) { console.error( `${chalk.red('✗')} There should be only one main.yaml file in the project folder`, ); return false; } console.log( `${chalk.green('✓')} Found main.yaml at ${chalk.green(path.dirname(mainYamls[0]))}`, ); cfg.currentProject = projectPath; cfg.componentPath = path.dirname(mainYamls[0]); cfg.variablesFilePath = path.join(cfg.componentPath, 'variables.yaml'); cfg.mainFilePath = mainYamls[0]; cfg.taskAssociationsFilePath = path.join( cfg.componentPath, 'ObjectAssociations.yaml', ); // create config folder if not exists cfg.configPath = path.join(projectPath, '__configs'); if (!fs.existsSync(cfg.configPath)) { fs.mkdirSync(cfg.configPath); } cfg.logPath = path.join(cfg.currentProject, '__logs'); if (!fs.existsSync(cfg.logPath)) { fs.mkdirSync(cfg.logPath); } cfg.taskCachesPath = path.join(cfg.configPath, '__task_caches.yaml'); cfg.taskRelationsCachesPath = path.join( cfg.configPath, '__task_relations_caches.yaml', ); cfg.pullerCachesPath = path.join(cfg.configPath, '__puller_caches.yaml'); cfg.pusherCachesPath = path.join(cfg.configPath, '__pusher_caches.yaml'); cfg.tableCachesPath = path.join(cfg.configPath, '__table_caches.yaml'); cfg.schemaCachesPath = path.join(cfg.configPath, '__schema_caches.yaml'); cfg.reportCachesPath = path.join(cfg.configPath, '__report_caches.yaml'); cfg.connectionCachesPath = path.join( cfg.configPath, '__connection_caches.yaml', ); if (fs.existsSync(cfg.connectionCachesPath)) { fs.rmSync(cfg.connectionCachesPath, { recursive: true }); } if (fs.existsSync(cfg.taskCachesPath)) { fs.rmSync(cfg.taskCachesPath, { recursive: true }); } if (fs.existsSync(cfg.pullerCachesPath)) { fs.rmSync(cfg.pullerCachesPath, { recursive: true }); } if (fs.existsSync(cfg.reportCachesPath)) { fs.rmSync(cfg.reportCachesPath, { recursive: true }); } if (fs.existsSync(cfg.tableCachesPath)) { fs.rmSync(cfg.tableCachesPath, { recursive: true }); } if (fs.existsSync(cfg.pusherCachesPath)) { fs.rmSync(cfg.pusherCachesPath, { recursive: true }); } if (fs.existsSync(cfg.schemaCachesPath)) { fs.rmSync(cfg.schemaCachesPath, { recursive: true }); } if (fs.existsSync(cfg.taskRelationsCachesPath)) { fs.rmSync(cfg.taskRelationsCachesPath, { recursive: true }); } fs.writeFileSync( HEXASYNC_GLOBAL_CONFIG, stringify(cfg, { keepSourceTokens: true, lineWidth: 0 }), ); console.log( `${chalk.green('✓')} Set context to profile ${chalk.green(projectPath)}\n`, ); if (options && Object.keys(options).length) { console.log( `${chalk.gray(stringify({ Options: options }, { keepSourceTokens: true, lineWidth: 0 }))}`, ); } return true; } export function getProjectPath() { const cfg = getGlobalConfig(); let componentPath = cfg.componentPath; // partials or partial if (!fs.existsSync(componentPath)) { fs.mkdirSync(componentPath); } return componentPath; } export function getProjectConfigurationPath() { const cfg = getGlobalConfig(); const projectPath = cfg.currentProject; const componentPath = path.join(projectPath, '__configs'); if (!fs.existsSync(componentPath)) { fs.mkdirSync(componentPath); } return componentPath; } export function getProjectNotificationDisplayConfigs() { const componentPath = path.join( getProjectConfigurationPath(), 'notification-displays.yaml', ); if (!fs.existsSync(componentPath)) { fs.writeFileSync(componentPath, ''); } return parse(fs.readFileSync(componentPath, 'utf8')); } export function getProjectNotificationConfigs() { const componentPath = path.join( getProjectConfigurationPath(), 'notifications.yaml', ); if (!fs.existsSync(componentPath)) { throw new Error('The notifications.yaml file does not exist.'); } return parse(fs.readFileSync(componentPath, 'utf8')); } export function getProjectOutput() { const cfg = getGlobalConfig(); const projectPath = cfg.currentProject; const componentPath = path.join(projectPath, 'output.yaml'); if (!fs.existsSync(componentPath)) { throw new Error('The output.yaml file does not exist.'); } return parse(fs.readFileSync(componentPath, 'utf8')); } export function loadVariables() { const cfg = getGlobalConfig(); const variableFiles = globSync(`${cfg.componentPath}/variables.yaml`, { windowsPathsNoEscape: true, }); const variables = variableFiles.reduce((acc, file) => { const fileContent = parse(fs.readFileSync(file, 'utf-8').toString()); return { ...acc, ...fileContent.variables }; }, {}); return variables; }