|
| 1 | +/** |
| 2 | + * @author sw5678 |
| 3 | + * @copyright Crown Copyright 2016 |
| 4 | + * @license Apache-2.0 |
| 5 | + */ |
| 6 | + |
| 7 | +import Operation from "../Operation.mjs"; |
| 8 | +import Utils from "../Utils.mjs"; |
| 9 | +import {INPUT_DELIM_OPTIONS} from "../lib/Delim.mjs"; |
| 10 | + |
| 11 | +/** |
| 12 | + * Unique operation |
| 13 | + */ |
| 14 | +class FileTree extends Operation { |
| 15 | + |
| 16 | + /** |
| 17 | + * Unique constructor |
| 18 | + */ |
| 19 | + constructor() { |
| 20 | + super(); |
| 21 | + |
| 22 | + this.name = "File Tree"; |
| 23 | + this.module = "Default"; |
| 24 | + this.description = "Creates file tree from list of file paths (Similar too tree linux command)"; |
| 25 | + this.inputType = "string"; |
| 26 | + this.outputType = "string"; |
| 27 | + this.args = [ |
| 28 | + { |
| 29 | + name: "File Path Delimiter", |
| 30 | + type: "binaryString", |
| 31 | + value: "/" |
| 32 | + }, |
| 33 | + { |
| 34 | + name: "Delimiter", |
| 35 | + type: "option", |
| 36 | + value: INPUT_DELIM_OPTIONS |
| 37 | + } |
| 38 | + ]; |
| 39 | + } |
| 40 | + |
| 41 | + /** |
| 42 | + * @param {string} input |
| 43 | + * @param {Object[]} args |
| 44 | + * @returns {string} |
| 45 | + */ |
| 46 | + run(input, args) { |
| 47 | + |
| 48 | + // Set up arrow and pipe for nice output display |
| 49 | + const ARROW = '|---'; |
| 50 | + const PIPE = '| '; |
| 51 | + |
| 52 | + // Get args from input |
| 53 | + const file_delim = args[0]; |
| 54 | + const entry_delim = Utils.charRep(args[1]); |
| 55 | + |
| 56 | + // Store path to print |
| 57 | + let completed_list = []; |
| 58 | + let print_list = []; |
| 59 | + |
| 60 | + // Loop through all entries |
| 61 | + const file_paths = input.split(entry_delim).unique().sort(); |
| 62 | + for (var i = 0; i < file_paths.length; i++) |
| 63 | + { |
| 64 | + // Split by file delimiter |
| 65 | + let path = file_paths[i].split(file_delim); |
| 66 | + |
| 67 | + if (path[0] == '') |
| 68 | + { |
| 69 | + path = path.slice(1, path.length); |
| 70 | + } |
| 71 | + |
| 72 | + for (var j = 0; j < path.length; j++) |
| 73 | + { |
| 74 | + let print_line; |
| 75 | + let key; |
| 76 | + if (j == 0) |
| 77 | + { |
| 78 | + print_line = path[j]; |
| 79 | + key = path[j]; |
| 80 | + } |
| 81 | + else |
| 82 | + { |
| 83 | + print_line = PIPE.repeat(j-1) + ARROW + path[j] |
| 84 | + key = path.slice(0, j+1).join("/"); |
| 85 | + } |
| 86 | + |
| 87 | + // Check to see we have already added that path |
| 88 | + if (!completed_list.includes(key)) |
| 89 | + { |
| 90 | + completed_list.push(key); |
| 91 | + print_list.push(print_line); |
| 92 | + } |
| 93 | + } |
| 94 | + } |
| 95 | + return print_list.join("\n"); |
| 96 | + } |
| 97 | + |
| 98 | +} |
| 99 | + |
| 100 | +export default FileTree; |
0 commit comments