|
| 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 fileDelim = args[0]; |
| 54 | + const entryDelim = Utils.charRep(args[1]); |
| 55 | + |
| 56 | + // Store path to print |
| 57 | + const completedList = []; |
| 58 | + const printList = []; |
| 59 | + |
| 60 | + // Loop through all entries |
| 61 | + const filePaths = input.split(entryDelim).unique().sort(); |
| 62 | + for (let i = 0; i < filePaths.length; i++) { |
| 63 | + // Split by file delimiter |
| 64 | + let path = filePaths[i].split(fileDelim); |
| 65 | + |
| 66 | + if (path[0] === "") { |
| 67 | + path = path.slice(1, path.length); |
| 68 | + } |
| 69 | + |
| 70 | + for (let j = 0; j < path.length; j++) { |
| 71 | + let printLine; |
| 72 | + let key; |
| 73 | + if (j === 0) { |
| 74 | + printLine = path[j]; |
| 75 | + key = path[j]; |
| 76 | + } else { |
| 77 | + printLine = PIPE.repeat(j-1) + ARROW + path[j]; |
| 78 | + key = path.slice(0, j+1).join("/"); |
| 79 | + } |
| 80 | + |
| 81 | + // Check to see we have already added that path |
| 82 | + if (!completedList.includes(key)) { |
| 83 | + completedList.push(key); |
| 84 | + printList.push(printLine); |
| 85 | + } |
| 86 | + } |
| 87 | + } |
| 88 | + return printList.join("\n"); |
| 89 | + } |
| 90 | + |
| 91 | +} |
| 92 | + |
| 93 | +export default FileTree; |
0 commit comments