Skip to content

Commit

Permalink
fix: ensure location and resolve path issue
Browse files Browse the repository at this point in the history
  • Loading branch information
twlite committed Dec 7, 2024
1 parent 4bf9a79 commit 1446767
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 10 deletions.
19 changes: 9 additions & 10 deletions packages/core/src/YasumuUtilities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,16 @@ export class YasumuUtilities {
*/
public joinPathSync(...paths: string[]): string {
const sep = this.yasumu.path.sep();
const normalized = paths.flatMap((p, i) => {
if (!p || typeof p !== 'string') return;
if (!p.includes(sep)) return p;
if (i === 0) return p;
return p.split(sep);
const isAbsolute = paths[0]?.startsWith(sep);

// Normalize and split paths
const segments = paths.flatMap((path) => {
if (!path || typeof path !== 'string') return [];
return path.split(/[/\\]/).filter(Boolean);
});

return normalized
.filter((p) => {
return p && typeof p === 'string' && p.length > 0 && p !== sep;
})
.join(sep);
// Join segments and handle absolute paths
const result = segments.join(sep);
return isAbsolute ? sep + result : result;
}
}
16 changes: 16 additions & 0 deletions packages/core/src/modules/common/BaseModule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,8 @@ export abstract class YasumuBaseModule<T extends WorkspaceModuleType = Workspace

const fs = this.workspace.yasumu.fs;

await this.ensureLocation();

for (const entity of Object.values(entities)) {
const location = this.workspace.yasumu.utils.joinPathSync(this.getLocation(), entity.blocks.Metadata.path);
const pathExists = await fs.exists(location);
Expand Down Expand Up @@ -200,6 +202,20 @@ export abstract class YasumuBaseModule<T extends WorkspaceModuleType = Workspace
}
}

/**
* Ensure the location for this module exists.
*/
public async ensureLocation() {
const location = this.getLocation();
const fs = this.workspace.yasumu.fs;

const exists = await fs.exists(location);

if (!exists) {
await fs.mkdir(location, { recursive: true });
}
}

/**
* Called by the entity when it changes.
* @param entity The entity that changed.
Expand Down

0 comments on commit 1446767

Please sign in to comment.