import path from 'path'; import fs from 'fs'; import { parse } from 'yaml'; import os from 'os'; import chalk from 'chalk'; import { ReservedVariableName } from '../../configurations'; import inquirer from 'inquirer'; var _shellConfigFilePath = ''; var _HexaSyncCLIYamlConfigFilePath = ''; // ---------------------------------------- function _constructShellExportKeyRegExp(key: string) { return new RegExp(`^export\\s+${key}=.*`, 'm'); } function _readShellConfigFilePath() { const shell = os.userInfo().shell; if (shell?.includes('zsh')) { _shellConfigFilePath = path.join(os.homedir(), '.zshrc'); } else if (shell?.includes('bash')) { _shellConfigFilePath = path.join(os.homedir(), '.bashrc'); } else { throw new Error( chalk.redBright( 'LoadConfig.ts > getShellConfigFile() > Can not get shell config file path', ), ); } } function _readShellExportKeyValue(key: string) { let shellFileContent = fs.readFileSync(_shellConfigFilePath, { encoding: 'utf-8', }); const exportRegExp = _constructShellExportKeyRegExp(key); const shellMatchStr = shellFileContent.match(exportRegExp); if (shellMatchStr) { const exportStr = shellMatchStr[0]; const exportValue = exportStr.split('=')[1]; const cleanedExportValue = exportValue.replace(/['"]+/g, ''); // clean export value string return cleanedExportValue; } return ''; } function _writeEnvironmentVariableToShell(key: string, value: string) { const shellExportStr = `export ${key}=${value}`; const checkKeyExistRegExp = _constructShellExportKeyRegExp(key); let shellFileContent = fs.readFileSync(_shellConfigFilePath, { encoding: 'utf-8', }); if (shellFileContent.match(checkKeyExistRegExp)) { shellFileContent = shellFileContent.replace( checkKeyExistRegExp, shellExportStr, ); } else { shellFileContent += `\n${shellExportStr}\n`; } fs.writeFileSync(_shellConfigFilePath, shellFileContent, { encoding: 'utf-8', }); } // ---------------------------------------- function _readHexaSyncCLIYamlConfigFilePath() { _HexaSyncCLIYamlConfigFilePath = _readShellExportKeyValue( ReservedVariableName.HEXASYNC_CLI_CONFIG_FILE_PATH, ); return _HexaSyncCLIYamlConfigFilePath ? true : false; } export async function PromptUserToSetHexaSyncCLIYamlConfigFilePath() { const answer = await inquirer.prompt<{ value: string }>([ { type: 'input', name: 'value', message: `Please enter the value:`, }, ]); const filePath = answer.value; console.log(chalk.green('HexaSync CLI YAML Config File Path: ') + filePath); if (filePath) { _writeEnvironmentVariableToShell( ReservedVariableName.HEXASYNC_CLI_CONFIG_FILE_PATH, filePath, ); _HexaSyncCLIYamlConfigFilePath = filePath; } } async function _readHexaSyncCLIYamlConfigFile() { const fileContent = fs.readFileSync(_HexaSyncCLIYamlConfigFilePath, 'utf-8'); const jsonContent = parse(fileContent); // Not use YAMLExtension because RegisterService run after LoadConfig Object.keys(jsonContent).forEach((key) => { const value = jsonContent[key]; if (!value) { throw new Error( `LoadConfig.ts > _readHexaSyncCLIYamlConfigFile() error: key ${key} does not have value`, ); } process.env[key] = jsonContent[key]; }); } // -------------------------------------------------------- // export async function PromptUserToSetOfflineOfficialTemplateFilePath() { const answer = await inquirer.prompt<{ value: string }>([ { type: 'input', name: 'value', message: `Please enter the value:`, }, ]); const filePath = answer.value; console.log(chalk.green('Offline Official Template File Path: ') + filePath); if (filePath) { _writeEnvironmentVariableToShell( ReservedVariableName.OFFLINE_OFFICIAL_TEMPLATE_FILE_PATH, filePath, ); _loadOfflineOfficialTemplateFilePath(); } } function _loadOfflineOfficialTemplateFilePath() { let filePath = _readShellExportKeyValue( ReservedVariableName.OFFLINE_OFFICIAL_TEMPLATE_FILE_PATH, ); if (filePath) { process.env[ReservedVariableName.OFFLINE_OFFICIAL_TEMPLATE_FILE_PATH] = filePath; return true; } return false; } // -------------------------------------------------------- // export async function LoadConfig() { _readShellConfigFilePath(); let success = false; success = _readHexaSyncCLIYamlConfigFilePath(); if (!success) { console.log(chalk.yellow('HexaSync CLI YAML config file path is not set.')); await PromptUserToSetHexaSyncCLIYamlConfigFilePath(); } _readHexaSyncCLIYamlConfigFile(); success = _loadOfflineOfficialTemplateFilePath(); if (!success) { console.log( chalk.yellow('Offline Official Template file path is not set.'), ); await PromptUserToSetOfflineOfficialTemplateFilePath(); } }