diff --git a/lib/src/commands/config_command/sub_commands/create_command.dart b/lib/src/commands/config_command/sub_commands/create_command.dart index 609a27d..ed1a5f8 100644 --- a/lib/src/commands/config_command/sub_commands/create_command.dart +++ b/lib/src/commands/config_command/sub_commands/create_command.dart @@ -39,7 +39,8 @@ class ConfigCreateCommand extends Command { @override Future run() async { - final configFileController = ConfigFile.fromArgResults(argResults!); + final configFileController = + ConfigFileController.fromArgResults(argResults!); try { final file = configFileController.configFileRef; @@ -51,7 +52,8 @@ class ConfigCreateCommand extends Command { return await _requestToOverwrite(file, configFileController); } else { - return await _promptForConfigFileCreation(configFileController); + return await _promptForConfigFileControllerCreation( + configFileController); } } catch (e) { logger.err(e.toString()); @@ -62,7 +64,7 @@ class ConfigCreateCommand extends Command { Future _requestToOverwrite( File file, - ConfigFile configFileController, + ConfigFileController configFileController, ) async { final confirmOverwrite = logger.confirm('Do you want to overwrite it?'); @@ -85,8 +87,8 @@ class ConfigCreateCommand extends Command { } } - Future _promptForConfigFileCreation( - ConfigFile configFileController, + Future _promptForConfigFileControllerCreation( + ConfigFileController configFileController, ) async { const examplePath = './locales/en.json'; @@ -166,14 +168,12 @@ class ConfigCreateCommand extends Command { ); // Create the config with the given - final config = configFileController.futureConfigToWrite( - config: LangSyncConfig( - outputDir: outputDir, - sourceFile: sourceLocalizationFilePath, - langs: targetLangsList, - instruction: instruction, - ), - ); + final config = LangSyncConfig( + outputDir: outputDir, + sourceFile: sourceLocalizationFilePath, + langs: targetLangsList, + instruction: instruction, + ).toMap(); final creationProgress = logger.customProgress( 'Creating ${configFileController.configFileName} file', @@ -186,7 +186,7 @@ class ConfigCreateCommand extends Command { '${configFileController.configFileName} file is created, updating it with your config...', ); - await configFileController.writeNewConfig(config); + configFileController.writeNewConfig(config); creationProgress.complete( '${configFileController.configFileName} file created & updated with your config successfully.', diff --git a/lib/src/commands/config_command/sub_commands/validate_command.dart b/lib/src/commands/config_command/sub_commands/validate_command.dart index 4ce8f2a..186d0d4 100644 --- a/lib/src/commands/config_command/sub_commands/validate_command.dart +++ b/lib/src/commands/config_command/sub_commands/validate_command.dart @@ -22,9 +22,9 @@ class ConfigValidateCommand extends Command { @override Future? run() async { - final configFiles = ConfigFile.configFilesInCurrentDir.toList(); + final configFiles = ConfigFileController.configFilesInCurrentDir.toList(); - ConfigFile configFile; + ConfigFileController configFile; try { configFile = _controllerFromFile(configFiles: configFiles); @@ -77,7 +77,7 @@ class ConfigValidateCommand extends Command { } } - ConfigFile _controllerFromFile({ + ConfigFileController _controllerFromFile({ required List configFiles, }) { if (configFiles.isEmpty) { @@ -99,6 +99,6 @@ class ConfigValidateCommand extends Command { throw ExitCode.software.code; } - return ConfigFile.controllerFromFile(configFiles.first); + return ConfigFileController.controllerFromFile(configFiles.first); } } diff --git a/lib/src/commands/start_command/start_command.dart b/lib/src/commands/start_command/start_command.dart index 9b14945..8895e65 100644 --- a/lib/src/commands/start_command/start_command.dart +++ b/lib/src/commands/start_command/start_command.dart @@ -43,10 +43,10 @@ class StartCommand extends Command { return ExitCode.config.code; } - ConfigFile configFile; + ConfigFileController configFile; try { - final configFiles = ConfigFile.configFilesInCurrentDir.toList(); + final configFiles = ConfigFileController.configFilesInCurrentDir.toList(); configFile = _controllerFromFile(configFiles: configFiles); } catch (e) { @@ -316,7 +316,7 @@ class StartCommand extends Command { return completer.future; } - ConfigFile _controllerFromFile({ + ConfigFileController _controllerFromFile({ required List configFiles, }) { if (configFiles.isEmpty) { @@ -338,6 +338,6 @@ class StartCommand extends Command { throw ExitCode.software.code; } - return ConfigFile.controllerFromFile(configFiles.first); + return ConfigFileController.controllerFromFile(configFiles.first); } } diff --git a/lib/src/etc/controllers/config_file.dart b/lib/src/etc/controllers/config_file.dart index c2960f7..91af6b8 100644 --- a/lib/src/etc/controllers/config_file.dart +++ b/lib/src/etc/controllers/config_file.dart @@ -7,47 +7,57 @@ import 'package:meta/meta.dart'; import 'package:langsync/src/etc/controllers/json.dart'; import 'package:langsync/src/etc/controllers/yaml.dart'; -import 'package:langsync/src/etc/models/config.dart'; -abstract class ConfigFile { - static ConfigFile fromArgResults(ArgResults argResults) { - if (argResults['json'] == true) { +/// Decided and manages the LangSync config file controller. +abstract class ConfigFileController { + /// Created the convenable [ConfigFileController] from the [argResults] of the CLI call. + /// If no [argResults] are provided, the default controller is returned. + static ConfigFileController fromArgResults(ArgResults argResults) { + final isJson = argResults['json'] == true; + final isYaml = argResults['yaml'] == true; + + if (isJson) { return JsonController(); - } else if (argResults['yaml'] == true) { + } else if (isYaml) { return YamlController(); } else { return defaultController; } } - static final Map _controllers = [ + /// The controllers that are supported by LangSync, the goal of this implementation is to make it easy to locate the expected config file name of each and to be able to add more in the future. + static final Map _controllers = [ YamlController(), JsonController(), ].asMap().map( (_, controller) => MapEntry(controller.configFileName, controller), ); - static Iterable get configFilesInCurrentDir => - Directory('.').listSync().where( - (file) { - final fileName = file.fileNameOnly; + /// Returns the existant expected config files of the controllers in the current directory. (the project directory) + static Iterable get configFilesInCurrentDir { + final fileEntities = Directory('.').listSync(); - return _controllers.containsKey(fileName); - }, - ); + return fileEntities.where( + (file) => _controllers.containsKey(file.fileNameOnly), + ); + } - static ConfigFile get defaultController => YamlController(); + /// Returns the default controller of LangSync, which is the [YamlController]. + /// This is the controller that is used when no controller is specified. + static ConfigFileController get defaultController => YamlController(); + /// The config file reference. File get configFileRef; // String get configFileExtension => configFileRef.path.split('.').last; - String get configFileName => configFileRef.fileNameOnly; + /// The config file content parsed as a map of dynamic keys and values. Future> parsed(); + /// ! @protected - Future> parsedConfigFileContent({ + Future> parsedConfigFileControllerContent({ required Future> Function(String fileContentAsString) loadConfigAsMapCallback, }) async { @@ -60,11 +70,13 @@ abstract class ConfigFile { return loadConfigAsMapCallback(asString); } + /// Creates the config file if it does not exist. Future createConfigFile() { return configFileRef.create(); } - Future writeToConfigFile(String writableContent) async { + /// Writes the raw [writableContent] string to the config file. + void writeToConfigFileController(String writableContent) { if (!configFileRef.existsSync()) { throw Exception('Config file does not exist.'); } @@ -75,6 +87,7 @@ abstract class ConfigFile { ); } + /// Validates the config file fields of LangSync, this is universal and static because it relies on the config file content parsed as a map. bool validateConfigFields(Map parsedConfigAsMap) { final langsyncConfig = parsedConfigAsMap['langsync'] as Map?; @@ -117,12 +130,7 @@ abstract class ConfigFile { } } - Map futureConfigToWrite({ - required LangSyncConfig config, - }) { - return config.toMap(); - } - + /// Logs the config file fields of LangSync, this is universal and static because it relies on the config file content parsed as a map. void iterateAndLogConfig(Map parsedYaml, Logger logger) { logger.info(''); @@ -136,6 +144,7 @@ abstract class ConfigFile { logger.info(''); } + /// Iterates over the config file fields of LangSync, this is universal and static because it relies on the config file content parsed as a map. void iterateOverConfig( Map config, { required void Function(MapEntry configEntry) callback, @@ -145,9 +154,11 @@ abstract class ConfigFile { } } - Future writeNewConfig(Map config); + /// Writes the new config file to the config file controller. + void writeNewConfig(Map config); - static ConfigFile controllerFromFile(FileSystemEntity file) { + /// Returns the config file controller from the [file]. + static ConfigFileController controllerFromFile(FileSystemEntity file) { final fileName = file.fileNameOnly; return _controllers[fileName]!; diff --git a/lib/src/etc/controllers/json.dart b/lib/src/etc/controllers/json.dart index 259a65b..6552214 100644 --- a/lib/src/etc/controllers/json.dart +++ b/lib/src/etc/controllers/json.dart @@ -4,13 +4,14 @@ import 'dart:io'; import 'package:langsync/src/etc/controllers/config_file.dart'; import 'package:langsync/src/etc/extensions.dart'; -class JsonController extends ConfigFile { +/// A JSON config file controller. +class JsonController extends ConfigFileController { @override File get configFileRef => File('./langsync.json'); @override Future> parsed() { - return super.parsedConfigFileContent( + return super.parsedConfigFileControllerContent( loadConfigAsMapCallback: (fileContentAsString) async { return await jsonDecode(fileContentAsString) as Map; }, @@ -18,8 +19,8 @@ class JsonController extends ConfigFile { } @override - Future writeNewConfig(Map config) { - return super.writeToConfigFile( + void writeNewConfig(Map config) { + return super.writeToConfigFileController( {'langsync': config}.toPrettyJson(), ); } diff --git a/lib/src/etc/controllers/yaml.dart b/lib/src/etc/controllers/yaml.dart index 49856b4..04b56b2 100644 --- a/lib/src/etc/controllers/yaml.dart +++ b/lib/src/etc/controllers/yaml.dart @@ -4,13 +4,13 @@ import 'package:langsync/src/etc/controllers/config_file.dart'; import 'package:langsync/src/etc/extensions.dart'; import 'package:yaml/yaml.dart' as yaml; -class YamlController extends ConfigFile { +class YamlController extends ConfigFileController { @override File get configFileRef => File('./langsync.yaml'); @override Future> parsed() async { - return super.parsedConfigFileContent( + return super.parsedConfigFileControllerContent( loadConfigAsMapCallback: (fileContentAsString) async { return await yaml.loadYaml(fileContentAsString) as Map; }, @@ -18,13 +18,13 @@ class YamlController extends ConfigFile { } @override - Future writeNewConfig(Map config) async { - await super.writeToConfigFile('langsync:\n'); + void writeNewConfig(Map config) async { + super.writeToConfigFileController('langsync:\n'); - await _iterateAndWriteToConfigFile(config); + await _iterateAndWriteToConfigFileController(config); } - Future _iterateAndWriteToConfigFile( + Future _iterateAndWriteToConfigFileController( Map config, ) async { super.iterateOverConfig( @@ -32,14 +32,14 @@ class YamlController extends ConfigFile { callback: (entry) async { if (entry.value is String) { if ((entry.value as String).isPathToFileOrFolder()) { - await super.writeToConfigFile( + super.writeToConfigFileController( "\n ${entry.key}: '${entry.value}' \n", ); return; } } - await super.writeToConfigFile( + super.writeToConfigFileController( '\n ${entry.key}: ${entry.value} \n', ); },