Skip to content

Commit

Permalink
Add getAllSegments to FSD-aware traversal functions
Browse files Browse the repository at this point in the history
  • Loading branch information
illright committed May 18, 2024
1 parent 1a53e08 commit a123b95
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 1 deletion.
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,19 @@ Extract slices from all layers of an FSD root. Returns a mapping of slice name (

A folder is detected as a slice when it has at least one folder/file with a name of a conventional segment (`ui`, `api`, `model`, `lib`, `config`). If your project contains slices that don't have those segments, you can provide additional segment names.

### `getAllSegments`

```ts
function getAllSegments(fsdRoot: Folder): Array<{
segment: Folder | File;
segmentName: string;
sliceName: string | null;
layerName: LayerName;
}>;
```

Extract segments from all slices and layers of an FSD root. Returns a flat array of segments along with their name and location in the FSD root (layer, slice).

### `isSliced`

```ts
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@feature-sliced/filesystem",
"version": "2.0.0",
"version": "2.1.0",
"description": "A set of utilities for locating and working with FSD roots in the file system.",
"scripts": {
"build": "tsup src/index.ts --dts --format esm,cjs --clean",
Expand Down
34 changes: 34 additions & 0 deletions src/fsd-aware-traverse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,40 @@ export function getAllSlices(
}, {});
}

/**
* Extract segments from all slices and layers of an FSD root.
*
* @returns A flat array of segments along with their name and location in the FSD root (layer, slice).
*/
export function getAllSegments(fsdRoot: Folder): Array<{
segment: Folder | File;
segmentName: string;
sliceName: string | null;
layerName: LayerName;
}> {
return Object.entries(getLayers(fsdRoot)).flatMap(([layerName, layer]) => {
if (isSliced(layer)) {
return Object.entries(getSlices(layer)).flatMap(([sliceName, slice]) =>
Object.entries(getSegments(slice)).map(([segmentName, segment]) => ({
segment,
segmentName,
sliceName: sliceName as string | null,
layerName: layerName as LayerName,
})),
);
} else {
return Object.entries(getSegments(layer)).map(
([segmentName, segment]) => ({
segment,
segmentName,
sliceName: null as string | null,
layerName: layerName as LayerName,
}),
);
}
});
}

/**
* Determine if this layer is sliced.
*
Expand Down
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ export {
getSegments,
getSlices,
getAllSlices,
getAllSegments,
isSliced,
getIndex,
} from "./fsd-aware-traverse.js";
Expand Down

0 comments on commit a123b95

Please sign in to comment.