configurable/lib/dotenv/dotenv.dart

36 lines
1021 B
Dart
Raw Permalink Normal View History

2021-03-24 13:52:20 +07:00
library dotenv;
import 'dart:io';
import 'package:meta/meta.dart';
// load functions from parser
part 'dotenv_parser.dart';
2021-03-24 13:52:20 +07:00
// create in-memory variables store
2021-03-24 13:52:20 +07:00
var _env = Map<String, String>.from(Platform.environment);
// move from platforms env with default vars
2021-03-24 13:52:20 +07:00
Map<String, String> get env => _env;
// clean the env and set default from platform environment
2021-03-24 13:52:20 +07:00
Map clean() => _env = Map.from(Platform.environment);
// check the variables that defined in .env or platform env
2021-03-24 13:52:20 +07:00
bool isEveryDefined(Iterable<String> vars) =>
vars.every((k) => _env[k] != null && (_env[k]?.isNotEmpty ?? false));
// load file .env from project root
void load([String filename = '.env', DotenvParser psr = const DotenvParser()]) {
2021-03-24 13:52:20 +07:00
var file = File.fromUri(Uri.file(filename));
var lines = _verify(file);
_env.addAll(psr.parse(lines));
}
// verify file .env
2021-03-24 13:52:20 +07:00
List<String> _verify(File file) {
if (file.existsSync()) return file.readAsLinesSync();
stderr.writeln('[dotenv] Load failed: file not found: $file');
return [];
}