From 0eed98fec072db6dcb8fb7cc050c2c2df2cb2255 Mon Sep 17 00:00:00 2001 From: Anton Golub Date: Mon, 14 Aug 2023 20:16:10 +0300 Subject: [PATCH] feat: export `iterateDeps` helper --- README.md | 22 +++++++++++++++++++++- package.json | 2 +- src/main/ts/topo.ts | 2 +- src/test/ts/topo.ts | 34 ++++++++++++++++++++++++++++++++++ 4 files changed, 57 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index d23a576..aff4bab 100644 --- a/README.md +++ b/README.md @@ -4,7 +4,7 @@ [![Maintainability](https://api.codeclimate.com/v1/badges/48b31cd38b905b729beb/maintainability)](https://codeclimate.com/github/semrel-extra/topo/maintainability) [![Test Coverage](https://api.codeclimate.com/v1/badges/48b31cd38b905b729beb/test_coverage)](https://codeclimate.com/github/semrel-extra/topo/test_coverage) -Helper to resolve monorepo dependencies graph by package workspaces +> Helpers to assist monorepo dependencies traversals ## Install ```shell @@ -108,5 +108,25 @@ const cb = async (name: string) => { await traverseQueue({ queue, prev, cb }) ``` +### `iterateDeps()` +Walks through the package dependencies and applies the `cb()` for each. +```ts +const results: any[] = [] +const manifest = { + name: 'foo', + version: '0.0.0', + dependencies: { + bar: '1.0.0' + }, + devDependencies: { + baz: '2.0.0' + } +} + +iterateDeps(manifest, ({ name, version, scope, deps }) => { + results.push({ name, version, scope, deps }) +}) +``` + ## License [MIT](./LICENSE) diff --git a/package.json b/package.json index e6fe634..0e62017 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "@semrel-extra/topo", "version": "1.13.1", - "description": "Helper to resolve monorepo dependencies graph", + "description": "Helpers to assist monorepo dependencies traversals", "publishConfig": { "access": "public" }, diff --git a/src/main/ts/topo.ts b/src/main/ts/topo.ts index 365478b..7bbb4f4 100644 --- a/src/main/ts/topo.ts +++ b/src/main/ts/topo.ts @@ -251,7 +251,7 @@ export const traverseDeps = async ({ await Promise.all(results) } -const iterateDeps = ( +export const iterateDeps = ( manifest: IPackageJson, cb: (ctx: IDepEntry & { deps: IPackageDeps }) => any, scopes = defaultScopes diff --git a/src/test/ts/topo.ts b/src/test/ts/topo.ts index 2acd64b..64e9635 100644 --- a/src/test/ts/topo.ts +++ b/src/test/ts/topo.ts @@ -10,6 +10,7 @@ import { topo, traverseQueue, traverseDeps, + iterateDeps, ITopoOptions, IDepEntry, IPackageEntry, @@ -408,4 +409,37 @@ test('`traversDeps` applies cb up to the deps tree', async () => { assert.equal(result, expected) }) +test('`iterateDeps` walks through the pkg deps', () => { + const results: any[] = [] + const manifest = { + name: 'foo', + version: '0.0.0', + dependencies: { + bar: '1.0.0' + }, + devDependencies: { + baz: '2.0.0' + } + } + + iterateDeps(manifest, ({ name, version, scope, deps }) => { + results.push({ name, version, scope, deps }) + }) + + assert.equal(results, [ + { + name: 'bar', + version: '1.0.0', + scope: 'dependencies', + deps: manifest.dependencies + }, + { + name: 'baz', + version: '2.0.0', + scope: 'devDependencies', + deps: manifest.devDependencies + } + ]) +}) + test.run()