build: Nuke script now able to remove empty JSON/File

This commit is contained in:
Pun Butrach 2025-04-14 17:50:15 +07:00
parent ba758f3d8b
commit 007dce800e
No known key found for this signature in database
GPG Key ID: DBA94253E1D3F267

View File

@ -1,9 +1,9 @@
// ignore_for_file: avoid_print // ignore_for_file: avoid_print, prefer_foreach
import 'dart:convert'; import 'dart:convert';
import 'dart:io'; import 'dart:io';
T? removeBlankEntries<T>(T? json) { dynamic removeBlankEntries(dynamic json) {
// This function is protected by BSD 3-Clause License // This function is protected by BSD 3-Clause License
// Changes made to this section are allow removing of '' values from JSON // Changes made to this section are allow removing of '' values from JSON
@ -37,23 +37,47 @@ T? removeBlankEntries<T>(T? json) {
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/ */
if (json == null) { if (json == null) {
return null; return null;
} }
if (json is List) { if (json is List) {
json.removeWhere((e) => e == null); for (int i = json.length - 1; i >= 0; i--) {
json.forEach(removeBlankEntries); final processedElement = removeBlankEntries(json[i]);
// If the list is empty after removing nulls, return null to remove it. if (processedElement == null) {
json.removeAt(i);
} else {
json[i] = processedElement;
}
}
return json.isEmpty ? null : json; return json.isEmpty ? null : json;
} else if (json is Map) { } else if (json is Map) {
json.removeWhere( final keysToRemove = <dynamic>{};
(key, value) => key == null || value == null || value == '', final keys = json.keys.toList();
);
json.values.forEach(removeBlankEntries);
// If the map is empty after removing blank entries, return null to remove it.
return json.isEmpty ? null : json;
for (final key in keys) {
if (key == null) {
keysToRemove.add(key);
continue;
} }
final processedValue = removeBlankEntries(json[key]);
if (processedValue == null || processedValue == '') {
keysToRemove.add(key);
} else {
json[key] = processedValue;
}
}
for (final key in keysToRemove) {
json.remove(key);
}
return json.isEmpty ? null : json;
}
if (json is String && json.isEmpty) {
return null;
}
return json; return json;
} }
@ -62,33 +86,44 @@ Future<void> processJsonFiles() async {
final List<FileSystemEntity> files = directory.listSync(); final List<FileSystemEntity> files = directory.listSync();
for (final file in files) { for (final file in files) {
if (!file.path.endsWith('.json') || file is! File) {
continue;
}
try { try {
if (file is File && file.path.endsWith('.json')) { final contents = await file.readAsString();
final String contents = await file.readAsString(); if (contents.trim().isEmpty) {
final dynamic json = jsonDecode(contents); print('🗑️ File is empty, deleting: ${file.path}');
final dynamic processedJson = removeBlankEntries(json); await file.delete();
bool isEmpty = false; continue;
if (processedJson is Map) {
isEmpty = processedJson.values.every((value) => value is Map && value.isEmpty);
} }
if (processedJson == null || isEmpty) { dynamic jsonInput;
try {
jsonInput = jsonDecode(contents);
} on FormatException catch (e, stackTrace) {
print('💥 Invalid JSON in file: ${file.path}: $e');
print(stackTrace);
continue;
}
final dynamic processedJson = removeBlankEntries(jsonInput);
if (processedJson == null) {
await file.delete(); await file.delete();
print('🗑️ File deleted: ${file.path}'); print('🗑️ File resulted in empty JSON, deleted: ${file.path}');
} else { } else {
await file.writeAsString( final prettyJson = const JsonEncoder.withIndent(
const JsonEncoder.withIndent(' ').convert(processedJson), ' ', // Two spaces
); ).convert(processedJson);
await file.writeAsString(prettyJson);
print('🥞 Task successful on: ${file.path}'); print('🥞 Task successful on: ${file.path}');
} }
} } catch (e, stackTrace) {
} catch (e) {
print('💥 Task failed on: ${file.path}: $e'); print('💥 Task failed on: ${file.path}: $e');
print(stackTrace);
} }
} }
} }
void main() async { void main() async {
processJsonFiles(); await processJsonFiles();
} }