2021-03-24 13:52:20 +07:00
|
|
|
library dotenv;
|
|
|
|
|
|
|
|
import 'dart:io';
|
|
|
|
|
|
|
|
import 'package:meta/meta.dart';
|
|
|
|
|
2021-03-24 14:10:25 +07:00
|
|
|
// load functions from parser
|
|
|
|
part 'dotenv_parser.dart';
|
2021-03-24 13:52:20 +07:00
|
|
|
|
2021-03-24 14:10:25 +07:00
|
|
|
// create in-memory variables store
|
2021-03-24 13:52:20 +07:00
|
|
|
var _env = Map<String, String>.from(Platform.environment);
|
|
|
|
|
2021-03-24 14:10:25 +07:00
|
|
|
// move from platforms env with default vars
|
2021-03-24 13:52:20 +07:00
|
|
|
Map<String, String> get env => _env;
|
|
|
|
|
2021-03-24 14:10:25 +07:00
|
|
|
// clean the env and set default from platform environment
|
2021-03-24 13:52:20 +07:00
|
|
|
Map clean() => _env = Map.from(Platform.environment);
|
|
|
|
|
2021-03-24 14:10:25 +07:00
|
|
|
// 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));
|
|
|
|
|
2021-03-24 14:10:25 +07:00
|
|
|
// 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));
|
|
|
|
}
|
|
|
|
|
2021-03-24 14:10:25 +07:00
|
|
|
// 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 [];
|
2021-03-24 14:10:25 +07:00
|
|
|
}
|