diff --git a/analysis_options.yaml b/analysis_options.yaml new file mode 100644 index 0000000..f424be0 --- /dev/null +++ b/analysis_options.yaml @@ -0,0 +1,32 @@ +# This file configures the analyzer, which statically analyzes Dart code to +# check for errors, warnings, and lints. +# +# The issues identified by the analyzer are surfaced in the UI of Dart-enabled +# IDEs (https://dart.dev/tools#ides-and-editors). The analyzer can also be +# invoked from the command line by running `flutter analyze`. + +# The following line activates a set of recommended lints for Flutter apps, +# packages, and plugins designed to encourage good coding practices. +include: package:flutter_lints/flutter.yaml + +# analyzer: +# errors: +# avoid_print: ignore + +linter: + # The lint rules applied to this project can be customized in the + # section below to disable rules from the `package:flutter_lints/flutter.yaml` + # included above or to enable additional rules. A list of all available lints + # and their documentation is published at https://dart.dev/tools/linter-rules. + # + # Instead of disabling a lint rule for the entire project in the + # section below, it can also be suppressed for a single line of code + # or a specific dart file by using the `// ignore: name_of_lint` and + # `// ignore_for_file: name_of_lint` syntax on the line or in the file + # producing the lint. + rules: + avoid_print: false # Uncomment to disable the `avoid_print` rule + # prefer_single_quotes: true # Uncomment to enable the `prefer_single_quotes` rule + +# Additional information about this file can be found at +# https://dart.dev/tools/analysis \ No newline at end of file diff --git a/bin/env_reader.dart b/bin/env_reader.dart index c65bcb3..d1f496d 100644 --- a/bin/env_reader.dart +++ b/bin/env_reader.dart @@ -1,40 +1,62 @@ -// ignore_for_file: avoid_print import 'dart:io'; + import 'package:args/args.dart'; import 'package:env_reader/src/env_encryption.dart'; part 'src/file.dart'; -part 'src/pubspec.dart'; -part 'src/json.dart'; part 'src/gitignore.dart'; +part 'src/json.dart'; +part 'src/pubspec.dart'; /// Dart runner for `EnvReader` library. void main(List arguments) async { - ArgParser runner = ArgParser() - ..addOption('input', - abbr: 'i', help: 'Input path of the .env file', mandatory: true) - ..addOption('output', - abbr: 'o', help: 'Output path for the encrypted .env file') - ..addOption('key', - abbr: 's', help: 'Secret key for encryption & decryption') + final runner = ArgParser() + ..addOption( + 'input', + abbr: 'i', + help: 'Input path of the .env file', + mandatory: true, + ) + ..addOption( + 'output', + abbr: 'o', + help: 'Output path for the encrypted .env file', + ) + ..addOption( + 'key', + abbr: 's', + help: 'Secret key for encryption & decryption', + ) ..addOption('model', help: 'Generate dart model to your desired file path') - ..addFlag('null-safety', - defaultsTo: false, negatable: false, help: 'Make the model null safety') - ..addFlag('obfuscate', - defaultsTo: true, help: 'Obfuscating generated values of model') - ..addFlag('pubspec', - defaultsTo: true, help: 'Inserting asset path to pubspec.yaml') - ..addFlag('gitignore', - defaultsTo: true, - help: 'Inserting .env input & output file into .gitignore') - ..addFlag('help', - defaultsTo: false, - abbr: 'h', - negatable: false, - help: 'Print this usage information'); + ..addFlag( + 'null-safety', + negatable: false, + help: 'Make the model null safety', + ) + ..addFlag( + 'obfuscate', + defaultsTo: true, + help: 'Obfuscating generated values of model', + ) + ..addFlag( + 'pubspec', + defaultsTo: true, + help: 'Inserting asset path to pubspec.yaml', + ) + ..addFlag( + 'gitignore', + defaultsTo: true, + help: 'Inserting .env input & output file into .gitignore', + ) + ..addFlag( + 'help', + abbr: 'h', + negatable: false, + help: 'Print this usage information', + ); try { - ArgResults argument = runner.parse(arguments); - if (argument["help"]) { + final argument = runner.parse(arguments); + if (argument['help']) { throw '\u001b[0mAvailable commands:'; } else { insertJson(from: argument); @@ -43,7 +65,7 @@ void main(List arguments) async { insertFile(from: argument); } } catch (e) { - print("\n\u001b[31m$e\u001b[0m\n"); + print('\n\u001b[31m$e\u001b[0m\n'); print(runner.usage); print('\n\u001b[32mdart run\u001b[0m ' '\u001b[36menv_reader\u001b[0m ' diff --git a/bin/src/gitignore.dart b/bin/src/gitignore.dart index 93a8fce..8f5a230 100644 --- a/bin/src/gitignore.dart +++ b/bin/src/gitignore.dart @@ -2,16 +2,16 @@ part of '../env_reader.dart'; /// A function to add your [input] into .gitignore file in the same directory. void insertGitignore({required ArgResults from}) { - bool insert = from["pubspec"]; + final bool insert = from['pubspec']; if (insert) { - File gitignore = File('.gitignore'); - String input = from["input"]!.toString(); - String? output = from["output"]?.toString(); - List lines = gitignore.readAsLinesSync(); - bool inputExisted = false; - bool outputExisted = false; + final gitignore = File('.gitignore'); + final input = from['input']!.toString(); + final output = from['output']?.toString(); + final lines = gitignore.readAsLinesSync(); + var inputExisted = false; + var outputExisted = false; - for (int i = 0; i < lines.length; i++) { + for (var i = 0; i < lines.length; i++) { if (lines[i].contains(input)) { inputExisted = true; break; @@ -19,7 +19,7 @@ void insertGitignore({required ArgResults from}) { } if (output != null) { - for (var item in lines) { + for (final item in lines) { if (item.contains(output)) { outputExisted = true; break; @@ -28,25 +28,25 @@ void insertGitignore({required ArgResults from}) { } if (!inputExisted) { - String comment = "# Env Reader related"; - int index = + const comment = '# Env Reader related'; + final index = lines.lastIndexWhere((line) => line.trim().startsWith(comment)); if (index != -1) { lines.insert(index + 1, input); } else { - lines.insert(0, "$comment\n$input"); + lines.insert(0, '$comment\n$input'); } } if (!outputExisted && output != null) { - String comment = "# Env Reader related"; - int index = + const comment = '# Env Reader related'; + final index = lines.lastIndexWhere((line) => line.trim().startsWith(comment)); - String export = outputExisted ? "" : output; + final export = outputExisted ? '' : output; if (index != -1) { lines.insert(index + 1, export); } else { - lines.insert(0, "$comment\n$export"); + lines.insert(0, '$comment\n$export'); } } diff --git a/bin/src/json.dart b/bin/src/json.dart index 4128722..7e79ad5 100644 --- a/bin/src/json.dart +++ b/bin/src/json.dart @@ -3,32 +3,32 @@ part of '../env_reader.dart'; /// A function to generate dart model out of .env file void insertJson({required ArgResults from}) { - String? model = from["model"]?.toString(); + final model = from['model']?.toString(); if (model != null) { // Fetching arguments - String path = model.replaceAll(RegExp(r'/[^/]+$'), "/"); - String name = model - .split("/") + final path = model.replaceAll(RegExp(r'/[^/]+$'), '/'); + final name = model + .split('/') .last - .split(".") + .split('.') .first - .split("_") - .map((e) => capitalize(e)) - .join(""); - String input = from["input"]!.toString(); - bool obfuscate = from['obfuscate']; - String data = File(input).readAsStringSync(); - bool nullSafety = from["null-safety"]; + .split('_') + .map(capitalize) + .join(); + final input = from['input']!.toString(); + final bool obfuscate = from['obfuscate']; + final data = File(input).readAsStringSync(); + final bool nullSafety = from['null-safety']; // Parsing .env toJson - Map json = toJson(data); + final json = toJson(data); // Generating model - String cast = json.entries.map((e) { - Type type = e.value.runtimeType; - String name = dartNamed(e.key); + final cast = json.entries.map((e) { + final type = e.value.runtimeType; + final name = dartNamed(e.key); if (obfuscate) { - String variable = nullSafety + final variable = nullSafety ? "Env.read<$type>('${e.key}') ?? ${type == bool ? 'false' : type == int ? '0' : type == double ? '0.0' : "'${e.key}'"}" : "Env.read<$type>('${e.key}')"; @@ -49,8 +49,8 @@ void insertJson({required ArgResults from}) { static const $type $name = ${type == String ? "'${e.value}'" : e.value ?? 'e.key'}; """; } - }).join("\n"); - String write = """ + }).join('\n'); + final write = """ // Env Reader Auto-Generated Model File // Created at ${DateTime.now()} // 🍔 [Buy me a coffee](https://www.buymeacoffee.com/nialixus) 🚀 @@ -65,7 +65,8 @@ $cast Directory(path).createSync(recursive: true); File(model).writeAsStringSync(write); print( - "\x1B[32m$input\x1B[0m successfully generated into \x1B[34m$model\x1B[0m 🎉"); + '\x1B[32m$input\x1B[0m successfully generated into \x1B[34m$model\x1B[0m 🎉', + ); } } @@ -110,8 +111,8 @@ String capitalize(String text) { /// A function to generate dart naming style of [String]. String dartNamed(String input) { - List words = input.split('_'); - String firstWord = words.first.toLowerCase(); - String restOfWords = words.sublist(1).map((word) => capitalize(word)).join(); + final words = input.split('_'); + final firstWord = words.first.toLowerCase(); + final restOfWords = words.sublist(1).map(capitalize).join(); return '$firstWord$restOfWords'; } diff --git a/bin/src/pubspec.dart b/bin/src/pubspec.dart index ada0496..2dfd4c4 100644 --- a/bin/src/pubspec.dart +++ b/bin/src/pubspec.dart @@ -4,28 +4,28 @@ part of '../env_reader.dart'; /// A function to check wether the output path (if its's an assets directory) already exist in `pubspec.yaml` or not. /// If it hasn't, this function inserting the output path given into your pubspec.yaml, and if its already there, this will do nothing. void insertPubspec({required ArgResults from}) { - bool insert = from["pubspec"] ?? true; - String input = from["input"]!.toString(); - String? output = from['output']?.toString(); + final bool insert = from['pubspec'] ?? true; + final input = from['input']!.toString(); + final output = from['output']?.toString(); if (insert && output != null) { - String output = - from["output"]!.toString().replaceAll(RegExp(r'/[^/]+$'), "/"); - if (output.startsWith("assets/")) { - File pubspec = File('pubspec.yaml'); - List lines = pubspec.readAsLinesSync(); - bool existed = false; + final output = + from['output']!.toString().replaceAll(RegExp(r'/[^/]+$'), '/'); + if (output.startsWith('assets/')) { + final pubspec = File('pubspec.yaml'); + final lines = pubspec.readAsLinesSync(); + var existed = false; - for (int i = 0; i < lines.length; i++) { - if (lines[i].contains("- $output")) { + for (var i = 0; i < lines.length; i++) { + if (lines[i].contains('- $output')) { existed = true; break; } } if (!existed) { - int flutterIndex = + final flutterIndex = lines.lastIndexWhere((line) => line.trim() == 'flutter:'); - int assetsIndex = + final assetsIndex = lines.lastIndexWhere((line) => line.trim() == 'assets:'); if (flutterIndex == -1) { lines.insert(lines.length - 1, 'flutter:\n assets:\n - $output'); @@ -41,9 +41,11 @@ void insertPubspec({required ArgResults from}) { } } else { print( - "\u001b[33m--pubspec\u001b[0m \u001b[2mflag ignored, due to output not in assets directory\u001b[0m"); + '\u001b[33m--pubspec\u001b[0m \u001b[2mflag ignored, due to output not in assets directory\u001b[0m', + ); print( - "\u001b[2mThis make the $input not accesible for every flutter platform\u001b[0m"); + '\u001b[2mThis make the $input not accesible for every flutter platform\u001b[0m', + ); } } } diff --git a/example/pubspec.lock b/example/pubspec.lock index ae2ab4f..29a54b5 100644 --- a/example/pubspec.lock +++ b/example/pubspec.lock @@ -13,42 +13,42 @@ packages: dependency: transitive description: name: async - sha256: "947bfcf187f74dbc5e146c9eb9c0f10c9f8b30743e341481c1e2ed3ecc18c20c" + sha256: d2872f9c19731c2e5f10444b14686eb7cc85c76274bd6c16e1816bff9a3bab63 url: "https://pub.dev" source: hosted - version: "2.11.0" + version: "2.12.0" boolean_selector: dependency: transitive description: name: boolean_selector - sha256: "6cfb5af12253eaf2b368f07bacc5a80d1301a071c73360d746b7f2e32d762c66" + sha256: "8aab1771e1243a5063b8b0ff68042d67334e3feab9e95b9490f9a6ebf73b42ea" url: "https://pub.dev" source: hosted - version: "2.1.1" + version: "2.1.2" characters: dependency: transitive description: name: characters - sha256: "04a925763edad70e8443c99234dc3328f442e811f1d8fd1a72f1c8ad0f69a605" + sha256: f71061c654a3380576a52b451dd5532377954cf9dbd272a78fc8479606670803 url: "https://pub.dev" source: hosted - version: "1.3.0" + version: "1.4.0" clock: dependency: transitive description: name: clock - sha256: cb6d7f03e1de671e34607e909a7213e31d7752be4fb66a86d29fe1eb14bfb5cf + sha256: fddb70d9b5277016c77a80201021d40a2247104d9f4aa7bab7157b7e3f05b84b url: "https://pub.dev" source: hosted - version: "1.1.1" + version: "1.1.2" collection: dependency: transitive description: name: collection - sha256: f092b211a4319e98e5ff58223576de6c2803db36221657b46c82574721240687 + sha256: "2f5709ae4d3d59dd8f7cd309b4e023046b57d8a6c82130785d2b0e5868084e76" url: "https://pub.dev" source: hosted - version: "1.17.2" + version: "1.19.1" crypto: dependency: transitive description: @@ -79,15 +79,15 @@ packages: path: ".." relative: true source: path - version: "2.0.3" + version: "2.1.0" fake_async: dependency: transitive description: name: fake_async - sha256: "511392330127add0b769b75a987850d136345d9227c6b94c96a04cf4a391bf78" + sha256: "6a95e56b2449df2273fd8c45a662d6947ce1ebb7aafe80e550a3f68297f3cacc" url: "https://pub.dev" source: hosted - version: "1.3.1" + version: "1.3.2" ffi: dependency: transitive description: @@ -138,6 +138,30 @@ packages: url: "https://pub.dev" source: hosted version: "0.6.7" + leak_tracker: + dependency: transitive + description: + name: leak_tracker + sha256: c35baad643ba394b40aac41080300150a4f08fd0fd6a10378f8f7c6bc161acec + url: "https://pub.dev" + source: hosted + version: "10.0.8" + leak_tracker_flutter_testing: + dependency: transitive + description: + name: leak_tracker_flutter_testing + sha256: f8b613e7e6a13ec79cfdc0e97638fddb3ab848452eff057653abd3edba760573 + url: "https://pub.dev" + source: hosted + version: "3.0.9" + leak_tracker_testing: + dependency: transitive + description: + name: leak_tracker_testing + sha256: "6ba465d5d76e67ddf503e1161d1f4a6bc42306f9d66ca1e8f079a47290fb06d3" + url: "https://pub.dev" + source: hosted + version: "3.0.1" lints: dependency: transitive description: @@ -150,87 +174,87 @@ packages: dependency: transitive description: name: matcher - sha256: "1803e76e6653768d64ed8ff2e1e67bea3ad4b923eb5c56a295c3e634bad5960e" + sha256: dc58c723c3c24bf8d3e2d3ad3f2f9d7bd9cf43ec6feaa64181775e60190153f2 url: "https://pub.dev" source: hosted - version: "0.12.16" + version: "0.12.17" material_color_utilities: dependency: transitive description: name: material_color_utilities - sha256: "9528f2f296073ff54cb9fee677df673ace1218163c3bc7628093e7eed5203d41" + sha256: f7142bb1154231d7ea5f96bc7bde4bda2a0945d2806bb11670e30b850d56bdec url: "https://pub.dev" source: hosted - version: "0.5.0" + version: "0.11.1" meta: dependency: transitive description: name: meta - sha256: "3c74dbf8763d36539f114c799d8a2d87343b5067e9d796ca22b5eb8437090ee3" + sha256: e3641ec5d63ebf0d9b41bd43201a66e3fc79a65db5f61fc181f04cd27aab950c url: "https://pub.dev" source: hosted - version: "1.9.1" + version: "1.16.0" path: dependency: transitive description: name: path - sha256: "8829d8a55c13fc0e37127c29fedf290c102f4e40ae94ada574091fe0ff96c917" + sha256: "75cca69d1490965be98c73ceaea117e8a04dd21217b37b292c9ddbec0d955bc5" url: "https://pub.dev" source: hosted - version: "1.8.3" + version: "1.9.1" sky_engine: dependency: transitive description: flutter source: sdk - version: "0.0.99" + version: "0.0.0" source_span: dependency: transitive description: name: source_span - sha256: "53e943d4206a5e30df338fd4c6e7a077e02254531b138a15aec3bd143c1a8b3c" + sha256: "254ee5351d6cb365c859e20ee823c3bb479bf4a293c22d17a9f1bf144ce86f7c" url: "https://pub.dev" source: hosted - version: "1.10.0" + version: "1.10.1" stack_trace: dependency: transitive description: name: stack_trace - sha256: c3c7d8edb15bee7f0f74debd4b9c5f3c2ea86766fe4178eb2a18eb30a0bdaed5 + sha256: "8b27215b45d22309b5cddda1aa2b19bdfec9df0e765f2de506401c071d38d1b1" url: "https://pub.dev" source: hosted - version: "1.11.0" + version: "1.12.1" stream_channel: dependency: transitive description: name: stream_channel - sha256: "83615bee9045c1d322bbbd1ba209b7a749c2cbcdcb3fdd1df8eb488b3279c1c8" + sha256: "969e04c80b8bcdf826f8f16579c7b14d780458bd97f56d107d3950fdbeef059d" url: "https://pub.dev" source: hosted - version: "2.1.1" + version: "2.1.4" string_scanner: dependency: transitive description: name: string_scanner - sha256: "556692adab6cfa87322a115640c11f13cb77b3f076ddcc5d6ae3c20242bedcde" + sha256: "921cd31725b72fe181906c6a94d987c78e3b98c2e205b397ea399d4054872b43" url: "https://pub.dev" source: hosted - version: "1.2.0" + version: "1.4.1" term_glyph: dependency: transitive description: name: term_glyph - sha256: a29248a84fbb7c79282b40b8c72a1209db169a2e0542bce341da992fe1bc7e84 + sha256: "7f554798625ea768a7518313e58f83891c7f5024f88e46e7182a4558850a4b8e" url: "https://pub.dev" source: hosted - version: "1.2.1" + version: "1.2.2" test_api: dependency: transitive description: name: test_api - sha256: "75760ffd7786fffdfb9597c35c5b27eaeec82be8edfb6d71d32651128ed7aab8" + sha256: fb31f383e2ee25fbbfe06b40fe21e1e458d14080e3c67e7ba0acfde4df4e0bbd url: "https://pub.dev" source: hosted - version: "0.6.0" + version: "0.7.4" typed_data: dependency: transitive description: @@ -247,13 +271,14 @@ packages: url: "https://pub.dev" source: hosted version: "2.1.4" - web: + vm_service: dependency: transitive description: - name: web - sha256: dc8ccd225a2005c1be616fe02951e2e342092edf968cf0844220383757ef8f10 + name: vm_service + sha256: "0968250880a6c5fe7edc067ed0a13d4bae1577fe2771dcf3010d52c4a9d3ca14" url: "https://pub.dev" source: hosted - version: "0.1.4-beta" + version: "14.3.1" sdks: - dart: ">=3.1.0 <4.0.0" + dart: ">=3.7.0-0 <4.0.0" + flutter: ">=3.18.0-18.0.pre.54" diff --git a/lib/env_reader.dart b/lib/env_reader.dart index ee62d48..f7ca7b9 100644 --- a/lib/env_reader.dart +++ b/lib/env_reader.dart @@ -16,12 +16,13 @@ /// int port = Env.read("PORT") ?? 8080; /// bool isDebug = Env.read("DEBUG") ?? false; /// ``` -library env_reader; +library; import 'dart:async'; import 'dart:developer'; import 'dart:io'; import 'dart:typed_data'; + import 'package:env_reader/src/env_encryption.dart'; import 'package:env_reader/src/env_parser.dart'; import 'package:http/http.dart'; @@ -82,7 +83,7 @@ class EnvReader { try { value = await source.data(key); } catch (e) { - log("\u001b[1mEnvReader:\u001b[31m Unable to load data\u001b[0m\n$e"); + log('\u001b[1mEnvReader:\u001b[31m Unable to load data\u001b[0m\n$e'); } } @@ -106,7 +107,7 @@ class EnvReader { /// ``` T? read(String key) { try { - return toJson[key]; + return toJson[key] as T?; } catch (e) { return null; } diff --git a/pubspec.yaml b/pubspec.yaml index 86b15ed..0084e58 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -27,8 +27,9 @@ environment: sdk: '>=3.1.0 <4.0.0' dependencies: - args: ^2.4.2 - cryptography: ^2.2.0 - http: ^1.1.0 + args: ^2.7.0 + cryptography: ^2.7.0 + http: ^1.3.0 dev_dependencies: + flutter_lints: ^5.0.0