migrateToWeb function

Future<void> migrateToWeb(
  1. List<String> args
)

Migrates a Flutter project to a new project that can be run on Flutter Web. Arguments: Project name (defaults to PROJECT NAME) -o overwrite. If true, creates a fresh project even if one exists. If false, only updates changed dart files.

Implementation

/// Arguments:
/// Project name (defaults to PROJECT NAME)
/// [-o] overwrite. If true, creates a fresh project even if one exists. If false, only updates changed dart files.
Future<void> migrateToWeb(List<String> args) async {
  final String projectName = Directory.current.path.split('/').last;

  final ArgResults results = getArgs(projectName: projectName, args: args);
  final Logger logger = setUpLogger(results: results);

  final String newProjectName = results['name'].toString().trim();

  logger.stdout(
    'Migrating $projectName to a web project named $newProjectName.',
  );

  if (!hasPubspec()) {
    logger.stderr(
      'No pubspec.yaml file found. Please run `flutter pub run migrate_to_web` from the root of your Fluter project.',
    );
  }

  final Directory newProjectDirectory = Directory('../$newProjectName')
    ..createSync();

  try {
    copyDirectory(Directory.current, newProjectDirectory);

    _addWebDirectory(
      projectName: newProjectName,
      directory: newProjectDirectory,
    );

    _updatePubspec(
      projectDirectory: newProjectDirectory,
      oldName: projectName,
      newName: newProjectName,
    );

    _updateLibImports(
      oldName: projectName,
      newName: newProjectName,
      dir: Directory(newProjectDirectory.path + '/lib'),
    );

    // await _runFlutterPackagesGet(newProjectName);
    logger.stdout('Successfully migrated project!');
  } catch (e) {
    logger.stderr('Error, deleting migration attempt: $e');
    newProjectDirectory.deleteSync(recursive: true);
  }
}