checkEnv function

DoctorCheck checkEnv({
  1. String projectRoot = '.',
})

Keys in .env that .env-example doesn't have.

APP_KEY is left out: each copy of the app makes its own with metro make:key.

Implementation

DoctorCheck checkEnv({String projectRoot = '.'}) {
  final _Project p = _Project(projectRoot);
  final String? env = p.read('.env');
  final String? example = p.read('.env-example');
  if (env == null) {
    return const DoctorCheck('Env', DoctorStatus.skipped, 'No .env file');
  }
  if (example == null) {
    return const DoctorCheck(
      'Env',
      DoctorStatus.skipped,
      'No .env-example to compare .env with',
    );
  }

  final Set<String> shared = _envValues(example).keys.toSet();
  final List<String> missing = [
    for (final String key in _envValues(env).keys)
      if (key != 'APP_KEY' && !shared.contains(key)) key,
  ];
  if (missing.isEmpty) {
    return const DoctorCheck(
      'Env',
      DoctorStatus.ok,
      'Every key in .env is in .env-example',
    );
  }
  return DoctorCheck(
    'Env',
    DoctorStatus.problem,
    missing.length == 1
        ? '${missing.single} is in .env but not in .env-example'
        : '${missing.length} keys are in .env but not in .env-example',
    details: missing.length == 1 ? const [] : missing,
    fixes: [for (final String key in missing) 'add $key= to .env-example'],
  );
}