Skip to content

Instantly share code, notes, and snippets.

@chapimenge3
Created July 22, 2024 20:36
Show Gist options
  • Save chapimenge3/200a2aedf41fbd86b423ee005b488a75 to your computer and use it in GitHub Desktop.
Save chapimenge3/200a2aedf41fbd86b423ee005b488a75 to your computer and use it in GitHub Desktop.
Source Map Generator Function
import json
import os
from pathlib import Path
def generate_sources_from_map(source_map_path):
# Read the source map file
with open(source_map_path, 'r') as f:
source_map = json.load(f)
# Get the current working directory
current_dir = Path.cwd()
# Iterate through the sources and sourcesContent
for source, content in zip(source_map['sources'], source_map['sourcesContent']):
# Skip if the file is under node_modules
if 'node_modules' in source:
print(f"Skipping {source} as it's under node_modules")
continue
# Create a Path object for the source file
source_path = Path(source)
# Ensure the path is relative and doesn't go outside the current directory
try:
relative_path = source_path.relative_to('/')
except ValueError:
relative_path = source_path
# Resolve the full path
full_path = current_dir / relative_path
# Check if the resolved path is within or equal to the current directory
if current_dir not in full_path.parents and current_dir != full_path:
print(f"Skipping {source} as it would be created outside the current directory")
continue
# Create necessary directories
full_path.parent.mkdir(parents=True, exist_ok=True)
# Write the content to the file
with open(full_path, 'w') as f:
f.write(content)
print(f"Created {full_path}")
# Example usage
if __name__ == "__main__":
generate_sources_from_map('path/to/your/source-map.json')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment