2023-04-10 15:36:59 +00:00
|
|
|
/**
|
|
|
|
* Copyright (c) HashiCorp, Inc.
|
|
|
|
* SPDX-License-Identifier: MPL-2.0
|
|
|
|
*/
|
|
|
|
|
2022-06-07 01:42:23 +00:00
|
|
|
// @ts-check
|
|
|
|
|
|
|
|
// eslint-disable-next-line no-unused-vars
|
|
|
|
import VariableModel from '../models/variable';
|
|
|
|
// eslint-disable-next-line no-unused-vars
|
|
|
|
import MutableArray from '@ember/array/mutable';
|
|
|
|
import { trimPath } from '../helpers/trim-path';
|
|
|
|
|
|
|
|
//#region Types
|
|
|
|
/**
|
2022-06-17 18:03:43 +00:00
|
|
|
* @typedef {Object} VariableFile
|
2022-06-07 01:42:23 +00:00
|
|
|
* @property {string} path - the folder path containing our "file", relative to parent
|
2022-08-29 18:45:49 +00:00
|
|
|
* @property {string} name - the variable "file" name
|
2022-06-17 18:03:43 +00:00
|
|
|
* @property {string} absoluteFilePath - the folder path containing our "file", absolute
|
2022-08-29 18:45:49 +00:00
|
|
|
* @property {VariableModel} variable - the variable itself
|
2022-06-07 01:42:23 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
2022-06-17 18:03:43 +00:00
|
|
|
* @typedef {Object} VariableFolder
|
|
|
|
* @property {Array<VariableFile>} files
|
|
|
|
* @property {NestedPathTreeNode} children
|
|
|
|
* @property {string} absolutePath - the folder path containing our "file", absolute
|
2022-06-07 01:42:23 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
2022-06-17 18:03:43 +00:00
|
|
|
* @typedef {Object.<string, VariableFolder>} NestedPathTreeNode
|
2022-06-07 01:42:23 +00:00
|
|
|
*/
|
2022-06-17 18:03:43 +00:00
|
|
|
//#endregion Types
|
2022-06-07 01:42:23 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @returns {NestedPathTreeNode}
|
|
|
|
*/
|
|
|
|
export default class PathTree {
|
|
|
|
/**
|
|
|
|
* @param {MutableArray<VariableModel>} variables
|
|
|
|
*/
|
|
|
|
constructor(variables) {
|
|
|
|
this.variables = variables;
|
2022-06-17 18:03:43 +00:00
|
|
|
this.paths = this.generatePaths(variables);
|
2022-06-07 01:42:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2022-06-17 18:03:43 +00:00
|
|
|
* @type {VariableFolder}
|
|
|
|
*/
|
|
|
|
root = { children: {}, files: [], absolutePath: '' };
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Takes our variables array and creates a tree of paths
|
|
|
|
* @param {MutableArray<VariableModel>} variables
|
2022-06-07 01:42:23 +00:00
|
|
|
* @returns {NestedPathTreeNode}
|
|
|
|
*/
|
2022-06-17 18:03:43 +00:00
|
|
|
generatePaths = (variables) => {
|
|
|
|
variables.forEach((variable) => {
|
|
|
|
const path = trimPath([variable.path]).split('/');
|
|
|
|
path.reduce((acc, segment, index, arr) => {
|
|
|
|
if (index === arr.length - 1) {
|
|
|
|
// If it's a file (end of the segment array)
|
|
|
|
acc.files.push({
|
|
|
|
name: segment,
|
|
|
|
absoluteFilePath: path.join('/'),
|
|
|
|
path: arr.slice(0, index + 1).join('/'),
|
|
|
|
variable,
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
// Otherwise, it's a folder
|
|
|
|
if (!acc.children[segment]) {
|
|
|
|
acc.children[segment] = {
|
|
|
|
children: {},
|
|
|
|
files: [],
|
|
|
|
absolutePath: trimPath([`${acc.absolutePath || ''}/${segment}`]),
|
|
|
|
};
|
2022-06-07 01:42:23 +00:00
|
|
|
}
|
2022-06-17 18:03:43 +00:00
|
|
|
}
|
|
|
|
return acc.children[segment];
|
|
|
|
}, this.root);
|
|
|
|
});
|
|
|
|
return { root: this.root };
|
2022-06-07 01:42:23 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Search for the named absolutePath within our tree using recursion
|
|
|
|
* @param {string} name
|
|
|
|
* @param {Object} root
|
|
|
|
*/
|
|
|
|
findPath = (name, root = this.paths.root) => {
|
|
|
|
if (root.absolutePath === name) {
|
|
|
|
return root;
|
|
|
|
}
|
|
|
|
if (root.children) {
|
|
|
|
return Object.keys(root.children).reduce((acc, cur) => {
|
|
|
|
if (!acc) {
|
|
|
|
return this.findPath(name, root.children[cur]);
|
|
|
|
}
|
|
|
|
return acc;
|
|
|
|
}, null);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|