configureEnv function

Future<String> configureEnv({
  1. bool forFlutter = false,
  2. String? defaultEnvPath,
})

Configure env file

Implementation

Future<String> configureEnv({
  bool forFlutter = false,
  String? defaultEnvPath,
}) async {
  // Get the value by prompt
  final envPath = ask(
    blue('Path to env file:'),
    defaultValue: defaultEnvPath ?? defaultValues[CmdOption.env] as String,
  );

  // confirm that the file exists
  if (!exists(envPath)) {
    printerr(red('File does not exist: $envPath\n'));

    final created = createEnvFile(envPath);
    if (!created) exit(1);
  }
  // confirm that the file matches the expected format
  final validated = validateEnvFile(envPath);
  if (!validated) {
    printerr(red('Env file not validated ❌'));
    exit(1);
  }

  print(green('Env file validated ✅'));

  // Return current path if not for flutter
  if (!forFlutter) return envPath;

  // Ask the user if they wish to load the supabase client using the env file
  final loadClientWithEnv = confirm(
    blue(
      'Would you like to use the env file to load '
      'the Supabase client in your application?',
    ),
    defaultValue: true,
  );

  // Write the location of the env file to the assets in pubspec
  if (loadClientWithEnv) {
    final added = await addEnvFileToAssets(envPath);
    print(
      green('${added ? 'Env file added to' : 'Env file present in'} '
          'flutter assets ✅'),
    );
  }

  return envPath;
}