2023-12-13 10:19:16 +01:00
|
|
|
/**
|
|
|
|
* @author sw5678
|
|
|
|
* @copyright Crown Copyright 2016
|
|
|
|
* @license Apache-2.0
|
|
|
|
*/
|
|
|
|
|
|
|
|
import Operation from "../Operation.mjs";
|
|
|
|
import Utils from "../Utils.mjs";
|
|
|
|
import {INPUT_DELIM_OPTIONS} from "../lib/Delim.mjs";
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Unique operation
|
|
|
|
*/
|
|
|
|
class FileTree extends Operation {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Unique constructor
|
|
|
|
*/
|
|
|
|
constructor() {
|
|
|
|
super();
|
|
|
|
|
|
|
|
this.name = "File Tree";
|
|
|
|
this.module = "Default";
|
|
|
|
this.description = "Creates file tree from list of file paths (Similar too tree linux command)";
|
|
|
|
this.inputType = "string";
|
|
|
|
this.outputType = "string";
|
|
|
|
this.args = [
|
|
|
|
{
|
|
|
|
name: "File Path Delimiter",
|
|
|
|
type: "binaryString",
|
|
|
|
value: "/"
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Delimiter",
|
|
|
|
type: "option",
|
|
|
|
value: INPUT_DELIM_OPTIONS
|
|
|
|
}
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {string} input
|
|
|
|
* @param {Object[]} args
|
|
|
|
* @returns {string}
|
|
|
|
*/
|
|
|
|
run(input, args) {
|
|
|
|
|
|
|
|
// Set up arrow and pipe for nice output display
|
2023-12-13 10:38:26 +01:00
|
|
|
const ARROW = "|---";
|
|
|
|
const PIPE = "| ";
|
2023-12-13 10:19:16 +01:00
|
|
|
|
|
|
|
// Get args from input
|
2023-12-13 10:38:26 +01:00
|
|
|
const fileDelim = args[0];
|
|
|
|
const entryDelim = Utils.charRep(args[1]);
|
2023-12-13 10:19:16 +01:00
|
|
|
|
|
|
|
// Store path to print
|
2023-12-13 10:38:26 +01:00
|
|
|
const completedList = [];
|
|
|
|
const printList = [];
|
2023-12-13 10:19:16 +01:00
|
|
|
|
|
|
|
// Loop through all entries
|
2023-12-13 10:38:26 +01:00
|
|
|
const filePaths = input.split(entryDelim).unique().sort();
|
|
|
|
for (let i = 0; i < filePaths.length; i++) {
|
2023-12-13 10:19:16 +01:00
|
|
|
// Split by file delimiter
|
2023-12-13 10:38:26 +01:00
|
|
|
let path = filePaths[i].split(fileDelim);
|
2023-12-13 10:19:16 +01:00
|
|
|
|
2023-12-13 10:38:26 +01:00
|
|
|
if (path[0] === "") {
|
|
|
|
path = path.slice(1, path.length);
|
2023-12-13 10:19:16 +01:00
|
|
|
}
|
|
|
|
|
2023-12-13 10:38:26 +01:00
|
|
|
for (let j = 0; j < path.length; j++) {
|
|
|
|
let printLine;
|
2023-12-13 10:19:16 +01:00
|
|
|
let key;
|
2023-12-13 10:38:26 +01:00
|
|
|
if (j === 0) {
|
|
|
|
printLine = path[j];
|
2023-12-13 10:19:16 +01:00
|
|
|
key = path[j];
|
2023-12-13 10:38:26 +01:00
|
|
|
} else {
|
|
|
|
printLine = PIPE.repeat(j-1) + ARROW + path[j];
|
2023-12-13 10:19:16 +01:00
|
|
|
key = path.slice(0, j+1).join("/");
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check to see we have already added that path
|
2023-12-13 10:38:26 +01:00
|
|
|
if (!completedList.includes(key)) {
|
|
|
|
completedList.push(key);
|
|
|
|
printList.push(printLine);
|
2023-12-13 10:19:16 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-12-13 10:38:26 +01:00
|
|
|
return printList.join("\n");
|
2023-12-13 10:19:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
export default FileTree;
|