validate static method
Validate .env exists, is complete, and database is reachable. Returns parsed env map on success, exits on failure.
Implementation
static Future<Map<String, String>> validate(String serverDir) async {
final File envFile = File('$serverDir${PlatformInfo.separator}.env');
final File exampleFile =
File('$serverDir${PlatformInfo.separator}.env.example');
// 1. Check .env exists
if (!envFile.existsSync()) {
Logger.fail('.env not found in $serverDir');
if (exampleFile.existsSync()) {
Logger.detail('Copy .env.example to .env and fill in values.');
}
exit(1);
}
// 2. Parse .env
final Map<String, String> env = _parseEnvFile(envFile);
// 3. Check completeness against .env.example
if (exampleFile.existsSync()) {
final Set<String> requiredKeys = _parseEnvFile(exampleFile).keys.toSet();
final List<String> missing = requiredKeys
.where((String key) => !env.containsKey(key))
.toList();
if (missing.isNotEmpty) {
Logger.fail('.env missing keys: ${missing.join(', ')}');
exit(1);
}
}
// 4. Check key values are not empty
final List<String> emptyKeys = env.entries
.where((MapEntry<String, String> e) => e.value.isEmpty)
.map((MapEntry<String, String> e) => e.key)
.toList();
if (emptyKeys.isNotEmpty) {
Logger.fail('.env has empty values: ${emptyKeys.join(', ')}');
exit(1);
}
Logger.ok('.env validated (${env.length} keys)');
// 5. Test database connection
final String? dbUrl = env['DATABASE_URL'];
if (dbUrl != null) {
await _testDatabaseConnection(dbUrl);
}
return env;
}