smartModeConfig method

Future<void> smartModeConfig({
  1. required String name,
  2. required String path,
})

Implementation

Future<void> smartModeConfig({
  required String name,
  required String path,
}) async {
  final rootPath = path;
  final sourceFilePath = name;

  const suffix = '_wm.dart';
  if (!sourceFilePath.endsWith(suffix)) {
    throw CommandLineUsageException(
      message: 'Widget model dart file name should end with "$suffix".',
    );
  }

  // export of basename
  basename = p.basename(sourceFilePath).replaceAll(suffix, '');

  final root = Directory(rootPath);
  final sourceFile = File(sourceFilePath);

  if (!root.existsSync()) {
    throw NonExistentFolderException(rootPath);
  }

  if (!sourceFile.existsSync()) {
    throw NonExistentFileException(sourceFilePath);
  }

  final lib = Directory(p.join(root.path, 'lib'));

  if (!lib.existsSync()) {
    throw CommandLineUsageException(
      message: 'Root folder must contain "lib" folder.',
    );
  }

  if (!p.isWithin(lib.path, sourceFile.path)) {
    throw CommandLineUsageException(
      message: 'Widget model dart file should be placed '
          'in ${"project's"} lib folder.',
    );
  }

  final relativePath = p.relative(sourceFile.parent.path, from: lib.path);
  final test = Directory(p.join(root.path, 'test'));
  // export of targetDirectory
  targetDirectory = Directory(p.join(test.path, relativePath));
}