Skip to content

Conversation

@rs-amp
Copy link
Owner

@rs-amp rs-amp commented Apr 9, 2021

This command allows you to print a dependency tree for any folder of content items on your system. The input directory should contain content items in the same format that the export command generates. It does not require repo/folder layouts, just that all dependent content is contained within the parent directory.

dc-cli content-item tree <dir>

This uses the same dependency tree class as the import task, which uses it to import the deepest content items first so that references can be properly resolved. Here, the "levels" of content items are maintained, and the highest level (most layers of dependencies) is printed first, followed by the lower levels that have not been printed yet.

Each item shows the items that it references or links via a tree structure:

=== LEVEL 3 (1) ===
item1 
├─ item2 
│  └─ item4 
└─ item3 

=== LEVEL 2 (2) ===
item6 
└─ item5 

=== LEVEL 1 (4) ===
item7

When items have been printed before, their name is shown in brackets and their children are not printed. This saves a lot of space on the tree for content items that are reused a lot.

Circular dependencies are a possibility within DC. Like the import task, they are separated from the rest of the tree into a list of "circular dependencies", now ordered by depth to our best ability.

=== CIRCULAR (3) ===
item1 ═════════════════╗
├─ item2               ║
│  └─ item4            ║
│     └─ *** (item1) ══╝
└─ (item3) 

Overlapping circular dependencies with ordering:

=== CIRCULAR (6) ===
item5 ══════════════╗
└─ item6            ║
   └─ *** (item5) ══╝

item1 ══════════════════════╗
└─ item2 ═════════════════╗ ║
   └─ item3               ║ ║
      ├─ *** (item2) ═════╝ ║
      └─ item4              ║
         ├─ *** (item1) ════╝
         └─ (item5) 

This is useful for examining the dependency structure of content that has been created, and identifying potential problems you might encounter when importing content items to a hub. Since circular dependencies are undesirable and sometimes hard to track manually, it's a useful way of identifying them for removal.

This PR also changes the behaviour of importing content with circular dependencies. The circular dependencies are now imported in order of depth first, and should correctly nullify content references that are not yet available. This will not fix required circular dependencies, but it will fix imports for all circular dependencies that do not use required fields.

This PR is built on top of the feature/clone branch. It's in this repo for easier review.

owner: RepositoryContentItem;

// eslint-disable-next-line @typescript-eslint/no-explicit-any
parent: any;
Copy link

@easen-amp easen-amp May 14, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use unknown instead of any, it's safer. To use the value of parent you will need to check the type before you can use it, see Type Guards.

Copy link
Owner Author

@rs-amp rs-amp Jun 2, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done, though I used the types that the content is normally traversed with.


item.dependancies.forEach(dep => {
rewriteDependancy(dep, mapping);
rewriteDependancy(dep, mapping, pass === 0);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is just a comment regarding this entire method, I think its too complex (cyclomatic complexity - there's a lot of loops and if statements). Can this be simplied down into a series of functions, where each one can be unit tested.

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you mean that the rewriteDependancy method is complex, or that the importTree method is complex? For the latter, it's probably refactoring it in another PR.

const circularPipes = ['╗', '║', '╝'];
const circularLine = '═';

export const printDependency = (

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can this function return lines, circularLinks & evalThis to make it clearer that this function manipulates these args.

Perhaps rename the function as technically it doesn't print anything.

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

evalThis is copied for each recursive call to this method. For the rest of the state, I've made a small class to hold it, and the method is now called addDependency.

return new ContentDependancyTree(contentItems, new ContentMapping());
};

type CircularLink = [number, number];

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe change the circularLinks to be an arrary of an object, as it's not clear what each number represents.

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Each number is an index in the lines of the tree. Neither of the numbers have any special meaning - they mean the same thing - it's just a pair of two linked indices. I think giving them names like index1, index2 might not be great.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

type CircularLinkFrom = number;
type CircularLinkTo = number;
type CircularLink = [CircularLinkFrom, CircularLinkTo];

@rs-amp rs-amp force-pushed the feature/tree-command branch from ca76162 to f3175fc Compare June 2, 2021 09:10
@rs-amp rs-amp force-pushed the feature/tree-command branch from f3175fc to ed6bac4 Compare June 16, 2021 13:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants