getExtractedData function
Reads the current extension settings back out of an already-enabled project.
It parses web/manifest.json for the name, version, and description, and
web/index.html for the popup width and height. Used by the update
command so unchanged fields keep their current values. Exits the process if
the project has not been enabled yet or the files are missing.
Implementation
ExtensionDetails getExtractedData() {
//TEMP FILE TO GET THE DATA FROM THE FILE
FileManipulation tempFile = FileManipulation();
ExtensionDetails extensionDetails = ExtensionDetails();
try {
//SET THE FILE PATH FOR MANIFEST.JSON
tempFile.setFilePath(filePath: "web", fileName: "manifest.json");
//CONVERT THE JSON FILE TO A MAP
Map<String, dynamic> manifest = jsonDecode(tempFile.getFileAsString());
//CHECK IF THE EXTENSION IS ENABLED
int isExtensionEnabled = manifest["manifest_version"] ?? 0;
if (isExtensionEnabled != 0) {
//GET THE DATA FROM THE MAP
extensionDetails.name = manifest["name"];
extensionDetails.version = manifest["version"];
extensionDetails.description = manifest["description"];
//SET THE FILE PATH FOR INDEX.HTML
tempFile.setFilePath(filePath: "web", fileName: "index.html");
//GET THE HTML FILE AS A DOCUMENT
Document document = tempFile.document;
//FIND THE HEAD TAG
List<Element> html = document.getElementsByTagName("html");
//GET THE STYLE ATTRIBUTE
String style = html[0].attributes["style"]!;
//IN EXAMPLE
//style="height: 456px; width: 234px;"
//GET CURRENT HEIGHT (KEEPING THE px SUFFIX)
extensionDetails.height = style.substring(
style.indexOf("height: ") + 8,
style.indexOf(";"),
);
//GET CURRENT WIDTH (KEEPING THE px SUFFIX)
extensionDetails.width = style.substring(
style.indexOf("width: ") + 7,
style.lastIndexOf(";"),
);
return extensionDetails;
} else {
Logger.error("❌ Extension is not enabled\n");
Logger.normal(
"To enable the extension, run the command [dart run extension_enabler enable]\n");
exit(1);
}
} on FileSystemException catch (e) {
//IF THE FILE IS NOT FOUND
getErrorForFileNotFound();
Logger.error("❌ ${e.toString()}");
exit(1);
} catch (e) {
Logger.error("❌ ${e.toString()}");
exit(1);
}
}