Skip to content

Commit

Permalink
Refactor SmartDirectories to integrate SmartGroups and enhance direct…
Browse files Browse the repository at this point in the history
…ory management

- Introduced `SmartGroups` and `SmartGroup` classes, replacing `SmartEntities` in `SmartDirectories` and `SmartDirectory` for improved group handling.
- Added `DirectoryGroupAdapter` and `SourceDirectoryGroupsAdapter` to manage directory group creation and structure.
- Updated `package.json` to reflect changes in main entry point and dependencies.
- Removed obsolete components related to directory rendering, streamlining the codebase.
- Added a test script to create a directory structure for integration testing of SmartDirectories.

These changes enhance the modularity and functionality of the SmartDirectories system, facilitating better directory management and integration with smart groups.
  • Loading branch information
Brian Joseph Petro committed Dec 20, 2024
1 parent 17d1068 commit ff1ea05
Show file tree
Hide file tree
Showing 17 changed files with 479 additions and 274 deletions.
10 changes: 10 additions & 0 deletions smart-directories/adapters/_adapter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { GroupAdapter } from '../../smart-groups/adapters/_adapter.js';

/**
* @class DirectoryGroupAdapter
* @extends GroupAdapter
* @description
* A base class for group-level adapters that build groups from directories.
*/
export class DirectoryGroupAdapter extends GroupAdapter {
}
47 changes: 47 additions & 0 deletions smart-directories/adapters/sources.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/**
* @file SourceDirectoryGroupsAdapter.js
* @description Adapts directory groups by scanning SmartSources to build directory items.
*/

import { GroupCollectionAdapter, GroupItemAdapter } from '../../smart-groups/adapters/_adapter.js';

export class SourceDirectoryGroupsAdapter extends GroupCollectionAdapter {
/**
* Build groups by scanning the `smart_sources` collection.
* For each source, derive its directory path and ensure a SmartDirectory group item exists.
*/
async build_groups() {
const source_paths = Object.keys(this.collection.env.smart_sources.items);
const created_dirs = new Set();

for (const path of source_paths) {
const dir_path = path.split('/').slice(0, -1).join('/') + '/';
await this.ensure_parent_directories(dir_path, created_dirs);
}
}

async ensure_parent_directories(dir_path, created_dirs) {
const parts = dir_path.split('/').filter(p => p);
let current_path = '';

for (const part of parts) {
current_path += part + '/';
if (!created_dirs.has(current_path)) {
const existing = this.collection.get(current_path);
if (!existing) {
const item = this.collection.create_or_update({ path: current_path });
// item.init() if needed
}
created_dirs.add(current_path);
}
}
}
}

export class SourceDirectoryGroupAdapter extends GroupItemAdapter {
}

export default {
collection: SourceDirectoryGroupsAdapter,
item: SourceDirectoryGroupAdapter
};
67 changes: 0 additions & 67 deletions smart-directories/components/directories.js

This file was deleted.

76 changes: 0 additions & 76 deletions smart-directories/components/directory.js

This file was deleted.

2 changes: 2 additions & 0 deletions smart-directories/main.js → smart-directories/index.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import { SmartDirectory } from "./smart_directory.js";
import { SmartDirectories } from "./smart_directories.js";
import source_directory_group_adapter from "./adapters/sources.js";

export {
SmartDirectory,
SmartDirectories,
source_directory_group_adapter
};

4 changes: 2 additions & 2 deletions smart-directories/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"version": "0.0.1",
"type": "module",
"description": "Easy to manage embedded directories.",
"main": "main.js",
"main": "index.js",
"scripts": {
"test": "npx ava --verbose"
},
Expand All @@ -25,7 +25,7 @@
"ava": "^6.0.1"
},
"dependencies": {
"smart-entities": "file:../smart-entities"
"smart-groups": "file:../smart-groups"
},
"ava": {
"files": [
Expand Down
17 changes: 5 additions & 12 deletions smart-directories/smart_directories.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
import { SmartEntities } from "smart-entities";
import { SmartDirectory } from "./smart_directory.js";
import { render as render_directories_component } from "./components/directories.js";
import { SmartGroups } from "smart-groups";

export class SmartDirectories extends SmartEntities {
export class SmartDirectories extends SmartGroups {
static get defaults() {
return {
item_type: SmartDirectory,
collection_key: 'smart_directories',
};
}
Expand Down Expand Up @@ -46,13 +43,9 @@ export class SmartDirectories extends SmartEntities {
*/
async init() {
await super.init();

// Create directories for all source paths
const source_paths = Object.keys(this.env.smart_sources.items);
for (const path of source_paths) {
const dir_path = path.split('/').slice(0, -1).join('/') + '/';
await this.ensure_parent_directories(dir_path);
}
// Build groups from sources
await this.group_adapter.build_groups();
await this.process_save_queue();
}

/**
Expand Down
Loading

0 comments on commit ff1ea05

Please sign in to comment.