nuke.dart 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. // ignore_for_file: avoid_print
  2. import 'dart:convert';
  3. import 'dart:io';
  4. T? removeBlankEntries<T>(T? json) {
  5. // This function is protected by BSD 3-Clause License
  6. // Changes made to this section are allow removing of '' values from JSON
  7. /*
  8. https://pub.dev/documentation/swiss_knife/latest/swiss_knife/removeEmptyEntries.html
  9. Copyright 2014, the Dart project authors. All rights reserved.
  10. Redistribution and use in source and binary forms, with or without
  11. modification, are permitted provided that the following conditions are
  12. met:
  13. * Redistributions of source code must retain the above copyright
  14. notice, this list of conditions and the following disclaimer.
  15. * Redistributions in binary form must reproduce the above
  16. copyright notice, this list of conditions and the following
  17. disclaimer in the documentation and/or other materials provided
  18. with the distribution.
  19. * Neither the name of Google Inc. nor the names of its
  20. contributors may be used to endorse or promote products derived
  21. from this software without specific prior written permission.
  22. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  23. "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  24. LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  25. A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  26. OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  27. SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  28. LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  29. DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  30. THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  31. (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  32. OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  33. */
  34. if (json == null) {
  35. return null;
  36. }
  37. if (json is List) {
  38. json.removeWhere((e) => e == null);
  39. json.forEach(removeBlankEntries);
  40. } else if (json is Map) {
  41. json.removeWhere(
  42. (key, value) => key == null || value == null || value == '',
  43. );
  44. json.values.forEach(removeBlankEntries);
  45. }
  46. return json;
  47. }
  48. Future<void> processJsonFiles() async {
  49. final Directory directory = Directory.current;
  50. final List<FileSystemEntity> files = directory.listSync();
  51. for (final file in files) {
  52. try {
  53. if (file is File && file.path.endsWith('.json')) {
  54. final String contents = await file.readAsString();
  55. final dynamic json = jsonDecode(contents);
  56. final dynamic processedJson = removeBlankEntries(json);
  57. file.writeAsString(
  58. const JsonEncoder.withIndent(' ').convert(processedJson),
  59. );
  60. print('🥞 Task successful on: ${file.path}');
  61. }
  62. } catch (e) {
  63. print('💥 Task failed on: ${file.path}: $e');
  64. }
  65. }
  66. }
  67. void main() async {
  68. processJsonFiles();
  69. }