Skip to content

Commit

Permalink
feat: replace name & path with breadcrumb (#105)
Browse files Browse the repository at this point in the history
  • Loading branch information
ignatiusmb authored Jan 4, 2024
1 parent 37de3cc commit 3725a00
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 9 deletions.
17 changes: 10 additions & 7 deletions workspace/marqua/src/fs/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,16 +40,19 @@ export function traverse(
/** @type {import('../types.js').HydrateChunk['siblings']} */
const tree = fs.readdirSync(entry).map((name) => {
const path = join(entry, name);
if (fs.lstatSync(path).isDirectory()) {
return { type: 'directory', name, path };
}
const buffer = fs.readFileSync(path);
return { type: 'file', name, path, buffer };
return {
/** @type {any} - discriminated union without multiple returns */
type: fs.lstatSync(path).isDirectory() ? 'directory' : 'file',
breadcrumb: path.split(/[/\\]/).reverse(),
get buffer() {
return this.type === 'file' ? fs.readFileSync(path) : void 0;
},
};
});

const backpack = tree.flatMap(({ type, path, buffer }) => {
const backpack = tree.flatMap(({ type, breadcrumb, buffer }) => {
const path = [...breadcrumb].reverse().join('/');
if (type === 'file' && files(path)) {
const breadcrumb = path.split(/[/\\]/).reverse();
return hydrate({ breadcrumb, buffer, marker, parse, siblings: tree }) ?? [];
} else if (level !== 0) {
const depth = level < 0 ? level : level - 1;
Expand Down
4 changes: 2 additions & 2 deletions workspace/marqua/src/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ export interface HydrateChunk {
parse: typeof parse;
// TODO: remove self from siblings
siblings: Array<
| { type: 'file'; name: string; path: string; buffer: Buffer }
| { type: 'directory'; name: string; path: string; buffer: undefined }
| { type: 'file'; breadcrumb: string[]; buffer: Buffer }
| { type: 'directory'; breadcrumb: string[]; buffer: undefined }
>;
}

Expand Down

1 comment on commit 3725a00

@vercel
Copy link

@vercel vercel bot commented on 3725a00 Jan 4, 2024

Choose a reason for hiding this comment

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

Please sign in to comment.