-
Notifications
You must be signed in to change notification settings - Fork 0
/
OutputTemplate.html
268 lines (250 loc) · 12.8 KB
/
OutputTemplate.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
<html lang="en">
<head>
<meta charset="utf-8"/>
<title>'DOCUMENT_TITLE'</title>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/jqueryui/1.12.1/themes/base/jquery-ui.min.css">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/jqueryui/1.12.1/themes/base/theme.min.css">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/jquery.fancytree/2.35.0/skin-win8/ui.fancytree.min.css">
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jqueryui/1.12.1/jquery-ui.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery.fancytree/2.35.0/jquery.fancytree-all.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/split.js/1.5.11/split.min.js"></script>
<script src="https://cdn.jsdelivr.net/g/filesaver.js"></script>
<style type="text/css">
/* Set the default font to something nice. */
* {
font-family: 'Ubuntu', 'Calibri', 'sans-serif';
}
/* Class added on a directory model to indicate it contains duplicate files. */
.contains-duplicate-files .fancytree-title {
font-weight: bold;
}
/* Class added on a directory model to indicate that itself and everything
below it is a duplicate */
.is-duplicate-tree .fancytree-title {
text-decoration: underline dotted;
}
/* Class added to fancytree folders that actually represent files. */
.custom-file-icon, .custom-file-icon:hover {
background-position: -16px 0px !important;
}
/* Class required for the vertical splitter. */
.split, .gutter.gutter-horizontal {
float: left;
}
/* Class required for the vertical splitter. */
.gutter.gutter-horizontal {
cursor: ew-resize;
}
</style>
<script type="text/javascript">
// Template for the delete file script created by one of the buttons.
const cleanupDirectoryScriptTemplate = `
#! /usr/bin/env -S dotnet fsi
open System
open System.IO
open System.Text.Json
let directoryInfo = DirectoryInfo __SOURCE_DIRECTORY__
let deleteFile = Option.iter (fun (fileInfo : FileInfo) ->
try fileInfo.Delete ()
with ex -> printfn "Could not delete file %s\\n%O" fileInfo.Name ex
)
if directoryInfo.Name <> "directoryInfoName" then
printfn "This file removal script was generated for a different directory."
else
let fileInfoMap =
directoryInfo.EnumerateFiles ()
|> Seq.map (fun fileInfo -> fileInfo.Name, fileInfo)
|> Map.ofSeq
"""fileNamesToDelete"""
|> JsonSerializer.Deserialize
|> Array.sortBy (Array.sortBy (fun (fileName : string) -> fileName.ToLowerInvariant ()))
|> Array.iter (function
| [| |] -> ()
| [| fileName |] ->
printfn "Deleting %s" fileName
fileInfoMap |> Map.tryFind fileName |> deleteFile
| fileNames ->
printfn "Which of these files do you want to keep:"
Seq.iteri (printfn "\\t%2d: %s") fileNames
let choice = printf "Choose: "; Console.ReadLine () |> Int32.Parse
fileNames
|> Seq.filter ((<>) (Array.get fileNames choice))
|> Seq.iter (fun fileName ->
printfn "Deleting %s" fileName
fileInfoMap |> Map.tryFind fileName |> deleteFile
)
)
`
.split(/\r?\n/)
.reduce((state, line) => {
if (state === undefined && line.trim() !== '') {
const [, leadingSpaces, trimmedLine] = line.match(/^(\s+)(.*)$/)
return {
result: trimmedLine,
indentation: leadingSpaces.length,
}
} else if (state !== undefined) {
return {
...state,
result: `${state.result}\n${line.substr(state.indentation)}`,
}
}
}, undefined)
.result;
$(() => {
// Add a vertical splitter.
Split(['#mainTreeContainer', '#contentContainer']);
// Initialize the main directory tree. The DOCUMENT_DATA source will
// be replaced by the actual JSON data in OutputGeneration.fs.
$('#mainTree').fancytree({
source: 'DOCUMENT_DATA',
clickFolderMode: 4, // Activate on click and expand on double-click.
selectMode: 1, // Single.
toggleEffect: false, // No animations.
});
// Initialize the file view. The source is set when the user clicks on
// a directory in the main directory tree.
$('#fileTree').fancytree({
source: [],
clickFolderMode: 4, // Activate on click and expand on double-click.
selectMode: 1, // Single.
toggleEffect: false, // No animations.
})
// Initialize all jQuery-UI buttons.
$('button').button();
// Clicking on this button generates an F# script to delete all duplicate
// files in the currently selected directory. It asks which to keep when there
// are multiple duplicates of the same file in the directory.
$('#removalScriptButton').click(event => {
event.preventDefault();
const node = $('#directoryButtons').data().node;
const duplicateMap = new Map(
node.data.files.duplicate.map(duplicateFile =>
[ duplicateFile.id, duplicateFile ]
)
);
const fileNamesToDelete =
node.data.files.duplicate.reduce(({ result, processedIds }, duplicate) => {
if (!processedIds.has(duplicate.id)) {
processedIds.add(duplicate.id);
result.push([
duplicate.name,
...duplicate.duplicateToIds.flatMap(duplicateToId => {
if (duplicateMap.has(duplicateToId)) {
processedIds.add(duplicateToId);
return [ duplicateMap.get(duplicateToId).name ];
} else {
return [];
}
}),
]);
}
return { result, processedIds };
}, { result: [], processedIds: new Set() })
.result;
const scriptContent =
cleanupDirectoryScriptTemplate
.replace('directoryInfoName', node.title)
.replace('fileNamesToDelete', JSON.stringify(fileNamesToDelete));
// Save the file without a BOM, otherwise the shebang isn't recognized.
saveAs(new Blob([ scriptContent ], { type: 'text/plain; charset=utf-8' }), 'Delete.fsx', true);
});
// Returns the directory path to the given tree node.
const getNodePath = node => node.parent === null ? '' : `${getNodePath(node.parent)}/${node.title}`;
// Iterate over all duplicate files in the given node and all subnodes
// and return the file ids and their directory and path information.
const iterateOverDuplicateFiles = function* (node) {
for (duplicateFile of node.data?.files?.duplicate || []) {
yield [
duplicateFile.id, {
directoryId: duplicateFile.directoryId,
directoryPath: getNodePath(node),
fileName: duplicateFile.name,
},
];
}
for (child of node.children || []) {
yield* iterateOverDuplicateFiles(child);
}
}
// Map containing all duplicate files and their directory and path information.
const duplicateFileMap = new Map(iterateOverDuplicateFiles($.ui.fancytree.getTree('#mainTree').rootNode));
// When the user clicks on a new directory in the main tree, show its information.
$('#mainTree').on('fancytreeactivate', (event, data) => {
$('#directoryTitle').text(`Directory ${getNodePath(data.node)}`);
if (data.node.data.files === null) {
$('#directoryFileInfo').text(`This directory is clean but contains ${data.node.children.length} subdirectories with duplicates.`);
$('#directoryButtons').hide();
$('#fileTree').hide();
} else {
const { duplicate, nonDuplicateNames } = data.node.data.files;
$('#directoryFileInfo').text(`This directory contains ${duplicate.length} duplicates and ${nonDuplicateNames.length} non-duplicates.`);
$('#directoryButtons').data('node', data.node).show();
const newFileTreeSource = [{
key: ':duplicatedirectories:',
title: 'Directories Containing Duplicates',
folder: true,
children: Array.from(
duplicate.reduce((directoryMap, { duplicateToIds }) => {
for (duplicateToId of duplicateToIds) {
const { directoryId, directoryPath } = duplicateFileMap.get(duplicateToId);
if (!directoryMap.has(directoryId)) {
directoryMap.set(directoryId, {
directoryPath: directoryPath,
duplicateFileCount: 0,
});
}
directoryMap.get(directoryId).duplicateFileCount++;
}
return directoryMap;
}, new Map()).values()
).map(({ directoryPath, duplicateFileCount }) => ({
title: `${directoryPath} (${duplicateFileCount} duplicates)`,
folder: true,
})),
}, {
key: ':duplicatefiles:',
title: 'Duplicate Files',
folder: true,
children: duplicate.map(({ id, name, duplicateToIds }) => ({
title: name,
folder: true,
icon: 'fancytree-icon custom-file-icon', // Show a file icon.
children: duplicateToIds.map(duplicateToId => ({
key: duplicateToId,
title: `${duplicateFileMap.get(duplicateToId).directoryPath}/${duplicateFileMap.get(duplicateToId).fileName}`,
})),
})),
}, {
key: ':nonduplicatefiles:',
title: 'Non Duplicate Files',
folder: true,
children: nonDuplicateNames.map(name => ({
title: name,
})),
}];
$.ui.fancytree.getTree('#fileTree').reload(newFileTreeSource);
$('#fileTree').show();
}
});
});
</script>
</head>
<body>
<div style="display: flex; flex-wrap: nowrap;">
<div id="mainTreeContainer">
<h2 style="margin-top: 0;">Duplicate Directory Tree</h2>
<div id="mainTree"></div>
</div>
<div id="contentContainer">
<h2 id="directoryTitle" style="margin-top: 0;">No folder selected</h2>
<div id="directoryFileInfo"></div>
<div id="directoryButtons" style="display: none; margin-top: 1em;">
<button id="removalScriptButton" class="ui-button ui-widget ui-corner-all">Create File Removal Script</button>
</div>
<div id="fileTree" style="display: none; margin-top: 1em;"></div>
</div>
</div>
</body>
</html>