Add configurable with functions

Add more tests for call functions and fixed
This commit is contained in:
2021-03-24 14:10:25 +07:00
parent a51d5fe00b
commit 33b6355bcf
7 changed files with 67 additions and 40 deletions

View File

@@ -1,54 +1,35 @@
/// Loads environment variables from a `.env` file.
///
/// ## usage
///
/// Once you call [load], the top-level [env] map is available.
/// You may wish to prefix the import.
///
/// import 'package:dotenv/dotenv.dart' show load, env;
///
/// void main() {
/// load();
/// var x = env['foo'];
/// // ...
/// }
///
/// Verify required variables are present:
///
/// const _requiredEnvVars = const ['host', 'port'];
/// bool get hasEnv => isEveryDefined(_requiredEnvVars);
library dotenv;
import 'dart:io';
import 'package:meta/meta.dart';
part 'parser.dart';
// load functions from parser
part 'dotenv_parser.dart';
// create in-memory variables store
var _env = Map<String, String>.from(Platform.environment);
/// A copy of [Platform.environment](dart:io) including variables loaded at runtime from a file.
// move from platforms env with default vars
Map<String, String> get env => _env;
/// Overwrite [env] with a new writable copy of [Platform.environment](dart:io).
// clean the env and set default from platform environment
Map clean() => _env = Map.from(Platform.environment);
/// True if all supplied variables have nonempty value; false otherwise.
/// Differs from [containsKey](dart:core) by excluding null values.
/// Note [load] should be called first.
// check the variables that defined in .env or platform env
bool isEveryDefined(Iterable<String> vars) =>
vars.every((k) => _env[k] != null && (_env[k]?.isNotEmpty ?? false));
/// Read environment variables from [filename] and add them to [env].
/// Logs to [stderr] if [filename] does not exist.
void load([String filename = '.env', Parser psr = const Parser()]) {
// load file .env from project root
void load([String filename = '.env', DotenvParser psr = const DotenvParser()]) {
var file = File.fromUri(Uri.file(filename));
var lines = _verify(file);
_env.addAll(psr.parse(lines));
}
// verify file .env
List<String> _verify(File file) {
if (file.existsSync()) return file.readAsLinesSync();
stderr.writeln('[dotenv] Load failed: file not found: $file');
return [];
}
}

View File

@@ -1,8 +1,6 @@
part of dotenv;
/// Creates key-value pairs from strings formatted as environment
/// variable definitions.
class Parser {
class DotenvParser {
static const _singleQuot = "'";
static const _keyword = 'export';
@@ -10,11 +8,9 @@ class Parser {
static final _surroundQuotes = RegExp(r'''^(['"])(.*)\1$''');
static final _bashVar = RegExp(r'(?:\\)?(\$)(?:{)?([a-zA-Z_][\w]*)+(?:})?');
/// [Parser] methods are pure functions.
const Parser();
// constructor for parser
const DotenvParser();
/// Creates a [Map](dart:core) suitable for merging into [Platform.environment](dart:io).
/// Duplicate keys are silently discarded.
Map<String, String> parse(Iterable<String> lines) {
var out = <String, String>{};
lines.forEach((line) {
@@ -79,7 +75,6 @@ class Parser {
bool _isValid(String s) => s.isNotEmpty && s.contains('=');
/// [null] is a valid value in a Dart map, but the env var representation is empty string, not the string 'null'
bool _has(Map<String, String> map, String key) =>
map.containsKey(key) && map[key] != null;