Last active
August 8, 2023 00:19
-
-
Save luandro/376465766129340bfddf21cf72b2c21f to your computer and use it in GitHub Desktop.
Generates a Mapeo config defaults.json based on the items within the presets folder and their geometry fields. Execute from the Mapeo configuration project's folder root.
This file contains 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
#!/bin/bash | |
# Path to the presets folder | |
presetsFolder="./presets" | |
# Path to defaults.json | |
defaultsPath="./defaults.json" | |
# Temp file for jq output | |
tempFile=$(mktemp) | |
# Iterate over each file in the presets folder | |
for presetFile in $presetsFolder/*; do | |
# Get the preset name without the file extension | |
presetName=$(basename "$presetFile" .json) | |
# Get the geometry types from the preset file | |
geometries=$(jq -r '.geometry[]' "$presetFile") | |
# Iterate over each geometry type | |
for geometry in $geometries; do | |
# Check if the preset name already exists in the corresponding array in defaults.json | |
if ! jq --arg presetName "$presetName" -e ".${geometry} | index(\$presetName)" $defaultsPath > /dev/null; then | |
# If not, add the preset name to the array | |
jq --arg presetName "$presetName" ".${geometry} += [\$presetName]" $defaultsPath > "$tempFile" && mv "$tempFile" $defaultsPath | |
fi | |
done | |
done | |
# Iterate over each geometry type in defaults.json | |
for geometry in area line point vertex relation; do | |
# Get the list of presets for the current geometry type | |
presets=$(jq -r ".${geometry}[]" $defaultsPath) | |
# Iterate over each preset | |
for preset in $presets; do | |
# Check if the preset file exists in the presets folder | |
if [ ! -f "$presetsFolder/$preset.json" ]; then | |
# If not, remove the preset from the array | |
jq --arg preset "$preset" ".${geometry} -= [\$preset]" $defaultsPath > "$tempFile" && mv "$tempFile" $defaultsPath | |
fi | |
done | |
done |
This file contains 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
const fs = require('fs'); | |
const path = require('path'); | |
// Path to the presets folder | |
const presetsFolder = path.join(__dirname, 'presets'); | |
// Read the current contents of defaults.json | |
const defaultsPath = path.join(__dirname, 'defaults.json'); | |
const data = JSON.parse(fs.readFileSync(defaultsPath, 'utf8')); | |
// Retrieve the list of preset files from the presets folder | |
const presetFiles = fs.readdirSync(presetsFolder); | |
presetFiles.forEach(file => { | |
// Read the preset file | |
const presetPath = path.join(presetsFolder, file); | |
const preset = JSON.parse(fs.readFileSync(presetPath, 'utf8')); | |
// Get the geometry type(s) from the preset | |
const geometries = preset.geometry; | |
geometries.forEach(geometry => { | |
// Get the preset name without the file extension | |
const presetName = path.parse(file).name; | |
// Check if the preset name already exists in the corresponding array in defaults.json | |
if (!data[geometry].includes(presetName)) { | |
// If not, add the preset name to the array | |
data[geometry].push(presetName); | |
} | |
}); | |
}); | |
// Iterate over each geometry type in defaults.json | |
for (const geometry in data) { | |
// Get the list of presets for the current geometry type | |
const presets = data[geometry]; | |
// Iterate over each preset | |
for (let i = presets.length - 1; i >= 0; i--) { | |
const preset = presets[i]; | |
// Check if the preset file exists in the presets folder | |
if (!fs.existsSync(path.join(presetsFolder, `${preset}.json`))) { | |
// If not, remove the preset from the array | |
presets.splice(i, 1); | |
} | |
} | |
} | |
// Write the updated contents back to defaults.json | |
fs.writeFileSync(defaultsPath, JSON.stringify(data, null, 4)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment