@hyperfrontend/versioning 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 +593 -0
- package/CHANGELOG.md +35 -0
- package/FUNDING.md +141 -0
- package/LICENSE.md +21 -0
- package/README.md +195 -0
- package/SECURITY.md +82 -0
- package/changelog/compare/diff.d.ts +128 -0
- package/changelog/compare/diff.d.ts.map +1 -0
- package/changelog/compare/index.cjs.js +628 -0
- package/changelog/compare/index.cjs.js.map +1 -0
- package/changelog/compare/index.d.ts +4 -0
- package/changelog/compare/index.d.ts.map +1 -0
- package/changelog/compare/index.esm.js +612 -0
- package/changelog/compare/index.esm.js.map +1 -0
- package/changelog/compare/is-equal.d.ts +114 -0
- package/changelog/compare/is-equal.d.ts.map +1 -0
- package/changelog/index.cjs.js +6448 -0
- package/changelog/index.cjs.js.map +1 -0
- package/changelog/index.d.ts +6 -0
- package/changelog/index.d.ts.map +1 -0
- package/changelog/index.esm.js +6358 -0
- package/changelog/index.esm.js.map +1 -0
- package/changelog/models/changelog.d.ts +86 -0
- package/changelog/models/changelog.d.ts.map +1 -0
- package/changelog/models/commit-ref.d.ts +51 -0
- package/changelog/models/commit-ref.d.ts.map +1 -0
- package/changelog/models/entry.d.ts +84 -0
- package/changelog/models/entry.d.ts.map +1 -0
- package/changelog/models/index.cjs.js +2043 -0
- package/changelog/models/index.cjs.js.map +1 -0
- package/changelog/models/index.d.ts +11 -0
- package/changelog/models/index.d.ts.map +1 -0
- package/changelog/models/index.esm.js +2026 -0
- package/changelog/models/index.esm.js.map +1 -0
- package/changelog/models/schema.d.ts +68 -0
- package/changelog/models/schema.d.ts.map +1 -0
- package/changelog/models/section.d.ts +25 -0
- package/changelog/models/section.d.ts.map +1 -0
- package/changelog/operations/add-entry.d.ts +56 -0
- package/changelog/operations/add-entry.d.ts.map +1 -0
- package/changelog/operations/add-item.d.ts +18 -0
- package/changelog/operations/add-item.d.ts.map +1 -0
- package/changelog/operations/filter-by-predicate.d.ts +81 -0
- package/changelog/operations/filter-by-predicate.d.ts.map +1 -0
- package/changelog/operations/filter-by-range.d.ts +63 -0
- package/changelog/operations/filter-by-range.d.ts.map +1 -0
- package/changelog/operations/filter-entries.d.ts +9 -0
- package/changelog/operations/filter-entries.d.ts.map +1 -0
- package/changelog/operations/index.cjs.js +2455 -0
- package/changelog/operations/index.cjs.js.map +1 -0
- package/changelog/operations/index.d.ts +15 -0
- package/changelog/operations/index.d.ts.map +1 -0
- package/changelog/operations/index.esm.js +2411 -0
- package/changelog/operations/index.esm.js.map +1 -0
- package/changelog/operations/merge.d.ts +88 -0
- package/changelog/operations/merge.d.ts.map +1 -0
- package/changelog/operations/remove-entry.d.ts +45 -0
- package/changelog/operations/remove-entry.d.ts.map +1 -0
- package/changelog/operations/remove-section.d.ts +50 -0
- package/changelog/operations/remove-section.d.ts.map +1 -0
- package/changelog/operations/transform.d.ts +143 -0
- package/changelog/operations/transform.d.ts.map +1 -0
- package/changelog/parse/index.cjs.js +1282 -0
- package/changelog/parse/index.cjs.js.map +1 -0
- package/changelog/parse/index.d.ts +5 -0
- package/changelog/parse/index.d.ts.map +1 -0
- package/changelog/parse/index.esm.js +1275 -0
- package/changelog/parse/index.esm.js.map +1 -0
- package/changelog/parse/line.d.ts +48 -0
- package/changelog/parse/line.d.ts.map +1 -0
- package/changelog/parse/parser.d.ts +16 -0
- package/changelog/parse/parser.d.ts.map +1 -0
- package/changelog/parse/tokenizer.d.ts +49 -0
- package/changelog/parse/tokenizer.d.ts.map +1 -0
- package/changelog/serialize/index.cjs.js +574 -0
- package/changelog/serialize/index.cjs.js.map +1 -0
- package/changelog/serialize/index.d.ts +6 -0
- package/changelog/serialize/index.d.ts.map +1 -0
- package/changelog/serialize/index.esm.js +564 -0
- package/changelog/serialize/index.esm.js.map +1 -0
- package/changelog/serialize/templates.d.ts +81 -0
- package/changelog/serialize/templates.d.ts.map +1 -0
- package/changelog/serialize/to-json.d.ts +57 -0
- package/changelog/serialize/to-json.d.ts.map +1 -0
- package/changelog/serialize/to-string.d.ts +30 -0
- package/changelog/serialize/to-string.d.ts.map +1 -0
- package/commits/index.cjs.js +648 -0
- package/commits/index.cjs.js.map +1 -0
- package/commits/index.d.ts +3 -0
- package/commits/index.d.ts.map +1 -0
- package/commits/index.esm.js +629 -0
- package/commits/index.esm.js.map +1 -0
- package/commits/models/breaking.d.ts +39 -0
- package/commits/models/breaking.d.ts.map +1 -0
- package/commits/models/commit-type.d.ts +32 -0
- package/commits/models/commit-type.d.ts.map +1 -0
- package/commits/models/conventional.d.ts +49 -0
- package/commits/models/conventional.d.ts.map +1 -0
- package/commits/models/index.cjs.js +207 -0
- package/commits/models/index.cjs.js.map +1 -0
- package/commits/models/index.d.ts +7 -0
- package/commits/models/index.d.ts.map +1 -0
- package/commits/models/index.esm.js +193 -0
- package/commits/models/index.esm.js.map +1 -0
- package/commits/parse/body.d.ts +18 -0
- package/commits/parse/body.d.ts.map +1 -0
- package/commits/parse/footer.d.ts +16 -0
- package/commits/parse/footer.d.ts.map +1 -0
- package/commits/parse/header.d.ts +15 -0
- package/commits/parse/header.d.ts.map +1 -0
- package/commits/parse/index.cjs.js +505 -0
- package/commits/parse/index.cjs.js.map +1 -0
- package/commits/parse/index.d.ts +5 -0
- package/commits/parse/index.d.ts.map +1 -0
- package/commits/parse/index.esm.js +499 -0
- package/commits/parse/index.esm.js.map +1 -0
- package/commits/parse/message.d.ts +17 -0
- package/commits/parse/message.d.ts.map +1 -0
- package/commits/utils/replace-char.d.ts +19 -0
- package/commits/utils/replace-char.d.ts.map +1 -0
- package/flow/executor/execute.d.ts +72 -0
- package/flow/executor/execute.d.ts.map +1 -0
- package/flow/executor/index.cjs.js +4402 -0
- package/flow/executor/index.cjs.js.map +1 -0
- package/flow/executor/index.d.ts +3 -0
- package/flow/executor/index.d.ts.map +1 -0
- package/flow/executor/index.esm.js +4398 -0
- package/flow/executor/index.esm.js.map +1 -0
- package/flow/factory.d.ts +58 -0
- package/flow/factory.d.ts.map +1 -0
- package/flow/index.cjs.js +8506 -0
- package/flow/index.cjs.js.map +1 -0
- package/flow/index.d.ts +7 -0
- package/flow/index.d.ts.map +1 -0
- package/flow/index.esm.js +8451 -0
- package/flow/index.esm.js.map +1 -0
- package/flow/models/flow.d.ts +130 -0
- package/flow/models/flow.d.ts.map +1 -0
- package/flow/models/index.cjs.js +285 -0
- package/flow/models/index.cjs.js.map +1 -0
- package/flow/models/index.d.ts +7 -0
- package/flow/models/index.d.ts.map +1 -0
- package/flow/models/index.esm.js +268 -0
- package/flow/models/index.esm.js.map +1 -0
- package/flow/models/step.d.ts +108 -0
- package/flow/models/step.d.ts.map +1 -0
- package/flow/models/types.d.ts +150 -0
- package/flow/models/types.d.ts.map +1 -0
- package/flow/presets/conventional.d.ts +59 -0
- package/flow/presets/conventional.d.ts.map +1 -0
- package/flow/presets/independent.d.ts +61 -0
- package/flow/presets/independent.d.ts.map +1 -0
- package/flow/presets/index.cjs.js +3903 -0
- package/flow/presets/index.cjs.js.map +1 -0
- package/flow/presets/index.d.ts +4 -0
- package/flow/presets/index.d.ts.map +1 -0
- package/flow/presets/index.esm.js +3889 -0
- package/flow/presets/index.esm.js.map +1 -0
- package/flow/presets/synced.d.ts +65 -0
- package/flow/presets/synced.d.ts.map +1 -0
- package/flow/steps/analyze-commits.d.ts +19 -0
- package/flow/steps/analyze-commits.d.ts.map +1 -0
- package/flow/steps/calculate-bump.d.ts +27 -0
- package/flow/steps/calculate-bump.d.ts.map +1 -0
- package/flow/steps/create-commit.d.ts +16 -0
- package/flow/steps/create-commit.d.ts.map +1 -0
- package/flow/steps/create-tag.d.ts +22 -0
- package/flow/steps/create-tag.d.ts.map +1 -0
- package/flow/steps/fetch-registry.d.ts +19 -0
- package/flow/steps/fetch-registry.d.ts.map +1 -0
- package/flow/steps/generate-changelog.d.ts +25 -0
- package/flow/steps/generate-changelog.d.ts.map +1 -0
- package/flow/steps/index.cjs.js +3523 -0
- package/flow/steps/index.cjs.js.map +1 -0
- package/flow/steps/index.d.ts +8 -0
- package/flow/steps/index.d.ts.map +1 -0
- package/flow/steps/index.esm.js +3504 -0
- package/flow/steps/index.esm.js.map +1 -0
- package/flow/steps/update-packages.d.ts +25 -0
- package/flow/steps/update-packages.d.ts.map +1 -0
- package/flow/utils/interpolate.d.ts +11 -0
- package/flow/utils/interpolate.d.ts.map +1 -0
- package/git/factory.d.ts +233 -0
- package/git/factory.d.ts.map +1 -0
- package/git/index.cjs.js +2863 -0
- package/git/index.cjs.js.map +1 -0
- package/git/index.d.ts +5 -0
- package/git/index.d.ts.map +1 -0
- package/git/index.esm.js +2785 -0
- package/git/index.esm.js.map +1 -0
- package/git/models/commit.d.ts +129 -0
- package/git/models/commit.d.ts.map +1 -0
- package/git/models/index.cjs.js +755 -0
- package/git/models/index.cjs.js.map +1 -0
- package/git/models/index.d.ts +7 -0
- package/git/models/index.d.ts.map +1 -0
- package/git/models/index.esm.js +729 -0
- package/git/models/index.esm.js.map +1 -0
- package/git/models/ref.d.ts +120 -0
- package/git/models/ref.d.ts.map +1 -0
- package/git/models/tag.d.ts +141 -0
- package/git/models/tag.d.ts.map +1 -0
- package/git/operations/commit.d.ts +97 -0
- package/git/operations/commit.d.ts.map +1 -0
- package/git/operations/head-info.d.ts +29 -0
- package/git/operations/head-info.d.ts.map +1 -0
- package/git/operations/index.cjs.js +1954 -0
- package/git/operations/index.cjs.js.map +1 -0
- package/git/operations/index.d.ts +14 -0
- package/git/operations/index.d.ts.map +1 -0
- package/git/operations/index.esm.js +1903 -0
- package/git/operations/index.esm.js.map +1 -0
- package/git/operations/log.d.ts +104 -0
- package/git/operations/log.d.ts.map +1 -0
- package/git/operations/manage-tags.d.ts +60 -0
- package/git/operations/manage-tags.d.ts.map +1 -0
- package/git/operations/query-tags.d.ts +88 -0
- package/git/operations/query-tags.d.ts.map +1 -0
- package/git/operations/stage.d.ts +66 -0
- package/git/operations/stage.d.ts.map +1 -0
- package/git/operations/status.d.ts +173 -0
- package/git/operations/status.d.ts.map +1 -0
- package/index.cjs.js +16761 -0
- package/index.cjs.js.map +1 -0
- package/index.d.ts +102 -0
- package/index.d.ts.map +1 -0
- package/index.esm.js +16427 -0
- package/index.esm.js.map +1 -0
- package/package.json +200 -0
- package/registry/factory.d.ts +18 -0
- package/registry/factory.d.ts.map +1 -0
- package/registry/index.cjs.js +543 -0
- package/registry/index.cjs.js.map +1 -0
- package/registry/index.d.ts +5 -0
- package/registry/index.d.ts.map +1 -0
- package/registry/index.esm.js +535 -0
- package/registry/index.esm.js.map +1 -0
- package/registry/models/index.cjs.js +69 -0
- package/registry/models/index.cjs.js.map +1 -0
- package/registry/models/index.d.ts +6 -0
- package/registry/models/index.d.ts.map +1 -0
- package/registry/models/index.esm.js +66 -0
- package/registry/models/index.esm.js.map +1 -0
- package/registry/models/package-info.d.ts +55 -0
- package/registry/models/package-info.d.ts.map +1 -0
- package/registry/models/registry.d.ts +62 -0
- package/registry/models/registry.d.ts.map +1 -0
- package/registry/models/version-info.d.ts +67 -0
- package/registry/models/version-info.d.ts.map +1 -0
- package/registry/npm/cache.d.ts +50 -0
- package/registry/npm/cache.d.ts.map +1 -0
- package/registry/npm/client.d.ts +30 -0
- package/registry/npm/client.d.ts.map +1 -0
- package/registry/npm/index.cjs.js +456 -0
- package/registry/npm/index.cjs.js.map +1 -0
- package/registry/npm/index.d.ts +4 -0
- package/registry/npm/index.d.ts.map +1 -0
- package/registry/npm/index.esm.js +451 -0
- package/registry/npm/index.esm.js.map +1 -0
- package/semver/compare/compare.d.ts +100 -0
- package/semver/compare/compare.d.ts.map +1 -0
- package/semver/compare/index.cjs.js +386 -0
- package/semver/compare/index.cjs.js.map +1 -0
- package/semver/compare/index.d.ts +3 -0
- package/semver/compare/index.d.ts.map +1 -0
- package/semver/compare/index.esm.js +370 -0
- package/semver/compare/index.esm.js.map +1 -0
- package/semver/compare/sort.d.ts +36 -0
- package/semver/compare/sort.d.ts.map +1 -0
- package/semver/format/index.cjs.js +58 -0
- package/semver/format/index.cjs.js.map +1 -0
- package/semver/format/index.d.ts +2 -0
- package/semver/format/index.d.ts.map +1 -0
- package/semver/format/index.esm.js +53 -0
- package/semver/format/index.esm.js.map +1 -0
- package/semver/format/to-string.d.ts +31 -0
- package/semver/format/to-string.d.ts.map +1 -0
- package/semver/increment/bump.d.ts +37 -0
- package/semver/increment/bump.d.ts.map +1 -0
- package/semver/increment/index.cjs.js +223 -0
- package/semver/increment/index.cjs.js.map +1 -0
- package/semver/increment/index.d.ts +2 -0
- package/semver/increment/index.d.ts.map +1 -0
- package/semver/increment/index.esm.js +219 -0
- package/semver/increment/index.esm.js.map +1 -0
- package/semver/index.cjs.js +1499 -0
- package/semver/index.cjs.js.map +1 -0
- package/semver/index.d.ts +6 -0
- package/semver/index.d.ts.map +1 -0
- package/semver/index.esm.js +1458 -0
- package/semver/index.esm.js.map +1 -0
- package/semver/models/index.cjs.js +153 -0
- package/semver/models/index.cjs.js.map +1 -0
- package/semver/models/index.d.ts +5 -0
- package/semver/models/index.d.ts.map +1 -0
- package/semver/models/index.esm.js +139 -0
- package/semver/models/index.esm.js.map +1 -0
- package/semver/models/range.d.ts +83 -0
- package/semver/models/range.d.ts.map +1 -0
- package/semver/models/version.d.ts +78 -0
- package/semver/models/version.d.ts.map +1 -0
- package/semver/parse/index.cjs.js +799 -0
- package/semver/parse/index.cjs.js.map +1 -0
- package/semver/parse/index.d.ts +5 -0
- package/semver/parse/index.d.ts.map +1 -0
- package/semver/parse/index.esm.js +793 -0
- package/semver/parse/index.esm.js.map +1 -0
- package/semver/parse/range.d.ts +38 -0
- package/semver/parse/range.d.ts.map +1 -0
- package/semver/parse/version.d.ts +49 -0
- package/semver/parse/version.d.ts.map +1 -0
- package/workspace/discovery/changelog-path.d.ts +21 -0
- package/workspace/discovery/changelog-path.d.ts.map +1 -0
- package/workspace/discovery/dependencies.d.ts +145 -0
- package/workspace/discovery/dependencies.d.ts.map +1 -0
- package/workspace/discovery/discover-changelogs.d.ts +76 -0
- package/workspace/discovery/discover-changelogs.d.ts.map +1 -0
- package/workspace/discovery/index.cjs.js +2300 -0
- package/workspace/discovery/index.cjs.js.map +1 -0
- package/workspace/discovery/index.d.ts +13 -0
- package/workspace/discovery/index.d.ts.map +1 -0
- package/workspace/discovery/index.esm.js +2283 -0
- package/workspace/discovery/index.esm.js.map +1 -0
- package/workspace/discovery/packages.d.ts +83 -0
- package/workspace/discovery/packages.d.ts.map +1 -0
- package/workspace/index.cjs.js +4445 -0
- package/workspace/index.cjs.js.map +1 -0
- package/workspace/index.d.ts +52 -0
- package/workspace/index.d.ts.map +1 -0
- package/workspace/index.esm.js +4394 -0
- package/workspace/index.esm.js.map +1 -0
- package/workspace/models/index.cjs.js +284 -0
- package/workspace/models/index.cjs.js.map +1 -0
- package/workspace/models/index.d.ts +10 -0
- package/workspace/models/index.d.ts.map +1 -0
- package/workspace/models/index.esm.js +261 -0
- package/workspace/models/index.esm.js.map +1 -0
- package/workspace/models/project.d.ts +118 -0
- package/workspace/models/project.d.ts.map +1 -0
- package/workspace/models/workspace.d.ts +139 -0
- package/workspace/models/workspace.d.ts.map +1 -0
- package/workspace/operations/batch-update.d.ts +99 -0
- package/workspace/operations/batch-update.d.ts.map +1 -0
- package/workspace/operations/cascade-bump.d.ts +125 -0
- package/workspace/operations/cascade-bump.d.ts.map +1 -0
- package/workspace/operations/index.cjs.js +2675 -0
- package/workspace/operations/index.cjs.js.map +1 -0
- package/workspace/operations/index.d.ts +12 -0
- package/workspace/operations/index.d.ts.map +1 -0
- package/workspace/operations/index.esm.js +2663 -0
- package/workspace/operations/index.esm.js.map +1 -0
- package/workspace/operations/validate.d.ts +85 -0
- package/workspace/operations/validate.d.ts.map +1 -0
|
@@ -0,0 +1,2675 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
require('node:util');
|
|
4
|
+
var node_path = require('node:path');
|
|
5
|
+
var node_fs = require('node:fs');
|
|
6
|
+
require('node:os');
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Safe copies of Error built-ins via factory functions.
|
|
10
|
+
*
|
|
11
|
+
* Since constructors cannot be safely captured via Object.assign, this module
|
|
12
|
+
* provides factory functions that use Reflect.construct internally.
|
|
13
|
+
*
|
|
14
|
+
* These references are captured at module initialization time to protect against
|
|
15
|
+
* prototype pollution attacks. Import only what you need for tree-shaking.
|
|
16
|
+
*
|
|
17
|
+
* @module @hyperfrontend/immutable-api-utils/built-in-copy/error
|
|
18
|
+
*/
|
|
19
|
+
// Capture references at module initialization time
|
|
20
|
+
const _Error = globalThis.Error;
|
|
21
|
+
const _Reflect$2 = globalThis.Reflect;
|
|
22
|
+
/**
|
|
23
|
+
* (Safe copy) Creates a new Error using the captured Error constructor.
|
|
24
|
+
* Use this instead of `new Error()`.
|
|
25
|
+
*
|
|
26
|
+
* @param message - Optional error message.
|
|
27
|
+
* @param options - Optional error options.
|
|
28
|
+
* @returns A new Error instance.
|
|
29
|
+
*/
|
|
30
|
+
const createError = (message, options) => _Reflect$2.construct(_Error, [message, options]);
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* Safe copies of Map built-in via factory function.
|
|
34
|
+
*
|
|
35
|
+
* Since constructors cannot be safely captured via Object.assign, this module
|
|
36
|
+
* provides a factory function that uses Reflect.construct internally.
|
|
37
|
+
*
|
|
38
|
+
* These references are captured at module initialization time to protect against
|
|
39
|
+
* prototype pollution attacks. Import only what you need for tree-shaking.
|
|
40
|
+
*
|
|
41
|
+
* @module @hyperfrontend/immutable-api-utils/built-in-copy/map
|
|
42
|
+
*/
|
|
43
|
+
// Capture references at module initialization time
|
|
44
|
+
const _Map = globalThis.Map;
|
|
45
|
+
const _Reflect$1 = globalThis.Reflect;
|
|
46
|
+
/**
|
|
47
|
+
* (Safe copy) Creates a new Map using the captured Map constructor.
|
|
48
|
+
* Use this instead of `new Map()`.
|
|
49
|
+
*
|
|
50
|
+
* @param iterable - Optional iterable of key-value pairs.
|
|
51
|
+
* @returns A new Map instance.
|
|
52
|
+
*/
|
|
53
|
+
const createMap = (iterable) => _Reflect$1.construct(_Map, iterable ? [iterable] : []);
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* Safe copies of Set built-in via factory function.
|
|
57
|
+
*
|
|
58
|
+
* Since constructors cannot be safely captured via Object.assign, this module
|
|
59
|
+
* provides a factory function that uses Reflect.construct internally.
|
|
60
|
+
*
|
|
61
|
+
* These references are captured at module initialization time to protect against
|
|
62
|
+
* prototype pollution attacks. Import only what you need for tree-shaking.
|
|
63
|
+
*
|
|
64
|
+
* @module @hyperfrontend/immutable-api-utils/built-in-copy/set
|
|
65
|
+
*/
|
|
66
|
+
// Capture references at module initialization time
|
|
67
|
+
const _Set = globalThis.Set;
|
|
68
|
+
const _Reflect = globalThis.Reflect;
|
|
69
|
+
/**
|
|
70
|
+
* (Safe copy) Creates a new Set using the captured Set constructor.
|
|
71
|
+
* Use this instead of `new Set()`.
|
|
72
|
+
*
|
|
73
|
+
* @param iterable - Optional iterable of values.
|
|
74
|
+
* @returns A new Set instance.
|
|
75
|
+
*/
|
|
76
|
+
const createSet = (iterable) => _Reflect.construct(_Set, iterable ? [iterable] : []);
|
|
77
|
+
|
|
78
|
+
/**
|
|
79
|
+
* Converts a SemVer to its canonical string representation.
|
|
80
|
+
*
|
|
81
|
+
* @param version - The version to format
|
|
82
|
+
* @returns The version string (e.g., "1.2.3-alpha.1+build.123")
|
|
83
|
+
*/
|
|
84
|
+
function format(version) {
|
|
85
|
+
let result = `${version.major}.${version.minor}.${version.patch}`;
|
|
86
|
+
if (version.prerelease.length > 0) {
|
|
87
|
+
result += '-' + version.prerelease.join('.');
|
|
88
|
+
}
|
|
89
|
+
if (version.build.length > 0) {
|
|
90
|
+
result += '+' + version.build.join('.');
|
|
91
|
+
}
|
|
92
|
+
return result;
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
/**
|
|
96
|
+
* Safe copies of Number built-in methods and constants.
|
|
97
|
+
*
|
|
98
|
+
* These references are captured at module initialization time to protect against
|
|
99
|
+
* prototype pollution attacks. Import only what you need for tree-shaking.
|
|
100
|
+
*
|
|
101
|
+
* @module @hyperfrontend/immutable-api-utils/built-in-copy/number
|
|
102
|
+
*/
|
|
103
|
+
// Capture references at module initialization time
|
|
104
|
+
const _parseInt = globalThis.parseInt;
|
|
105
|
+
const _isNaN = globalThis.isNaN;
|
|
106
|
+
// ============================================================================
|
|
107
|
+
// Parsing
|
|
108
|
+
// ============================================================================
|
|
109
|
+
/**
|
|
110
|
+
* (Safe copy) Parses a string and returns an integer.
|
|
111
|
+
*/
|
|
112
|
+
const parseInt = _parseInt;
|
|
113
|
+
// ============================================================================
|
|
114
|
+
// Global Type Checking (legacy, less strict)
|
|
115
|
+
// ============================================================================
|
|
116
|
+
/**
|
|
117
|
+
* (Safe copy) Global isNaN function (coerces to number first, less strict than Number.isNaN).
|
|
118
|
+
*/
|
|
119
|
+
const globalIsNaN = _isNaN;
|
|
120
|
+
|
|
121
|
+
/**
|
|
122
|
+
* Creates a new SemVer object.
|
|
123
|
+
*
|
|
124
|
+
* @param options - Version components
|
|
125
|
+
* @returns A new SemVer object
|
|
126
|
+
*/
|
|
127
|
+
function createSemVer(options) {
|
|
128
|
+
return {
|
|
129
|
+
major: options.major,
|
|
130
|
+
minor: options.minor,
|
|
131
|
+
patch: options.patch,
|
|
132
|
+
prerelease: options.prerelease ?? [],
|
|
133
|
+
build: options.build ?? [],
|
|
134
|
+
raw: options.raw,
|
|
135
|
+
};
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
/**
|
|
139
|
+
* Increments a version based on the bump type.
|
|
140
|
+
*
|
|
141
|
+
* @param version - The version to increment
|
|
142
|
+
* @param type - The type of bump (major, minor, patch, etc.)
|
|
143
|
+
* @param prereleaseId - Optional prerelease identifier for prerelease bumps
|
|
144
|
+
* @returns A new incremented SemVer
|
|
145
|
+
*
|
|
146
|
+
* @example
|
|
147
|
+
* increment(parseVersion('1.2.3'), 'minor') // 1.3.0
|
|
148
|
+
* increment(parseVersion('1.2.3'), 'major') // 2.0.0
|
|
149
|
+
* increment(parseVersion('1.2.3'), 'prerelease', 'alpha') // 1.2.4-alpha.0
|
|
150
|
+
*/
|
|
151
|
+
function increment(version, type, prereleaseId) {
|
|
152
|
+
switch (type) {
|
|
153
|
+
case 'major':
|
|
154
|
+
return createSemVer({
|
|
155
|
+
major: version.major + 1,
|
|
156
|
+
minor: 0,
|
|
157
|
+
patch: 0,
|
|
158
|
+
prerelease: [],
|
|
159
|
+
build: [],
|
|
160
|
+
});
|
|
161
|
+
case 'minor':
|
|
162
|
+
return createSemVer({
|
|
163
|
+
major: version.major,
|
|
164
|
+
minor: version.minor + 1,
|
|
165
|
+
patch: 0,
|
|
166
|
+
prerelease: [],
|
|
167
|
+
build: [],
|
|
168
|
+
});
|
|
169
|
+
case 'patch':
|
|
170
|
+
// If version has prerelease, just remove it (1.2.3-alpha -> 1.2.3)
|
|
171
|
+
if (version.prerelease.length > 0) {
|
|
172
|
+
return createSemVer({
|
|
173
|
+
major: version.major,
|
|
174
|
+
minor: version.minor,
|
|
175
|
+
patch: version.patch,
|
|
176
|
+
prerelease: [],
|
|
177
|
+
build: [],
|
|
178
|
+
});
|
|
179
|
+
}
|
|
180
|
+
return createSemVer({
|
|
181
|
+
major: version.major,
|
|
182
|
+
minor: version.minor,
|
|
183
|
+
patch: version.patch + 1,
|
|
184
|
+
prerelease: [],
|
|
185
|
+
build: [],
|
|
186
|
+
});
|
|
187
|
+
case 'premajor':
|
|
188
|
+
return createSemVer({
|
|
189
|
+
major: version.major + 1,
|
|
190
|
+
minor: 0,
|
|
191
|
+
patch: 0,
|
|
192
|
+
prerelease: [prereleaseId ?? 'alpha', '0'],
|
|
193
|
+
build: [],
|
|
194
|
+
});
|
|
195
|
+
case 'preminor':
|
|
196
|
+
return createSemVer({
|
|
197
|
+
major: version.major,
|
|
198
|
+
minor: version.minor + 1,
|
|
199
|
+
patch: 0,
|
|
200
|
+
prerelease: [prereleaseId ?? 'alpha', '0'],
|
|
201
|
+
build: [],
|
|
202
|
+
});
|
|
203
|
+
case 'prepatch':
|
|
204
|
+
return createSemVer({
|
|
205
|
+
major: version.major,
|
|
206
|
+
minor: version.minor,
|
|
207
|
+
patch: version.patch + 1,
|
|
208
|
+
prerelease: [prereleaseId ?? 'alpha', '0'],
|
|
209
|
+
build: [],
|
|
210
|
+
});
|
|
211
|
+
case 'prerelease':
|
|
212
|
+
return incrementPrerelease(version, prereleaseId);
|
|
213
|
+
case 'none':
|
|
214
|
+
default:
|
|
215
|
+
return version;
|
|
216
|
+
}
|
|
217
|
+
}
|
|
218
|
+
/**
|
|
219
|
+
* Increments the prerelease portion of a version.
|
|
220
|
+
*
|
|
221
|
+
* @param version - The version to increment
|
|
222
|
+
* @param id - Optional prerelease identifier
|
|
223
|
+
* @returns A new version with incremented prerelease
|
|
224
|
+
*/
|
|
225
|
+
function incrementPrerelease(version, id) {
|
|
226
|
+
const prerelease = [...version.prerelease];
|
|
227
|
+
if (prerelease.length === 0) {
|
|
228
|
+
// No existing prerelease - start at patch+1 with id.0
|
|
229
|
+
return createSemVer({
|
|
230
|
+
major: version.major,
|
|
231
|
+
minor: version.minor,
|
|
232
|
+
patch: version.patch + 1,
|
|
233
|
+
prerelease: [id ?? 'alpha', '0'],
|
|
234
|
+
build: [],
|
|
235
|
+
});
|
|
236
|
+
}
|
|
237
|
+
// Check if the last identifier is numeric
|
|
238
|
+
const lastIdx = prerelease.length - 1;
|
|
239
|
+
const last = prerelease[lastIdx];
|
|
240
|
+
const lastNum = parseInt(last, 10);
|
|
241
|
+
if (!globalIsNaN(lastNum)) {
|
|
242
|
+
// Increment the numeric part
|
|
243
|
+
prerelease[lastIdx] = String(lastNum + 1);
|
|
244
|
+
}
|
|
245
|
+
else {
|
|
246
|
+
// Append .0
|
|
247
|
+
prerelease.push('0');
|
|
248
|
+
}
|
|
249
|
+
// If a different id is specified, replace the base identifier
|
|
250
|
+
if (id && prerelease.length > 0 && prerelease[0] !== id) {
|
|
251
|
+
prerelease[0] = id;
|
|
252
|
+
// Reset numeric part
|
|
253
|
+
if (prerelease.length > 1) {
|
|
254
|
+
prerelease[prerelease.length - 1] = '0';
|
|
255
|
+
}
|
|
256
|
+
}
|
|
257
|
+
return createSemVer({
|
|
258
|
+
major: version.major,
|
|
259
|
+
minor: version.minor,
|
|
260
|
+
patch: version.patch,
|
|
261
|
+
prerelease,
|
|
262
|
+
build: [],
|
|
263
|
+
});
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
/**
|
|
267
|
+
* Maximum version string length to prevent memory exhaustion.
|
|
268
|
+
*/
|
|
269
|
+
const MAX_VERSION_LENGTH = 256;
|
|
270
|
+
/**
|
|
271
|
+
* Parses a semantic version string.
|
|
272
|
+
*
|
|
273
|
+
* Accepts versions in the format: MAJOR.MINOR.PATCH[-prerelease][+build]
|
|
274
|
+
* Optional leading 'v' or '=' prefixes are stripped.
|
|
275
|
+
*
|
|
276
|
+
* @param input - The version string to parse
|
|
277
|
+
* @returns A ParseVersionResult with the parsed version or error
|
|
278
|
+
*
|
|
279
|
+
* @example
|
|
280
|
+
* parseVersion('1.2.3') // { success: true, version: { major: 1, minor: 2, patch: 3, ... } }
|
|
281
|
+
* parseVersion('v1.0.0-alpha.1+build.123') // { success: true, ... }
|
|
282
|
+
* parseVersion('invalid') // { success: false, error: '...' }
|
|
283
|
+
*/
|
|
284
|
+
function parseVersion(input) {
|
|
285
|
+
// Input validation
|
|
286
|
+
if (!input || typeof input !== 'string') {
|
|
287
|
+
return { success: false, error: 'Version string is required' };
|
|
288
|
+
}
|
|
289
|
+
if (input.length > MAX_VERSION_LENGTH) {
|
|
290
|
+
return { success: false, error: `Version string exceeds maximum length of ${MAX_VERSION_LENGTH}` };
|
|
291
|
+
}
|
|
292
|
+
// Strip leading whitespace
|
|
293
|
+
let pos = 0;
|
|
294
|
+
while (pos < input.length && isWhitespace(input.charCodeAt(pos))) {
|
|
295
|
+
pos++;
|
|
296
|
+
}
|
|
297
|
+
// Strip trailing whitespace
|
|
298
|
+
let end = input.length;
|
|
299
|
+
while (end > pos && isWhitespace(input.charCodeAt(end - 1))) {
|
|
300
|
+
end--;
|
|
301
|
+
}
|
|
302
|
+
// Strip optional leading 'v' or '='
|
|
303
|
+
if (pos < end) {
|
|
304
|
+
const code = input.charCodeAt(pos);
|
|
305
|
+
if (code === 118 || code === 86) {
|
|
306
|
+
// 'v' or 'V'
|
|
307
|
+
pos++;
|
|
308
|
+
}
|
|
309
|
+
else if (code === 61) {
|
|
310
|
+
// '='
|
|
311
|
+
pos++;
|
|
312
|
+
}
|
|
313
|
+
}
|
|
314
|
+
// Parse major version
|
|
315
|
+
const majorResult = parseNumericIdentifier(input, pos, end);
|
|
316
|
+
if (!majorResult.success) {
|
|
317
|
+
return { success: false, error: majorResult.error ?? 'Invalid major version' };
|
|
318
|
+
}
|
|
319
|
+
pos = majorResult.endPos;
|
|
320
|
+
// Expect dot
|
|
321
|
+
if (pos >= end || input.charCodeAt(pos) !== 46) {
|
|
322
|
+
// '.'
|
|
323
|
+
return { success: false, error: 'Expected "." after major version' };
|
|
324
|
+
}
|
|
325
|
+
pos++;
|
|
326
|
+
// Parse minor version
|
|
327
|
+
const minorResult = parseNumericIdentifier(input, pos, end);
|
|
328
|
+
if (!minorResult.success) {
|
|
329
|
+
return { success: false, error: minorResult.error ?? 'Invalid minor version' };
|
|
330
|
+
}
|
|
331
|
+
pos = minorResult.endPos;
|
|
332
|
+
// Expect dot
|
|
333
|
+
if (pos >= end || input.charCodeAt(pos) !== 46) {
|
|
334
|
+
// '.'
|
|
335
|
+
return { success: false, error: 'Expected "." after minor version' };
|
|
336
|
+
}
|
|
337
|
+
pos++;
|
|
338
|
+
// Parse patch version
|
|
339
|
+
const patchResult = parseNumericIdentifier(input, pos, end);
|
|
340
|
+
if (!patchResult.success) {
|
|
341
|
+
return { success: false, error: patchResult.error ?? 'Invalid patch version' };
|
|
342
|
+
}
|
|
343
|
+
pos = patchResult.endPos;
|
|
344
|
+
// Parse optional prerelease
|
|
345
|
+
const prerelease = [];
|
|
346
|
+
if (pos < end && input.charCodeAt(pos) === 45) {
|
|
347
|
+
// '-'
|
|
348
|
+
pos++;
|
|
349
|
+
const prereleaseResult = parseIdentifiers(input, pos, end, [43]); // Stop at '+'
|
|
350
|
+
if (!prereleaseResult.success) {
|
|
351
|
+
return { success: false, error: prereleaseResult.error ?? 'Invalid prerelease' };
|
|
352
|
+
}
|
|
353
|
+
prerelease.push(...prereleaseResult.identifiers);
|
|
354
|
+
pos = prereleaseResult.endPos;
|
|
355
|
+
}
|
|
356
|
+
// Parse optional build metadata
|
|
357
|
+
const build = [];
|
|
358
|
+
if (pos < end && input.charCodeAt(pos) === 43) {
|
|
359
|
+
// '+'
|
|
360
|
+
pos++;
|
|
361
|
+
const buildResult = parseIdentifiers(input, pos, end, []);
|
|
362
|
+
if (!buildResult.success) {
|
|
363
|
+
return { success: false, error: buildResult.error ?? 'Invalid build metadata' };
|
|
364
|
+
}
|
|
365
|
+
build.push(...buildResult.identifiers);
|
|
366
|
+
pos = buildResult.endPos;
|
|
367
|
+
}
|
|
368
|
+
// Check for trailing characters
|
|
369
|
+
if (pos < end) {
|
|
370
|
+
return { success: false, error: `Unexpected character at position ${pos}: "${input[pos]}"` };
|
|
371
|
+
}
|
|
372
|
+
return {
|
|
373
|
+
success: true,
|
|
374
|
+
version: createSemVer({
|
|
375
|
+
major: majorResult.value,
|
|
376
|
+
minor: minorResult.value,
|
|
377
|
+
patch: patchResult.value,
|
|
378
|
+
prerelease,
|
|
379
|
+
build,
|
|
380
|
+
raw: input,
|
|
381
|
+
}),
|
|
382
|
+
};
|
|
383
|
+
}
|
|
384
|
+
/**
|
|
385
|
+
* Parses a numeric identifier (non-negative integer, no leading zeros except for "0").
|
|
386
|
+
*
|
|
387
|
+
* @param input - Input string to parse
|
|
388
|
+
* @param start - Start position in the input
|
|
389
|
+
* @param end - End position in the input
|
|
390
|
+
* @returns Numeric parsing result
|
|
391
|
+
*/
|
|
392
|
+
function parseNumericIdentifier(input, start, end) {
|
|
393
|
+
if (start >= end) {
|
|
394
|
+
return { success: false, value: 0, endPos: start, error: 'Expected numeric identifier' };
|
|
395
|
+
}
|
|
396
|
+
let pos = start;
|
|
397
|
+
const firstCode = input.charCodeAt(pos);
|
|
398
|
+
// Must start with a digit
|
|
399
|
+
if (!isDigit(firstCode)) {
|
|
400
|
+
return { success: false, value: 0, endPos: pos, error: 'Expected digit' };
|
|
401
|
+
}
|
|
402
|
+
// Check for leading zero (only "0" is valid, not "01", "007", etc.)
|
|
403
|
+
if (firstCode === 48 && pos + 1 < end && isDigit(input.charCodeAt(pos + 1))) {
|
|
404
|
+
return { success: false, value: 0, endPos: pos, error: 'Numeric identifier cannot have leading zeros' };
|
|
405
|
+
}
|
|
406
|
+
// Consume digits
|
|
407
|
+
let value = 0;
|
|
408
|
+
while (pos < end && isDigit(input.charCodeAt(pos))) {
|
|
409
|
+
value = value * 10 + (input.charCodeAt(pos) - 48);
|
|
410
|
+
pos++;
|
|
411
|
+
// Prevent overflow
|
|
412
|
+
if (value > Number.MAX_SAFE_INTEGER) {
|
|
413
|
+
return { success: false, value: 0, endPos: pos, error: 'Numeric identifier is too large' };
|
|
414
|
+
}
|
|
415
|
+
}
|
|
416
|
+
return { success: true, value, endPos: pos };
|
|
417
|
+
}
|
|
418
|
+
/**
|
|
419
|
+
* Parses dot-separated identifiers (for prerelease/build).
|
|
420
|
+
*
|
|
421
|
+
* @param input - Input string to parse
|
|
422
|
+
* @param start - Start position in the input
|
|
423
|
+
* @param end - End position in the input
|
|
424
|
+
* @param stopCodes - Character codes that signal end of identifiers
|
|
425
|
+
* @returns Identifiers parsing result
|
|
426
|
+
*/
|
|
427
|
+
function parseIdentifiers(input, start, end, stopCodes) {
|
|
428
|
+
const identifiers = [];
|
|
429
|
+
let pos = start;
|
|
430
|
+
while (pos < end) {
|
|
431
|
+
// Check for stop characters
|
|
432
|
+
if (stopCodes.includes(input.charCodeAt(pos))) {
|
|
433
|
+
break;
|
|
434
|
+
}
|
|
435
|
+
// Parse one identifier
|
|
436
|
+
const identStart = pos;
|
|
437
|
+
while (pos < end) {
|
|
438
|
+
const code = input.charCodeAt(pos);
|
|
439
|
+
// Stop at dot or stop characters
|
|
440
|
+
if (code === 46 || stopCodes.includes(code)) {
|
|
441
|
+
break;
|
|
442
|
+
}
|
|
443
|
+
// Must be alphanumeric or hyphen
|
|
444
|
+
if (!isAlphanumeric(code) && code !== 45) {
|
|
445
|
+
return { success: false, identifiers: [], endPos: pos, error: `Invalid character in identifier: "${input[pos]}"` };
|
|
446
|
+
}
|
|
447
|
+
pos++;
|
|
448
|
+
}
|
|
449
|
+
// Empty identifier is not allowed
|
|
450
|
+
if (pos === identStart) {
|
|
451
|
+
return { success: false, identifiers: [], endPos: pos, error: 'Empty identifier' };
|
|
452
|
+
}
|
|
453
|
+
identifiers.push(input.slice(identStart, pos));
|
|
454
|
+
// Consume dot separator
|
|
455
|
+
if (pos < end && input.charCodeAt(pos) === 46) {
|
|
456
|
+
pos++;
|
|
457
|
+
// Dot at end is invalid
|
|
458
|
+
if (pos >= end || stopCodes.includes(input.charCodeAt(pos))) {
|
|
459
|
+
return { success: false, identifiers: [], endPos: pos, error: 'Identifier expected after dot' };
|
|
460
|
+
}
|
|
461
|
+
}
|
|
462
|
+
}
|
|
463
|
+
return { success: true, identifiers, endPos: pos };
|
|
464
|
+
}
|
|
465
|
+
/**
|
|
466
|
+
* Checks if a character code is a digit (0-9).
|
|
467
|
+
*
|
|
468
|
+
* @param code - Character code to check
|
|
469
|
+
* @returns True if the code represents a digit
|
|
470
|
+
*/
|
|
471
|
+
function isDigit(code) {
|
|
472
|
+
return code >= 48 && code <= 57;
|
|
473
|
+
}
|
|
474
|
+
/**
|
|
475
|
+
* Checks if a character code is alphanumeric or hyphen.
|
|
476
|
+
*
|
|
477
|
+
* @param code - Character code to check
|
|
478
|
+
* @returns True if the code represents an alphanumeric character
|
|
479
|
+
*/
|
|
480
|
+
function isAlphanumeric(code) {
|
|
481
|
+
return ((code >= 48 && code <= 57) || // 0-9
|
|
482
|
+
(code >= 65 && code <= 90) || // A-Z
|
|
483
|
+
(code >= 97 && code <= 122) // a-z
|
|
484
|
+
);
|
|
485
|
+
}
|
|
486
|
+
/**
|
|
487
|
+
* Checks if a character code is whitespace.
|
|
488
|
+
*
|
|
489
|
+
* @param code - Character code to check
|
|
490
|
+
* @returns True if the code represents whitespace
|
|
491
|
+
*/
|
|
492
|
+
function isWhitespace(code) {
|
|
493
|
+
return code === 32 || code === 9 || code === 10 || code === 13;
|
|
494
|
+
}
|
|
495
|
+
|
|
496
|
+
/**
|
|
497
|
+
* Safe copies of Object built-in methods.
|
|
498
|
+
*
|
|
499
|
+
* These references are captured at module initialization time to protect against
|
|
500
|
+
* prototype pollution attacks. Import only what you need for tree-shaking.
|
|
501
|
+
*
|
|
502
|
+
* @module @hyperfrontend/immutable-api-utils/built-in-copy/object
|
|
503
|
+
*/
|
|
504
|
+
// Capture references at module initialization time
|
|
505
|
+
const _Object = globalThis.Object;
|
|
506
|
+
/**
|
|
507
|
+
* (Safe copy) Prevents modification of existing property attributes and values,
|
|
508
|
+
* and prevents the addition of new properties.
|
|
509
|
+
*/
|
|
510
|
+
const freeze = _Object.freeze;
|
|
511
|
+
/**
|
|
512
|
+
* (Safe copy) Returns the names of the enumerable string properties and methods of an object.
|
|
513
|
+
*/
|
|
514
|
+
const keys = _Object.keys;
|
|
515
|
+
/**
|
|
516
|
+
* (Safe copy) Returns an array of key/values of the enumerable own properties of an object.
|
|
517
|
+
*/
|
|
518
|
+
const entries = _Object.entries;
|
|
519
|
+
/**
|
|
520
|
+
* (Safe copy) Adds one or more properties to an object, and/or modifies attributes of existing properties.
|
|
521
|
+
*/
|
|
522
|
+
const defineProperties = _Object.defineProperties;
|
|
523
|
+
|
|
524
|
+
/**
|
|
525
|
+
* Dependency Graph
|
|
526
|
+
*
|
|
527
|
+
* Builds and analyzes dependency relationships between workspace projects.
|
|
528
|
+
* Provides functions for traversing the dependency graph and determining
|
|
529
|
+
* build/release order.
|
|
530
|
+
*/
|
|
531
|
+
/**
|
|
532
|
+
* Gets all transitive dependents of a package (direct and indirect).
|
|
533
|
+
*
|
|
534
|
+
* @param workspace - The workspace containing projects
|
|
535
|
+
* @param packageName - Name of the package to analyze
|
|
536
|
+
* @returns Set of all packages that depend on this package
|
|
537
|
+
*
|
|
538
|
+
* @example
|
|
539
|
+
* ```typescript
|
|
540
|
+
* // If lib-a depends on lib-utils and app-main depends on lib-a
|
|
541
|
+
* // Then getTransitiveDependents('lib-utils') returns ['lib-a', 'app-main']
|
|
542
|
+
* ```
|
|
543
|
+
*/
|
|
544
|
+
function getTransitiveDependents(workspace, packageName) {
|
|
545
|
+
const dependents = createSet();
|
|
546
|
+
const queue = [packageName];
|
|
547
|
+
while (queue.length > 0) {
|
|
548
|
+
const current = queue.shift();
|
|
549
|
+
if (current === undefined) {
|
|
550
|
+
break;
|
|
551
|
+
}
|
|
552
|
+
const directDependents = workspace.dependencyGraph.get(current) ?? [];
|
|
553
|
+
for (const dep of directDependents) {
|
|
554
|
+
if (!dependents.has(dep)) {
|
|
555
|
+
dependents.add(dep);
|
|
556
|
+
queue.push(dep);
|
|
557
|
+
}
|
|
558
|
+
}
|
|
559
|
+
}
|
|
560
|
+
return dependents;
|
|
561
|
+
}
|
|
562
|
+
|
|
563
|
+
/**
|
|
564
|
+
* Cascade Bump
|
|
565
|
+
*
|
|
566
|
+
* Calculates which packages need version bumps when dependencies change.
|
|
567
|
+
* Implements cascade versioning for monorepos where dependents may need
|
|
568
|
+
* to be bumped when their dependencies are updated.
|
|
569
|
+
*/
|
|
570
|
+
/**
|
|
571
|
+
* Default cascade bump options.
|
|
572
|
+
*/
|
|
573
|
+
const DEFAULT_CASCADE_OPTIONS = {
|
|
574
|
+
cascadeBumpType: 'patch',
|
|
575
|
+
includeDevDependencies: false,
|
|
576
|
+
includePeerDependencies: true,
|
|
577
|
+
prereleaseId: 'alpha',
|
|
578
|
+
};
|
|
579
|
+
/**
|
|
580
|
+
* Calculates cascade bumps for a workspace given direct bumps.
|
|
581
|
+
*
|
|
582
|
+
* When packages are directly bumped (e.g., due to commits), their dependents
|
|
583
|
+
* may also need version bumps. This function calculates all affected packages.
|
|
584
|
+
*
|
|
585
|
+
* @param workspace - Workspace containing projects and dependency graph
|
|
586
|
+
* @param directBumps - Packages with direct changes
|
|
587
|
+
* @param options - Configuration for cascade bump calculation
|
|
588
|
+
* @returns Cascade bump result
|
|
589
|
+
*
|
|
590
|
+
* @example
|
|
591
|
+
* ```typescript
|
|
592
|
+
* import { calculateCascadeBumps } from '@hyperfrontend/versioning'
|
|
593
|
+
*
|
|
594
|
+
* // If lib-utils is getting a minor bump
|
|
595
|
+
* const result = calculateCascadeBumps(workspace, [
|
|
596
|
+
* { name: 'lib-utils', bumpType: 'minor' }
|
|
597
|
+
* ])
|
|
598
|
+
*
|
|
599
|
+
* // result.bumps includes lib-utils and all packages that depend on it
|
|
600
|
+
* for (const bump of result.bumps) {
|
|
601
|
+
* console.log(`${bump.name}: ${bump.currentVersion} -> ${bump.nextVersion}`)
|
|
602
|
+
* }
|
|
603
|
+
* ```
|
|
604
|
+
*/
|
|
605
|
+
function calculateCascadeBumps(workspace, directBumps, options = {}) {
|
|
606
|
+
const opts = { ...DEFAULT_CASCADE_OPTIONS, ...options };
|
|
607
|
+
const directBumpMap = createMap(directBumps.map((b) => [b.name, b]));
|
|
608
|
+
const allBumps = createMap();
|
|
609
|
+
// Process direct bumps first
|
|
610
|
+
for (const input of directBumps) {
|
|
611
|
+
const project = workspace.projects.get(input.name);
|
|
612
|
+
if (!project) {
|
|
613
|
+
continue;
|
|
614
|
+
}
|
|
615
|
+
const planned = createPlannedBump(project, input.bumpType, 'direct', [], opts.prereleaseId);
|
|
616
|
+
allBumps.set(input.name, planned);
|
|
617
|
+
}
|
|
618
|
+
// Calculate cascade bumps
|
|
619
|
+
const processed = createSet();
|
|
620
|
+
const queue = [...directBumps.map((b) => b.name)];
|
|
621
|
+
while (queue.length > 0) {
|
|
622
|
+
const current = queue.shift();
|
|
623
|
+
if (current === undefined || processed.has(current)) {
|
|
624
|
+
continue;
|
|
625
|
+
}
|
|
626
|
+
processed.add(current);
|
|
627
|
+
// Get dependents
|
|
628
|
+
const dependents = getTransitiveDependents(workspace, current);
|
|
629
|
+
for (const depName of dependents) {
|
|
630
|
+
// Skip if already has a direct bump
|
|
631
|
+
if (directBumpMap.has(depName)) {
|
|
632
|
+
continue;
|
|
633
|
+
}
|
|
634
|
+
// Skip if already planned
|
|
635
|
+
if (allBumps.has(depName)) {
|
|
636
|
+
// Update triggered by list
|
|
637
|
+
const existing = allBumps.get(depName);
|
|
638
|
+
if (existing && !existing.triggeredBy.includes(current)) {
|
|
639
|
+
allBumps.set(depName, {
|
|
640
|
+
...existing,
|
|
641
|
+
triggeredBy: [...existing.triggeredBy, current],
|
|
642
|
+
});
|
|
643
|
+
}
|
|
644
|
+
continue;
|
|
645
|
+
}
|
|
646
|
+
const project = workspace.projects.get(depName);
|
|
647
|
+
if (!project) {
|
|
648
|
+
continue;
|
|
649
|
+
}
|
|
650
|
+
// Check if we should include this dependent based on dependency type
|
|
651
|
+
if (!shouldCascade(workspace, depName, current, opts)) {
|
|
652
|
+
continue;
|
|
653
|
+
}
|
|
654
|
+
const planned = createPlannedBump(project, opts.cascadeBumpType, 'cascade', [current], opts.prereleaseId);
|
|
655
|
+
allBumps.set(depName, planned);
|
|
656
|
+
// Continue cascading from this dependent
|
|
657
|
+
queue.push(depName);
|
|
658
|
+
}
|
|
659
|
+
}
|
|
660
|
+
// Convert to arrays and categorize
|
|
661
|
+
const bumps = [...allBumps.values()];
|
|
662
|
+
const directBumpsArray = bumps.filter((b) => b.reason === 'direct');
|
|
663
|
+
const cascadeBumpsArray = bumps.filter((b) => b.reason === 'cascade');
|
|
664
|
+
// Sort bumps by name for consistent output
|
|
665
|
+
bumps.sort((a, b) => a.name.localeCompare(b.name));
|
|
666
|
+
return {
|
|
667
|
+
bumps,
|
|
668
|
+
directBumps: directBumpsArray,
|
|
669
|
+
cascadeBumps: cascadeBumpsArray,
|
|
670
|
+
totalAffected: bumps.length,
|
|
671
|
+
};
|
|
672
|
+
}
|
|
673
|
+
/**
|
|
674
|
+
* Determines if a cascade should propagate based on dependency type.
|
|
675
|
+
*
|
|
676
|
+
* @param workspace - Workspace containing the project
|
|
677
|
+
* @param dependent - Name of the dependent package
|
|
678
|
+
* @param dependency - Name of the dependency being bumped
|
|
679
|
+
* @param opts - Cascade bump options
|
|
680
|
+
* @returns True if the bump should cascade to this dependent
|
|
681
|
+
*/
|
|
682
|
+
function shouldCascade(workspace, dependent, dependency, opts) {
|
|
683
|
+
const project = workspace.projects.get(dependent);
|
|
684
|
+
if (!project) {
|
|
685
|
+
return false;
|
|
686
|
+
}
|
|
687
|
+
const pkg = project.packageJson;
|
|
688
|
+
// Check production dependencies (always cascades)
|
|
689
|
+
if (pkg.dependencies?.[dependency]) {
|
|
690
|
+
return true;
|
|
691
|
+
}
|
|
692
|
+
// Check dev dependencies
|
|
693
|
+
if (opts.includeDevDependencies && pkg.devDependencies?.[dependency]) {
|
|
694
|
+
return true;
|
|
695
|
+
}
|
|
696
|
+
// Check peer dependencies
|
|
697
|
+
if (opts.includePeerDependencies && pkg.peerDependencies?.[dependency]) {
|
|
698
|
+
return true;
|
|
699
|
+
}
|
|
700
|
+
return false;
|
|
701
|
+
}
|
|
702
|
+
/**
|
|
703
|
+
* Creates a planned bump for a project.
|
|
704
|
+
*
|
|
705
|
+
* @param project - Project to create bump plan for
|
|
706
|
+
* @param bumpType - Type of version bump to apply
|
|
707
|
+
* @param reason - Reason for the bump (direct, cascade, or sync)
|
|
708
|
+
* @param triggeredBy - List of packages that triggered this bump
|
|
709
|
+
* @param prereleaseId - Optional prerelease identifier
|
|
710
|
+
* @returns Planned bump object with version information
|
|
711
|
+
*/
|
|
712
|
+
function createPlannedBump(project, bumpType, reason, triggeredBy, prereleaseId) {
|
|
713
|
+
const parseResult = parseVersion(project.version);
|
|
714
|
+
if (!parseResult.success || !parseResult.version) {
|
|
715
|
+
throw createError(`Invalid version for ${project.name}: ${project.version}`);
|
|
716
|
+
}
|
|
717
|
+
const next = computeNextVersion(parseResult.version, bumpType, prereleaseId);
|
|
718
|
+
return {
|
|
719
|
+
name: project.name,
|
|
720
|
+
currentVersion: project.version,
|
|
721
|
+
nextVersion: format(next),
|
|
722
|
+
bumpType,
|
|
723
|
+
reason,
|
|
724
|
+
triggeredBy,
|
|
725
|
+
};
|
|
726
|
+
}
|
|
727
|
+
/**
|
|
728
|
+
* Computes the next version based on bump type.
|
|
729
|
+
*
|
|
730
|
+
* @param current - Current semantic version
|
|
731
|
+
* @param bumpType - Type of version bump to apply
|
|
732
|
+
* @param prereleaseId - Optional prerelease identifier
|
|
733
|
+
* @returns New semantic version after bump
|
|
734
|
+
*/
|
|
735
|
+
function computeNextVersion(current, bumpType, prereleaseId) {
|
|
736
|
+
if (bumpType === 'none') {
|
|
737
|
+
return current;
|
|
738
|
+
}
|
|
739
|
+
return increment(current, bumpType, prereleaseId);
|
|
740
|
+
}
|
|
741
|
+
/**
|
|
742
|
+
* Calculates cascade bumps starting from a single package.
|
|
743
|
+
*
|
|
744
|
+
* @param workspace - Workspace containing projects and dependency graph
|
|
745
|
+
* @param packageName - Package with direct changes
|
|
746
|
+
* @param bumpType - Type of bump for the direct change
|
|
747
|
+
* @param options - Configuration for cascade behavior
|
|
748
|
+
* @returns Cascade bump result
|
|
749
|
+
*/
|
|
750
|
+
function calculateCascadeBumpsFromPackage(workspace, packageName, bumpType, options = {}) {
|
|
751
|
+
return calculateCascadeBumps(workspace, [{ name: packageName, bumpType }], options);
|
|
752
|
+
}
|
|
753
|
+
/**
|
|
754
|
+
* Gets a summary of the cascade bump calculation.
|
|
755
|
+
*
|
|
756
|
+
* @param result - Result object from cascade bump calculation
|
|
757
|
+
* @returns Human-readable summary
|
|
758
|
+
*/
|
|
759
|
+
function summarizeCascadeBumps(result) {
|
|
760
|
+
if (result.totalAffected === 0) {
|
|
761
|
+
return 'No packages affected';
|
|
762
|
+
}
|
|
763
|
+
const lines = [];
|
|
764
|
+
lines.push(`${result.totalAffected} package(s) affected:`);
|
|
765
|
+
lines.push(` - ${result.directBumps.length} direct bump(s)`);
|
|
766
|
+
lines.push(` - ${result.cascadeBumps.length} cascade bump(s)`);
|
|
767
|
+
lines.push('');
|
|
768
|
+
lines.push('Planned bumps:');
|
|
769
|
+
for (const bump of result.bumps) {
|
|
770
|
+
const suffix = bump.reason === 'cascade' ? ` (triggered by ${bump.triggeredBy.join(', ')})` : '';
|
|
771
|
+
lines.push(` ${bump.name}: ${bump.currentVersion} -> ${bump.nextVersion} [${bump.bumpType}]${suffix}`);
|
|
772
|
+
}
|
|
773
|
+
return lines.join('\n');
|
|
774
|
+
}
|
|
775
|
+
|
|
776
|
+
/**
|
|
777
|
+
* Safe copies of JSON built-in methods.
|
|
778
|
+
*
|
|
779
|
+
* These references are captured at module initialization time to protect against
|
|
780
|
+
* prototype pollution attacks. Import only what you need for tree-shaking.
|
|
781
|
+
*
|
|
782
|
+
* @module @hyperfrontend/immutable-api-utils/built-in-copy/json
|
|
783
|
+
*/
|
|
784
|
+
// Capture references at module initialization time
|
|
785
|
+
const _JSON = globalThis.JSON;
|
|
786
|
+
/**
|
|
787
|
+
* (Safe copy) Converts a JavaScript Object Notation (JSON) string into an object.
|
|
788
|
+
*/
|
|
789
|
+
const parse = _JSON.parse;
|
|
790
|
+
/**
|
|
791
|
+
* (Safe copy) Converts a JavaScript value to a JavaScript Object Notation (JSON) string.
|
|
792
|
+
*/
|
|
793
|
+
const stringify = _JSON.stringify;
|
|
794
|
+
|
|
795
|
+
/**
|
|
796
|
+
* Safe copies of Console built-in methods.
|
|
797
|
+
*
|
|
798
|
+
* These references are captured at module initialization time to protect against
|
|
799
|
+
* prototype pollution attacks. Import only what you need for tree-shaking.
|
|
800
|
+
*
|
|
801
|
+
* @module @hyperfrontend/immutable-api-utils/built-in-copy/console
|
|
802
|
+
*/
|
|
803
|
+
// Capture references at module initialization time
|
|
804
|
+
const _console = globalThis.console;
|
|
805
|
+
/**
|
|
806
|
+
* (Safe copy) Outputs a message to the console.
|
|
807
|
+
*/
|
|
808
|
+
const log = _console.log.bind(_console);
|
|
809
|
+
/**
|
|
810
|
+
* (Safe copy) Outputs a warning message to the console.
|
|
811
|
+
*/
|
|
812
|
+
const warn = _console.warn.bind(_console);
|
|
813
|
+
/**
|
|
814
|
+
* (Safe copy) Outputs an error message to the console.
|
|
815
|
+
*/
|
|
816
|
+
const error = _console.error.bind(_console);
|
|
817
|
+
/**
|
|
818
|
+
* (Safe copy) Outputs an informational message to the console.
|
|
819
|
+
*/
|
|
820
|
+
const info = _console.info.bind(_console);
|
|
821
|
+
/**
|
|
822
|
+
* (Safe copy) Outputs a debug message to the console.
|
|
823
|
+
*/
|
|
824
|
+
const debug = _console.debug.bind(_console);
|
|
825
|
+
/**
|
|
826
|
+
* (Safe copy) Outputs a stack trace to the console.
|
|
827
|
+
*/
|
|
828
|
+
_console.trace.bind(_console);
|
|
829
|
+
/**
|
|
830
|
+
* (Safe copy) Displays an interactive listing of the properties of a specified object.
|
|
831
|
+
*/
|
|
832
|
+
_console.dir.bind(_console);
|
|
833
|
+
/**
|
|
834
|
+
* (Safe copy) Displays tabular data as a table.
|
|
835
|
+
*/
|
|
836
|
+
_console.table.bind(_console);
|
|
837
|
+
/**
|
|
838
|
+
* (Safe copy) Writes an error message to the console if the assertion is false.
|
|
839
|
+
*/
|
|
840
|
+
_console.assert.bind(_console);
|
|
841
|
+
/**
|
|
842
|
+
* (Safe copy) Clears the console.
|
|
843
|
+
*/
|
|
844
|
+
_console.clear.bind(_console);
|
|
845
|
+
/**
|
|
846
|
+
* (Safe copy) Logs the number of times that this particular call to count() has been called.
|
|
847
|
+
*/
|
|
848
|
+
_console.count.bind(_console);
|
|
849
|
+
/**
|
|
850
|
+
* (Safe copy) Resets the counter used with console.count().
|
|
851
|
+
*/
|
|
852
|
+
_console.countReset.bind(_console);
|
|
853
|
+
/**
|
|
854
|
+
* (Safe copy) Creates a new inline group in the console.
|
|
855
|
+
*/
|
|
856
|
+
_console.group.bind(_console);
|
|
857
|
+
/**
|
|
858
|
+
* (Safe copy) Creates a new inline group in the console that is initially collapsed.
|
|
859
|
+
*/
|
|
860
|
+
_console.groupCollapsed.bind(_console);
|
|
861
|
+
/**
|
|
862
|
+
* (Safe copy) Exits the current inline group.
|
|
863
|
+
*/
|
|
864
|
+
_console.groupEnd.bind(_console);
|
|
865
|
+
/**
|
|
866
|
+
* (Safe copy) Starts a timer with a name specified as an input parameter.
|
|
867
|
+
*/
|
|
868
|
+
_console.time.bind(_console);
|
|
869
|
+
/**
|
|
870
|
+
* (Safe copy) Stops a timer that was previously started.
|
|
871
|
+
*/
|
|
872
|
+
_console.timeEnd.bind(_console);
|
|
873
|
+
/**
|
|
874
|
+
* (Safe copy) Logs the current value of a timer that was previously started.
|
|
875
|
+
*/
|
|
876
|
+
_console.timeLog.bind(_console);
|
|
877
|
+
|
|
878
|
+
/**
|
|
879
|
+
* Safe copies of Array built-in static methods.
|
|
880
|
+
*
|
|
881
|
+
* These references are captured at module initialization time to protect against
|
|
882
|
+
* prototype pollution attacks. Import only what you need for tree-shaking.
|
|
883
|
+
*
|
|
884
|
+
* @module @hyperfrontend/immutable-api-utils/built-in-copy/array
|
|
885
|
+
*/
|
|
886
|
+
// Capture references at module initialization time
|
|
887
|
+
const _Array = globalThis.Array;
|
|
888
|
+
/**
|
|
889
|
+
* (Safe copy) Determines whether the passed value is an Array.
|
|
890
|
+
*/
|
|
891
|
+
const isArray = _Array.isArray;
|
|
892
|
+
|
|
893
|
+
const registeredClasses = [];
|
|
894
|
+
|
|
895
|
+
/**
|
|
896
|
+
* Returns the data type of the target.
|
|
897
|
+
* Uses native `typeof` operator, however, makes distinction between `null`, `array`, and `object`.
|
|
898
|
+
* Also, when classes are registered via `registerClass`, it checks if objects are instance of any known registered class.
|
|
899
|
+
*
|
|
900
|
+
* @param target - The target to get the data type of.
|
|
901
|
+
* @returns The data type of the target.
|
|
902
|
+
*/
|
|
903
|
+
const getType = (target) => {
|
|
904
|
+
if (target === null)
|
|
905
|
+
return 'null';
|
|
906
|
+
const nativeDataType = typeof target;
|
|
907
|
+
if (nativeDataType === 'object') {
|
|
908
|
+
if (isArray(target))
|
|
909
|
+
return 'array';
|
|
910
|
+
for (const registeredClass of registeredClasses) {
|
|
911
|
+
if (target instanceof registeredClass)
|
|
912
|
+
return registeredClass.name;
|
|
913
|
+
}
|
|
914
|
+
}
|
|
915
|
+
return nativeDataType;
|
|
916
|
+
};
|
|
917
|
+
|
|
918
|
+
/**
|
|
919
|
+
* Safe copies of Math built-in methods.
|
|
920
|
+
*
|
|
921
|
+
* These references are captured at module initialization time to protect against
|
|
922
|
+
* prototype pollution attacks. Import only what you need for tree-shaking.
|
|
923
|
+
*
|
|
924
|
+
* @module @hyperfrontend/immutable-api-utils/built-in-copy/math
|
|
925
|
+
*/
|
|
926
|
+
// Capture references at module initialization time
|
|
927
|
+
const _Math = globalThis.Math;
|
|
928
|
+
// ============================================================================
|
|
929
|
+
// Min/Max
|
|
930
|
+
// ============================================================================
|
|
931
|
+
/**
|
|
932
|
+
* (Safe copy) Returns the larger of zero or more numbers.
|
|
933
|
+
*/
|
|
934
|
+
const max = _Math.max;
|
|
935
|
+
|
|
936
|
+
/* eslint-disable @typescript-eslint/no-explicit-any */
|
|
937
|
+
/**
|
|
938
|
+
* Creates a wrapper function that only executes the wrapped function if the condition function returns true.
|
|
939
|
+
*
|
|
940
|
+
* @param func - The function to be conditionally executed.
|
|
941
|
+
* @param conditionFunc - A function that returns a boolean, determining if `func` should be executed.
|
|
942
|
+
* @returns A wrapped version of `func` that executes conditionally.
|
|
943
|
+
*/
|
|
944
|
+
function createConditionalExecutionFunction(func, conditionFunc) {
|
|
945
|
+
return function (...args) {
|
|
946
|
+
if (conditionFunc()) {
|
|
947
|
+
return func(...args);
|
|
948
|
+
}
|
|
949
|
+
};
|
|
950
|
+
}
|
|
951
|
+
|
|
952
|
+
/* eslint-disable @typescript-eslint/no-explicit-any */
|
|
953
|
+
/**
|
|
954
|
+
* Creates a wrapper function that silently ignores any errors thrown by the wrapped void function.
|
|
955
|
+
* This function is specifically for wrapping functions that do not return a value (void functions).
|
|
956
|
+
* Exceptions are swallowed without any logging or handling.
|
|
957
|
+
*
|
|
958
|
+
* @param func - The void function to be wrapped.
|
|
959
|
+
* @returns A wrapped version of the input function that ignores errors.
|
|
960
|
+
*/
|
|
961
|
+
function createErrorIgnoringFunction(func) {
|
|
962
|
+
return function (...args) {
|
|
963
|
+
try {
|
|
964
|
+
func(...args);
|
|
965
|
+
}
|
|
966
|
+
catch {
|
|
967
|
+
// Deliberately swallowing/ignoring the exception
|
|
968
|
+
}
|
|
969
|
+
};
|
|
970
|
+
}
|
|
971
|
+
|
|
972
|
+
/* eslint-disable @typescript-eslint/no-unused-vars */
|
|
973
|
+
/**
|
|
974
|
+
* A no-operation function (noop) that does nothing regardless of the arguments passed.
|
|
975
|
+
* It is designed to be as permissive as possible in its typing without using the `Function` keyword.
|
|
976
|
+
*
|
|
977
|
+
* @param args - Any arguments passed to the function (ignored)
|
|
978
|
+
*/
|
|
979
|
+
const noop = (...args) => {
|
|
980
|
+
// Intentionally does nothing
|
|
981
|
+
};
|
|
982
|
+
|
|
983
|
+
const logLevels = ['none', 'error', 'warn', 'log', 'info', 'debug'];
|
|
984
|
+
const priority = {
|
|
985
|
+
error: 4,
|
|
986
|
+
warn: 3,
|
|
987
|
+
log: 2,
|
|
988
|
+
info: 1,
|
|
989
|
+
debug: 0,
|
|
990
|
+
};
|
|
991
|
+
/**
|
|
992
|
+
* Validates whether a given string is a valid log level.
|
|
993
|
+
*
|
|
994
|
+
* @param level - The log level to validate
|
|
995
|
+
* @returns True if the level is valid, false otherwise
|
|
996
|
+
*/
|
|
997
|
+
function isValidLogLevel(level) {
|
|
998
|
+
return logLevels.includes(level);
|
|
999
|
+
}
|
|
1000
|
+
/**
|
|
1001
|
+
* Creates a log level configuration manager for controlling logging behavior.
|
|
1002
|
+
* Provides methods to get, set, and evaluate log levels based on priority.
|
|
1003
|
+
*
|
|
1004
|
+
* @param level - The initial log level (defaults to 'error')
|
|
1005
|
+
* @returns A configuration object with log level management methods
|
|
1006
|
+
* @throws {Error} When the provided level is not a valid log level
|
|
1007
|
+
*/
|
|
1008
|
+
function createLogLevelConfig(level = 'error') {
|
|
1009
|
+
if (!isValidLogLevel(level)) {
|
|
1010
|
+
throw createError('Cannot create log level configuration with a valid default log level');
|
|
1011
|
+
}
|
|
1012
|
+
const state = { level };
|
|
1013
|
+
const getLogLevel = () => state.level;
|
|
1014
|
+
const setLogLevel = (level) => {
|
|
1015
|
+
if (!isValidLogLevel(level)) {
|
|
1016
|
+
throw createError(`Cannot set value '${level}' level. Expected levels are ${logLevels}.`);
|
|
1017
|
+
}
|
|
1018
|
+
state.level = level;
|
|
1019
|
+
};
|
|
1020
|
+
const shouldLog = (level) => {
|
|
1021
|
+
if (state.level === 'none' || level === 'none' || !isValidLogLevel(level)) {
|
|
1022
|
+
return false;
|
|
1023
|
+
}
|
|
1024
|
+
return priority[level] >= priority[state.level];
|
|
1025
|
+
};
|
|
1026
|
+
return freeze({
|
|
1027
|
+
getLogLevel,
|
|
1028
|
+
setLogLevel,
|
|
1029
|
+
shouldLog,
|
|
1030
|
+
});
|
|
1031
|
+
}
|
|
1032
|
+
|
|
1033
|
+
/**
|
|
1034
|
+
* Creates a logger instance with configurable log level filtering.
|
|
1035
|
+
* Each log function is wrapped to respect the current log level setting.
|
|
1036
|
+
*
|
|
1037
|
+
* @param error - Function to handle error-level logs (required)
|
|
1038
|
+
* @param warn - Function to handle warning-level logs (optional, defaults to noop)
|
|
1039
|
+
* @param log - Function to handle standard logs (optional, defaults to noop)
|
|
1040
|
+
* @param info - Function to handle info-level logs (optional, defaults to noop)
|
|
1041
|
+
* @param debug - Function to handle debug-level logs (optional, defaults to noop)
|
|
1042
|
+
* @returns A frozen logger object with log methods and level control
|
|
1043
|
+
* @throws {ErrorLevelFn} When any provided log function is invalid
|
|
1044
|
+
*/
|
|
1045
|
+
function createLogger(error, warn = noop, log = noop, info = noop, debug = noop) {
|
|
1046
|
+
if (notValidLogFn(error)) {
|
|
1047
|
+
throw createError(notFnMsg('error'));
|
|
1048
|
+
}
|
|
1049
|
+
if (notValidLogFn(warn)) {
|
|
1050
|
+
throw createError(notFnMsg('warn'));
|
|
1051
|
+
}
|
|
1052
|
+
if (notValidLogFn(log)) {
|
|
1053
|
+
throw createError(notFnMsg('log'));
|
|
1054
|
+
}
|
|
1055
|
+
if (notValidLogFn(info)) {
|
|
1056
|
+
throw createError(notFnMsg('info'));
|
|
1057
|
+
}
|
|
1058
|
+
if (notValidLogFn(debug)) {
|
|
1059
|
+
throw createError(notFnMsg('debug'));
|
|
1060
|
+
}
|
|
1061
|
+
const { setLogLevel, getLogLevel, shouldLog } = createLogLevelConfig();
|
|
1062
|
+
const wrapLogFn = (fn, level) => {
|
|
1063
|
+
if (fn === noop)
|
|
1064
|
+
return fn;
|
|
1065
|
+
const condition = () => shouldLog(level);
|
|
1066
|
+
return createConditionalExecutionFunction(createErrorIgnoringFunction(fn), condition);
|
|
1067
|
+
};
|
|
1068
|
+
return freeze({
|
|
1069
|
+
error: wrapLogFn(error, 'error'),
|
|
1070
|
+
warn: wrapLogFn(warn, 'warn'),
|
|
1071
|
+
log: wrapLogFn(log, 'log'),
|
|
1072
|
+
info: wrapLogFn(info, 'info'),
|
|
1073
|
+
debug: wrapLogFn(debug, 'debug'),
|
|
1074
|
+
setLogLevel,
|
|
1075
|
+
getLogLevel,
|
|
1076
|
+
});
|
|
1077
|
+
}
|
|
1078
|
+
/**
|
|
1079
|
+
* Validates whether a given value is a valid log function.
|
|
1080
|
+
*
|
|
1081
|
+
* @param fn - The value to validate
|
|
1082
|
+
* @returns True if the value is not a function (invalid), false if it is valid
|
|
1083
|
+
*/
|
|
1084
|
+
function notValidLogFn(fn) {
|
|
1085
|
+
return getType(fn) !== 'function' && fn !== noop;
|
|
1086
|
+
}
|
|
1087
|
+
/**
|
|
1088
|
+
* Generates an error message for invalid log function parameters.
|
|
1089
|
+
*
|
|
1090
|
+
* @param label - The name of the log function that failed validation
|
|
1091
|
+
* @returns A formatted error message string
|
|
1092
|
+
*/
|
|
1093
|
+
function notFnMsg(label) {
|
|
1094
|
+
return `Cannot create a logger when ${label} is not a function`;
|
|
1095
|
+
}
|
|
1096
|
+
|
|
1097
|
+
createLogger(error, warn, log, info, debug);
|
|
1098
|
+
|
|
1099
|
+
/**
|
|
1100
|
+
* Global log level registry.
|
|
1101
|
+
* Tracks all created scoped loggers to allow global log level changes.
|
|
1102
|
+
*/
|
|
1103
|
+
const loggerRegistry = createSet();
|
|
1104
|
+
/** Redacted placeholder for sensitive values */
|
|
1105
|
+
const REDACTED = '[REDACTED]';
|
|
1106
|
+
/**
|
|
1107
|
+
* Patterns that indicate a sensitive key name.
|
|
1108
|
+
* Keys containing these patterns will have their values sanitized.
|
|
1109
|
+
*/
|
|
1110
|
+
const SENSITIVE_KEY_PATTERNS = [
|
|
1111
|
+
/token/i,
|
|
1112
|
+
/key/i,
|
|
1113
|
+
/password/i,
|
|
1114
|
+
/secret/i,
|
|
1115
|
+
/credential/i,
|
|
1116
|
+
/auth/i,
|
|
1117
|
+
/bearer/i,
|
|
1118
|
+
/api[_-]?key/i,
|
|
1119
|
+
/private/i,
|
|
1120
|
+
/passphrase/i,
|
|
1121
|
+
];
|
|
1122
|
+
/**
|
|
1123
|
+
* Checks if a key name indicates sensitive data.
|
|
1124
|
+
*
|
|
1125
|
+
* @param key - Key name to check
|
|
1126
|
+
* @returns True if the key indicates sensitive data
|
|
1127
|
+
*/
|
|
1128
|
+
function isSensitiveKey(key) {
|
|
1129
|
+
return SENSITIVE_KEY_PATTERNS.some((pattern) => pattern.test(key));
|
|
1130
|
+
}
|
|
1131
|
+
/**
|
|
1132
|
+
* Sanitizes an object by replacing sensitive values with REDACTED.
|
|
1133
|
+
* This function recursively processes nested objects and arrays.
|
|
1134
|
+
*
|
|
1135
|
+
* @param obj - Object to sanitize
|
|
1136
|
+
* @returns New object with sensitive values redacted
|
|
1137
|
+
*/
|
|
1138
|
+
function sanitize(obj) {
|
|
1139
|
+
if (obj === null || obj === undefined) {
|
|
1140
|
+
return obj;
|
|
1141
|
+
}
|
|
1142
|
+
if (isArray(obj)) {
|
|
1143
|
+
return obj.map((item) => sanitize(item));
|
|
1144
|
+
}
|
|
1145
|
+
if (typeof obj === 'object') {
|
|
1146
|
+
const result = {};
|
|
1147
|
+
for (const [key, value] of entries(obj)) {
|
|
1148
|
+
if (isSensitiveKey(key)) {
|
|
1149
|
+
result[key] = REDACTED;
|
|
1150
|
+
}
|
|
1151
|
+
else if (typeof value === 'object' && value !== null) {
|
|
1152
|
+
result[key] = sanitize(value);
|
|
1153
|
+
}
|
|
1154
|
+
else {
|
|
1155
|
+
result[key] = value;
|
|
1156
|
+
}
|
|
1157
|
+
}
|
|
1158
|
+
return result;
|
|
1159
|
+
}
|
|
1160
|
+
return obj;
|
|
1161
|
+
}
|
|
1162
|
+
/**
|
|
1163
|
+
* Formats a log message with optional metadata.
|
|
1164
|
+
*
|
|
1165
|
+
* @param namespace - Logger namespace prefix
|
|
1166
|
+
* @param message - Log message
|
|
1167
|
+
* @param meta - Optional metadata object
|
|
1168
|
+
* @returns Formatted log string
|
|
1169
|
+
*/
|
|
1170
|
+
function formatMessage(namespace, message, meta) {
|
|
1171
|
+
const prefix = `[${namespace}]`;
|
|
1172
|
+
if (meta && keys(meta).length > 0) {
|
|
1173
|
+
const sanitizedMeta = sanitize(meta);
|
|
1174
|
+
return `${prefix} ${message} ${stringify(sanitizedMeta)}`;
|
|
1175
|
+
}
|
|
1176
|
+
return `${prefix} ${message}`;
|
|
1177
|
+
}
|
|
1178
|
+
/**
|
|
1179
|
+
* Creates a scoped logger with namespace prefix and optional secret sanitization.
|
|
1180
|
+
* All log messages will be prefixed with [namespace] and sensitive metadata
|
|
1181
|
+
* values will be automatically redacted.
|
|
1182
|
+
*
|
|
1183
|
+
* @param namespace - Logger namespace (e.g., 'project-scope', 'analyze')
|
|
1184
|
+
* @param options - Logger configuration options
|
|
1185
|
+
* @returns A configured scoped logger instance
|
|
1186
|
+
*
|
|
1187
|
+
* @example
|
|
1188
|
+
* ```typescript
|
|
1189
|
+
* const logger = createScopedLogger('project-scope')
|
|
1190
|
+
* logger.setLogLevel('debug')
|
|
1191
|
+
*
|
|
1192
|
+
* // Basic logging
|
|
1193
|
+
* logger.info('Starting analysis', { path: './project' })
|
|
1194
|
+
*
|
|
1195
|
+
* // Sensitive data is automatically redacted
|
|
1196
|
+
* logger.debug('Config loaded', { apiKey: 'secret123' })
|
|
1197
|
+
* // Output: [project-scope] Config loaded {"apiKey":"[REDACTED]"}
|
|
1198
|
+
* ```
|
|
1199
|
+
*/
|
|
1200
|
+
function createScopedLogger(namespace, options = {}) {
|
|
1201
|
+
const { level = 'error', sanitizeSecrets = true } = options;
|
|
1202
|
+
// Create wrapper functions that add namespace prefix and sanitization
|
|
1203
|
+
const createLogFn = (baseFn) => (message, meta) => {
|
|
1204
|
+
const processedMeta = sanitizeSecrets && meta ? sanitize(meta) : meta;
|
|
1205
|
+
baseFn(formatMessage(namespace, message, processedMeta));
|
|
1206
|
+
};
|
|
1207
|
+
// Create base logger with wrapped functions
|
|
1208
|
+
const baseLogger = createLogger(createLogFn(error), createLogFn(warn), createLogFn(log), createLogFn(info), createLogFn(debug));
|
|
1209
|
+
// Set initial log level (use global override if set)
|
|
1210
|
+
baseLogger.setLogLevel(level);
|
|
1211
|
+
const scopedLogger = freeze({
|
|
1212
|
+
error: (message, meta) => baseLogger.error(message, meta),
|
|
1213
|
+
warn: (message, meta) => baseLogger.warn(message, meta),
|
|
1214
|
+
log: (message, meta) => baseLogger.log(message, meta),
|
|
1215
|
+
info: (message, meta) => baseLogger.info(message, meta),
|
|
1216
|
+
debug: (message, meta) => baseLogger.debug(message, meta),
|
|
1217
|
+
setLogLevel: baseLogger.setLogLevel,
|
|
1218
|
+
getLogLevel: baseLogger.getLogLevel,
|
|
1219
|
+
});
|
|
1220
|
+
// Register logger for global level management
|
|
1221
|
+
loggerRegistry.add(scopedLogger);
|
|
1222
|
+
return scopedLogger;
|
|
1223
|
+
}
|
|
1224
|
+
/**
|
|
1225
|
+
* Default logger instance for the project-scope library.
|
|
1226
|
+
* Use this for general logging within the library.
|
|
1227
|
+
*
|
|
1228
|
+
* @example
|
|
1229
|
+
* ```typescript
|
|
1230
|
+
* import { logger } from '@hyperfrontend/project-scope/core'
|
|
1231
|
+
*
|
|
1232
|
+
* logger.setLogLevel('debug')
|
|
1233
|
+
* logger.debug('Analyzing project', { path: './src' })
|
|
1234
|
+
* ```
|
|
1235
|
+
*/
|
|
1236
|
+
createScopedLogger('project-scope');
|
|
1237
|
+
|
|
1238
|
+
const fsLogger = createScopedLogger('project-scope:fs');
|
|
1239
|
+
/**
|
|
1240
|
+
* Create a file system error with code and context.
|
|
1241
|
+
*
|
|
1242
|
+
* @param message - The error message describing what went wrong
|
|
1243
|
+
* @param code - The category code for this type of filesystem failure
|
|
1244
|
+
* @param context - Additional context including path, operation, and cause
|
|
1245
|
+
* @returns A configured Error object with code and context properties
|
|
1246
|
+
*/
|
|
1247
|
+
function createFileSystemError(message, code, context) {
|
|
1248
|
+
const error = createError(message);
|
|
1249
|
+
defineProperties(error, {
|
|
1250
|
+
code: { value: code, enumerable: true },
|
|
1251
|
+
context: { value: context, enumerable: true },
|
|
1252
|
+
});
|
|
1253
|
+
return error;
|
|
1254
|
+
}
|
|
1255
|
+
/**
|
|
1256
|
+
* Read file contents as string.
|
|
1257
|
+
*
|
|
1258
|
+
* @param filePath - Path to file
|
|
1259
|
+
* @param encoding - File encoding (default: utf-8)
|
|
1260
|
+
* @returns File contents as string
|
|
1261
|
+
* @throws {Error} If file doesn't exist or can't be read
|
|
1262
|
+
*
|
|
1263
|
+
* @example
|
|
1264
|
+
* ```typescript
|
|
1265
|
+
* import { readFileContent } from '@hyperfrontend/project-scope'
|
|
1266
|
+
*
|
|
1267
|
+
* const content = readFileContent('./package.json')
|
|
1268
|
+
* console.log(content) // JSON string
|
|
1269
|
+
* ```
|
|
1270
|
+
*/
|
|
1271
|
+
function readFileContent(filePath, encoding = 'utf-8') {
|
|
1272
|
+
if (!node_fs.existsSync(filePath)) {
|
|
1273
|
+
fsLogger.debug('File not found', { path: filePath });
|
|
1274
|
+
throw createFileSystemError(`File not found: ${filePath}`, 'FS_NOT_FOUND', { path: filePath, operation: 'read' });
|
|
1275
|
+
}
|
|
1276
|
+
try {
|
|
1277
|
+
return node_fs.readFileSync(filePath, { encoding });
|
|
1278
|
+
}
|
|
1279
|
+
catch (error) {
|
|
1280
|
+
fsLogger.warn('Failed to read file', { path: filePath });
|
|
1281
|
+
throw createFileSystemError(`Failed to read file: ${filePath}`, 'FS_READ_ERROR', { path: filePath, operation: 'read', cause: error });
|
|
1282
|
+
}
|
|
1283
|
+
}
|
|
1284
|
+
|
|
1285
|
+
const fsWriteLogger = createScopedLogger('project-scope:fs:write');
|
|
1286
|
+
/**
|
|
1287
|
+
* Ensure directory exists, create recursively if not.
|
|
1288
|
+
*
|
|
1289
|
+
* @param dirPath - Directory path to ensure exists
|
|
1290
|
+
*/
|
|
1291
|
+
function ensureDir(dirPath) {
|
|
1292
|
+
if (!node_fs.existsSync(dirPath)) {
|
|
1293
|
+
fsWriteLogger.debug('Creating directory recursively', { path: dirPath });
|
|
1294
|
+
node_fs.mkdirSync(dirPath, { recursive: true });
|
|
1295
|
+
}
|
|
1296
|
+
}
|
|
1297
|
+
/**
|
|
1298
|
+
* Write string content to file.
|
|
1299
|
+
* Creates parent directories if needed.
|
|
1300
|
+
*
|
|
1301
|
+
* @param filePath - Absolute or relative path to the target file
|
|
1302
|
+
* @param content - String content to write to the file
|
|
1303
|
+
* @param options - Optional write configuration (encoding, mode)
|
|
1304
|
+
* @throws {Error} If write fails
|
|
1305
|
+
*
|
|
1306
|
+
* @example
|
|
1307
|
+
* ```typescript
|
|
1308
|
+
* import { writeFileContent } from '@hyperfrontend/project-scope'
|
|
1309
|
+
*
|
|
1310
|
+
* // Write a simple text file
|
|
1311
|
+
* writeFileContent('./output/data.txt', 'Hello, World!')
|
|
1312
|
+
*
|
|
1313
|
+
* // Write with custom encoding
|
|
1314
|
+
* writeFileContent('./output/data.txt', 'Content', { encoding: 'utf-8' })
|
|
1315
|
+
* ```
|
|
1316
|
+
*/
|
|
1317
|
+
function writeFileContent(filePath, content, options) {
|
|
1318
|
+
try {
|
|
1319
|
+
fsWriteLogger.debug('Writing file content', { path: filePath, size: content.length, encoding: options?.encoding ?? 'utf-8' });
|
|
1320
|
+
ensureDir(node_path.dirname(filePath));
|
|
1321
|
+
node_fs.writeFileSync(filePath, content, {
|
|
1322
|
+
encoding: options?.encoding ?? 'utf-8',
|
|
1323
|
+
mode: options?.mode,
|
|
1324
|
+
});
|
|
1325
|
+
fsWriteLogger.debug('File written successfully', { path: filePath });
|
|
1326
|
+
}
|
|
1327
|
+
catch (error) {
|
|
1328
|
+
fsWriteLogger.warn('Failed to write file', { path: filePath, error: error instanceof Error ? error.message : String(error) });
|
|
1329
|
+
throw createFileSystemError(`Failed to write file: ${filePath}`, 'FS_WRITE_ERROR', { path: filePath, operation: 'write', cause: error });
|
|
1330
|
+
}
|
|
1331
|
+
}
|
|
1332
|
+
|
|
1333
|
+
createScopedLogger('project-scope:fs:dir');
|
|
1334
|
+
|
|
1335
|
+
createScopedLogger('project-scope:fs:traversal');
|
|
1336
|
+
|
|
1337
|
+
createScopedLogger('project-scope:project:package');
|
|
1338
|
+
|
|
1339
|
+
createScopedLogger('project-scope:heuristics:deps');
|
|
1340
|
+
|
|
1341
|
+
/**
|
|
1342
|
+
* Global registry of all caches for bulk operations.
|
|
1343
|
+
*/
|
|
1344
|
+
const cacheRegistry = createSet();
|
|
1345
|
+
/**
|
|
1346
|
+
* Create a cache with optional TTL and size limits.
|
|
1347
|
+
*
|
|
1348
|
+
* The cache provides a simple key-value store with:
|
|
1349
|
+
* - Optional TTL (time-to-live) for automatic expiration
|
|
1350
|
+
* - Optional maxSize for limiting cache size with FIFO eviction
|
|
1351
|
+
* - Lazy expiration (entries are checked on access)
|
|
1352
|
+
*
|
|
1353
|
+
* @param options - Cache configuration options
|
|
1354
|
+
* @returns Cache instance
|
|
1355
|
+
*
|
|
1356
|
+
* @example
|
|
1357
|
+
* ```typescript
|
|
1358
|
+
* // Basic cache
|
|
1359
|
+
* const cache = createCache<string, number>()
|
|
1360
|
+
* cache.set('answer', 42)
|
|
1361
|
+
* cache.get('answer') // 42
|
|
1362
|
+
*
|
|
1363
|
+
* // Cache with TTL (expires after 60 seconds)
|
|
1364
|
+
* const ttlCache = createCache<string, object>({ ttl: 60000 })
|
|
1365
|
+
*
|
|
1366
|
+
* // Cache with max size (evicts oldest when full)
|
|
1367
|
+
* const lruCache = createCache<string, object>({ maxSize: 100 })
|
|
1368
|
+
*
|
|
1369
|
+
* // Combined options
|
|
1370
|
+
* const configCache = createCache<string, object>({
|
|
1371
|
+
* ttl: 30000,
|
|
1372
|
+
* maxSize: 50
|
|
1373
|
+
* })
|
|
1374
|
+
* ```
|
|
1375
|
+
*/
|
|
1376
|
+
function createCache(options) {
|
|
1377
|
+
const { ttl, maxSize } = options ?? {};
|
|
1378
|
+
const store = createMap();
|
|
1379
|
+
// Track insertion order for FIFO eviction
|
|
1380
|
+
const insertionOrder = [];
|
|
1381
|
+
/**
|
|
1382
|
+
* Check if an entry is expired.
|
|
1383
|
+
*
|
|
1384
|
+
* @param entry - Cache entry to check
|
|
1385
|
+
* @returns True if entry is expired
|
|
1386
|
+
*/
|
|
1387
|
+
function isExpired(entry) {
|
|
1388
|
+
if (ttl === undefined)
|
|
1389
|
+
return false;
|
|
1390
|
+
// eslint-disable-next-line workspace/no-unsafe-builtin-methods -- Date.now() is needed for Jest fake timers compatibility
|
|
1391
|
+
return Date.now() - entry.timestamp > ttl;
|
|
1392
|
+
}
|
|
1393
|
+
/**
|
|
1394
|
+
* Evict oldest entries to make room for new ones.
|
|
1395
|
+
*/
|
|
1396
|
+
function evictIfNeeded() {
|
|
1397
|
+
if (maxSize === undefined)
|
|
1398
|
+
return;
|
|
1399
|
+
while (store.size >= maxSize && insertionOrder.length > 0) {
|
|
1400
|
+
const oldestKey = insertionOrder.shift();
|
|
1401
|
+
if (oldestKey !== undefined) {
|
|
1402
|
+
store.delete(oldestKey);
|
|
1403
|
+
}
|
|
1404
|
+
}
|
|
1405
|
+
}
|
|
1406
|
+
/**
|
|
1407
|
+
* Remove key from insertion order tracking.
|
|
1408
|
+
*
|
|
1409
|
+
* @param key - Key to remove from order tracking
|
|
1410
|
+
*/
|
|
1411
|
+
function removeFromOrder(key) {
|
|
1412
|
+
const index = insertionOrder.indexOf(key);
|
|
1413
|
+
if (index !== -1) {
|
|
1414
|
+
insertionOrder.splice(index, 1);
|
|
1415
|
+
}
|
|
1416
|
+
}
|
|
1417
|
+
const cache = {
|
|
1418
|
+
get(key) {
|
|
1419
|
+
const entry = store.get(key);
|
|
1420
|
+
if (!entry)
|
|
1421
|
+
return undefined;
|
|
1422
|
+
if (isExpired(entry)) {
|
|
1423
|
+
store.delete(key);
|
|
1424
|
+
removeFromOrder(key);
|
|
1425
|
+
return undefined;
|
|
1426
|
+
}
|
|
1427
|
+
return entry.value;
|
|
1428
|
+
},
|
|
1429
|
+
set(key, value) {
|
|
1430
|
+
// If key exists, remove from order first
|
|
1431
|
+
if (store.has(key)) {
|
|
1432
|
+
removeFromOrder(key);
|
|
1433
|
+
}
|
|
1434
|
+
else {
|
|
1435
|
+
// Evict if needed before adding new entry
|
|
1436
|
+
evictIfNeeded();
|
|
1437
|
+
}
|
|
1438
|
+
// eslint-disable-next-line workspace/no-unsafe-builtin-methods -- Date.now() is needed for Jest fake timers compatibility
|
|
1439
|
+
store.set(key, { value, timestamp: Date.now() });
|
|
1440
|
+
insertionOrder.push(key);
|
|
1441
|
+
},
|
|
1442
|
+
has(key) {
|
|
1443
|
+
const entry = store.get(key);
|
|
1444
|
+
if (!entry)
|
|
1445
|
+
return false;
|
|
1446
|
+
if (isExpired(entry)) {
|
|
1447
|
+
store.delete(key);
|
|
1448
|
+
removeFromOrder(key);
|
|
1449
|
+
return false;
|
|
1450
|
+
}
|
|
1451
|
+
return true;
|
|
1452
|
+
},
|
|
1453
|
+
delete(key) {
|
|
1454
|
+
removeFromOrder(key);
|
|
1455
|
+
return store.delete(key);
|
|
1456
|
+
},
|
|
1457
|
+
clear() {
|
|
1458
|
+
store.clear();
|
|
1459
|
+
insertionOrder.length = 0;
|
|
1460
|
+
},
|
|
1461
|
+
size() {
|
|
1462
|
+
return store.size;
|
|
1463
|
+
},
|
|
1464
|
+
keys() {
|
|
1465
|
+
return [...insertionOrder];
|
|
1466
|
+
},
|
|
1467
|
+
};
|
|
1468
|
+
// Register cache for global operations
|
|
1469
|
+
cacheRegistry.add(cache);
|
|
1470
|
+
return freeze(cache);
|
|
1471
|
+
}
|
|
1472
|
+
|
|
1473
|
+
createScopedLogger('project-scope:project:walk');
|
|
1474
|
+
|
|
1475
|
+
createScopedLogger('project-scope:project:search');
|
|
1476
|
+
|
|
1477
|
+
createScopedLogger('project-scope:heuristics:entry-points');
|
|
1478
|
+
/**
|
|
1479
|
+
* Cache for entry point discovery results.
|
|
1480
|
+
* TTL: 60 seconds (entry points are relatively stable)
|
|
1481
|
+
*/
|
|
1482
|
+
createCache({ ttl: 60000, maxSize: 50 });
|
|
1483
|
+
|
|
1484
|
+
createScopedLogger('project-scope:tech');
|
|
1485
|
+
/**
|
|
1486
|
+
* Cache for tech detection results.
|
|
1487
|
+
* TTL: 60 seconds (tech stack can change during active development)
|
|
1488
|
+
*/
|
|
1489
|
+
createCache({ ttl: 60000, maxSize: 50 });
|
|
1490
|
+
|
|
1491
|
+
createScopedLogger('project-scope:heuristics:project-type');
|
|
1492
|
+
|
|
1493
|
+
createScopedLogger('project-scope:root');
|
|
1494
|
+
|
|
1495
|
+
createScopedLogger('project-scope:nx');
|
|
1496
|
+
|
|
1497
|
+
createScopedLogger('project-scope:nx:devkit');
|
|
1498
|
+
|
|
1499
|
+
createScopedLogger('project-scope:nx:config');
|
|
1500
|
+
|
|
1501
|
+
createScopedLogger('project-scope:config');
|
|
1502
|
+
/**
|
|
1503
|
+
* Cache for config detection results.
|
|
1504
|
+
* TTL: 30 seconds (configs can change frequently during setup)
|
|
1505
|
+
*/
|
|
1506
|
+
createCache({ ttl: 30000, maxSize: 50 });
|
|
1507
|
+
|
|
1508
|
+
/** Logger for analysis operations */
|
|
1509
|
+
createScopedLogger('project-scope:analyze');
|
|
1510
|
+
|
|
1511
|
+
/** Logger for CLI operations */
|
|
1512
|
+
createScopedLogger('project-scope:cli');
|
|
1513
|
+
|
|
1514
|
+
createScopedLogger('project-scope:encoding');
|
|
1515
|
+
|
|
1516
|
+
createScopedLogger('project-scope:encoding:convert');
|
|
1517
|
+
|
|
1518
|
+
createScopedLogger('project-scope:heuristics:framework');
|
|
1519
|
+
/**
|
|
1520
|
+
* Cache for framework identification results.
|
|
1521
|
+
* TTL: 60 seconds (frameworks are stable but can change during development)
|
|
1522
|
+
*/
|
|
1523
|
+
createCache({ ttl: 60000, maxSize: 50 });
|
|
1524
|
+
|
|
1525
|
+
createScopedLogger('project-scope:vfs:tree');
|
|
1526
|
+
|
|
1527
|
+
createScopedLogger('project-scope:vfs:factory');
|
|
1528
|
+
|
|
1529
|
+
createScopedLogger('project-scope:vfs');
|
|
1530
|
+
|
|
1531
|
+
createScopedLogger('project-scope:vfs:diff');
|
|
1532
|
+
|
|
1533
|
+
/**
|
|
1534
|
+
* Batch Update
|
|
1535
|
+
*
|
|
1536
|
+
* Utilities for updating multiple packages at once.
|
|
1537
|
+
* Supports updating versions, dependencies, and other package.json fields.
|
|
1538
|
+
*/
|
|
1539
|
+
/**
|
|
1540
|
+
* Default batch update options.
|
|
1541
|
+
*/
|
|
1542
|
+
const DEFAULT_BATCH_UPDATE_OPTIONS = {
|
|
1543
|
+
dryRun: false,
|
|
1544
|
+
updateDependencyReferences: true,
|
|
1545
|
+
};
|
|
1546
|
+
/**
|
|
1547
|
+
* Applies planned bumps to the workspace.
|
|
1548
|
+
* Updates package.json version fields for all affected packages.
|
|
1549
|
+
*
|
|
1550
|
+
* @param workspace - Workspace containing projects to update
|
|
1551
|
+
* @param bumps - Planned version bumps
|
|
1552
|
+
* @param options - Update options
|
|
1553
|
+
* @returns Batch update result
|
|
1554
|
+
*
|
|
1555
|
+
* @example
|
|
1556
|
+
* ```typescript
|
|
1557
|
+
* import { applyBumps, calculateCascadeBumps } from '@hyperfrontend/versioning'
|
|
1558
|
+
*
|
|
1559
|
+
* const cascadeResult = calculateCascadeBumps(workspace, directBumps)
|
|
1560
|
+
* const updateResult = applyBumps(workspace, cascadeResult.bumps)
|
|
1561
|
+
*
|
|
1562
|
+
* if (updateResult.success) {
|
|
1563
|
+
* console.log(`Updated ${updateResult.updated.length} packages`)
|
|
1564
|
+
* } else {
|
|
1565
|
+
* console.error('Some updates failed:', updateResult.failed)
|
|
1566
|
+
* }
|
|
1567
|
+
* ```
|
|
1568
|
+
*/
|
|
1569
|
+
function applyBumps(workspace, bumps, options = {}) {
|
|
1570
|
+
const opts = { ...DEFAULT_BATCH_UPDATE_OPTIONS, ...options };
|
|
1571
|
+
const updated = [];
|
|
1572
|
+
const failed = [];
|
|
1573
|
+
// Build a map for dependency reference updates
|
|
1574
|
+
const versionUpdates = createMap();
|
|
1575
|
+
for (const bump of bumps) {
|
|
1576
|
+
versionUpdates.set(bump.name, bump.nextVersion);
|
|
1577
|
+
}
|
|
1578
|
+
for (const bump of bumps) {
|
|
1579
|
+
const project = workspace.projects.get(bump.name);
|
|
1580
|
+
if (!project) {
|
|
1581
|
+
failed.push({
|
|
1582
|
+
name: bump.name,
|
|
1583
|
+
packageJsonPath: '',
|
|
1584
|
+
error: 'Project not found in workspace',
|
|
1585
|
+
});
|
|
1586
|
+
continue;
|
|
1587
|
+
}
|
|
1588
|
+
try {
|
|
1589
|
+
if (!opts.dryRun) {
|
|
1590
|
+
updatePackageVersion(project.packageJsonPath, bump.nextVersion);
|
|
1591
|
+
}
|
|
1592
|
+
updated.push({
|
|
1593
|
+
name: bump.name,
|
|
1594
|
+
packageJsonPath: project.packageJsonPath,
|
|
1595
|
+
previousVersion: bump.currentVersion,
|
|
1596
|
+
newVersion: bump.nextVersion,
|
|
1597
|
+
});
|
|
1598
|
+
}
|
|
1599
|
+
catch (error) {
|
|
1600
|
+
failed.push({
|
|
1601
|
+
name: bump.name,
|
|
1602
|
+
packageJsonPath: project.packageJsonPath,
|
|
1603
|
+
error: error instanceof Error ? error.message : String(error),
|
|
1604
|
+
});
|
|
1605
|
+
}
|
|
1606
|
+
}
|
|
1607
|
+
// Update dependency references if requested
|
|
1608
|
+
if (opts.updateDependencyReferences && !opts.dryRun) {
|
|
1609
|
+
for (const project of workspace.projectList) {
|
|
1610
|
+
try {
|
|
1611
|
+
updateDependencyReferences(project.packageJsonPath, versionUpdates);
|
|
1612
|
+
}
|
|
1613
|
+
catch {
|
|
1614
|
+
// Dependency reference updates are best-effort
|
|
1615
|
+
}
|
|
1616
|
+
}
|
|
1617
|
+
}
|
|
1618
|
+
return {
|
|
1619
|
+
updated,
|
|
1620
|
+
failed,
|
|
1621
|
+
total: bumps.length,
|
|
1622
|
+
success: failed.length === 0,
|
|
1623
|
+
};
|
|
1624
|
+
}
|
|
1625
|
+
/**
|
|
1626
|
+
* Updates the version field in a package.json file.
|
|
1627
|
+
*
|
|
1628
|
+
* @param packageJsonPath - Path to package.json
|
|
1629
|
+
* @param newVersion - New version string
|
|
1630
|
+
*/
|
|
1631
|
+
function updatePackageVersion(packageJsonPath, newVersion) {
|
|
1632
|
+
const content = readFileContent(packageJsonPath);
|
|
1633
|
+
const pkg = parse(content);
|
|
1634
|
+
pkg.version = newVersion;
|
|
1635
|
+
const formatted = stringify(pkg, null, 2) + '\n';
|
|
1636
|
+
writeFileContent(packageJsonPath, formatted);
|
|
1637
|
+
}
|
|
1638
|
+
/**
|
|
1639
|
+
* Updates dependency version references in a package.json file.
|
|
1640
|
+
*
|
|
1641
|
+
* @param packageJsonPath - Path to package.json
|
|
1642
|
+
* @param versionUpdates - Map of package name to new version
|
|
1643
|
+
*/
|
|
1644
|
+
function updateDependencyReferences(packageJsonPath, versionUpdates) {
|
|
1645
|
+
const content = readFileContent(packageJsonPath);
|
|
1646
|
+
const pkg = parse(content);
|
|
1647
|
+
let modified = false;
|
|
1648
|
+
const depTypes = ['dependencies', 'devDependencies', 'peerDependencies', 'optionalDependencies'];
|
|
1649
|
+
for (const depType of depTypes) {
|
|
1650
|
+
const deps = pkg[depType];
|
|
1651
|
+
if (deps) {
|
|
1652
|
+
for (const [name, newVersion] of versionUpdates) {
|
|
1653
|
+
if (deps[name]) {
|
|
1654
|
+
// Preserve version prefix (^, ~, etc.) or use exact version
|
|
1655
|
+
const currentRange = deps[name];
|
|
1656
|
+
const prefix = extractVersionPrefix(currentRange);
|
|
1657
|
+
deps[name] = prefix + newVersion;
|
|
1658
|
+
modified = true;
|
|
1659
|
+
}
|
|
1660
|
+
}
|
|
1661
|
+
}
|
|
1662
|
+
}
|
|
1663
|
+
if (modified) {
|
|
1664
|
+
const formatted = stringify(pkg, null, 2) + '\n';
|
|
1665
|
+
writeFileContent(packageJsonPath, formatted);
|
|
1666
|
+
}
|
|
1667
|
+
}
|
|
1668
|
+
/**
|
|
1669
|
+
* Extracts the version prefix from a version range.
|
|
1670
|
+
*
|
|
1671
|
+
* @param versionRange - Version range string
|
|
1672
|
+
* @returns The prefix (^, ~, >=, etc.) or empty string
|
|
1673
|
+
*/
|
|
1674
|
+
function extractVersionPrefix(versionRange) {
|
|
1675
|
+
if (versionRange.startsWith('^'))
|
|
1676
|
+
return '^';
|
|
1677
|
+
if (versionRange.startsWith('~'))
|
|
1678
|
+
return '~';
|
|
1679
|
+
if (versionRange.startsWith('>='))
|
|
1680
|
+
return '>=';
|
|
1681
|
+
if (versionRange.startsWith('>'))
|
|
1682
|
+
return '>';
|
|
1683
|
+
if (versionRange.startsWith('<='))
|
|
1684
|
+
return '<=';
|
|
1685
|
+
if (versionRange.startsWith('<'))
|
|
1686
|
+
return '<';
|
|
1687
|
+
if (versionRange.startsWith('='))
|
|
1688
|
+
return '=';
|
|
1689
|
+
return '';
|
|
1690
|
+
}
|
|
1691
|
+
/**
|
|
1692
|
+
* Updates a package.json file using a VFS Tree.
|
|
1693
|
+
*
|
|
1694
|
+
* @param tree - Virtual file system tree
|
|
1695
|
+
* @param packageJsonPath - Relative path to package.json
|
|
1696
|
+
* @param newVersion - New version string
|
|
1697
|
+
*/
|
|
1698
|
+
function updatePackageVersionInTree(tree, packageJsonPath, newVersion) {
|
|
1699
|
+
const content = tree.read(packageJsonPath, 'utf-8');
|
|
1700
|
+
if (!content) {
|
|
1701
|
+
throw createError(`Could not read ${packageJsonPath}`);
|
|
1702
|
+
}
|
|
1703
|
+
const pkg = parse(content);
|
|
1704
|
+
pkg.version = newVersion;
|
|
1705
|
+
const formatted = stringify(pkg, null, 2) + '\n';
|
|
1706
|
+
tree.write(packageJsonPath, formatted);
|
|
1707
|
+
}
|
|
1708
|
+
/**
|
|
1709
|
+
* Creates a summary of the batch update result.
|
|
1710
|
+
*
|
|
1711
|
+
* @param result - Result object from batch update operation
|
|
1712
|
+
* @returns Human-readable summary
|
|
1713
|
+
*/
|
|
1714
|
+
function summarizeBatchUpdate(result) {
|
|
1715
|
+
const lines = [];
|
|
1716
|
+
if (result.success) {
|
|
1717
|
+
lines.push(`Successfully updated ${result.updated.length} package(s)`);
|
|
1718
|
+
}
|
|
1719
|
+
else {
|
|
1720
|
+
lines.push(`Updated ${result.updated.length}/${result.total} package(s)`);
|
|
1721
|
+
lines.push(`Failed: ${result.failed.length} package(s)`);
|
|
1722
|
+
}
|
|
1723
|
+
if (result.updated.length > 0) {
|
|
1724
|
+
lines.push('');
|
|
1725
|
+
lines.push('Updated packages:');
|
|
1726
|
+
for (const pkg of result.updated) {
|
|
1727
|
+
lines.push(` ${pkg.name}: ${pkg.previousVersion} -> ${pkg.newVersion}`);
|
|
1728
|
+
}
|
|
1729
|
+
}
|
|
1730
|
+
if (result.failed.length > 0) {
|
|
1731
|
+
lines.push('');
|
|
1732
|
+
lines.push('Failed packages:');
|
|
1733
|
+
for (const pkg of result.failed) {
|
|
1734
|
+
lines.push(` ${pkg.name}: ${pkg.error}`);
|
|
1735
|
+
}
|
|
1736
|
+
}
|
|
1737
|
+
return lines.join('\n');
|
|
1738
|
+
}
|
|
1739
|
+
|
|
1740
|
+
/**
|
|
1741
|
+
* Compares two semantic versions.
|
|
1742
|
+
*
|
|
1743
|
+
* @param a - First version
|
|
1744
|
+
* @param b - Second version
|
|
1745
|
+
* @returns -1 if a < b, 0 if a == b, 1 if a > b
|
|
1746
|
+
*
|
|
1747
|
+
* @example
|
|
1748
|
+
* compare(parseVersion('1.0.0'), parseVersion('2.0.0')) // -1
|
|
1749
|
+
* compare(parseVersion('1.0.0'), parseVersion('1.0.0')) // 0
|
|
1750
|
+
* compare(parseVersion('2.0.0'), parseVersion('1.0.0')) // 1
|
|
1751
|
+
*/
|
|
1752
|
+
function compare(a, b) {
|
|
1753
|
+
// Compare major, minor, patch
|
|
1754
|
+
if (a.major !== b.major) {
|
|
1755
|
+
return a.major < b.major ? -1 : 1;
|
|
1756
|
+
}
|
|
1757
|
+
if (a.minor !== b.minor) {
|
|
1758
|
+
return a.minor < b.minor ? -1 : 1;
|
|
1759
|
+
}
|
|
1760
|
+
if (a.patch !== b.patch) {
|
|
1761
|
+
return a.patch < b.patch ? -1 : 1;
|
|
1762
|
+
}
|
|
1763
|
+
// Compare prerelease
|
|
1764
|
+
// Version with prerelease has lower precedence than release
|
|
1765
|
+
if (a.prerelease.length === 0 && b.prerelease.length > 0) {
|
|
1766
|
+
return 1; // a is release, b is prerelease -> a > b
|
|
1767
|
+
}
|
|
1768
|
+
if (a.prerelease.length > 0 && b.prerelease.length === 0) {
|
|
1769
|
+
return -1; // a is prerelease, b is release -> a < b
|
|
1770
|
+
}
|
|
1771
|
+
// Both have prerelease - compare identifiers
|
|
1772
|
+
const maxLen = max(a.prerelease.length, b.prerelease.length);
|
|
1773
|
+
for (let i = 0; i < maxLen; i++) {
|
|
1774
|
+
const aId = a.prerelease[i];
|
|
1775
|
+
const bId = b.prerelease[i];
|
|
1776
|
+
// Shorter prerelease array has lower precedence
|
|
1777
|
+
if (aId === undefined && bId !== undefined) {
|
|
1778
|
+
return -1;
|
|
1779
|
+
}
|
|
1780
|
+
if (aId !== undefined && bId === undefined) {
|
|
1781
|
+
return 1;
|
|
1782
|
+
}
|
|
1783
|
+
if (aId === undefined || bId === undefined) {
|
|
1784
|
+
continue;
|
|
1785
|
+
}
|
|
1786
|
+
// Compare identifiers
|
|
1787
|
+
const cmp = compareIdentifiers(aId, bId);
|
|
1788
|
+
if (cmp !== 0) {
|
|
1789
|
+
return cmp;
|
|
1790
|
+
}
|
|
1791
|
+
}
|
|
1792
|
+
return 0;
|
|
1793
|
+
}
|
|
1794
|
+
/**
|
|
1795
|
+
* Checks if a version satisfies a comparator.
|
|
1796
|
+
*
|
|
1797
|
+
* @param version - Version to check
|
|
1798
|
+
* @param comparator - Comparator to test against
|
|
1799
|
+
* @returns True if version satisfies the comparator
|
|
1800
|
+
*/
|
|
1801
|
+
function satisfiesComparator(version, comparator) {
|
|
1802
|
+
const cmp = compare(version, comparator.version);
|
|
1803
|
+
switch (comparator.operator) {
|
|
1804
|
+
case '=':
|
|
1805
|
+
return cmp === 0;
|
|
1806
|
+
case '>':
|
|
1807
|
+
return cmp === 1;
|
|
1808
|
+
case '>=':
|
|
1809
|
+
return cmp >= 0;
|
|
1810
|
+
case '<':
|
|
1811
|
+
return cmp === -1;
|
|
1812
|
+
case '<=':
|
|
1813
|
+
return cmp <= 0;
|
|
1814
|
+
case '^':
|
|
1815
|
+
case '~':
|
|
1816
|
+
// These should have been expanded during parsing
|
|
1817
|
+
// If we encounter them here, treat as >=
|
|
1818
|
+
return cmp >= 0;
|
|
1819
|
+
default:
|
|
1820
|
+
return false;
|
|
1821
|
+
}
|
|
1822
|
+
}
|
|
1823
|
+
/**
|
|
1824
|
+
* Checks if a version satisfies a range.
|
|
1825
|
+
*
|
|
1826
|
+
* @param version - Version to check
|
|
1827
|
+
* @param range - Range to test against
|
|
1828
|
+
* @returns True if version satisfies the range
|
|
1829
|
+
*
|
|
1830
|
+
* @example
|
|
1831
|
+
* satisfies(parseVersion('1.2.3'), parseRange('^1.0.0')) // true
|
|
1832
|
+
* satisfies(parseVersion('2.0.0'), parseRange('^1.0.0')) // false
|
|
1833
|
+
*/
|
|
1834
|
+
function satisfies(version, range) {
|
|
1835
|
+
// Empty range matches any
|
|
1836
|
+
if (range.sets.length === 0) {
|
|
1837
|
+
return true;
|
|
1838
|
+
}
|
|
1839
|
+
// OR logic: at least one set must be satisfied
|
|
1840
|
+
for (const set of range.sets) {
|
|
1841
|
+
// AND logic: all comparators in set must be satisfied
|
|
1842
|
+
let allSatisfied = true;
|
|
1843
|
+
// Empty comparator set matches any
|
|
1844
|
+
if (set.comparators.length === 0) {
|
|
1845
|
+
return true;
|
|
1846
|
+
}
|
|
1847
|
+
for (const comp of set.comparators) {
|
|
1848
|
+
if (!satisfiesComparator(version, comp)) {
|
|
1849
|
+
allSatisfied = false;
|
|
1850
|
+
break;
|
|
1851
|
+
}
|
|
1852
|
+
}
|
|
1853
|
+
if (allSatisfied) {
|
|
1854
|
+
return true;
|
|
1855
|
+
}
|
|
1856
|
+
}
|
|
1857
|
+
return false;
|
|
1858
|
+
}
|
|
1859
|
+
// ============================================================================
|
|
1860
|
+
// Internal helpers
|
|
1861
|
+
// ============================================================================
|
|
1862
|
+
/**
|
|
1863
|
+
* Compares two prerelease identifiers.
|
|
1864
|
+
* Numeric identifiers have lower precedence than alphanumeric.
|
|
1865
|
+
* Numeric identifiers are compared numerically.
|
|
1866
|
+
* Alphanumeric identifiers are compared lexically.
|
|
1867
|
+
*
|
|
1868
|
+
* @param a - First prerelease identifier
|
|
1869
|
+
* @param b - Second prerelease identifier
|
|
1870
|
+
* @returns -1 if a < b, 0 if equal, 1 if a > b
|
|
1871
|
+
*/
|
|
1872
|
+
function compareIdentifiers(a, b) {
|
|
1873
|
+
const aIsNumeric = isNumeric(a);
|
|
1874
|
+
const bIsNumeric = isNumeric(b);
|
|
1875
|
+
// Numeric identifiers have lower precedence
|
|
1876
|
+
if (aIsNumeric && !bIsNumeric) {
|
|
1877
|
+
return -1;
|
|
1878
|
+
}
|
|
1879
|
+
if (!aIsNumeric && bIsNumeric) {
|
|
1880
|
+
return 1;
|
|
1881
|
+
}
|
|
1882
|
+
// Both numeric - compare as numbers
|
|
1883
|
+
if (aIsNumeric && bIsNumeric) {
|
|
1884
|
+
const aNum = parseInt(a, 10);
|
|
1885
|
+
const bNum = parseInt(b, 10);
|
|
1886
|
+
if (aNum < bNum)
|
|
1887
|
+
return -1;
|
|
1888
|
+
if (aNum > bNum)
|
|
1889
|
+
return 1;
|
|
1890
|
+
return 0;
|
|
1891
|
+
}
|
|
1892
|
+
// Both alphanumeric - compare lexically
|
|
1893
|
+
if (a < b)
|
|
1894
|
+
return -1;
|
|
1895
|
+
if (a > b)
|
|
1896
|
+
return 1;
|
|
1897
|
+
return 0;
|
|
1898
|
+
}
|
|
1899
|
+
/**
|
|
1900
|
+
* Checks if a string consists only of digits.
|
|
1901
|
+
*
|
|
1902
|
+
* @param str - String to check for numeric content
|
|
1903
|
+
* @returns True if string contains only digits
|
|
1904
|
+
*/
|
|
1905
|
+
function isNumeric(str) {
|
|
1906
|
+
if (str.length === 0)
|
|
1907
|
+
return false;
|
|
1908
|
+
for (let i = 0; i < str.length; i++) {
|
|
1909
|
+
const code = str.charCodeAt(i);
|
|
1910
|
+
if (code < 48 || code > 57) {
|
|
1911
|
+
return false;
|
|
1912
|
+
}
|
|
1913
|
+
}
|
|
1914
|
+
return true;
|
|
1915
|
+
}
|
|
1916
|
+
|
|
1917
|
+
/**
|
|
1918
|
+
* Creates a new Comparator.
|
|
1919
|
+
*
|
|
1920
|
+
* @param operator - The comparison operator
|
|
1921
|
+
* @param version - The version to compare against
|
|
1922
|
+
* @returns A new Comparator
|
|
1923
|
+
*/
|
|
1924
|
+
function createComparator(operator, version) {
|
|
1925
|
+
return { operator, version };
|
|
1926
|
+
}
|
|
1927
|
+
/**
|
|
1928
|
+
* Creates a new ComparatorSet.
|
|
1929
|
+
*
|
|
1930
|
+
* @param comparators - Array of comparators (AND logic)
|
|
1931
|
+
* @returns A new ComparatorSet
|
|
1932
|
+
*/
|
|
1933
|
+
function createComparatorSet(comparators) {
|
|
1934
|
+
return { comparators };
|
|
1935
|
+
}
|
|
1936
|
+
/**
|
|
1937
|
+
* Creates a new Range.
|
|
1938
|
+
*
|
|
1939
|
+
* @param sets - Array of comparator sets (OR logic)
|
|
1940
|
+
* @param raw - Original raw string
|
|
1941
|
+
* @returns A new Range
|
|
1942
|
+
*/
|
|
1943
|
+
function createRange(sets, raw) {
|
|
1944
|
+
return { sets, raw };
|
|
1945
|
+
}
|
|
1946
|
+
|
|
1947
|
+
/**
|
|
1948
|
+
* Maximum range string length.
|
|
1949
|
+
*/
|
|
1950
|
+
const MAX_RANGE_LENGTH = 1024;
|
|
1951
|
+
/**
|
|
1952
|
+
* Parses a semver range string.
|
|
1953
|
+
*
|
|
1954
|
+
* Supports:
|
|
1955
|
+
* - Exact: 1.2.3, =1.2.3
|
|
1956
|
+
* - Comparators: >1.0.0, >=1.0.0, <2.0.0, <=2.0.0
|
|
1957
|
+
* - Caret: ^1.2.3 (compatible with version)
|
|
1958
|
+
* - Tilde: ~1.2.3 (approximately equivalent)
|
|
1959
|
+
* - X-ranges: 1.x, 1.2.x, *
|
|
1960
|
+
* - Hyphen ranges: 1.0.0 - 2.0.0
|
|
1961
|
+
* - OR: 1.0.0 || 2.0.0
|
|
1962
|
+
* - AND: >=1.0.0 <2.0.0
|
|
1963
|
+
*
|
|
1964
|
+
* @param input - The range string to parse
|
|
1965
|
+
* @returns A ParseRangeResult with the parsed range or error
|
|
1966
|
+
*/
|
|
1967
|
+
function parseRange(input) {
|
|
1968
|
+
if (!input || typeof input !== 'string') {
|
|
1969
|
+
return { success: false, error: 'Range string is required' };
|
|
1970
|
+
}
|
|
1971
|
+
if (input.length > MAX_RANGE_LENGTH) {
|
|
1972
|
+
return { success: false, error: `Range string exceeds maximum length of ${MAX_RANGE_LENGTH}` };
|
|
1973
|
+
}
|
|
1974
|
+
// Trim whitespace
|
|
1975
|
+
const trimmed = input.trim();
|
|
1976
|
+
// Handle wildcard/any
|
|
1977
|
+
if (trimmed === '' || trimmed === '*' || trimmed.toLowerCase() === 'x') {
|
|
1978
|
+
return { success: true, range: createRange([], input) };
|
|
1979
|
+
}
|
|
1980
|
+
// Split by || for OR logic
|
|
1981
|
+
const orParts = splitByOr(trimmed);
|
|
1982
|
+
const sets = [];
|
|
1983
|
+
for (const part of orParts) {
|
|
1984
|
+
const setResult = parseComparatorSet(part.trim());
|
|
1985
|
+
if (!setResult.success) {
|
|
1986
|
+
return { success: false, error: setResult.error };
|
|
1987
|
+
}
|
|
1988
|
+
if (setResult.set) {
|
|
1989
|
+
sets.push(setResult.set);
|
|
1990
|
+
}
|
|
1991
|
+
}
|
|
1992
|
+
return { success: true, range: createRange(sets, input) };
|
|
1993
|
+
}
|
|
1994
|
+
/**
|
|
1995
|
+
* Splits a string by || delimiter, respecting nesting.
|
|
1996
|
+
*
|
|
1997
|
+
* @param input - Range string containing OR groups
|
|
1998
|
+
* @returns Array of OR-separated parts
|
|
1999
|
+
*/
|
|
2000
|
+
function splitByOr(input) {
|
|
2001
|
+
const parts = [];
|
|
2002
|
+
let current = '';
|
|
2003
|
+
let pos = 0;
|
|
2004
|
+
while (pos < input.length) {
|
|
2005
|
+
if (input[pos] === '|' && pos + 1 < input.length && input[pos + 1] === '|') {
|
|
2006
|
+
parts.push(current);
|
|
2007
|
+
current = '';
|
|
2008
|
+
pos += 2;
|
|
2009
|
+
}
|
|
2010
|
+
else {
|
|
2011
|
+
current += input[pos];
|
|
2012
|
+
pos++;
|
|
2013
|
+
}
|
|
2014
|
+
}
|
|
2015
|
+
parts.push(current);
|
|
2016
|
+
return parts;
|
|
2017
|
+
}
|
|
2018
|
+
/**
|
|
2019
|
+
* Parses a single comparator set (space-separated comparators = AND logic).
|
|
2020
|
+
*
|
|
2021
|
+
* @param input - Comparator set string
|
|
2022
|
+
* @returns Parsed set result
|
|
2023
|
+
*/
|
|
2024
|
+
function parseComparatorSet(input) {
|
|
2025
|
+
if (!input || input.trim() === '') {
|
|
2026
|
+
return { success: true }; // Empty set matches any
|
|
2027
|
+
}
|
|
2028
|
+
const trimmed = input.trim();
|
|
2029
|
+
// Check for hyphen range: "1.0.0 - 2.0.0"
|
|
2030
|
+
const hyphenMatch = parseHyphenRange(trimmed);
|
|
2031
|
+
if (hyphenMatch.isHyphenRange) {
|
|
2032
|
+
if (!hyphenMatch.success) {
|
|
2033
|
+
return { success: false, error: hyphenMatch.error };
|
|
2034
|
+
}
|
|
2035
|
+
return { success: true, set: hyphenMatch.set };
|
|
2036
|
+
}
|
|
2037
|
+
// Split by whitespace for AND logic
|
|
2038
|
+
const tokens = splitByWhitespace(trimmed);
|
|
2039
|
+
const comparators = [];
|
|
2040
|
+
for (const token of tokens) {
|
|
2041
|
+
const compResult = parseSingleComparator(token);
|
|
2042
|
+
if (!compResult.success) {
|
|
2043
|
+
return { success: false, error: compResult.error };
|
|
2044
|
+
}
|
|
2045
|
+
if (compResult.comparators) {
|
|
2046
|
+
comparators.push(...compResult.comparators);
|
|
2047
|
+
}
|
|
2048
|
+
}
|
|
2049
|
+
if (comparators.length === 0) {
|
|
2050
|
+
return { success: true }; // Empty matches any
|
|
2051
|
+
}
|
|
2052
|
+
return { success: true, set: createComparatorSet(comparators) };
|
|
2053
|
+
}
|
|
2054
|
+
/**
|
|
2055
|
+
* Checks for and parses hyphen ranges like "1.0.0 - 2.0.0".
|
|
2056
|
+
*
|
|
2057
|
+
* @param input - Potential hyphen range string
|
|
2058
|
+
* @returns Hyphen range parsing result
|
|
2059
|
+
*/
|
|
2060
|
+
function parseHyphenRange(input) {
|
|
2061
|
+
// Look for " - " (space-hyphen-space)
|
|
2062
|
+
let hyphenPos = -1;
|
|
2063
|
+
for (let i = 0; i < input.length - 2; i++) {
|
|
2064
|
+
if (input[i] === ' ' && input[i + 1] === '-' && input[i + 2] === ' ') {
|
|
2065
|
+
hyphenPos = i;
|
|
2066
|
+
break;
|
|
2067
|
+
}
|
|
2068
|
+
}
|
|
2069
|
+
if (hyphenPos === -1) {
|
|
2070
|
+
return { isHyphenRange: false, success: true };
|
|
2071
|
+
}
|
|
2072
|
+
const leftPart = input.slice(0, hyphenPos).trim();
|
|
2073
|
+
const rightPart = input.slice(hyphenPos + 3).trim();
|
|
2074
|
+
const leftVersion = parseSimpleVersion(leftPart);
|
|
2075
|
+
if (!leftVersion) {
|
|
2076
|
+
return { isHyphenRange: true, success: false, error: `Invalid left side of hyphen range: "${leftPart}"` };
|
|
2077
|
+
}
|
|
2078
|
+
const rightVersion = parseSimpleVersion(rightPart);
|
|
2079
|
+
if (!rightVersion) {
|
|
2080
|
+
return { isHyphenRange: true, success: false, error: `Invalid right side of hyphen range: "${rightPart}"` };
|
|
2081
|
+
}
|
|
2082
|
+
// Hyphen range: >=left <=right
|
|
2083
|
+
const comparators = [createComparator('>=', leftVersion), createComparator('<=', rightVersion)];
|
|
2084
|
+
return {
|
|
2085
|
+
isHyphenRange: true,
|
|
2086
|
+
success: true,
|
|
2087
|
+
set: createComparatorSet(comparators),
|
|
2088
|
+
};
|
|
2089
|
+
}
|
|
2090
|
+
/**
|
|
2091
|
+
* Splits by whitespace.
|
|
2092
|
+
*
|
|
2093
|
+
* @param input - String to split
|
|
2094
|
+
* @returns Array of whitespace-separated tokens
|
|
2095
|
+
*/
|
|
2096
|
+
function splitByWhitespace(input) {
|
|
2097
|
+
const tokens = [];
|
|
2098
|
+
let current = '';
|
|
2099
|
+
for (const char of input) {
|
|
2100
|
+
if (char === ' ' || char === '\t') {
|
|
2101
|
+
if (current) {
|
|
2102
|
+
tokens.push(current);
|
|
2103
|
+
current = '';
|
|
2104
|
+
}
|
|
2105
|
+
}
|
|
2106
|
+
else {
|
|
2107
|
+
current += char;
|
|
2108
|
+
}
|
|
2109
|
+
}
|
|
2110
|
+
if (current) {
|
|
2111
|
+
tokens.push(current);
|
|
2112
|
+
}
|
|
2113
|
+
return tokens;
|
|
2114
|
+
}
|
|
2115
|
+
/**
|
|
2116
|
+
* Parses a single comparator token (e.g., ">=1.0.0", "^1.2.3", "~1.0").
|
|
2117
|
+
*
|
|
2118
|
+
* @param token - Comparator token to parse
|
|
2119
|
+
* @returns Parsed comparator result
|
|
2120
|
+
*/
|
|
2121
|
+
function parseSingleComparator(token) {
|
|
2122
|
+
let pos = 0;
|
|
2123
|
+
let operator = '=';
|
|
2124
|
+
// Parse operator
|
|
2125
|
+
if (token[pos] === '^') {
|
|
2126
|
+
operator = '^';
|
|
2127
|
+
pos++;
|
|
2128
|
+
}
|
|
2129
|
+
else if (token[pos] === '~') {
|
|
2130
|
+
operator = '~';
|
|
2131
|
+
pos++;
|
|
2132
|
+
}
|
|
2133
|
+
else if (token[pos] === '>') {
|
|
2134
|
+
if (token[pos + 1] === '=') {
|
|
2135
|
+
operator = '>=';
|
|
2136
|
+
pos += 2;
|
|
2137
|
+
}
|
|
2138
|
+
else {
|
|
2139
|
+
operator = '>';
|
|
2140
|
+
pos++;
|
|
2141
|
+
}
|
|
2142
|
+
}
|
|
2143
|
+
else if (token[pos] === '<') {
|
|
2144
|
+
if (token[pos + 1] === '=') {
|
|
2145
|
+
operator = '<=';
|
|
2146
|
+
pos += 2;
|
|
2147
|
+
}
|
|
2148
|
+
else {
|
|
2149
|
+
operator = '<';
|
|
2150
|
+
pos++;
|
|
2151
|
+
}
|
|
2152
|
+
}
|
|
2153
|
+
else if (token[pos] === '=') {
|
|
2154
|
+
operator = '=';
|
|
2155
|
+
pos++;
|
|
2156
|
+
}
|
|
2157
|
+
const versionPart = token.slice(pos);
|
|
2158
|
+
// Handle wildcards: *, x, X
|
|
2159
|
+
if (versionPart === '*' || versionPart.toLowerCase() === 'x') {
|
|
2160
|
+
// Wildcard matches any - return empty (will be handled as match-all)
|
|
2161
|
+
return { success: true, comparators: [] };
|
|
2162
|
+
}
|
|
2163
|
+
// Handle x-ranges: 1.x, 1.2.x
|
|
2164
|
+
if (versionPart.includes('x') || versionPart.includes('X') || versionPart.includes('*')) {
|
|
2165
|
+
return parseXRange(versionPart);
|
|
2166
|
+
}
|
|
2167
|
+
// Parse version
|
|
2168
|
+
const version = parseSimpleVersion(versionPart);
|
|
2169
|
+
if (!version) {
|
|
2170
|
+
return { success: false, error: `Invalid version in comparator: "${versionPart}"` };
|
|
2171
|
+
}
|
|
2172
|
+
// For caret and tilde, expand to range
|
|
2173
|
+
if (operator === '^') {
|
|
2174
|
+
return expandCaretRange(version);
|
|
2175
|
+
}
|
|
2176
|
+
if (operator === '~') {
|
|
2177
|
+
return expandTildeRange(version);
|
|
2178
|
+
}
|
|
2179
|
+
return { success: true, comparators: [createComparator(operator, version)] };
|
|
2180
|
+
}
|
|
2181
|
+
/**
|
|
2182
|
+
* Parses x-ranges like 1.x, 1.2.x, etc.
|
|
2183
|
+
*
|
|
2184
|
+
* @param input - X-range string to parse
|
|
2185
|
+
* @param _operator - Range operator (unused but kept for interface consistency)
|
|
2186
|
+
* @returns Comparator result
|
|
2187
|
+
*/
|
|
2188
|
+
function parseXRange(input, _operator) {
|
|
2189
|
+
const parts = input.split('.');
|
|
2190
|
+
const nums = [];
|
|
2191
|
+
for (const part of parts) {
|
|
2192
|
+
const lower = part.toLowerCase();
|
|
2193
|
+
if (lower === 'x' || lower === '*' || lower === '') {
|
|
2194
|
+
break;
|
|
2195
|
+
}
|
|
2196
|
+
const num = parseInt(part, 10);
|
|
2197
|
+
if (globalIsNaN(num) || num < 0) {
|
|
2198
|
+
return { success: false, error: `Invalid x-range: "${input}"` };
|
|
2199
|
+
}
|
|
2200
|
+
nums.push(num);
|
|
2201
|
+
}
|
|
2202
|
+
if (nums.length === 0) {
|
|
2203
|
+
// * or X alone - match any
|
|
2204
|
+
return { success: true, comparators: [] };
|
|
2205
|
+
}
|
|
2206
|
+
if (nums.length === 1) {
|
|
2207
|
+
// 1.x or 1.* -> >=1.0.0 <2.0.0
|
|
2208
|
+
const lower = createSemVer({ major: nums[0], minor: 0, patch: 0 });
|
|
2209
|
+
const upper = createSemVer({ major: nums[0] + 1, minor: 0, patch: 0 });
|
|
2210
|
+
return { success: true, comparators: [createComparator('>=', lower), createComparator('<', upper)] };
|
|
2211
|
+
}
|
|
2212
|
+
// 1.2.x -> >=1.2.0 <1.3.0
|
|
2213
|
+
const lower = createSemVer({ major: nums[0], minor: nums[1], patch: 0 });
|
|
2214
|
+
const upper = createSemVer({ major: nums[0], minor: nums[1] + 1, patch: 0 });
|
|
2215
|
+
return { success: true, comparators: [createComparator('>=', lower), createComparator('<', upper)] };
|
|
2216
|
+
}
|
|
2217
|
+
/**
|
|
2218
|
+
* Expands caret range: ^1.2.3 -> >=1.2.3 <2.0.0
|
|
2219
|
+
*
|
|
2220
|
+
* @param version - Base version for caret range
|
|
2221
|
+
* @returns Expanded comparator result
|
|
2222
|
+
*/
|
|
2223
|
+
function expandCaretRange(version) {
|
|
2224
|
+
let upperMajor = version.major;
|
|
2225
|
+
let upperMinor = 0;
|
|
2226
|
+
let upperPatch = 0;
|
|
2227
|
+
if (version.major === 0) {
|
|
2228
|
+
if (version.minor === 0) {
|
|
2229
|
+
// ^0.0.x -> >=0.0.x <0.0.(x+1)
|
|
2230
|
+
upperPatch = version.patch + 1;
|
|
2231
|
+
upperMinor = version.minor;
|
|
2232
|
+
}
|
|
2233
|
+
else {
|
|
2234
|
+
// ^0.x.y -> >=0.x.y <0.(x+1).0
|
|
2235
|
+
upperMinor = version.minor + 1;
|
|
2236
|
+
}
|
|
2237
|
+
}
|
|
2238
|
+
else {
|
|
2239
|
+
// ^x.y.z -> >=x.y.z <(x+1).0.0
|
|
2240
|
+
upperMajor = version.major + 1;
|
|
2241
|
+
}
|
|
2242
|
+
const upper = createSemVer({ major: upperMajor, minor: upperMinor, patch: upperPatch });
|
|
2243
|
+
return { success: true, comparators: [createComparator('>=', version), createComparator('<', upper)] };
|
|
2244
|
+
}
|
|
2245
|
+
/**
|
|
2246
|
+
* Expands tilde range: ~1.2.3 -> >=1.2.3 <1.3.0
|
|
2247
|
+
*
|
|
2248
|
+
* @param version - Base version for tilde range
|
|
2249
|
+
* @returns Expanded comparator result
|
|
2250
|
+
*/
|
|
2251
|
+
function expandTildeRange(version) {
|
|
2252
|
+
const upper = createSemVer({
|
|
2253
|
+
major: version.major,
|
|
2254
|
+
minor: version.minor + 1,
|
|
2255
|
+
patch: 0,
|
|
2256
|
+
});
|
|
2257
|
+
return { success: true, comparators: [createComparator('>=', version), createComparator('<', upper)] };
|
|
2258
|
+
}
|
|
2259
|
+
/**
|
|
2260
|
+
* Parses a simple version string (no range operators).
|
|
2261
|
+
* More lenient - accepts partial versions.
|
|
2262
|
+
*
|
|
2263
|
+
* @param input - Version string to parse
|
|
2264
|
+
* @returns Parsed SemVer or null if invalid
|
|
2265
|
+
*/
|
|
2266
|
+
function parseSimpleVersion(input) {
|
|
2267
|
+
if (!input)
|
|
2268
|
+
return null;
|
|
2269
|
+
let pos = 0;
|
|
2270
|
+
// Skip leading v
|
|
2271
|
+
if (input[pos] === 'v' || input[pos] === 'V') {
|
|
2272
|
+
pos++;
|
|
2273
|
+
}
|
|
2274
|
+
const parts = input.slice(pos).split('.');
|
|
2275
|
+
if (parts.length === 0)
|
|
2276
|
+
return null;
|
|
2277
|
+
const nums = [];
|
|
2278
|
+
for (const part of parts) {
|
|
2279
|
+
// Stop at prerelease or build
|
|
2280
|
+
const dashIdx = part.indexOf('-');
|
|
2281
|
+
const plusIdx = part.indexOf('+');
|
|
2282
|
+
let numPart = part;
|
|
2283
|
+
if (dashIdx !== -1) {
|
|
2284
|
+
numPart = part.slice(0, dashIdx);
|
|
2285
|
+
}
|
|
2286
|
+
else if (plusIdx !== -1) {
|
|
2287
|
+
numPart = part.slice(0, plusIdx);
|
|
2288
|
+
}
|
|
2289
|
+
if (numPart === '' || numPart.toLowerCase() === 'x' || numPart === '*') {
|
|
2290
|
+
break;
|
|
2291
|
+
}
|
|
2292
|
+
const num = parseInt(numPart, 10);
|
|
2293
|
+
if (globalIsNaN(num) || num < 0)
|
|
2294
|
+
return null;
|
|
2295
|
+
nums.push(num);
|
|
2296
|
+
}
|
|
2297
|
+
if (nums.length === 0)
|
|
2298
|
+
return null;
|
|
2299
|
+
return createSemVer({
|
|
2300
|
+
major: nums[0],
|
|
2301
|
+
minor: nums[1] ?? 0,
|
|
2302
|
+
patch: nums[2] ?? 0,
|
|
2303
|
+
prerelease: [],
|
|
2304
|
+
build: [],
|
|
2305
|
+
raw: input,
|
|
2306
|
+
});
|
|
2307
|
+
}
|
|
2308
|
+
|
|
2309
|
+
/**
|
|
2310
|
+
* Workspace Validation
|
|
2311
|
+
*
|
|
2312
|
+
* Validation utilities for workspace integrity checks.
|
|
2313
|
+
* Verifies package configurations, dependencies, and workspace structure.
|
|
2314
|
+
*/
|
|
2315
|
+
/**
|
|
2316
|
+
* Validates a workspace for common issues.
|
|
2317
|
+
*
|
|
2318
|
+
* @param workspace - The workspace to validate
|
|
2319
|
+
* @returns Validation report
|
|
2320
|
+
*
|
|
2321
|
+
* @example
|
|
2322
|
+
* ```typescript
|
|
2323
|
+
* import { validateWorkspace } from '@hyperfrontend/versioning'
|
|
2324
|
+
*
|
|
2325
|
+
* const report = validateWorkspace(workspace)
|
|
2326
|
+
*
|
|
2327
|
+
* if (!report.valid) {
|
|
2328
|
+
* console.error(`${report.errorCount} error(s) found`)
|
|
2329
|
+
* for (const result of report.results) {
|
|
2330
|
+
* if (!result.result.valid) {
|
|
2331
|
+
* console.error(` ${result.checkName}: ${result.result.error}`)
|
|
2332
|
+
* }
|
|
2333
|
+
* }
|
|
2334
|
+
* }
|
|
2335
|
+
* ```
|
|
2336
|
+
*/
|
|
2337
|
+
function validateWorkspace(workspace) {
|
|
2338
|
+
const results = [];
|
|
2339
|
+
// Workspace-level checks
|
|
2340
|
+
results.push({
|
|
2341
|
+
checkId: 'workspace-has-projects',
|
|
2342
|
+
checkName: 'Workspace has projects',
|
|
2343
|
+
packageName: null,
|
|
2344
|
+
result: validateHasProjects(workspace),
|
|
2345
|
+
});
|
|
2346
|
+
results.push({
|
|
2347
|
+
checkId: 'no-circular-dependencies',
|
|
2348
|
+
checkName: 'No circular dependencies',
|
|
2349
|
+
packageName: null,
|
|
2350
|
+
result: validateNoCircularDependencies(workspace),
|
|
2351
|
+
});
|
|
2352
|
+
// Package-level checks
|
|
2353
|
+
for (const project of workspace.projectList) {
|
|
2354
|
+
results.push({
|
|
2355
|
+
checkId: 'valid-version',
|
|
2356
|
+
checkName: `Valid semver version`,
|
|
2357
|
+
packageName: project.name,
|
|
2358
|
+
result: validateProjectVersion(project),
|
|
2359
|
+
});
|
|
2360
|
+
results.push({
|
|
2361
|
+
checkId: 'valid-name',
|
|
2362
|
+
checkName: `Valid package name`,
|
|
2363
|
+
packageName: project.name,
|
|
2364
|
+
result: validateProjectName(project),
|
|
2365
|
+
});
|
|
2366
|
+
results.push({
|
|
2367
|
+
checkId: 'dependency-versions',
|
|
2368
|
+
checkName: `Internal dependency versions`,
|
|
2369
|
+
packageName: project.name,
|
|
2370
|
+
result: validateDependencyVersions(workspace, project),
|
|
2371
|
+
});
|
|
2372
|
+
}
|
|
2373
|
+
// Aggregate results
|
|
2374
|
+
const errors = results.filter((r) => !r.result.valid);
|
|
2375
|
+
const warnings = results.filter((r) => r.result.warning !== undefined);
|
|
2376
|
+
const invalidPackageNames = errors
|
|
2377
|
+
.filter((r) => r.packageName !== null)
|
|
2378
|
+
.map((r) => r.packageName)
|
|
2379
|
+
.filter((name) => name !== null);
|
|
2380
|
+
const invalidPackages = createSet(invalidPackageNames);
|
|
2381
|
+
return {
|
|
2382
|
+
results,
|
|
2383
|
+
valid: errors.length === 0,
|
|
2384
|
+
errorCount: errors.length,
|
|
2385
|
+
warningCount: warnings.length,
|
|
2386
|
+
invalidPackages: [...invalidPackages],
|
|
2387
|
+
};
|
|
2388
|
+
}
|
|
2389
|
+
/**
|
|
2390
|
+
* Validates that the workspace has at least one project.
|
|
2391
|
+
*
|
|
2392
|
+
* @param workspace - Workspace to validate for project existence
|
|
2393
|
+
* @returns Validation result indicating success or failure
|
|
2394
|
+
*/
|
|
2395
|
+
function validateHasProjects(workspace) {
|
|
2396
|
+
if (workspace.projects.size === 0) {
|
|
2397
|
+
return {
|
|
2398
|
+
valid: false,
|
|
2399
|
+
error: 'Workspace has no projects',
|
|
2400
|
+
};
|
|
2401
|
+
}
|
|
2402
|
+
return { valid: true };
|
|
2403
|
+
}
|
|
2404
|
+
/**
|
|
2405
|
+
* Validates that there are no circular dependencies.
|
|
2406
|
+
*
|
|
2407
|
+
* @param workspace - Workspace to check for circular dependencies
|
|
2408
|
+
* @returns Validation result indicating success or failure with cycle info
|
|
2409
|
+
*/
|
|
2410
|
+
function validateNoCircularDependencies(workspace) {
|
|
2411
|
+
// Build adjacency list
|
|
2412
|
+
const visited = createSet();
|
|
2413
|
+
const recursionStack = createSet();
|
|
2414
|
+
/**
|
|
2415
|
+
* Depth-first search to detect cycles in the dependency graph.
|
|
2416
|
+
*
|
|
2417
|
+
* @param node - Current node being visited
|
|
2418
|
+
* @returns True if a cycle was found starting from this node
|
|
2419
|
+
*/
|
|
2420
|
+
function hasCycle(node) {
|
|
2421
|
+
visited.add(node);
|
|
2422
|
+
recursionStack.add(node);
|
|
2423
|
+
const deps = workspace.reverseDependencyGraph.get(node) ?? [];
|
|
2424
|
+
for (const dep of deps) {
|
|
2425
|
+
if (!visited.has(dep)) {
|
|
2426
|
+
if (hasCycle(dep)) {
|
|
2427
|
+
return true;
|
|
2428
|
+
}
|
|
2429
|
+
}
|
|
2430
|
+
else if (recursionStack.has(dep)) {
|
|
2431
|
+
return true;
|
|
2432
|
+
}
|
|
2433
|
+
}
|
|
2434
|
+
recursionStack.delete(node);
|
|
2435
|
+
return false;
|
|
2436
|
+
}
|
|
2437
|
+
for (const name of workspace.projects.keys()) {
|
|
2438
|
+
if (!visited.has(name)) {
|
|
2439
|
+
if (hasCycle(name)) {
|
|
2440
|
+
return {
|
|
2441
|
+
valid: false,
|
|
2442
|
+
error: 'Circular dependency detected',
|
|
2443
|
+
};
|
|
2444
|
+
}
|
|
2445
|
+
}
|
|
2446
|
+
}
|
|
2447
|
+
return { valid: true };
|
|
2448
|
+
}
|
|
2449
|
+
/**
|
|
2450
|
+
* Validates a project's version is valid semver.
|
|
2451
|
+
*
|
|
2452
|
+
* @param project - Project to validate version for
|
|
2453
|
+
* @returns Validation result indicating success or failure
|
|
2454
|
+
*/
|
|
2455
|
+
function validateProjectVersion(project) {
|
|
2456
|
+
const result = parseVersion(project.version);
|
|
2457
|
+
if (!result.success) {
|
|
2458
|
+
return {
|
|
2459
|
+
valid: false,
|
|
2460
|
+
error: `Invalid semver version: ${project.version}`,
|
|
2461
|
+
};
|
|
2462
|
+
}
|
|
2463
|
+
return { valid: true };
|
|
2464
|
+
}
|
|
2465
|
+
/**
|
|
2466
|
+
* Validates a project's package name.
|
|
2467
|
+
*
|
|
2468
|
+
* @param project - Project to validate name for
|
|
2469
|
+
* @returns Validation result indicating success or failure
|
|
2470
|
+
*/
|
|
2471
|
+
function validateProjectName(project) {
|
|
2472
|
+
if (!project.name || project.name.trim() === '') {
|
|
2473
|
+
return {
|
|
2474
|
+
valid: false,
|
|
2475
|
+
error: 'Package name is required',
|
|
2476
|
+
};
|
|
2477
|
+
}
|
|
2478
|
+
// Check for valid npm package name using safe validation without complex regex
|
|
2479
|
+
const nameValidationResult = validatePackageNameFormat(project.name);
|
|
2480
|
+
if (!nameValidationResult.valid) {
|
|
2481
|
+
return {
|
|
2482
|
+
valid: false,
|
|
2483
|
+
error: `Invalid package name format: ${project.name}`,
|
|
2484
|
+
};
|
|
2485
|
+
}
|
|
2486
|
+
// Check length
|
|
2487
|
+
if (project.name.length > 214) {
|
|
2488
|
+
return {
|
|
2489
|
+
valid: false,
|
|
2490
|
+
error: 'Package name exceeds 214 characters',
|
|
2491
|
+
};
|
|
2492
|
+
}
|
|
2493
|
+
return { valid: true };
|
|
2494
|
+
}
|
|
2495
|
+
/**
|
|
2496
|
+
* Validates that internal dependency versions are satisfiable.
|
|
2497
|
+
*
|
|
2498
|
+
* @param workspace - Workspace containing all projects
|
|
2499
|
+
* @param project - Project to validate dependencies for
|
|
2500
|
+
* @returns Validation result with warnings for unsatisfied versions
|
|
2501
|
+
*/
|
|
2502
|
+
function validateDependencyVersions(workspace, project) {
|
|
2503
|
+
const warnings = [];
|
|
2504
|
+
for (const depName of project.internalDependencies) {
|
|
2505
|
+
const dep = workspace.projects.get(depName);
|
|
2506
|
+
if (!dep) {
|
|
2507
|
+
continue; // Handled by dependency existence check
|
|
2508
|
+
}
|
|
2509
|
+
// Get the version range from package.json
|
|
2510
|
+
const depTypes = ['dependencies', 'devDependencies', 'peerDependencies', 'optionalDependencies'];
|
|
2511
|
+
let versionRange;
|
|
2512
|
+
for (const depType of depTypes) {
|
|
2513
|
+
const deps = project.packageJson[depType];
|
|
2514
|
+
if (deps?.[depName]) {
|
|
2515
|
+
versionRange = deps[depName];
|
|
2516
|
+
break;
|
|
2517
|
+
}
|
|
2518
|
+
}
|
|
2519
|
+
if (versionRange) {
|
|
2520
|
+
const depVersionResult = parseVersion(dep.version);
|
|
2521
|
+
if (depVersionResult.success && depVersionResult.version && !isWorkspaceVersion(versionRange)) {
|
|
2522
|
+
// Parse the version range
|
|
2523
|
+
const rangeResult = parseRange(versionRange);
|
|
2524
|
+
if (rangeResult.success && rangeResult.range) {
|
|
2525
|
+
// Check if the current version satisfies the range
|
|
2526
|
+
if (!satisfies(depVersionResult.version, rangeResult.range)) {
|
|
2527
|
+
warnings.push(`${depName}@${dep.version} does not satisfy ${versionRange}`);
|
|
2528
|
+
}
|
|
2529
|
+
}
|
|
2530
|
+
}
|
|
2531
|
+
}
|
|
2532
|
+
}
|
|
2533
|
+
if (warnings.length > 0) {
|
|
2534
|
+
return {
|
|
2535
|
+
valid: true, // Warning, not error
|
|
2536
|
+
warning: warnings.join('; '),
|
|
2537
|
+
};
|
|
2538
|
+
}
|
|
2539
|
+
return { valid: true };
|
|
2540
|
+
}
|
|
2541
|
+
/**
|
|
2542
|
+
* Validates npm package name format without using complex regex.
|
|
2543
|
+
* This avoids ReDoS vulnerabilities from backtracking regex patterns.
|
|
2544
|
+
*
|
|
2545
|
+
* @param name - Package name to validate
|
|
2546
|
+
* @returns Validation result
|
|
2547
|
+
*/
|
|
2548
|
+
function validatePackageNameFormat(name) {
|
|
2549
|
+
// Valid characters for package names: lowercase letters, digits, hyphens, underscores, dots
|
|
2550
|
+
const isValidChar = (char) => {
|
|
2551
|
+
const code = char.charCodeAt(0);
|
|
2552
|
+
return ((code >= 97 && code <= 122) || // a-z
|
|
2553
|
+
(code >= 48 && code <= 57) || // 0-9
|
|
2554
|
+
code === 45 || // -
|
|
2555
|
+
code === 95 || // _
|
|
2556
|
+
code === 46 // .
|
|
2557
|
+
);
|
|
2558
|
+
};
|
|
2559
|
+
// First char must be lowercase letter or digit
|
|
2560
|
+
const isValidFirstChar = (char) => {
|
|
2561
|
+
const code = char.charCodeAt(0);
|
|
2562
|
+
return (code >= 97 && code <= 122) || (code >= 48 && code <= 57);
|
|
2563
|
+
};
|
|
2564
|
+
// Handle scoped packages (@scope/name)
|
|
2565
|
+
if (name.startsWith('@')) {
|
|
2566
|
+
const slashIndex = name.indexOf('/');
|
|
2567
|
+
if (slashIndex === -1 || slashIndex === 1 || slashIndex === name.length - 1) {
|
|
2568
|
+
return { valid: false };
|
|
2569
|
+
}
|
|
2570
|
+
// Validate scope (after @, before /)
|
|
2571
|
+
const scope = name.slice(1, slashIndex);
|
|
2572
|
+
if (!isValidFirstChar(scope[0]))
|
|
2573
|
+
return { valid: false };
|
|
2574
|
+
for (const char of scope) {
|
|
2575
|
+
if (!isValidChar(char))
|
|
2576
|
+
return { valid: false };
|
|
2577
|
+
}
|
|
2578
|
+
// Validate name (after /)
|
|
2579
|
+
const packageName = name.slice(slashIndex + 1);
|
|
2580
|
+
if (!isValidFirstChar(packageName[0]))
|
|
2581
|
+
return { valid: false };
|
|
2582
|
+
for (const char of packageName) {
|
|
2583
|
+
if (!isValidChar(char))
|
|
2584
|
+
return { valid: false };
|
|
2585
|
+
}
|
|
2586
|
+
}
|
|
2587
|
+
else {
|
|
2588
|
+
// Unscoped package
|
|
2589
|
+
if (!isValidFirstChar(name[0]))
|
|
2590
|
+
return { valid: false };
|
|
2591
|
+
for (const char of name) {
|
|
2592
|
+
if (!isValidChar(char))
|
|
2593
|
+
return { valid: false };
|
|
2594
|
+
}
|
|
2595
|
+
}
|
|
2596
|
+
return { valid: true };
|
|
2597
|
+
}
|
|
2598
|
+
/**
|
|
2599
|
+
* Checks if a version range is a workspace protocol version.
|
|
2600
|
+
*
|
|
2601
|
+
* @param versionRange - Version range string to check
|
|
2602
|
+
* @returns True if the range uses workspace protocol
|
|
2603
|
+
*/
|
|
2604
|
+
function isWorkspaceVersion(versionRange) {
|
|
2605
|
+
return versionRange.startsWith('workspace:') || versionRange === '*' || versionRange === 'link:';
|
|
2606
|
+
}
|
|
2607
|
+
/**
|
|
2608
|
+
* Validates a single project.
|
|
2609
|
+
*
|
|
2610
|
+
* @param project - The project to validate
|
|
2611
|
+
* @returns Validation result
|
|
2612
|
+
*/
|
|
2613
|
+
function validateProject(project) {
|
|
2614
|
+
const versionResult = validateProjectVersion(project);
|
|
2615
|
+
if (!versionResult.valid) {
|
|
2616
|
+
return versionResult;
|
|
2617
|
+
}
|
|
2618
|
+
const nameResult = validateProjectName(project);
|
|
2619
|
+
if (!nameResult.valid) {
|
|
2620
|
+
return nameResult;
|
|
2621
|
+
}
|
|
2622
|
+
return { valid: true };
|
|
2623
|
+
}
|
|
2624
|
+
/**
|
|
2625
|
+
* Creates a summary of the validation report.
|
|
2626
|
+
*
|
|
2627
|
+
* @param report - Report object from workspace validation
|
|
2628
|
+
* @returns Human-readable summary
|
|
2629
|
+
*/
|
|
2630
|
+
function summarizeValidation(report) {
|
|
2631
|
+
const lines = [];
|
|
2632
|
+
if (report.valid) {
|
|
2633
|
+
lines.push('Workspace validation passed');
|
|
2634
|
+
if (report.warningCount > 0) {
|
|
2635
|
+
lines.push(` ${report.warningCount} warning(s)`);
|
|
2636
|
+
}
|
|
2637
|
+
}
|
|
2638
|
+
else {
|
|
2639
|
+
lines.push('Workspace validation failed');
|
|
2640
|
+
lines.push(` ${report.errorCount} error(s)`);
|
|
2641
|
+
lines.push(` ${report.warningCount} warning(s)`);
|
|
2642
|
+
lines.push('');
|
|
2643
|
+
lines.push('Errors:');
|
|
2644
|
+
for (const result of report.results) {
|
|
2645
|
+
if (!result.result.valid) {
|
|
2646
|
+
const pkg = result.packageName ? `[${result.packageName}] ` : '';
|
|
2647
|
+
lines.push(` ${pkg}${result.checkName}: ${result.result.error}`);
|
|
2648
|
+
}
|
|
2649
|
+
}
|
|
2650
|
+
if (report.warningCount > 0) {
|
|
2651
|
+
lines.push('');
|
|
2652
|
+
lines.push('Warnings:');
|
|
2653
|
+
for (const result of report.results) {
|
|
2654
|
+
if (result.result.warning) {
|
|
2655
|
+
const pkg = result.packageName ? `[${result.packageName}] ` : '';
|
|
2656
|
+
lines.push(` ${pkg}${result.checkName}: ${result.result.warning}`);
|
|
2657
|
+
}
|
|
2658
|
+
}
|
|
2659
|
+
}
|
|
2660
|
+
}
|
|
2661
|
+
return lines.join('\n');
|
|
2662
|
+
}
|
|
2663
|
+
|
|
2664
|
+
exports.DEFAULT_BATCH_UPDATE_OPTIONS = DEFAULT_BATCH_UPDATE_OPTIONS;
|
|
2665
|
+
exports.DEFAULT_CASCADE_OPTIONS = DEFAULT_CASCADE_OPTIONS;
|
|
2666
|
+
exports.applyBumps = applyBumps;
|
|
2667
|
+
exports.calculateCascadeBumps = calculateCascadeBumps;
|
|
2668
|
+
exports.calculateCascadeBumpsFromPackage = calculateCascadeBumpsFromPackage;
|
|
2669
|
+
exports.summarizeBatchUpdate = summarizeBatchUpdate;
|
|
2670
|
+
exports.summarizeCascadeBumps = summarizeCascadeBumps;
|
|
2671
|
+
exports.summarizeValidation = summarizeValidation;
|
|
2672
|
+
exports.updatePackageVersionInTree = updatePackageVersionInTree;
|
|
2673
|
+
exports.validateProject = validateProject;
|
|
2674
|
+
exports.validateWorkspace = validateWorkspace;
|
|
2675
|
+
//# sourceMappingURL=index.cjs.js.map
|