Created
May 24, 2024 03:14
-
-
Save kaigouthro/37df8e1303018cc17cbef0694119a8d9 to your computer and use it in GitHub Desktop.
Fastest ever folder of ./index.js.map making for source map to files extractor, quick and dirty.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import json | |
import os | |
def extract_source_code(map_file_path): | |
""" | |
Extracts source code from a webpack source map file. | |
Args: | |
map_file_path (str): Path to the source map file. | |
Returns: | |
None. Creates a folder structure with extracted source files. | |
""" | |
print(map_file_path) | |
with open(map_file_path, "r") as map_file: | |
map_data = json.load(map_file) | |
output_dir = os.path.dirname(map_file_path) | |
# os.makedirs(output_dir, exist_ok=True) | |
for i, source_path in enumerate(map_data["sources"]): | |
# Handle webpack:// URLs | |
if source_path.startswith("webpack://"): | |
source_path = source_path.split("webpack://")[1] | |
try: | |
source_path = source_path.split(".\\src")[1] | |
except Exception as e: | |
continue | |
# Extract the source file name | |
source_file_name = os.path.basename(source_path) | |
pathways = output_dir + source_path.split(source_file_name)[0] | |
# Extract the source content | |
try: | |
source_content = map_data["sourcesContent"][i] | |
except Exception as e: | |
print(f"Error decoding source content: {e}") | |
continue | |
# Create subdirectories for nested paths | |
source_file_dir = os.path.join(pathways) | |
os.makedirs(source_file_dir, exist_ok=True) | |
# Write the source code to the file | |
source_file_path = (os.path.join(source_file_dir, source_file_name).replace(" ", "_").replace( | |
'"', "_").replace("'", "_").replace("?", "_")) | |
with open(source_file_path, "w") as source_file: | |
source_file.write(source_content) | |
if __name__ == "__main__": | |
# get all files with .js.map from the . folder and extracct all of them... | |
for root, dirs, files in os.walk(os.getcwd()): | |
for file in files: | |
if file.endswith(".js.map"): | |
extract_source_code(os.path.join(root, file)) | |
print(f"Source code for {file} extracted.") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment