createEnvFile function

bool createEnvFile(
  1. String envPath, {
  2. ({bool? create, String? key, String? url})? defaults,
})

Create env file

Implementation

bool createEnvFile(
  String envPath, {
  // Used for testing
  ({bool? create, String? url, String? key})? defaults,
}) {
  final created = confirm(
    blue('Would you like to create the env file? ($envPath)'),
    defaultValue: defaults?.create ?? true,
  );

  // Create the file and write the values
  if (created) {
    // Create the file
    touch(envPath, create: true);
    // Get the variables
    final url = ask(
      blue('Enter the url to your Supabase instance:'),
      validator: Ask.url(protocols: ['http', 'https']),
      defaultValue: defaults?.url ?? '',
    );
    final key = ask(
      blue('Enter the anon key for your Supabase instance:'),
      hidden: true,
      defaultValue: defaults?.key ?? '',
    );
    // Write the values to the file
    withOpenFile(envPath, (file) {
      file
        ..write('SUPABASE_URL=$url')
        ..append('SUPABASE_ANON_KEY=$key');

      print(
        green('\nSupabase credentials written to '
            'the environment file created at $envPath\n'),
      );
    });
  }
  return created;
}