mirror of
https://github.com/java-james/flutter_dotenv.git
synced 2025-07-14 21:11:41 +08:00
@ -1,9 +1,32 @@
|
|||||||
import 'dart:io';
|
import 'dart:io';
|
||||||
|
|
||||||
|
import 'package:args/args.dart';
|
||||||
import 'package:dotenv/dotenv.dart' as dotenv;
|
import 'package:dotenv/dotenv.dart' as dotenv;
|
||||||
|
|
||||||
|
final _argPsr = new ArgParser()
|
||||||
|
..addFlag('help', abbr: 'h', negatable: false, help: 'Print this help text.')
|
||||||
|
..addOption('file',
|
||||||
|
abbr: 'f',
|
||||||
|
defaultsTo: '.env',
|
||||||
|
help: 'File to read.\nProvides environment variable definitions, one per line.');
|
||||||
|
|
||||||
/// Prints the [env] map.
|
/// Prints the [env] map.
|
||||||
void main() {
|
///
|
||||||
dotenv.load();
|
/// ## usage
|
||||||
|
///
|
||||||
|
/// pub global run dotenv --help
|
||||||
|
void main(List<String> argv) {
|
||||||
|
var opts = _argPsr.parse(argv);
|
||||||
|
|
||||||
|
if (opts['help']) return _usage();
|
||||||
|
|
||||||
|
dotenv.load(opts['file']);
|
||||||
stdout.writeln(dotenv.env);
|
stdout.writeln(dotenv.env);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void _usage() {
|
||||||
|
_p('Parse variable definitions from a file, print the environment and exit.');
|
||||||
|
_p('Usage: pub global run dotenv [-f <file>]\n${_argPsr.usage}');
|
||||||
|
}
|
||||||
|
|
||||||
|
void _p(String msg) => stdout.writeln(msg);
|
||||||
|
44
bin/new.dart
44
bin/new.dart
@ -1,25 +1,40 @@
|
|||||||
import 'dart:io';
|
import 'dart:io';
|
||||||
|
|
||||||
const String envFile = '.env';
|
import 'package:args/args.dart';
|
||||||
|
|
||||||
const String gitignore = '.gitignore';
|
const String gitignore = '.gitignore';
|
||||||
|
|
||||||
|
final _argPsr = new ArgParser()
|
||||||
|
..addFlag('help', abbr: 'h', negatable: false, help: 'Print this help text.')
|
||||||
|
..addOption('file',
|
||||||
|
abbr: 'f',
|
||||||
|
defaultsTo: '.env',
|
||||||
|
help: 'File to create.\nDo not check secrets into version control!');
|
||||||
|
|
||||||
/// Creates `.env` if the file does not exist, and adds it to `.gitignore`.
|
/// Creates `.env` if the file does not exist, and adds it to `.gitignore`.
|
||||||
void main() {
|
///
|
||||||
|
/// ## usage
|
||||||
|
///
|
||||||
|
/// pub global run dotenv:new --help
|
||||||
|
void main(List<String> args) {
|
||||||
|
var opts = _argPsr.parse(args);
|
||||||
|
|
||||||
|
if (opts['help']) return _usage();
|
||||||
|
|
||||||
|
String envFile = opts['file'];
|
||||||
_touch(envFile);
|
_touch(envFile);
|
||||||
|
|
||||||
var g = _touch(gitignore);
|
var g = _touch(gitignore);
|
||||||
if (_anyLineContains(envFile, g)) return _handleError();
|
if (_anyLineContains(envFile, g)) return _handleIgnored(envFile);
|
||||||
|
|
||||||
g.writeAsStringSync('$envFile*\n', mode: FileMode.APPEND);
|
_appendTo(g, '$envFile*');
|
||||||
stdout.writeln('Added \'$envFile*\' to $gitignore.');
|
|
||||||
}
|
}
|
||||||
|
|
||||||
bool _anyLineContains(String str, File f) =>
|
bool _anyLineContains(String str, File f) =>
|
||||||
f.readAsLinesSync().any((line) => line.contains(str));
|
f.readAsLinesSync().any((line) => line.contains(str));
|
||||||
|
|
||||||
void _handleError() {
|
void _handleIgnored(String filename) {
|
||||||
stderr
|
_pErr("Found $gitignore with line containing '$filename'; exiting.");
|
||||||
.writeln('Found $gitignore with line containing \'$envFile\'; exiting.');
|
|
||||||
exitCode = 1;
|
exitCode = 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -31,3 +46,16 @@ File _touch(String filename) {
|
|||||||
stdout.writeln('Created file: $filename');
|
stdout.writeln('Created file: $filename');
|
||||||
return f;
|
return f;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void _appendTo(File f, String line) {
|
||||||
|
f.writeAsStringSync('$line\n', mode: FileMode.APPEND);
|
||||||
|
_p("Added '$line' to ${f.path}.");
|
||||||
|
}
|
||||||
|
|
||||||
|
void _usage() {
|
||||||
|
_p('Create a new file and gitignore it.');
|
||||||
|
_p('Usage: pub global run dotenv:new [-f <file>]\n${_argPsr.usage}');
|
||||||
|
}
|
||||||
|
|
||||||
|
void _p(String msg) => stdout.writeln(msg);
|
||||||
|
void _pErr(String msg) => stderr.writeln(msg);
|
||||||
|
@ -1,6 +1,5 @@
|
|||||||
#!/bin/sh
|
#!/bin/sh
|
||||||
|
|
||||||
# Add to `.git/hooks/pre-commit`:
|
# Autoformat code in-place, per style guidelines.
|
||||||
# exec ./tool/fmt.sh
|
|
||||||
|
|
||||||
dartfmt -w bin lib test example
|
dartfmt -w bin lib test example
|
||||||
|
Reference in New Issue
Block a user