diff --git a/README.md b/README.md index a2e9d50..61766c5 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/package.json b/package.json index c6e55db..ec42baa 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/src/fsd-aware-traverse.ts b/src/fsd-aware-traverse.ts index 556742e..def7123 100644 --- a/src/fsd-aware-traverse.ts +++ b/src/fsd-aware-traverse.ts @@ -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. * diff --git a/src/index.ts b/src/index.ts index 1c3be8f..775910a 100644 --- a/src/index.ts +++ b/src/index.ts @@ -3,6 +3,7 @@ export { getSegments, getSlices, getAllSlices, + getAllSegments, isSliced, getIndex, } from "./fsd-aware-traverse.js";