diff --git a/CHANGELOG.md b/CHANGELOG.md index 0203ea8..6b410a6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,22 +1,6 @@ -## 0.1.1 (2023-10-06) +## 0.1.2 (2023-10-07) ### Added -- Add `example` folder -- update docs intruction - -### Fixed - -- None. - -### Changed - -- None. - -### Deprecated - -- None. - -### Removed - -- None. \ No newline at end of file +- Improve documentation and performance +- Add unit test generate file diff --git a/lib/directory.dart b/lib/directory.dart index f332228..bce9050 100644 --- a/lib/directory.dart +++ b/lib/directory.dart @@ -2,74 +2,85 @@ import 'dart:io'; import 'package:init_folder_architecture/files.dart'; +// This function generates a folder with the given name if it doesn't already exist. +// It also creates three sub-folders inside the main folder: 'themes', 'core', and 'utils'. +// For each sub-folder, it checks if it already exists, and if not, it creates the sub-folder and generates a file inside it. +// Finally, it calls the 'subFolder' function passing the main folder name as an argument. + +/// The [name] parameter is the name of the folder to generate. + void generateFolder(String name) { + // Create a Directory object with the given name Directory root = Directory(name); + // Check if the directory doesn't exist if (!root.existsSync()) { - root.createSync(); + // Create the directory recursively + root.createSync(recursive: true); + + // Generate a file inside the created directory + generateFile(root.path); + + // Print a success message print('Folder $root created successfully'); } else { + // If the directory already exists, print a message print('Folder $root already exits'); } + // Create three additional directories: themes, core, and utils Directory themes = Directory('themes'); - - if (!themes.existsSync()) { - themes.createSync(); - generateFile(themes.path); - print('Folder $themes created successfully'); - } else { - print('Folder $themes already exits'); - } - Directory core = Directory('core'); - - if (!core.existsSync()) { - core.createSync(); - generateFile(core.path); - print('Folder $core created successfully'); - } else { - print('Folder $core already exits'); - } - Directory utils = Directory('utils'); - if (!utils.existsSync()) { - utils.createSync(); - generateFile(utils.path); - print('Folder $utils created successfully'); - } else { - print('Folder $utils already exits'); - } - + // Create the themes directory and generate a file inside it + themes + .create(recursive: true) + .then((value) => generateFile(value.path)) + .catchError((error) => print('Folder $error already exists')); + + // Create the core directory and generate a file inside it + core + .create(recursive: true) + .then((value) => generateFile(value.path)) + .catchError((error) => print('Folder $error already exists')); + + // Create the utils directory and generate a file inside it + utils + .create(recursive: true) + .then((value) => generateFile(value.path)) + .catchError((error) => print('Folder $error already exists')); + + // Call the subFolder function with the given name subFolder(name); } +// Define a function named subFolder that takes a string parameter named name void subFolder(String name) { + // Create a Directory object named repository with the path '$name/repository' Directory repository = Directory('$name/repository'); - if (!repository.existsSync()) { - repository.createSync(); - print('Sub Folder $repository created successfully'); - } else { - print('Sub Folder $repository already exits'); - } - + // Create a Directory object named modules with the path '$name/modules' Directory modules = Directory('$name/modules'); - if (!modules.existsSync()) { - modules.createSync(); - print('Sub Folder $modules created successfully'); - } else { - print('Sub Folder $modules already exits'); - } - + // Create a Directory object named data with the path '$name/data' Directory data = Directory('$name/data'); - if (!data.existsSync()) { - data.createSync(); - print('Sub Folder $data created successfully'); - } else { - print('Sub Folder $data already exits'); - } + // Create the repository directory and its parent directories if they don't exist + repository + .create(recursive: true) + .then((value) => print('Sub Folder $repository created successfully')) + .catchError((error) => print('Error creating $repository: $error')); + + // Create the modules directory and its parent directories if they don't exist + modules + .create(recursive: true) + .then((value) => print('Sub Folder $modules created successfully')) + .catchError((error) => print('Error creating $modules: $error')); + + // Create the data directory and its parent directories if they don't exist + data + .create(recursive: true) + .then((value) => print('Sub Folder $data created successfully')) + .catchError((error) => print('Error creating $data: $error')); } diff --git a/lib/files.dart b/lib/files.dart index fdaad0c..415d834 100644 --- a/lib/files.dart +++ b/lib/files.dart @@ -1,13 +1,39 @@ import 'dart:io'; +/// Generates a file with the given [name]. +/// Method to generate a file void generateFile(String name) { - File fileName = File('$name/$name.dart'); + // Create a File object with the given name and path + File fileName = File('$name/index.dart'); + + // Initialize a variable to store the content of the file + String content = ""; + + // Check the value of the name parameter using a switch statement + switch (name) { + // If name is 'app', set the content variable with specific comments + case 'app': + content = + "// export 'repository/name_file.dart';\n// export 'modules/name_file.dart';\n// export 'data/name_file.dart';"; + break; + + // For any other name, set the content variable with a generic comment + default: + content = "// export 'file_name.dart' "; + } + + // Check if the file already exists if (!fileName.existsSync()) { - var content = "// export '${fileName.path}' "; - fileName.createSync(); + // If the file doesn't exist, create it and its parent directories + fileName.createSync(recursive: true); + + // Write the content to the file fileName.writeAsStringSync(content); + + // Print a success message print('File $fileName created successfully'); } else { - print('File $fileName already exits'); + // If the file already exists, print a message + print('File $fileName already exists'); } } diff --git a/pubspec.yaml b/pubspec.yaml index 833002f..c1f0301 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -1,7 +1,7 @@ name: init_folder_architecture description: >- CLI utility will generate folder architecture to help you and save your time when creating the first Flutter project. -version: 0.1.1 +version: 0.1.2 homepage: https://github.com/anwar907/init_folder_architecture diff --git a/test/init_folder_architecture_test.dart b/test/init_folder_architecture_test.dart index bad3950..54d5797 100644 --- a/test/init_folder_architecture_test.dart +++ b/test/init_folder_architecture_test.dart @@ -1,7 +1,50 @@ +import 'dart:io'; + +import 'package:init_folder_architecture/files.dart'; import 'package:test/test.dart'; void main() { - test('calculate', () { - // expect(calculate(), 42); + // Group of unit tests for generating a file + group('unit test generate file', () { + // Test case: create a new file successfully + test('generateFile create a new file successfully', () { + // Create a test directory + final directoryName = 'test_directory'; + + // Create a file path within the test directory + final fileName = '$directoryName/$directoryName.dart'; + + // Create the file + File(fileName).createSync(recursive: true); + + // Insert file content + File(fileName).writeAsStringSync("// export '$fileName' "); + + // Create the test directory + Directory(directoryName).createSync(recursive: true); + + // Call the generateFile function + generateFile(directoryName); + + // Assert that the file exists + expect(File(fileName).existsSync(), isTrue); + }); + +// Test case: prints a message if the file already exists + test('generateFile prints a message if the file already exists', () async { + // Set up test data + final directoryName = 'test_directory'; + final fileName = '$directoryName/$directoryName.dart'; + + // Create the file and directory + File(fileName).createSync(recursive: true); + + // Call the method being tested + generateFile(directoryName); + + // Check if the file contains the expected message + expect(await File(fileName).readAsString(), + contains("// export '$fileName' ")); + }); }); }