Skip to content

Instantly share code, notes, and snippets.

@stretch07
Last active May 7, 2022 03:28
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save stretch07/9faafc8080f26f557625c286e80856d6 to your computer and use it in GitHub Desktop.
Save stretch07/9faafc8080f26f557625c286e80856d6 to your computer and use it in GitHub Desktop.
Easily structure your directory into JSON with dir2json
<!doctype html>
<html>
<head>
<title>dir2json</title>
<style>
button {
background-color: khaki;
border: none;
color: white;
padding: 15px 15px;
text-align: center;
text-decoration: none;
display: inline-block;
font-size: 16px;
cursor: pointer;
border-radius: 12px;
}
.hidden {
display: none;
}
</style>
<script src="//cdn.jsdelivr.net/npm/sweetalert2@11"></script>
</head>
<body>
<h1>dir2json</h1>
<p>A simple tool that converts your files to JSON. This will work on all browsers except for IE.</p>
<div>
<input class="hidden" id="input" type="file" webkitdirectory directory />
<button onclick="document.getElementById('input').click()">Upload directory</button>
<br />
<fieldset>
<legend>Additional config</legend>
<label for="prefixText">Prefix</label><input placeholder="www.mysite.net or /, or leave empty for no prefix" type="text" name="prefix" id="prefixText">
<fieldset>
<legend>JSON type</legend>
<input type="radio" name="jsonType" id="jsonTypeArray"><label for="jsonTypeArray">Array</label><br>
<input type="radio" name="jsonType" id="jsonTypeObject"><label for="jsonTypeArray" disabled>Object
(coming soon)</label>
</fieldset>
</fieldset>
<button id="go">Go</button>
</div>
<br />
<button id="clipboardCopy">Copy to clipboard</button>
<script>
let txt = ""
document.querySelector('#go').addEventListener("click", function (e) {
txt = "[\n"
for (let i = 0; i < input.files.length; i++) {
let lastThing;
if (input.files.length === i + 1) {
lastThing = "\"\n"
} else {
lastThing = "\",\n"
}
txt += "\t\"" + input.files[i].webkitRelativePath + lastThing
}
txt += "]"
const Toast = Swal.mixin({
toast: true,
position: 'top-end',
showConfirmButton: false,
timer: 3000,
timerProgressBar: true
})
Toast.fire({
icon: 'success',
title: 'Files formatted successfully',
showCancelButton: true,
showConfirmButton: true,
confirmButtonText: 'Copy JSON to clipboard',
}).then((result) => {
if (result.isConfirmed) {
Swal.fire({ title: "Copied", icon: "success" })
}
})
})
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment