Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added file tree functionality #1666

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/core/config/Categories.json
Original file line number Diff line number Diff line change
Expand Up @@ -294,7 +294,8 @@
"Escape string",
"Unescape string",
"Pseudo-Random Number Generator",
"Sleep"
"Sleep",
"File Tree"
]
},
{
Expand Down
100 changes: 100 additions & 0 deletions src/core/operations/FileTree.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
/**
* @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
const ARROW = '|---';

Check failure on line 49 in src/core/operations/FileTree.mjs

View workflow job for this annotation

GitHub Actions / main

Strings must use doublequote
const PIPE = '| ';

Check failure on line 50 in src/core/operations/FileTree.mjs

View workflow job for this annotation

GitHub Actions / main

Strings must use doublequote

// Get args from input
const file_delim = args[0];

Check failure on line 53 in src/core/operations/FileTree.mjs

View workflow job for this annotation

GitHub Actions / main

Identifier 'file_delim' is not in camel case
const entry_delim = Utils.charRep(args[1]);

Check failure on line 54 in src/core/operations/FileTree.mjs

View workflow job for this annotation

GitHub Actions / main

Identifier 'entry_delim' is not in camel case

Check warning on line 54 in src/core/operations/FileTree.mjs

View workflow job for this annotation

GitHub Actions / main

Trailing spaces not allowed

// Store path to print
let completed_list = [];

Check failure on line 57 in src/core/operations/FileTree.mjs

View workflow job for this annotation

GitHub Actions / main

Identifier 'completed_list' is not in camel case

Check failure on line 57 in src/core/operations/FileTree.mjs

View workflow job for this annotation

GitHub Actions / main

'completed_list' is never reassigned. Use 'const' instead
let print_list = [];

Check failure on line 58 in src/core/operations/FileTree.mjs

View workflow job for this annotation

GitHub Actions / main

Identifier 'print_list' is not in camel case

Check failure on line 58 in src/core/operations/FileTree.mjs

View workflow job for this annotation

GitHub Actions / main

'print_list' is never reassigned. Use 'const' instead

// Loop through all entries
const file_paths = input.split(entry_delim).unique().sort();

Check failure on line 61 in src/core/operations/FileTree.mjs

View workflow job for this annotation

GitHub Actions / main

Identifier 'file_paths' is not in camel case
for (var i = 0; i < file_paths.length; i++)

Check failure on line 62 in src/core/operations/FileTree.mjs

View workflow job for this annotation

GitHub Actions / main

Unexpected var, use let or const instead
{
// Split by file delimiter
let path = file_paths[i].split(file_delim);

if (path[0] == '')
{
path = path.slice(1, path.length);
}

for (var j = 0; j < path.length; j++)
{
let print_line;
let key;
if (j == 0)
{
print_line = path[j];
key = path[j];
}
else
{
print_line = PIPE.repeat(j-1) + ARROW + path[j]
key = path.slice(0, j+1).join("/");
}

// Check to see we have already added that path
if (!completed_list.includes(key))
{
completed_list.push(key);
print_list.push(print_line);
}
}
}
return print_list.join("\n");
}

}

export default FileTree;
21 changes: 21 additions & 0 deletions tests/operations/tests/FileTree.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/**
* @author sw5678
* @copyright Crown Copyright 2023
* @license Apache-2.0
*/
import TestRegister from "../../lib/TestRegister.mjs";

TestRegister.addTests([
{
"name": "Swap Case: basic example",
"input": "/test_dir1/test_file1.txt\n/test_dir1/test_file2.txt\n/test_dir2/test_file1.txt",
"expectedOutput": "test_dir1\n|---test_file1.txt\n|---test_file2.txt\ntest_dir2\n|---test_file1.txt",
"recipeConfig": [
{
"op": "File Tree",
"args": [
],
},
],
}
]);