@hyperfrontend/project-scope 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/ARCHITECTURE.md +370 -0
- package/CHANGELOG.md +10 -0
- package/FUNDING.md +141 -0
- package/LICENSE.md +21 -0
- package/README.md +242 -0
- package/SECURITY.md +82 -0
- package/analyze.d.ts +33 -0
- package/analyze.d.ts.map +1 -0
- package/cli/commands/analyze.d.ts +20 -0
- package/cli/commands/analyze.d.ts.map +1 -0
- package/cli/commands/config.d.ts +20 -0
- package/cli/commands/config.d.ts.map +1 -0
- package/cli/commands/deps.d.ts +18 -0
- package/cli/commands/deps.d.ts.map +1 -0
- package/cli/commands/tree.d.ts +24 -0
- package/cli/commands/tree.d.ts.map +1 -0
- package/cli/index.cjs.js +6639 -0
- package/cli/index.cjs.js.map +1 -0
- package/cli/index.d.ts +7 -0
- package/cli/index.d.ts.map +1 -0
- package/cli/index.esm.js +6629 -0
- package/cli/index.esm.js.map +1 -0
- package/cli/run.d.ts +25 -0
- package/cli/run.d.ts.map +1 -0
- package/cli/types.d.ts +55 -0
- package/cli/types.d.ts.map +1 -0
- package/core/cache.d.ts +157 -0
- package/core/cache.d.ts.map +1 -0
- package/core/encoding/convert.d.ts +32 -0
- package/core/encoding/convert.d.ts.map +1 -0
- package/core/encoding/detect.d.ts +86 -0
- package/core/encoding/detect.d.ts.map +1 -0
- package/core/encoding/index.cjs.js +751 -0
- package/core/encoding/index.cjs.js.map +1 -0
- package/core/encoding/index.d.ts +3 -0
- package/core/encoding/index.d.ts.map +1 -0
- package/core/encoding/index.esm.js +736 -0
- package/core/encoding/index.esm.js.map +1 -0
- package/core/errors/structured-errors.d.ts +64 -0
- package/core/errors/structured-errors.d.ts.map +1 -0
- package/core/fs/directory.d.ts +88 -0
- package/core/fs/directory.d.ts.map +1 -0
- package/core/fs/index.cjs.js +1079 -0
- package/core/fs/index.cjs.js.map +1 -0
- package/core/fs/index.d.ts +6 -0
- package/core/fs/index.d.ts.map +1 -0
- package/core/fs/index.esm.js +1056 -0
- package/core/fs/index.esm.js.map +1 -0
- package/core/fs/read.d.ts +86 -0
- package/core/fs/read.d.ts.map +1 -0
- package/core/fs/stat.d.ts +58 -0
- package/core/fs/stat.d.ts.map +1 -0
- package/core/fs/traversal.d.ts +26 -0
- package/core/fs/traversal.d.ts.map +1 -0
- package/core/fs/write.d.ts +75 -0
- package/core/fs/write.d.ts.map +1 -0
- package/core/index.cjs.js +2376 -0
- package/core/index.cjs.js.map +1 -0
- package/core/index.d.ts +9 -0
- package/core/index.d.ts.map +1 -0
- package/core/index.esm.js +2286 -0
- package/core/index.esm.js.map +1 -0
- package/core/logger.d.ts +111 -0
- package/core/logger.d.ts.map +1 -0
- package/core/path/index.cjs.js +254 -0
- package/core/path/index.cjs.js.map +1 -0
- package/core/path/index.d.ts +5 -0
- package/core/path/index.d.ts.map +1 -0
- package/core/path/index.esm.js +233 -0
- package/core/path/index.esm.js.map +1 -0
- package/core/path/join.d.ts +17 -0
- package/core/path/join.d.ts.map +1 -0
- package/core/path/normalize.d.ts +37 -0
- package/core/path/normalize.d.ts.map +1 -0
- package/core/path/resolve.d.ts +52 -0
- package/core/path/resolve.d.ts.map +1 -0
- package/core/path/segments.d.ts +59 -0
- package/core/path/segments.d.ts.map +1 -0
- package/core/patterns/glob.d.ts +46 -0
- package/core/patterns/glob.d.ts.map +1 -0
- package/core/platform/detect.d.ts +66 -0
- package/core/platform/detect.d.ts.map +1 -0
- package/core/platform/index.cjs.js +241 -0
- package/core/platform/index.cjs.js.map +1 -0
- package/core/platform/index.d.ts +3 -0
- package/core/platform/index.d.ts.map +1 -0
- package/core/platform/index.esm.js +226 -0
- package/core/platform/index.esm.js.map +1 -0
- package/core/platform/line-endings.d.ts +48 -0
- package/core/platform/line-endings.d.ts.map +1 -0
- package/heuristics/dependencies/analyze.d.ts +77 -0
- package/heuristics/dependencies/analyze.d.ts.map +1 -0
- package/heuristics/dependencies/index.cjs.js +1126 -0
- package/heuristics/dependencies/index.cjs.js.map +1 -0
- package/heuristics/dependencies/index.d.ts +2 -0
- package/heuristics/dependencies/index.d.ts.map +1 -0
- package/heuristics/dependencies/index.esm.js +1122 -0
- package/heuristics/dependencies/index.esm.js.map +1 -0
- package/heuristics/entry-points/discover.d.ts +86 -0
- package/heuristics/entry-points/discover.d.ts.map +1 -0
- package/heuristics/entry-points/index.cjs.js +1581 -0
- package/heuristics/entry-points/index.cjs.js.map +1 -0
- package/heuristics/entry-points/index.d.ts +2 -0
- package/heuristics/entry-points/index.d.ts.map +1 -0
- package/heuristics/entry-points/index.esm.js +1577 -0
- package/heuristics/entry-points/index.esm.js.map +1 -0
- package/heuristics/framework/identify.d.ts +84 -0
- package/heuristics/framework/identify.d.ts.map +1 -0
- package/heuristics/framework/index.cjs.js +3618 -0
- package/heuristics/framework/index.cjs.js.map +1 -0
- package/heuristics/framework/index.d.ts +2 -0
- package/heuristics/framework/index.d.ts.map +1 -0
- package/heuristics/framework/index.esm.js +3614 -0
- package/heuristics/framework/index.esm.js.map +1 -0
- package/heuristics/index.cjs.js +4833 -0
- package/heuristics/index.cjs.js.map +1 -0
- package/heuristics/index.d.ts +5 -0
- package/heuristics/index.d.ts.map +1 -0
- package/heuristics/index.esm.js +4822 -0
- package/heuristics/index.esm.js.map +1 -0
- package/heuristics/project-type/detect.d.ts +61 -0
- package/heuristics/project-type/detect.d.ts.map +1 -0
- package/heuristics/project-type/index.cjs.js +3633 -0
- package/heuristics/project-type/index.cjs.js.map +1 -0
- package/heuristics/project-type/index.d.ts +2 -0
- package/heuristics/project-type/index.d.ts.map +1 -0
- package/heuristics/project-type/index.esm.js +3631 -0
- package/heuristics/project-type/index.esm.js.map +1 -0
- package/index.cjs.js +10255 -0
- package/index.cjs.js.map +1 -0
- package/index.d.ts +10 -0
- package/index.d.ts.map +1 -0
- package/index.esm.js +10006 -0
- package/index.esm.js.map +1 -0
- package/models/index.cjs.js +3 -0
- package/models/index.cjs.js.map +1 -0
- package/models/index.d.ts +176 -0
- package/models/index.d.ts.map +1 -0
- package/models/index.esm.js +2 -0
- package/models/index.esm.js.map +1 -0
- package/nx/detect.d.ts +105 -0
- package/nx/detect.d.ts.map +1 -0
- package/nx/devkit-loader.d.ts +62 -0
- package/nx/devkit-loader.d.ts.map +1 -0
- package/nx/index.cjs.js +1302 -0
- package/nx/index.cjs.js.map +1 -0
- package/nx/index.d.ts +4 -0
- package/nx/index.d.ts.map +1 -0
- package/nx/index.esm.js +1286 -0
- package/nx/index.esm.js.map +1 -0
- package/nx/project-config.d.ts +109 -0
- package/nx/project-config.d.ts.map +1 -0
- package/package.json +218 -0
- package/project/config/detect.d.ts +77 -0
- package/project/config/detect.d.ts.map +1 -0
- package/project/config/index.cjs.js +1982 -0
- package/project/config/index.cjs.js.map +1 -0
- package/project/config/index.d.ts +4 -0
- package/project/config/index.d.ts.map +1 -0
- package/project/config/index.esm.js +1971 -0
- package/project/config/index.esm.js.map +1 -0
- package/project/config/parse.d.ts +53 -0
- package/project/config/parse.d.ts.map +1 -0
- package/project/config/patterns.d.ts +31 -0
- package/project/config/patterns.d.ts.map +1 -0
- package/project/index.cjs.js +2585 -0
- package/project/index.cjs.js.map +1 -0
- package/project/index.d.ts +5 -0
- package/project/index.d.ts.map +1 -0
- package/project/index.esm.js +2549 -0
- package/project/index.esm.js.map +1 -0
- package/project/package/dependencies.d.ts +101 -0
- package/project/package/dependencies.d.ts.map +1 -0
- package/project/package/index.cjs.js +972 -0
- package/project/package/index.cjs.js.map +1 -0
- package/project/package/index.d.ts +3 -0
- package/project/package/index.d.ts.map +1 -0
- package/project/package/index.esm.js +957 -0
- package/project/package/index.esm.js.map +1 -0
- package/project/package/read.d.ts +66 -0
- package/project/package/read.d.ts.map +1 -0
- package/project/root/detect.d.ts +65 -0
- package/project/root/detect.d.ts.map +1 -0
- package/project/root/index.cjs.js +860 -0
- package/project/root/index.cjs.js.map +1 -0
- package/project/root/index.d.ts +2 -0
- package/project/root/index.d.ts.map +1 -0
- package/project/root/index.esm.js +853 -0
- package/project/root/index.esm.js.map +1 -0
- package/project/traversal/index.cjs.js +1179 -0
- package/project/traversal/index.cjs.js.map +1 -0
- package/project/traversal/index.d.ts +3 -0
- package/project/traversal/index.d.ts.map +1 -0
- package/project/traversal/index.esm.js +1173 -0
- package/project/traversal/index.esm.js.map +1 -0
- package/project/traversal/search.d.ts +59 -0
- package/project/traversal/search.d.ts.map +1 -0
- package/project/traversal/walk.d.ts +63 -0
- package/project/traversal/walk.d.ts.map +1 -0
- package/tech/backend/detect-all.d.ts +13 -0
- package/tech/backend/detect-all.d.ts.map +1 -0
- package/tech/backend/express.d.ts +11 -0
- package/tech/backend/express.d.ts.map +1 -0
- package/tech/backend/fastify.d.ts +11 -0
- package/tech/backend/fastify.d.ts.map +1 -0
- package/tech/backend/hono.d.ts +11 -0
- package/tech/backend/hono.d.ts.map +1 -0
- package/tech/backend/index.cjs.js +939 -0
- package/tech/backend/index.cjs.js.map +1 -0
- package/tech/backend/index.d.ts +8 -0
- package/tech/backend/index.d.ts.map +1 -0
- package/tech/backend/index.esm.js +931 -0
- package/tech/backend/index.esm.js.map +1 -0
- package/tech/backend/koa.d.ts +11 -0
- package/tech/backend/koa.d.ts.map +1 -0
- package/tech/backend/nestjs.d.ts +11 -0
- package/tech/backend/nestjs.d.ts.map +1 -0
- package/tech/backend/types.d.ts +31 -0
- package/tech/backend/types.d.ts.map +1 -0
- package/tech/build/babel.d.ts +13 -0
- package/tech/build/babel.d.ts.map +1 -0
- package/tech/build/detect-all.d.ts +13 -0
- package/tech/build/detect-all.d.ts.map +1 -0
- package/tech/build/esbuild.d.ts +11 -0
- package/tech/build/esbuild.d.ts.map +1 -0
- package/tech/build/index.cjs.js +1118 -0
- package/tech/build/index.cjs.js.map +1 -0
- package/tech/build/index.d.ts +10 -0
- package/tech/build/index.d.ts.map +1 -0
- package/tech/build/index.esm.js +1102 -0
- package/tech/build/index.esm.js.map +1 -0
- package/tech/build/parcel.d.ts +13 -0
- package/tech/build/parcel.d.ts.map +1 -0
- package/tech/build/rollup.d.ts +13 -0
- package/tech/build/rollup.d.ts.map +1 -0
- package/tech/build/swc.d.ts +13 -0
- package/tech/build/swc.d.ts.map +1 -0
- package/tech/build/types.d.ts +31 -0
- package/tech/build/types.d.ts.map +1 -0
- package/tech/build/vite.d.ts +13 -0
- package/tech/build/vite.d.ts.map +1 -0
- package/tech/build/webpack.d.ts +13 -0
- package/tech/build/webpack.d.ts.map +1 -0
- package/tech/frontend/angular.d.ts +11 -0
- package/tech/frontend/angular.d.ts.map +1 -0
- package/tech/frontend/astro.d.ts +11 -0
- package/tech/frontend/astro.d.ts.map +1 -0
- package/tech/frontend/detect-all.d.ts +13 -0
- package/tech/frontend/detect-all.d.ts.map +1 -0
- package/tech/frontend/gatsby.d.ts +11 -0
- package/tech/frontend/gatsby.d.ts.map +1 -0
- package/tech/frontend/index.cjs.js +1310 -0
- package/tech/frontend/index.cjs.js.map +1 -0
- package/tech/frontend/index.d.ts +15 -0
- package/tech/frontend/index.d.ts.map +1 -0
- package/tech/frontend/index.esm.js +1295 -0
- package/tech/frontend/index.esm.js.map +1 -0
- package/tech/frontend/nextjs.d.ts +11 -0
- package/tech/frontend/nextjs.d.ts.map +1 -0
- package/tech/frontend/nuxt.d.ts +11 -0
- package/tech/frontend/nuxt.d.ts.map +1 -0
- package/tech/frontend/qwik.d.ts +11 -0
- package/tech/frontend/qwik.d.ts.map +1 -0
- package/tech/frontend/react.d.ts +11 -0
- package/tech/frontend/react.d.ts.map +1 -0
- package/tech/frontend/remix.d.ts +11 -0
- package/tech/frontend/remix.d.ts.map +1 -0
- package/tech/frontend/solid.d.ts +11 -0
- package/tech/frontend/solid.d.ts.map +1 -0
- package/tech/frontend/svelte.d.ts +11 -0
- package/tech/frontend/svelte.d.ts.map +1 -0
- package/tech/frontend/sveltekit.d.ts +11 -0
- package/tech/frontend/sveltekit.d.ts.map +1 -0
- package/tech/frontend/types.d.ts +35 -0
- package/tech/frontend/types.d.ts.map +1 -0
- package/tech/frontend/vue.d.ts +11 -0
- package/tech/frontend/vue.d.ts.map +1 -0
- package/tech/index.cjs.js +3684 -0
- package/tech/index.cjs.js.map +1 -0
- package/tech/index.d.ts +96 -0
- package/tech/index.d.ts.map +1 -0
- package/tech/index.esm.js +3603 -0
- package/tech/index.esm.js.map +1 -0
- package/tech/legacy/angularjs.d.ts +12 -0
- package/tech/legacy/angularjs.d.ts.map +1 -0
- package/tech/legacy/backbone.d.ts +11 -0
- package/tech/legacy/backbone.d.ts.map +1 -0
- package/tech/legacy/detect-all.d.ts +13 -0
- package/tech/legacy/detect-all.d.ts.map +1 -0
- package/tech/legacy/ember.d.ts +11 -0
- package/tech/legacy/ember.d.ts.map +1 -0
- package/tech/legacy/index.cjs.js +903 -0
- package/tech/legacy/index.cjs.js.map +1 -0
- package/tech/legacy/index.d.ts +7 -0
- package/tech/legacy/index.d.ts.map +1 -0
- package/tech/legacy/index.esm.js +896 -0
- package/tech/legacy/index.esm.js.map +1 -0
- package/tech/legacy/jquery.d.ts +11 -0
- package/tech/legacy/jquery.d.ts.map +1 -0
- package/tech/legacy/types.d.ts +33 -0
- package/tech/legacy/types.d.ts.map +1 -0
- package/tech/linting/biome.d.ts +11 -0
- package/tech/linting/biome.d.ts.map +1 -0
- package/tech/linting/detect-all.d.ts +13 -0
- package/tech/linting/detect-all.d.ts.map +1 -0
- package/tech/linting/eslint.d.ts +13 -0
- package/tech/linting/eslint.d.ts.map +1 -0
- package/tech/linting/index.cjs.js +992 -0
- package/tech/linting/index.cjs.js.map +1 -0
- package/tech/linting/index.d.ts +7 -0
- package/tech/linting/index.d.ts.map +1 -0
- package/tech/linting/index.esm.js +982 -0
- package/tech/linting/index.esm.js.map +1 -0
- package/tech/linting/prettier.d.ts +13 -0
- package/tech/linting/prettier.d.ts.map +1 -0
- package/tech/linting/stylelint.d.ts +13 -0
- package/tech/linting/stylelint.d.ts.map +1 -0
- package/tech/linting/types.d.ts +31 -0
- package/tech/linting/types.d.ts.map +1 -0
- package/tech/monorepo/detect-all.d.ts +13 -0
- package/tech/monorepo/detect-all.d.ts.map +1 -0
- package/tech/monorepo/index.cjs.js +1021 -0
- package/tech/monorepo/index.cjs.js.map +1 -0
- package/tech/monorepo/index.d.ts +10 -0
- package/tech/monorepo/index.d.ts.map +1 -0
- package/tech/monorepo/index.esm.js +1011 -0
- package/tech/monorepo/index.esm.js.map +1 -0
- package/tech/monorepo/lerna.d.ts +11 -0
- package/tech/monorepo/lerna.d.ts.map +1 -0
- package/tech/monorepo/npm-workspaces.d.ts +11 -0
- package/tech/monorepo/npm-workspaces.d.ts.map +1 -0
- package/tech/monorepo/nx.d.ts +11 -0
- package/tech/monorepo/nx.d.ts.map +1 -0
- package/tech/monorepo/pnpm-workspaces.d.ts +9 -0
- package/tech/monorepo/pnpm-workspaces.d.ts.map +1 -0
- package/tech/monorepo/rush.d.ts +11 -0
- package/tech/monorepo/rush.d.ts.map +1 -0
- package/tech/monorepo/turborepo.d.ts +11 -0
- package/tech/monorepo/turborepo.d.ts.map +1 -0
- package/tech/monorepo/types.d.ts +39 -0
- package/tech/monorepo/types.d.ts.map +1 -0
- package/tech/monorepo/yarn-workspaces.d.ts +11 -0
- package/tech/monorepo/yarn-workspaces.d.ts.map +1 -0
- package/tech/shared-utils/detector-helpers.d.ts +52 -0
- package/tech/shared-utils/detector-helpers.d.ts.map +1 -0
- package/tech/shared-utils/types.d.ts +41 -0
- package/tech/shared-utils/types.d.ts.map +1 -0
- package/tech/testing/cypress.d.ts +13 -0
- package/tech/testing/cypress.d.ts.map +1 -0
- package/tech/testing/detect-all.d.ts +13 -0
- package/tech/testing/detect-all.d.ts.map +1 -0
- package/tech/testing/index.cjs.js +1031 -0
- package/tech/testing/index.cjs.js.map +1 -0
- package/tech/testing/index.d.ts +8 -0
- package/tech/testing/index.d.ts.map +1 -0
- package/tech/testing/index.esm.js +1018 -0
- package/tech/testing/index.esm.js.map +1 -0
- package/tech/testing/jest.d.ts +13 -0
- package/tech/testing/jest.d.ts.map +1 -0
- package/tech/testing/mocha.d.ts +13 -0
- package/tech/testing/mocha.d.ts.map +1 -0
- package/tech/testing/playwright.d.ts +13 -0
- package/tech/testing/playwright.d.ts.map +1 -0
- package/tech/testing/types.d.ts +35 -0
- package/tech/testing/types.d.ts.map +1 -0
- package/tech/testing/vitest.d.ts +13 -0
- package/tech/testing/vitest.d.ts.map +1 -0
- package/tech/types/detectors.d.ts +67 -0
- package/tech/types/detectors.d.ts.map +1 -0
- package/tech/types/index.cjs.js +1045 -0
- package/tech/types/index.cjs.js.map +1 -0
- package/tech/types/index.d.ts +2 -0
- package/tech/types/index.d.ts.map +1 -0
- package/tech/types/index.esm.js +1039 -0
- package/tech/types/index.esm.js.map +1 -0
- package/vfs/commit.d.ts +32 -0
- package/vfs/commit.d.ts.map +1 -0
- package/vfs/diff.d.ts +73 -0
- package/vfs/diff.d.ts.map +1 -0
- package/vfs/factory.d.ts +37 -0
- package/vfs/factory.d.ts.map +1 -0
- package/vfs/fs-tree.d.ts +13 -0
- package/vfs/fs-tree.d.ts.map +1 -0
- package/vfs/index.cjs.js +1600 -0
- package/vfs/index.cjs.js.map +1 -0
- package/vfs/index.d.ts +6 -0
- package/vfs/index.d.ts.map +1 -0
- package/vfs/index.esm.js +1590 -0
- package/vfs/index.esm.js.map +1 -0
- package/vfs/types.d.ts +178 -0
- package/vfs/types.d.ts.map +1 -0
|
@@ -0,0 +1,1179 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var node_path = require('node:path');
|
|
4
|
+
var node_fs = require('node:fs');
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Safe copies of Error built-ins via factory functions.
|
|
8
|
+
*
|
|
9
|
+
* Since constructors cannot be safely captured via Object.assign, this module
|
|
10
|
+
* provides factory functions that use Reflect.construct internally.
|
|
11
|
+
*
|
|
12
|
+
* These references are captured at module initialization time to protect against
|
|
13
|
+
* prototype pollution attacks. Import only what you need for tree-shaking.
|
|
14
|
+
*
|
|
15
|
+
* @module @hyperfrontend/immutable-api-utils/built-in-copy/error
|
|
16
|
+
*/
|
|
17
|
+
// Capture references at module initialization time
|
|
18
|
+
const _Error = globalThis.Error;
|
|
19
|
+
const _Reflect$1 = globalThis.Reflect;
|
|
20
|
+
/**
|
|
21
|
+
* (Safe copy) Creates a new Error using the captured Error constructor.
|
|
22
|
+
* Use this instead of `new Error()`.
|
|
23
|
+
*
|
|
24
|
+
* @param message - Optional error message.
|
|
25
|
+
* @param options - Optional error options.
|
|
26
|
+
* @returns A new Error instance.
|
|
27
|
+
*/
|
|
28
|
+
const createError = (message, options) => _Reflect$1.construct(_Error, [message, options]);
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* Safe copies of JSON built-in methods.
|
|
32
|
+
*
|
|
33
|
+
* These references are captured at module initialization time to protect against
|
|
34
|
+
* prototype pollution attacks. Import only what you need for tree-shaking.
|
|
35
|
+
*
|
|
36
|
+
* @module @hyperfrontend/immutable-api-utils/built-in-copy/json
|
|
37
|
+
*/
|
|
38
|
+
// Capture references at module initialization time
|
|
39
|
+
const _JSON = globalThis.JSON;
|
|
40
|
+
/**
|
|
41
|
+
* (Safe copy) Converts a JavaScript value to a JavaScript Object Notation (JSON) string.
|
|
42
|
+
*/
|
|
43
|
+
const stringify = _JSON.stringify;
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* Safe copies of Object built-in methods.
|
|
47
|
+
*
|
|
48
|
+
* These references are captured at module initialization time to protect against
|
|
49
|
+
* prototype pollution attacks. Import only what you need for tree-shaking.
|
|
50
|
+
*
|
|
51
|
+
* @module @hyperfrontend/immutable-api-utils/built-in-copy/object
|
|
52
|
+
*/
|
|
53
|
+
// Capture references at module initialization time
|
|
54
|
+
const _Object = globalThis.Object;
|
|
55
|
+
/**
|
|
56
|
+
* (Safe copy) Prevents modification of existing property attributes and values,
|
|
57
|
+
* and prevents the addition of new properties.
|
|
58
|
+
*/
|
|
59
|
+
const freeze = _Object.freeze;
|
|
60
|
+
/**
|
|
61
|
+
* (Safe copy) Returns the names of the enumerable string properties and methods of an object.
|
|
62
|
+
*/
|
|
63
|
+
const keys = _Object.keys;
|
|
64
|
+
/**
|
|
65
|
+
* (Safe copy) Returns an array of key/values of the enumerable own properties of an object.
|
|
66
|
+
*/
|
|
67
|
+
const entries = _Object.entries;
|
|
68
|
+
/**
|
|
69
|
+
* (Safe copy) Adds one or more properties to an object, and/or modifies attributes of existing properties.
|
|
70
|
+
*/
|
|
71
|
+
const defineProperties = _Object.defineProperties;
|
|
72
|
+
|
|
73
|
+
/**
|
|
74
|
+
* Safe copies of Array built-in static methods.
|
|
75
|
+
*
|
|
76
|
+
* These references are captured at module initialization time to protect against
|
|
77
|
+
* prototype pollution attacks. Import only what you need for tree-shaking.
|
|
78
|
+
*
|
|
79
|
+
* @module @hyperfrontend/immutable-api-utils/built-in-copy/array
|
|
80
|
+
*/
|
|
81
|
+
// Capture references at module initialization time
|
|
82
|
+
const _Array = globalThis.Array;
|
|
83
|
+
/**
|
|
84
|
+
* (Safe copy) Determines whether the passed value is an Array.
|
|
85
|
+
*/
|
|
86
|
+
const isArray = _Array.isArray;
|
|
87
|
+
|
|
88
|
+
/**
|
|
89
|
+
* Safe copies of Console built-in methods.
|
|
90
|
+
*
|
|
91
|
+
* These references are captured at module initialization time to protect against
|
|
92
|
+
* prototype pollution attacks. Import only what you need for tree-shaking.
|
|
93
|
+
*
|
|
94
|
+
* @module @hyperfrontend/immutable-api-utils/built-in-copy/console
|
|
95
|
+
*/
|
|
96
|
+
// Capture references at module initialization time
|
|
97
|
+
const _console = globalThis.console;
|
|
98
|
+
/**
|
|
99
|
+
* (Safe copy) Outputs a message to the console.
|
|
100
|
+
*/
|
|
101
|
+
const log = _console.log.bind(_console);
|
|
102
|
+
/**
|
|
103
|
+
* (Safe copy) Outputs a warning message to the console.
|
|
104
|
+
*/
|
|
105
|
+
const warn = _console.warn.bind(_console);
|
|
106
|
+
/**
|
|
107
|
+
* (Safe copy) Outputs an error message to the console.
|
|
108
|
+
*/
|
|
109
|
+
const error = _console.error.bind(_console);
|
|
110
|
+
/**
|
|
111
|
+
* (Safe copy) Outputs an informational message to the console.
|
|
112
|
+
*/
|
|
113
|
+
const info = _console.info.bind(_console);
|
|
114
|
+
/**
|
|
115
|
+
* (Safe copy) Outputs a debug message to the console.
|
|
116
|
+
*/
|
|
117
|
+
const debug = _console.debug.bind(_console);
|
|
118
|
+
/**
|
|
119
|
+
* (Safe copy) Outputs a stack trace to the console.
|
|
120
|
+
*/
|
|
121
|
+
_console.trace.bind(_console);
|
|
122
|
+
/**
|
|
123
|
+
* (Safe copy) Displays an interactive listing of the properties of a specified object.
|
|
124
|
+
*/
|
|
125
|
+
_console.dir.bind(_console);
|
|
126
|
+
/**
|
|
127
|
+
* (Safe copy) Displays tabular data as a table.
|
|
128
|
+
*/
|
|
129
|
+
_console.table.bind(_console);
|
|
130
|
+
/**
|
|
131
|
+
* (Safe copy) Writes an error message to the console if the assertion is false.
|
|
132
|
+
*/
|
|
133
|
+
_console.assert.bind(_console);
|
|
134
|
+
/**
|
|
135
|
+
* (Safe copy) Clears the console.
|
|
136
|
+
*/
|
|
137
|
+
_console.clear.bind(_console);
|
|
138
|
+
/**
|
|
139
|
+
* (Safe copy) Logs the number of times that this particular call to count() has been called.
|
|
140
|
+
*/
|
|
141
|
+
_console.count.bind(_console);
|
|
142
|
+
/**
|
|
143
|
+
* (Safe copy) Resets the counter used with console.count().
|
|
144
|
+
*/
|
|
145
|
+
_console.countReset.bind(_console);
|
|
146
|
+
/**
|
|
147
|
+
* (Safe copy) Creates a new inline group in the console.
|
|
148
|
+
*/
|
|
149
|
+
_console.group.bind(_console);
|
|
150
|
+
/**
|
|
151
|
+
* (Safe copy) Creates a new inline group in the console that is initially collapsed.
|
|
152
|
+
*/
|
|
153
|
+
_console.groupCollapsed.bind(_console);
|
|
154
|
+
/**
|
|
155
|
+
* (Safe copy) Exits the current inline group.
|
|
156
|
+
*/
|
|
157
|
+
_console.groupEnd.bind(_console);
|
|
158
|
+
/**
|
|
159
|
+
* (Safe copy) Starts a timer with a name specified as an input parameter.
|
|
160
|
+
*/
|
|
161
|
+
_console.time.bind(_console);
|
|
162
|
+
/**
|
|
163
|
+
* (Safe copy) Stops a timer that was previously started.
|
|
164
|
+
*/
|
|
165
|
+
_console.timeEnd.bind(_console);
|
|
166
|
+
/**
|
|
167
|
+
* (Safe copy) Logs the current value of a timer that was previously started.
|
|
168
|
+
*/
|
|
169
|
+
_console.timeLog.bind(_console);
|
|
170
|
+
|
|
171
|
+
/**
|
|
172
|
+
* Safe copies of Set built-in via factory function.
|
|
173
|
+
*
|
|
174
|
+
* Since constructors cannot be safely captured via Object.assign, this module
|
|
175
|
+
* provides a factory function that uses Reflect.construct internally.
|
|
176
|
+
*
|
|
177
|
+
* These references are captured at module initialization time to protect against
|
|
178
|
+
* prototype pollution attacks. Import only what you need for tree-shaking.
|
|
179
|
+
*
|
|
180
|
+
* @module @hyperfrontend/immutable-api-utils/built-in-copy/set
|
|
181
|
+
*/
|
|
182
|
+
// Capture references at module initialization time
|
|
183
|
+
const _Set = globalThis.Set;
|
|
184
|
+
const _Reflect = globalThis.Reflect;
|
|
185
|
+
/**
|
|
186
|
+
* (Safe copy) Creates a new Set using the captured Set constructor.
|
|
187
|
+
* Use this instead of `new Set()`.
|
|
188
|
+
*
|
|
189
|
+
* @param iterable - Optional iterable of values.
|
|
190
|
+
* @returns A new Set instance.
|
|
191
|
+
*/
|
|
192
|
+
const createSet = (iterable) => _Reflect.construct(_Set, iterable ? [iterable] : []);
|
|
193
|
+
|
|
194
|
+
const registeredClasses = [];
|
|
195
|
+
|
|
196
|
+
/**
|
|
197
|
+
* Returns the data type of the target.
|
|
198
|
+
* Uses native `typeof` operator, however, makes distinction between `null`, `array`, and `object`.
|
|
199
|
+
* Also, when classes are registered via `registerClass`, it checks if objects are instance of any known registered class.
|
|
200
|
+
*
|
|
201
|
+
* @param target - The target to get the data type of.
|
|
202
|
+
* @returns The data type of the target.
|
|
203
|
+
*/
|
|
204
|
+
const getType = (target) => {
|
|
205
|
+
if (target === null)
|
|
206
|
+
return 'null';
|
|
207
|
+
const nativeDataType = typeof target;
|
|
208
|
+
if (nativeDataType === 'object') {
|
|
209
|
+
if (isArray(target))
|
|
210
|
+
return 'array';
|
|
211
|
+
for (const registeredClass of registeredClasses) {
|
|
212
|
+
if (target instanceof registeredClass)
|
|
213
|
+
return registeredClass.name;
|
|
214
|
+
}
|
|
215
|
+
}
|
|
216
|
+
return nativeDataType;
|
|
217
|
+
};
|
|
218
|
+
|
|
219
|
+
/* eslint-disable @typescript-eslint/no-explicit-any */
|
|
220
|
+
/**
|
|
221
|
+
* Creates a wrapper function that only executes the wrapped function if the condition function returns true.
|
|
222
|
+
*
|
|
223
|
+
* @param func - The function to be conditionally executed.
|
|
224
|
+
* @param conditionFunc - A function that returns a boolean, determining if `func` should be executed.
|
|
225
|
+
* @returns A wrapped version of `func` that executes conditionally.
|
|
226
|
+
*/
|
|
227
|
+
function createConditionalExecutionFunction(func, conditionFunc) {
|
|
228
|
+
return function (...args) {
|
|
229
|
+
if (conditionFunc()) {
|
|
230
|
+
return func(...args);
|
|
231
|
+
}
|
|
232
|
+
};
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
/* eslint-disable @typescript-eslint/no-explicit-any */
|
|
236
|
+
/**
|
|
237
|
+
* Creates a wrapper function that silently ignores any errors thrown by the wrapped void function.
|
|
238
|
+
* This function is specifically for wrapping functions that do not return a value (void functions).
|
|
239
|
+
* Exceptions are swallowed without any logging or handling.
|
|
240
|
+
*
|
|
241
|
+
* @param func - The void function to be wrapped.
|
|
242
|
+
* @returns A wrapped version of the input function that ignores errors.
|
|
243
|
+
*/
|
|
244
|
+
function createErrorIgnoringFunction(func) {
|
|
245
|
+
return function (...args) {
|
|
246
|
+
try {
|
|
247
|
+
func(...args);
|
|
248
|
+
}
|
|
249
|
+
catch {
|
|
250
|
+
// Deliberately swallowing/ignoring the exception
|
|
251
|
+
}
|
|
252
|
+
};
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
/* eslint-disable @typescript-eslint/no-unused-vars */
|
|
256
|
+
/**
|
|
257
|
+
* A no-operation function (noop) that does nothing regardless of the arguments passed.
|
|
258
|
+
* It is designed to be as permissive as possible in its typing without using the `Function` keyword.
|
|
259
|
+
*
|
|
260
|
+
* @param args - Any arguments passed to the function (ignored)
|
|
261
|
+
*/
|
|
262
|
+
const noop = (...args) => {
|
|
263
|
+
// Intentionally does nothing
|
|
264
|
+
};
|
|
265
|
+
|
|
266
|
+
const logLevels = ['none', 'error', 'warn', 'log', 'info', 'debug'];
|
|
267
|
+
const priority = {
|
|
268
|
+
error: 4,
|
|
269
|
+
warn: 3,
|
|
270
|
+
log: 2,
|
|
271
|
+
info: 1,
|
|
272
|
+
debug: 0,
|
|
273
|
+
};
|
|
274
|
+
/**
|
|
275
|
+
* Validates whether a given string is a valid log level.
|
|
276
|
+
*
|
|
277
|
+
* @param level - The log level to validate
|
|
278
|
+
* @returns True if the level is valid, false otherwise
|
|
279
|
+
*/
|
|
280
|
+
function isValidLogLevel(level) {
|
|
281
|
+
return logLevels.includes(level);
|
|
282
|
+
}
|
|
283
|
+
/**
|
|
284
|
+
* Creates a log level configuration manager for controlling logging behavior.
|
|
285
|
+
* Provides methods to get, set, and evaluate log levels based on priority.
|
|
286
|
+
*
|
|
287
|
+
* @param level - The initial log level (defaults to 'error')
|
|
288
|
+
* @returns A configuration object with log level management methods
|
|
289
|
+
* @throws {Error} When the provided level is not a valid log level
|
|
290
|
+
*/
|
|
291
|
+
function createLogLevelConfig(level = 'error') {
|
|
292
|
+
if (!isValidLogLevel(level)) {
|
|
293
|
+
throw createError('Cannot create log level configuration with a valid default log level');
|
|
294
|
+
}
|
|
295
|
+
const state = { level };
|
|
296
|
+
const getLogLevel = () => state.level;
|
|
297
|
+
const setLogLevel = (level) => {
|
|
298
|
+
if (!isValidLogLevel(level)) {
|
|
299
|
+
throw createError(`Cannot set value '${level}' level. Expected levels are ${logLevels}.`);
|
|
300
|
+
}
|
|
301
|
+
state.level = level;
|
|
302
|
+
};
|
|
303
|
+
const shouldLog = (level) => {
|
|
304
|
+
if (state.level === 'none' || level === 'none' || !isValidLogLevel(level)) {
|
|
305
|
+
return false;
|
|
306
|
+
}
|
|
307
|
+
return priority[level] >= priority[state.level];
|
|
308
|
+
};
|
|
309
|
+
return freeze({
|
|
310
|
+
getLogLevel,
|
|
311
|
+
setLogLevel,
|
|
312
|
+
shouldLog,
|
|
313
|
+
});
|
|
314
|
+
}
|
|
315
|
+
|
|
316
|
+
/**
|
|
317
|
+
* Creates a logger instance with configurable log level filtering.
|
|
318
|
+
* Each log function is wrapped to respect the current log level setting.
|
|
319
|
+
*
|
|
320
|
+
* @param error - Function to handle error-level logs (required)
|
|
321
|
+
* @param warn - Function to handle warning-level logs (optional, defaults to noop)
|
|
322
|
+
* @param log - Function to handle standard logs (optional, defaults to noop)
|
|
323
|
+
* @param info - Function to handle info-level logs (optional, defaults to noop)
|
|
324
|
+
* @param debug - Function to handle debug-level logs (optional, defaults to noop)
|
|
325
|
+
* @returns A frozen logger object with log methods and level control
|
|
326
|
+
* @throws {ErrorLevelFn} When any provided log function is invalid
|
|
327
|
+
*/
|
|
328
|
+
function createLogger(error, warn = noop, log = noop, info = noop, debug = noop) {
|
|
329
|
+
if (notValidLogFn(error)) {
|
|
330
|
+
throw createError(notFnMsg('error'));
|
|
331
|
+
}
|
|
332
|
+
if (notValidLogFn(warn)) {
|
|
333
|
+
throw createError(notFnMsg('warn'));
|
|
334
|
+
}
|
|
335
|
+
if (notValidLogFn(log)) {
|
|
336
|
+
throw createError(notFnMsg('log'));
|
|
337
|
+
}
|
|
338
|
+
if (notValidLogFn(info)) {
|
|
339
|
+
throw createError(notFnMsg('info'));
|
|
340
|
+
}
|
|
341
|
+
if (notValidLogFn(debug)) {
|
|
342
|
+
throw createError(notFnMsg('debug'));
|
|
343
|
+
}
|
|
344
|
+
const { setLogLevel, getLogLevel, shouldLog } = createLogLevelConfig();
|
|
345
|
+
const wrapLogFn = (fn, level) => {
|
|
346
|
+
if (fn === noop)
|
|
347
|
+
return fn;
|
|
348
|
+
const condition = () => shouldLog(level);
|
|
349
|
+
return createConditionalExecutionFunction(createErrorIgnoringFunction(fn), condition);
|
|
350
|
+
};
|
|
351
|
+
return freeze({
|
|
352
|
+
error: wrapLogFn(error, 'error'),
|
|
353
|
+
warn: wrapLogFn(warn, 'warn'),
|
|
354
|
+
log: wrapLogFn(log, 'log'),
|
|
355
|
+
info: wrapLogFn(info, 'info'),
|
|
356
|
+
debug: wrapLogFn(debug, 'debug'),
|
|
357
|
+
setLogLevel,
|
|
358
|
+
getLogLevel,
|
|
359
|
+
});
|
|
360
|
+
}
|
|
361
|
+
/**
|
|
362
|
+
* Validates whether a given value is a valid log function.
|
|
363
|
+
*
|
|
364
|
+
* @param fn - The value to validate
|
|
365
|
+
* @returns True if the value is not a function (invalid), false if it is valid
|
|
366
|
+
*/
|
|
367
|
+
function notValidLogFn(fn) {
|
|
368
|
+
return getType(fn) !== 'function' && fn !== noop;
|
|
369
|
+
}
|
|
370
|
+
/**
|
|
371
|
+
* Generates an error message for invalid log function parameters.
|
|
372
|
+
*
|
|
373
|
+
* @param label - The name of the log function that failed validation
|
|
374
|
+
* @returns A formatted error message string
|
|
375
|
+
*/
|
|
376
|
+
function notFnMsg(label) {
|
|
377
|
+
return `Cannot create a logger when ${label} is not a function`;
|
|
378
|
+
}
|
|
379
|
+
|
|
380
|
+
createLogger(error, warn, log, info, debug);
|
|
381
|
+
|
|
382
|
+
/**
|
|
383
|
+
* Global log level registry.
|
|
384
|
+
* Tracks all created scoped loggers to allow global log level changes.
|
|
385
|
+
*/
|
|
386
|
+
const loggerRegistry = createSet();
|
|
387
|
+
/** Redacted placeholder for sensitive values */
|
|
388
|
+
const REDACTED = '[REDACTED]';
|
|
389
|
+
/**
|
|
390
|
+
* Patterns that indicate a sensitive key name.
|
|
391
|
+
* Keys containing these patterns will have their values sanitized.
|
|
392
|
+
*/
|
|
393
|
+
const SENSITIVE_KEY_PATTERNS = [
|
|
394
|
+
/token/i,
|
|
395
|
+
/key/i,
|
|
396
|
+
/password/i,
|
|
397
|
+
/secret/i,
|
|
398
|
+
/credential/i,
|
|
399
|
+
/auth/i,
|
|
400
|
+
/bearer/i,
|
|
401
|
+
/api[_-]?key/i,
|
|
402
|
+
/private/i,
|
|
403
|
+
/passphrase/i,
|
|
404
|
+
];
|
|
405
|
+
/**
|
|
406
|
+
* Checks if a key name indicates sensitive data.
|
|
407
|
+
*
|
|
408
|
+
* @param key - Key name to check
|
|
409
|
+
* @returns True if the key indicates sensitive data
|
|
410
|
+
*/
|
|
411
|
+
function isSensitiveKey(key) {
|
|
412
|
+
return SENSITIVE_KEY_PATTERNS.some((pattern) => pattern.test(key));
|
|
413
|
+
}
|
|
414
|
+
/**
|
|
415
|
+
* Sanitizes an object by replacing sensitive values with REDACTED.
|
|
416
|
+
* This function recursively processes nested objects and arrays.
|
|
417
|
+
*
|
|
418
|
+
* @param obj - Object to sanitize
|
|
419
|
+
* @returns New object with sensitive values redacted
|
|
420
|
+
*/
|
|
421
|
+
function sanitize(obj) {
|
|
422
|
+
if (obj === null || obj === undefined) {
|
|
423
|
+
return obj;
|
|
424
|
+
}
|
|
425
|
+
if (isArray(obj)) {
|
|
426
|
+
return obj.map((item) => sanitize(item));
|
|
427
|
+
}
|
|
428
|
+
if (typeof obj === 'object') {
|
|
429
|
+
const result = {};
|
|
430
|
+
for (const [key, value] of entries(obj)) {
|
|
431
|
+
if (isSensitiveKey(key)) {
|
|
432
|
+
result[key] = REDACTED;
|
|
433
|
+
}
|
|
434
|
+
else if (typeof value === 'object' && value !== null) {
|
|
435
|
+
result[key] = sanitize(value);
|
|
436
|
+
}
|
|
437
|
+
else {
|
|
438
|
+
result[key] = value;
|
|
439
|
+
}
|
|
440
|
+
}
|
|
441
|
+
return result;
|
|
442
|
+
}
|
|
443
|
+
return obj;
|
|
444
|
+
}
|
|
445
|
+
/**
|
|
446
|
+
* Formats a log message with optional metadata.
|
|
447
|
+
*
|
|
448
|
+
* @param namespace - Logger namespace prefix
|
|
449
|
+
* @param message - Log message
|
|
450
|
+
* @param meta - Optional metadata object
|
|
451
|
+
* @returns Formatted log string
|
|
452
|
+
*/
|
|
453
|
+
function formatMessage(namespace, message, meta) {
|
|
454
|
+
const prefix = `[${namespace}]`;
|
|
455
|
+
if (meta && keys(meta).length > 0) {
|
|
456
|
+
const sanitizedMeta = sanitize(meta);
|
|
457
|
+
return `${prefix} ${message} ${stringify(sanitizedMeta)}`;
|
|
458
|
+
}
|
|
459
|
+
return `${prefix} ${message}`;
|
|
460
|
+
}
|
|
461
|
+
/**
|
|
462
|
+
* Creates a scoped logger with namespace prefix and optional secret sanitization.
|
|
463
|
+
* All log messages will be prefixed with [namespace] and sensitive metadata
|
|
464
|
+
* values will be automatically redacted.
|
|
465
|
+
*
|
|
466
|
+
* @param namespace - Logger namespace (e.g., 'project-scope', 'analyze')
|
|
467
|
+
* @param options - Logger configuration options
|
|
468
|
+
* @returns A configured scoped logger instance
|
|
469
|
+
*
|
|
470
|
+
* @example
|
|
471
|
+
* ```typescript
|
|
472
|
+
* const logger = createScopedLogger('project-scope')
|
|
473
|
+
* logger.setLogLevel('debug')
|
|
474
|
+
*
|
|
475
|
+
* // Basic logging
|
|
476
|
+
* logger.info('Starting analysis', { path: './project' })
|
|
477
|
+
*
|
|
478
|
+
* // Sensitive data is automatically redacted
|
|
479
|
+
* logger.debug('Config loaded', { apiKey: 'secret123' })
|
|
480
|
+
* // Output: [project-scope] Config loaded {"apiKey":"[REDACTED]"}
|
|
481
|
+
* ```
|
|
482
|
+
*/
|
|
483
|
+
function createScopedLogger(namespace, options = {}) {
|
|
484
|
+
const { level = 'error', sanitizeSecrets = true } = options;
|
|
485
|
+
// Create wrapper functions that add namespace prefix and sanitization
|
|
486
|
+
const createLogFn = (baseFn) => (message, meta) => {
|
|
487
|
+
const processedMeta = sanitizeSecrets && meta ? sanitize(meta) : meta;
|
|
488
|
+
baseFn(formatMessage(namespace, message, processedMeta));
|
|
489
|
+
};
|
|
490
|
+
// Create base logger with wrapped functions
|
|
491
|
+
const baseLogger = createLogger(createLogFn(error), createLogFn(warn), createLogFn(log), createLogFn(info), createLogFn(debug));
|
|
492
|
+
// Set initial log level (use global override if set)
|
|
493
|
+
baseLogger.setLogLevel(level);
|
|
494
|
+
const scopedLogger = freeze({
|
|
495
|
+
error: (message, meta) => baseLogger.error(message, meta),
|
|
496
|
+
warn: (message, meta) => baseLogger.warn(message, meta),
|
|
497
|
+
log: (message, meta) => baseLogger.log(message, meta),
|
|
498
|
+
info: (message, meta) => baseLogger.info(message, meta),
|
|
499
|
+
debug: (message, meta) => baseLogger.debug(message, meta),
|
|
500
|
+
setLogLevel: baseLogger.setLogLevel,
|
|
501
|
+
getLogLevel: baseLogger.getLogLevel,
|
|
502
|
+
});
|
|
503
|
+
// Register logger for global level management
|
|
504
|
+
loggerRegistry.add(scopedLogger);
|
|
505
|
+
return scopedLogger;
|
|
506
|
+
}
|
|
507
|
+
/**
|
|
508
|
+
* Default logger instance for the project-scope library.
|
|
509
|
+
* Use this for general logging within the library.
|
|
510
|
+
*
|
|
511
|
+
* @example
|
|
512
|
+
* ```typescript
|
|
513
|
+
* import { logger } from '@hyperfrontend/project-scope/core'
|
|
514
|
+
*
|
|
515
|
+
* logger.setLogLevel('debug')
|
|
516
|
+
* logger.debug('Analyzing project', { path: './src' })
|
|
517
|
+
* ```
|
|
518
|
+
*/
|
|
519
|
+
createScopedLogger('project-scope');
|
|
520
|
+
|
|
521
|
+
createScopedLogger('project-scope:fs');
|
|
522
|
+
/**
|
|
523
|
+
* Create a file system error with code and context.
|
|
524
|
+
*
|
|
525
|
+
* @param message - The error message describing what went wrong
|
|
526
|
+
* @param code - The category code for this type of filesystem failure
|
|
527
|
+
* @param context - Additional context including path, operation, and cause
|
|
528
|
+
* @returns A configured Error object with code and context properties
|
|
529
|
+
*/
|
|
530
|
+
function createFileSystemError(message, code, context) {
|
|
531
|
+
const error = createError(message);
|
|
532
|
+
defineProperties(error, {
|
|
533
|
+
code: { value: code, enumerable: true },
|
|
534
|
+
context: { value: context, enumerable: true },
|
|
535
|
+
});
|
|
536
|
+
return error;
|
|
537
|
+
}
|
|
538
|
+
/**
|
|
539
|
+
* Read file if exists, return null otherwise.
|
|
540
|
+
*
|
|
541
|
+
* @param filePath - Path to file
|
|
542
|
+
* @param encoding - File encoding (default: utf-8)
|
|
543
|
+
* @returns File contents or null if file doesn't exist
|
|
544
|
+
*/
|
|
545
|
+
function readFileIfExists(filePath, encoding = 'utf-8') {
|
|
546
|
+
if (!node_fs.existsSync(filePath)) {
|
|
547
|
+
return null;
|
|
548
|
+
}
|
|
549
|
+
try {
|
|
550
|
+
return node_fs.readFileSync(filePath, { encoding });
|
|
551
|
+
}
|
|
552
|
+
catch {
|
|
553
|
+
return null;
|
|
554
|
+
}
|
|
555
|
+
}
|
|
556
|
+
|
|
557
|
+
createScopedLogger('project-scope:fs:write');
|
|
558
|
+
|
|
559
|
+
/**
|
|
560
|
+
* Get file stats with error handling.
|
|
561
|
+
*
|
|
562
|
+
* @param filePath - Path to file
|
|
563
|
+
* @param followSymlinks - Whether to follow symlinks (default: true)
|
|
564
|
+
* @returns File stats or null if path doesn't exist
|
|
565
|
+
*/
|
|
566
|
+
function getFileStat(filePath, followSymlinks = true) {
|
|
567
|
+
if (!node_fs.existsSync(filePath)) {
|
|
568
|
+
return null;
|
|
569
|
+
}
|
|
570
|
+
try {
|
|
571
|
+
const stat = followSymlinks ? node_fs.statSync(filePath) : node_fs.lstatSync(filePath);
|
|
572
|
+
return {
|
|
573
|
+
isFile: stat.isFile(),
|
|
574
|
+
isDirectory: stat.isDirectory(),
|
|
575
|
+
isSymlink: stat.isSymbolicLink(),
|
|
576
|
+
size: stat.size,
|
|
577
|
+
created: stat.birthtime,
|
|
578
|
+
modified: stat.mtime,
|
|
579
|
+
accessed: stat.atime,
|
|
580
|
+
mode: stat.mode,
|
|
581
|
+
};
|
|
582
|
+
}
|
|
583
|
+
catch {
|
|
584
|
+
return null;
|
|
585
|
+
}
|
|
586
|
+
}
|
|
587
|
+
/**
|
|
588
|
+
* Check if path is a directory.
|
|
589
|
+
*
|
|
590
|
+
* @param dirPath - Path to check
|
|
591
|
+
* @returns True if path is a directory
|
|
592
|
+
*/
|
|
593
|
+
function isDirectory(dirPath) {
|
|
594
|
+
const stats = getFileStat(dirPath);
|
|
595
|
+
return stats?.isDirectory ?? false;
|
|
596
|
+
}
|
|
597
|
+
|
|
598
|
+
const fsDirLogger = createScopedLogger('project-scope:fs:dir');
|
|
599
|
+
/**
|
|
600
|
+
* List immediate contents of a directory.
|
|
601
|
+
*
|
|
602
|
+
* @param dirPath - Absolute or relative path to the directory
|
|
603
|
+
* @returns Array of entries with metadata for each file/directory
|
|
604
|
+
* @throws {Error} If directory doesn't exist or isn't a directory
|
|
605
|
+
*
|
|
606
|
+
* @example
|
|
607
|
+
* ```typescript
|
|
608
|
+
* import { readDirectory } from '@hyperfrontend/project-scope'
|
|
609
|
+
*
|
|
610
|
+
* const entries = readDirectory('./src')
|
|
611
|
+
* for (const entry of entries) {
|
|
612
|
+
* console.log(entry.name, entry.isFile ? 'file' : 'directory')
|
|
613
|
+
* }
|
|
614
|
+
* ```
|
|
615
|
+
*/
|
|
616
|
+
function readDirectory(dirPath) {
|
|
617
|
+
fsDirLogger.debug('Reading directory', { path: dirPath });
|
|
618
|
+
if (!node_fs.existsSync(dirPath)) {
|
|
619
|
+
fsDirLogger.debug('Directory not found', { path: dirPath });
|
|
620
|
+
throw createFileSystemError(`Directory not found: ${dirPath}`, 'FS_NOT_FOUND', { path: dirPath, operation: 'readdir' });
|
|
621
|
+
}
|
|
622
|
+
if (!isDirectory(dirPath)) {
|
|
623
|
+
fsDirLogger.debug('Path is not a directory', { path: dirPath });
|
|
624
|
+
throw createFileSystemError(`Not a directory: ${dirPath}`, 'FS_NOT_A_DIRECTORY', { path: dirPath, operation: 'readdir' });
|
|
625
|
+
}
|
|
626
|
+
try {
|
|
627
|
+
const entries = node_fs.readdirSync(dirPath, { withFileTypes: true });
|
|
628
|
+
fsDirLogger.debug('Directory read complete', { path: dirPath, entryCount: entries.length });
|
|
629
|
+
return entries.map((entry) => ({
|
|
630
|
+
name: entry.name,
|
|
631
|
+
path: node_path.join(dirPath, entry.name),
|
|
632
|
+
isFile: entry.isFile(),
|
|
633
|
+
isDirectory: entry.isDirectory(),
|
|
634
|
+
isSymlink: entry.isSymbolicLink(),
|
|
635
|
+
}));
|
|
636
|
+
}
|
|
637
|
+
catch (error) {
|
|
638
|
+
fsDirLogger.warn('Failed to read directory', { path: dirPath, error: error instanceof Error ? error.message : String(error) });
|
|
639
|
+
throw createFileSystemError(`Failed to read directory: ${dirPath}`, 'FS_READ_ERROR', {
|
|
640
|
+
path: dirPath,
|
|
641
|
+
operation: 'readdir',
|
|
642
|
+
cause: error,
|
|
643
|
+
});
|
|
644
|
+
}
|
|
645
|
+
}
|
|
646
|
+
|
|
647
|
+
createScopedLogger('project-scope:fs:traversal');
|
|
648
|
+
|
|
649
|
+
/**
|
|
650
|
+
* Pattern matching utilities with ReDoS protection.
|
|
651
|
+
* Uses character-by-character matching instead of regex where possible.
|
|
652
|
+
*/
|
|
653
|
+
/**
|
|
654
|
+
* Match path against glob pattern using safe character iteration.
|
|
655
|
+
* Avoids regex to prevent ReDoS attacks.
|
|
656
|
+
*
|
|
657
|
+
* Supported patterns:
|
|
658
|
+
* - * matches any characters except /
|
|
659
|
+
* - ** matches any characters including /
|
|
660
|
+
* - ? matches exactly one character except /
|
|
661
|
+
* - {a,b,c} matches any of the alternatives
|
|
662
|
+
*
|
|
663
|
+
* @param path - The filesystem path to test against the pattern
|
|
664
|
+
* @param pattern - The glob pattern to match against
|
|
665
|
+
* @returns True if path matches pattern
|
|
666
|
+
*
|
|
667
|
+
* @example
|
|
668
|
+
* ```typescript
|
|
669
|
+
* import { matchGlobPattern } from '@hyperfrontend/project-scope'
|
|
670
|
+
*
|
|
671
|
+
* matchGlobPattern('src/utils/helper.ts', '\*\*\/*.ts') // true
|
|
672
|
+
* matchGlobPattern('test.spec.ts', '\*.spec.ts') // true
|
|
673
|
+
* matchGlobPattern('config.json', '\*.{json,yaml}') // true
|
|
674
|
+
* matchGlobPattern('src/index.ts', 'src/\*.ts') // true
|
|
675
|
+
* ```
|
|
676
|
+
*/
|
|
677
|
+
function matchGlobPattern(path, pattern) {
|
|
678
|
+
return matchSegments(path.split('/'), pattern.split('/'), 0, 0);
|
|
679
|
+
}
|
|
680
|
+
/**
|
|
681
|
+
* Internal recursive function to match path segments against pattern segments.
|
|
682
|
+
*
|
|
683
|
+
* @param pathParts - Array of path segments split by '/'
|
|
684
|
+
* @param patternParts - Array of pattern segments split by '/'
|
|
685
|
+
* @param pathIdx - Current index in pathParts being examined
|
|
686
|
+
* @param patternIdx - Current index in patternParts being examined
|
|
687
|
+
* @returns True if remaining segments match
|
|
688
|
+
*/
|
|
689
|
+
function matchSegments(pathParts, patternParts, pathIdx, patternIdx) {
|
|
690
|
+
// Base cases
|
|
691
|
+
if (pathIdx === pathParts.length && patternIdx === patternParts.length) {
|
|
692
|
+
return true; // Both exhausted = match
|
|
693
|
+
}
|
|
694
|
+
if (patternIdx >= patternParts.length) {
|
|
695
|
+
return false; // Pattern exhausted but path remains
|
|
696
|
+
}
|
|
697
|
+
const patternPart = patternParts[patternIdx];
|
|
698
|
+
// Handle ** (globstar) - matches zero or more directories
|
|
699
|
+
if (patternPart === '**') {
|
|
700
|
+
// Try matching rest of pattern against current position and all future positions
|
|
701
|
+
for (let i = pathIdx; i <= pathParts.length; i++) {
|
|
702
|
+
if (matchSegments(pathParts, patternParts, i, patternIdx + 1)) {
|
|
703
|
+
return true;
|
|
704
|
+
}
|
|
705
|
+
}
|
|
706
|
+
return false;
|
|
707
|
+
}
|
|
708
|
+
if (pathIdx >= pathParts.length) {
|
|
709
|
+
return false; // Path exhausted but pattern remains (and it's not **)
|
|
710
|
+
}
|
|
711
|
+
const pathPart = pathParts[pathIdx];
|
|
712
|
+
// Match current segment
|
|
713
|
+
if (matchSegment(pathPart, patternPart)) {
|
|
714
|
+
return matchSegments(pathParts, patternParts, pathIdx + 1, patternIdx + 1);
|
|
715
|
+
}
|
|
716
|
+
return false;
|
|
717
|
+
}
|
|
718
|
+
/**
|
|
719
|
+
* Match a single path segment against a pattern segment.
|
|
720
|
+
* Handles *, ?, and {a,b,c} patterns.
|
|
721
|
+
*
|
|
722
|
+
* @param text - The path segment text to match
|
|
723
|
+
* @param pattern - The pattern segment to match against
|
|
724
|
+
* @returns True if the text matches the pattern
|
|
725
|
+
*/
|
|
726
|
+
function matchSegment(text, pattern) {
|
|
727
|
+
let textIdx = 0;
|
|
728
|
+
let patternIdx = 0;
|
|
729
|
+
while (patternIdx < pattern.length) {
|
|
730
|
+
const char = pattern[patternIdx];
|
|
731
|
+
if (char === '*') {
|
|
732
|
+
// * matches zero or more characters
|
|
733
|
+
patternIdx++;
|
|
734
|
+
if (patternIdx === pattern.length) {
|
|
735
|
+
return true; // * at end matches rest of string
|
|
736
|
+
}
|
|
737
|
+
// Try matching rest of pattern at each position in text
|
|
738
|
+
for (let i = textIdx; i <= text.length; i++) {
|
|
739
|
+
if (matchSegmentFrom(text, i, pattern, patternIdx)) {
|
|
740
|
+
return true;
|
|
741
|
+
}
|
|
742
|
+
}
|
|
743
|
+
return false;
|
|
744
|
+
}
|
|
745
|
+
else if (char === '?') {
|
|
746
|
+
// ? matches exactly one character
|
|
747
|
+
if (textIdx >= text.length) {
|
|
748
|
+
return false;
|
|
749
|
+
}
|
|
750
|
+
textIdx++;
|
|
751
|
+
patternIdx++;
|
|
752
|
+
}
|
|
753
|
+
else if (char === '{') {
|
|
754
|
+
// {a,b,c} matches any alternative
|
|
755
|
+
const closeIdx = findClosingBrace(pattern, patternIdx);
|
|
756
|
+
if (closeIdx === -1) {
|
|
757
|
+
// Unmatched brace, treat as literal
|
|
758
|
+
if (textIdx >= text.length || text[textIdx] !== char) {
|
|
759
|
+
return false;
|
|
760
|
+
}
|
|
761
|
+
textIdx++;
|
|
762
|
+
patternIdx++;
|
|
763
|
+
}
|
|
764
|
+
else {
|
|
765
|
+
const alternatives = extractAlternatives(pattern.slice(patternIdx + 1, closeIdx));
|
|
766
|
+
for (const alt of alternatives) {
|
|
767
|
+
if (matchSegmentFrom(text, textIdx, text.slice(0, textIdx) + alt + pattern.slice(closeIdx + 1), textIdx)) {
|
|
768
|
+
return true;
|
|
769
|
+
}
|
|
770
|
+
}
|
|
771
|
+
return false;
|
|
772
|
+
}
|
|
773
|
+
}
|
|
774
|
+
else {
|
|
775
|
+
// Literal character
|
|
776
|
+
if (textIdx >= text.length || text[textIdx] !== char) {
|
|
777
|
+
return false;
|
|
778
|
+
}
|
|
779
|
+
textIdx++;
|
|
780
|
+
patternIdx++;
|
|
781
|
+
}
|
|
782
|
+
}
|
|
783
|
+
return textIdx === text.length;
|
|
784
|
+
}
|
|
785
|
+
/**
|
|
786
|
+
* Helper to match from a specific position.
|
|
787
|
+
*
|
|
788
|
+
* @param text - The full text being matched
|
|
789
|
+
* @param textIdx - The starting index in text to match from
|
|
790
|
+
* @param pattern - The full pattern being matched
|
|
791
|
+
* @param patternIdx - The starting index in pattern to match from
|
|
792
|
+
* @returns True if the text matches the pattern from the given positions
|
|
793
|
+
*/
|
|
794
|
+
function matchSegmentFrom(text, textIdx, pattern, patternIdx) {
|
|
795
|
+
const remainingText = text.slice(textIdx);
|
|
796
|
+
const remainingPattern = pattern.slice(patternIdx);
|
|
797
|
+
return matchSegment(remainingText, remainingPattern);
|
|
798
|
+
}
|
|
799
|
+
/**
|
|
800
|
+
* Find closing brace for {a,b,c} pattern.
|
|
801
|
+
*
|
|
802
|
+
* @param pattern - The pattern string to search within
|
|
803
|
+
* @param startIdx - The index of the opening brace
|
|
804
|
+
* @returns The index of the matching closing brace, or -1 if not found
|
|
805
|
+
*/
|
|
806
|
+
function findClosingBrace(pattern, startIdx) {
|
|
807
|
+
let depth = 0;
|
|
808
|
+
for (let i = startIdx; i < pattern.length; i++) {
|
|
809
|
+
if (pattern[i] === '{') {
|
|
810
|
+
depth++;
|
|
811
|
+
}
|
|
812
|
+
else if (pattern[i] === '}') {
|
|
813
|
+
depth--;
|
|
814
|
+
if (depth === 0) {
|
|
815
|
+
return i;
|
|
816
|
+
}
|
|
817
|
+
}
|
|
818
|
+
}
|
|
819
|
+
return -1;
|
|
820
|
+
}
|
|
821
|
+
/**
|
|
822
|
+
* Extract alternatives from {a,b,c} pattern content.
|
|
823
|
+
*
|
|
824
|
+
* @param content - The content between braces (without the braces themselves)
|
|
825
|
+
* @returns Array of alternative strings split by commas at depth 0
|
|
826
|
+
*/
|
|
827
|
+
function extractAlternatives(content) {
|
|
828
|
+
const alternatives = [];
|
|
829
|
+
let current = '';
|
|
830
|
+
let depth = 0;
|
|
831
|
+
for (let i = 0; i < content.length; i++) {
|
|
832
|
+
const char = content[i];
|
|
833
|
+
if (char === '{') {
|
|
834
|
+
depth++;
|
|
835
|
+
current += char;
|
|
836
|
+
}
|
|
837
|
+
else if (char === '}') {
|
|
838
|
+
depth--;
|
|
839
|
+
current += char;
|
|
840
|
+
}
|
|
841
|
+
else if (char === ',' && depth === 0) {
|
|
842
|
+
alternatives.push(current);
|
|
843
|
+
current = '';
|
|
844
|
+
}
|
|
845
|
+
else {
|
|
846
|
+
current += char;
|
|
847
|
+
}
|
|
848
|
+
}
|
|
849
|
+
if (current) {
|
|
850
|
+
alternatives.push(current);
|
|
851
|
+
}
|
|
852
|
+
return alternatives;
|
|
853
|
+
}
|
|
854
|
+
|
|
855
|
+
const walkLogger = createScopedLogger('project-scope:project:walk');
|
|
856
|
+
/**
|
|
857
|
+
* Reads .gitignore file from the given directory and extracts
|
|
858
|
+
* non-comment patterns for use in file traversal filtering.
|
|
859
|
+
*
|
|
860
|
+
* @param startPath - Directory containing the .gitignore file
|
|
861
|
+
* @returns Array of gitignore patterns
|
|
862
|
+
*/
|
|
863
|
+
function loadGitignorePatterns(startPath) {
|
|
864
|
+
const patterns = [];
|
|
865
|
+
const gitignorePath = node_path.join(startPath, '.gitignore');
|
|
866
|
+
const content = readFileIfExists(gitignorePath);
|
|
867
|
+
if (content) {
|
|
868
|
+
const lines = content.split('\n');
|
|
869
|
+
for (const line of lines) {
|
|
870
|
+
const trimmed = line.trim();
|
|
871
|
+
if (trimmed && !trimmed.startsWith('#')) {
|
|
872
|
+
patterns.push(trimmed);
|
|
873
|
+
}
|
|
874
|
+
}
|
|
875
|
+
}
|
|
876
|
+
return patterns;
|
|
877
|
+
}
|
|
878
|
+
/**
|
|
879
|
+
* Evaluates whether a relative path should be ignored based on
|
|
880
|
+
* a list of gitignore-style patterns.
|
|
881
|
+
*
|
|
882
|
+
* @param relativePath - Path relative to the root directory
|
|
883
|
+
* @param patterns - Array of gitignore-style patterns to test
|
|
884
|
+
* @returns True if the path matches any ignore pattern
|
|
885
|
+
*/
|
|
886
|
+
function matchesIgnorePattern(relativePath, patterns) {
|
|
887
|
+
for (const pattern of patterns) {
|
|
888
|
+
if (matchPattern(relativePath, pattern)) {
|
|
889
|
+
return true;
|
|
890
|
+
}
|
|
891
|
+
}
|
|
892
|
+
return false;
|
|
893
|
+
}
|
|
894
|
+
/**
|
|
895
|
+
* Tests if the given path matches a gitignore-style pattern,
|
|
896
|
+
* supporting negation patterns with '!' prefix.
|
|
897
|
+
* Uses safe character-by-character matching to prevent ReDoS attacks.
|
|
898
|
+
*
|
|
899
|
+
* @param path - File or directory path to test
|
|
900
|
+
* @param pattern - Gitignore-style pattern (may include wildcards)
|
|
901
|
+
* @returns True if the path matches the pattern (or doesn't match if negated)
|
|
902
|
+
*/
|
|
903
|
+
function matchPattern(path, pattern) {
|
|
904
|
+
const normalizedPattern = pattern.startsWith('/') ? pattern.slice(1) : pattern;
|
|
905
|
+
const isNegation = normalizedPattern.startsWith('!');
|
|
906
|
+
const actualPattern = isNegation ? normalizedPattern.slice(1) : normalizedPattern;
|
|
907
|
+
const matchesFullPath = matchGlobPattern(path, actualPattern) || matchGlobPattern(path, `**/${actualPattern}`);
|
|
908
|
+
const matchesSegment = path.split('/').some((segment) => matchGlobPattern(segment, actualPattern));
|
|
909
|
+
const matches = matchesFullPath || matchesSegment;
|
|
910
|
+
return isNegation ? !matches : matches;
|
|
911
|
+
}
|
|
912
|
+
/**
|
|
913
|
+
* Traverses a directory tree synchronously, calling a visitor function
|
|
914
|
+
* for each file and directory encountered. Supports depth limiting,
|
|
915
|
+
* hidden file filtering, and gitignore pattern matching.
|
|
916
|
+
*
|
|
917
|
+
* @param startPath - Root directory to begin traversal
|
|
918
|
+
* @param visitor - Callback function invoked for each file system entry
|
|
919
|
+
* @param options - Configuration for traversal behavior
|
|
920
|
+
*/
|
|
921
|
+
function walkDirectory(startPath, visitor, options) {
|
|
922
|
+
walkLogger.debug('Starting directory walk', {
|
|
923
|
+
startPath,
|
|
924
|
+
maxDepth: options?.maxDepth ?? -1,
|
|
925
|
+
includeHidden: options?.includeHidden ?? false,
|
|
926
|
+
respectGitignore: options?.respectGitignore ?? true,
|
|
927
|
+
ignorePatterns: options?.ignorePatterns?.length ?? 0,
|
|
928
|
+
});
|
|
929
|
+
const maxDepth = options?.maxDepth ?? -1;
|
|
930
|
+
const includeHidden = options?.includeHidden ?? false;
|
|
931
|
+
const ignorePatterns = options?.ignorePatterns ?? [];
|
|
932
|
+
const respectGitignore = options?.respectGitignore ?? true;
|
|
933
|
+
const gitignorePatterns = respectGitignore ? loadGitignorePatterns(startPath) : [];
|
|
934
|
+
const allIgnorePatterns = [...ignorePatterns, ...gitignorePatterns];
|
|
935
|
+
if (gitignorePatterns.length > 0) {
|
|
936
|
+
walkLogger.debug('Loaded gitignore patterns', { count: gitignorePatterns.length });
|
|
937
|
+
}
|
|
938
|
+
/**
|
|
939
|
+
* Recursively walks directory entries, applying visitor to each.
|
|
940
|
+
*
|
|
941
|
+
* @param currentPath - Absolute path to current directory
|
|
942
|
+
* @param relativePath - Path relative to the starting directory
|
|
943
|
+
* @param depth - Current recursion depth
|
|
944
|
+
* @returns False to stop walking, true to continue
|
|
945
|
+
*/
|
|
946
|
+
function walk(currentPath, relativePath, depth) {
|
|
947
|
+
if (maxDepth !== -1 && depth > maxDepth) {
|
|
948
|
+
return true;
|
|
949
|
+
}
|
|
950
|
+
let entries;
|
|
951
|
+
try {
|
|
952
|
+
entries = readDirectory(currentPath);
|
|
953
|
+
}
|
|
954
|
+
catch {
|
|
955
|
+
return true;
|
|
956
|
+
}
|
|
957
|
+
for (const entry of entries) {
|
|
958
|
+
if (!includeHidden && entry.name.startsWith('.')) {
|
|
959
|
+
continue;
|
|
960
|
+
}
|
|
961
|
+
const entryRelativePath = relativePath ? `${relativePath}/${entry.name}` : entry.name;
|
|
962
|
+
if (matchesIgnorePattern(entryRelativePath, allIgnorePatterns)) {
|
|
963
|
+
continue;
|
|
964
|
+
}
|
|
965
|
+
const walkEntry = {
|
|
966
|
+
name: entry.name,
|
|
967
|
+
path: entry.path,
|
|
968
|
+
relativePath: entryRelativePath,
|
|
969
|
+
isFile: entry.isFile,
|
|
970
|
+
isDirectory: entry.isDirectory,
|
|
971
|
+
isSymlink: entry.isSymlink,
|
|
972
|
+
depth,
|
|
973
|
+
};
|
|
974
|
+
const result = visitor(walkEntry);
|
|
975
|
+
if (result === 'stop') {
|
|
976
|
+
return false;
|
|
977
|
+
}
|
|
978
|
+
if (result === 'skip') {
|
|
979
|
+
continue;
|
|
980
|
+
}
|
|
981
|
+
if (entry.isDirectory) {
|
|
982
|
+
const shouldContinue = walk(entry.path, entryRelativePath, depth + 1);
|
|
983
|
+
if (!shouldContinue) {
|
|
984
|
+
return false;
|
|
985
|
+
}
|
|
986
|
+
}
|
|
987
|
+
}
|
|
988
|
+
return true;
|
|
989
|
+
}
|
|
990
|
+
walk(startPath, '', 0);
|
|
991
|
+
walkLogger.debug('Directory walk complete', { startPath });
|
|
992
|
+
}
|
|
993
|
+
/**
|
|
994
|
+
* Traverses a virtual file system tree, calling a visitor function
|
|
995
|
+
* for each file and directory. Operates on in-memory tree structure
|
|
996
|
+
* without disk I/O.
|
|
997
|
+
*
|
|
998
|
+
* @param tree - In-memory virtual file system representation
|
|
999
|
+
* @param startPath - Root path within the tree to begin traversal
|
|
1000
|
+
* @param visitor - Callback function invoked for each tree entry
|
|
1001
|
+
* @param options - Configuration for traversal behavior
|
|
1002
|
+
*/
|
|
1003
|
+
function walkTree(tree, startPath, visitor, options) {
|
|
1004
|
+
const maxDepth = options?.maxDepth ?? -1;
|
|
1005
|
+
const includeHidden = options?.includeHidden ?? false;
|
|
1006
|
+
/**
|
|
1007
|
+
* Recursively walks tree entries, applying visitor to each.
|
|
1008
|
+
*
|
|
1009
|
+
* @param currentPath - Current path within the tree
|
|
1010
|
+
* @param relativePath - Path relative to the starting path
|
|
1011
|
+
* @param depth - Current recursion depth
|
|
1012
|
+
* @returns False to stop walking, true to continue
|
|
1013
|
+
*/
|
|
1014
|
+
function walk(currentPath, relativePath, depth) {
|
|
1015
|
+
if (maxDepth !== -1 && depth > maxDepth) {
|
|
1016
|
+
return true;
|
|
1017
|
+
}
|
|
1018
|
+
let children;
|
|
1019
|
+
try {
|
|
1020
|
+
children = tree.children(currentPath);
|
|
1021
|
+
}
|
|
1022
|
+
catch {
|
|
1023
|
+
return true;
|
|
1024
|
+
}
|
|
1025
|
+
for (const name of children) {
|
|
1026
|
+
if (!includeHidden && name.startsWith('.')) {
|
|
1027
|
+
continue;
|
|
1028
|
+
}
|
|
1029
|
+
const childPath = currentPath ? `${currentPath}/${name}` : name;
|
|
1030
|
+
const entryRelativePath = relativePath ? `${relativePath}/${name}` : name;
|
|
1031
|
+
const isFileEntry = tree.isFile(childPath);
|
|
1032
|
+
const walkEntry = {
|
|
1033
|
+
name,
|
|
1034
|
+
path: childPath,
|
|
1035
|
+
relativePath: entryRelativePath,
|
|
1036
|
+
isFile: isFileEntry,
|
|
1037
|
+
isDirectory: !isFileEntry,
|
|
1038
|
+
isSymlink: false,
|
|
1039
|
+
depth,
|
|
1040
|
+
};
|
|
1041
|
+
const result = visitor(walkEntry);
|
|
1042
|
+
if (result === 'stop') {
|
|
1043
|
+
return false;
|
|
1044
|
+
}
|
|
1045
|
+
if (result === 'skip') {
|
|
1046
|
+
continue;
|
|
1047
|
+
}
|
|
1048
|
+
if (!isFileEntry) {
|
|
1049
|
+
const shouldContinue = walk(childPath, entryRelativePath, depth + 1);
|
|
1050
|
+
if (!shouldContinue) {
|
|
1051
|
+
return false;
|
|
1052
|
+
}
|
|
1053
|
+
}
|
|
1054
|
+
}
|
|
1055
|
+
return true;
|
|
1056
|
+
}
|
|
1057
|
+
walk(startPath, '', 0);
|
|
1058
|
+
}
|
|
1059
|
+
|
|
1060
|
+
const searchLogger = createScopedLogger('project-scope:project:search');
|
|
1061
|
+
/**
|
|
1062
|
+
* Tests if a path matches at least one pattern from an array of globs,
|
|
1063
|
+
* enabling flexible multi-pattern file filtering.
|
|
1064
|
+
* Uses safe character-by-character matching to prevent ReDoS attacks.
|
|
1065
|
+
*
|
|
1066
|
+
* @param path - File path to test
|
|
1067
|
+
* @param patterns - Array of glob patterns
|
|
1068
|
+
* @returns True if path matches any pattern
|
|
1069
|
+
*/
|
|
1070
|
+
function matchesPatterns(path, patterns) {
|
|
1071
|
+
return patterns.some((pattern) => matchGlobPattern(path, pattern));
|
|
1072
|
+
}
|
|
1073
|
+
/**
|
|
1074
|
+
* Searches a directory tree for files matching one or more glob patterns,
|
|
1075
|
+
* returning relative or absolute paths based on options.
|
|
1076
|
+
*
|
|
1077
|
+
* @param startPath - Root directory to begin the search
|
|
1078
|
+
* @param patterns - Glob patterns (e.g., '*.ts', '**\/*.json') to filter files
|
|
1079
|
+
* @param options - Configuration for search behavior
|
|
1080
|
+
* @returns List of relative file paths that match the patterns
|
|
1081
|
+
*
|
|
1082
|
+
* @example
|
|
1083
|
+
* ```typescript
|
|
1084
|
+
* import { findFiles } from '@hyperfrontend/project-scope'
|
|
1085
|
+
*
|
|
1086
|
+
* // Find all TypeScript files
|
|
1087
|
+
* const tsFiles = findFiles('./src', '\*\*\/*.ts')
|
|
1088
|
+
*
|
|
1089
|
+
* // Find multiple file types
|
|
1090
|
+
* const configFiles = findFiles('./', ['\*.json', '\*.yaml', '\*.yml'])
|
|
1091
|
+
*
|
|
1092
|
+
* // Limit results and get absolute paths
|
|
1093
|
+
* const first10 = findFiles('./src', '\*\*\/*.ts', {
|
|
1094
|
+
* maxResults: 10,
|
|
1095
|
+
* absolutePaths: true
|
|
1096
|
+
* })
|
|
1097
|
+
* ```
|
|
1098
|
+
*/
|
|
1099
|
+
function findFiles(startPath, patterns, options) {
|
|
1100
|
+
const normalizedPatterns = isArray(patterns) ? patterns : [patterns];
|
|
1101
|
+
searchLogger.debug('Finding files', { startPath, patterns: normalizedPatterns, maxResults: options?.maxResults });
|
|
1102
|
+
const results = [];
|
|
1103
|
+
const maxResults = options?.maxResults ?? Infinity;
|
|
1104
|
+
walkDirectory(startPath, (entry) => {
|
|
1105
|
+
if (results.length >= maxResults) {
|
|
1106
|
+
return 'stop';
|
|
1107
|
+
}
|
|
1108
|
+
if (!entry.isFile) {
|
|
1109
|
+
return undefined;
|
|
1110
|
+
}
|
|
1111
|
+
if (matchesPatterns(entry.relativePath, normalizedPatterns)) {
|
|
1112
|
+
results.push(options?.absolutePaths ? entry.path : entry.relativePath);
|
|
1113
|
+
}
|
|
1114
|
+
return undefined;
|
|
1115
|
+
}, options);
|
|
1116
|
+
searchLogger.debug('File search complete', { startPath, matchCount: results.length });
|
|
1117
|
+
return results;
|
|
1118
|
+
}
|
|
1119
|
+
/**
|
|
1120
|
+
* Searches a virtual file system tree for files matching glob patterns,
|
|
1121
|
+
* useful for analyzing project structure without disk I/O.
|
|
1122
|
+
*
|
|
1123
|
+
* @param tree - In-memory virtual file system representation
|
|
1124
|
+
* @param patterns - Glob patterns (e.g., '*.ts', '**\/*.json') to filter files
|
|
1125
|
+
* @param options - Configuration for search behavior
|
|
1126
|
+
* @returns List of virtual file paths that match the patterns
|
|
1127
|
+
*/
|
|
1128
|
+
function findFilesInTree(tree, patterns, options) {
|
|
1129
|
+
const normalizedPatterns = isArray(patterns) ? patterns : [patterns];
|
|
1130
|
+
const results = [];
|
|
1131
|
+
const maxResults = options?.maxResults ?? Infinity;
|
|
1132
|
+
walkTree(tree, tree.root || '', (entry) => {
|
|
1133
|
+
if (results.length >= maxResults) {
|
|
1134
|
+
return 'stop';
|
|
1135
|
+
}
|
|
1136
|
+
if (!entry.isFile) {
|
|
1137
|
+
return undefined;
|
|
1138
|
+
}
|
|
1139
|
+
if (matchesPatterns(entry.relativePath, normalizedPatterns)) {
|
|
1140
|
+
results.push(entry.relativePath);
|
|
1141
|
+
}
|
|
1142
|
+
return undefined;
|
|
1143
|
+
}, options);
|
|
1144
|
+
return results;
|
|
1145
|
+
}
|
|
1146
|
+
/**
|
|
1147
|
+
* Searches a directory tree for directories matching one or more glob patterns,
|
|
1148
|
+
* returning relative or absolute paths based on options.
|
|
1149
|
+
*
|
|
1150
|
+
* @param startPath - Root directory to begin the search
|
|
1151
|
+
* @param patterns - Glob patterns to filter directories (supports wildcards)
|
|
1152
|
+
* @param options - Configuration for search behavior
|
|
1153
|
+
* @returns List of relative directory paths that match the patterns
|
|
1154
|
+
*/
|
|
1155
|
+
function findDirectories(startPath, patterns, options) {
|
|
1156
|
+
const normalizedPatterns = isArray(patterns) ? patterns : [patterns];
|
|
1157
|
+
const results = [];
|
|
1158
|
+
const maxResults = options?.maxResults ?? Infinity;
|
|
1159
|
+
walkDirectory(startPath, (entry) => {
|
|
1160
|
+
if (results.length >= maxResults) {
|
|
1161
|
+
return 'stop';
|
|
1162
|
+
}
|
|
1163
|
+
if (!entry.isDirectory) {
|
|
1164
|
+
return undefined;
|
|
1165
|
+
}
|
|
1166
|
+
if (matchesPatterns(entry.relativePath, normalizedPatterns)) {
|
|
1167
|
+
results.push(options?.absolutePaths ? entry.path : entry.relativePath);
|
|
1168
|
+
}
|
|
1169
|
+
return undefined;
|
|
1170
|
+
}, options);
|
|
1171
|
+
return results;
|
|
1172
|
+
}
|
|
1173
|
+
|
|
1174
|
+
exports.findDirectories = findDirectories;
|
|
1175
|
+
exports.findFiles = findFiles;
|
|
1176
|
+
exports.findFilesInTree = findFilesInTree;
|
|
1177
|
+
exports.walkDirectory = walkDirectory;
|
|
1178
|
+
exports.walkTree = walkTree;
|
|
1179
|
+
//# sourceMappingURL=index.cjs.js.map
|