@lorion-org/descriptor-discovery 1.0.0-beta.0 → 1.0.0-beta.2
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/LICENSE +21 -21
- package/README.md +13 -11
- package/dist/{chunk-PIMQRGRQ.js → chunk-MUTMPDXM.js} +29 -3
- package/dist/index.cjs +29 -3
- package/dist/index.js +1 -1
- package/dist/schema.cjs +29 -3
- package/dist/schema.js +1 -1
- package/package.json +15 -4
- package/src/descriptor.schema.json +67 -0
- package/src/index.ts +336 -0
- package/src/schema.ts +5 -0
package/LICENSE
CHANGED
|
@@ -1,21 +1,21 @@
|
|
|
1
|
-
MIT License
|
|
2
|
-
|
|
3
|
-
Copyright (c) 2026
|
|
4
|
-
|
|
5
|
-
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
-
of this software and associated documentation files (the 'Software'), to deal
|
|
7
|
-
in the Software without restriction, including without limitation the rights
|
|
8
|
-
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
-
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
-
furnished to do so, subject to the following conditions:
|
|
11
|
-
|
|
12
|
-
The above copyright notice and this permission notice shall be included in all
|
|
13
|
-
copies or substantial portions of the Software.
|
|
14
|
-
|
|
15
|
-
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
-
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
-
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
-
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
-
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
-
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
-
SOFTWARE.
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the 'Software'), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -31,11 +31,11 @@ pnpm add @lorion-org/descriptor-discovery @lorion-org/composition-graph
|
|
|
31
31
|
## Directory shape
|
|
32
32
|
|
|
33
33
|
```text
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
34
|
+
layer-extensions/
|
|
35
|
+
checkout/
|
|
36
|
+
extension.json
|
|
37
|
+
shops/
|
|
38
|
+
extension.json
|
|
39
39
|
```
|
|
40
40
|
|
|
41
41
|
## Basic example
|
|
@@ -44,7 +44,8 @@ descriptors/
|
|
|
44
44
|
import { discoverDescriptors } from '@lorion-org/descriptor-discovery';
|
|
45
45
|
|
|
46
46
|
const discovered = discoverDescriptors({
|
|
47
|
-
|
|
47
|
+
cwd: './playground',
|
|
48
|
+
descriptorPaths: ['layer-extensions/*/extension.json'],
|
|
48
49
|
});
|
|
49
50
|
|
|
50
51
|
const descriptors = discovered.map((entry) => entry.descriptor);
|
|
@@ -60,9 +61,8 @@ id when the descriptor does not define one.
|
|
|
60
61
|
import { discoverDescriptors } from '@lorion-org/descriptor-discovery';
|
|
61
62
|
|
|
62
63
|
const discovered = discoverDescriptors({
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
idField: 'name',
|
|
64
|
+
cwd: './playground',
|
|
65
|
+
descriptorPaths: ['layer-extensions/*/extension.json'],
|
|
66
66
|
nestedField: 'bundles',
|
|
67
67
|
});
|
|
68
68
|
|
|
@@ -79,7 +79,9 @@ import { createDescriptorCatalog } from '@lorion-org/composition-graph';
|
|
|
79
79
|
import { discoverDescriptors } from '@lorion-org/descriptor-discovery';
|
|
80
80
|
|
|
81
81
|
const discovered = discoverDescriptors({
|
|
82
|
-
|
|
82
|
+
cwd: './playground',
|
|
83
|
+
descriptorPaths: ['layer-extensions/*/extension.json'],
|
|
84
|
+
nestedField: 'bundles',
|
|
83
85
|
});
|
|
84
86
|
|
|
85
87
|
const catalog = createDescriptorCatalog({
|
|
@@ -87,7 +89,7 @@ const catalog = createDescriptorCatalog({
|
|
|
87
89
|
});
|
|
88
90
|
|
|
89
91
|
catalog.resolveSelection({
|
|
90
|
-
selected: ['
|
|
92
|
+
selected: ['default'],
|
|
91
93
|
});
|
|
92
94
|
```
|
|
93
95
|
|
|
@@ -21,8 +21,19 @@ var descriptor_schema_default = {
|
|
|
21
21
|
},
|
|
22
22
|
version: { $ref: "#/$defs/semver" },
|
|
23
23
|
providesFor: {
|
|
24
|
-
|
|
25
|
-
|
|
24
|
+
oneOf: [
|
|
25
|
+
{
|
|
26
|
+
type: "string",
|
|
27
|
+
minLength: 1
|
|
28
|
+
},
|
|
29
|
+
{
|
|
30
|
+
type: "array",
|
|
31
|
+
items: {
|
|
32
|
+
type: "string",
|
|
33
|
+
minLength: 1
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
]
|
|
26
37
|
},
|
|
27
38
|
capabilities: {
|
|
28
39
|
type: "array",
|
|
@@ -33,7 +44,22 @@ var descriptor_schema_default = {
|
|
|
33
44
|
},
|
|
34
45
|
dependencies: { $ref: "#/$defs/dependencyMap" },
|
|
35
46
|
disabled: { type: "boolean" },
|
|
36
|
-
location: { type: "string" }
|
|
47
|
+
location: { type: "string" },
|
|
48
|
+
defaultFor: {
|
|
49
|
+
oneOf: [
|
|
50
|
+
{
|
|
51
|
+
type: "string",
|
|
52
|
+
minLength: 1
|
|
53
|
+
},
|
|
54
|
+
{
|
|
55
|
+
type: "array",
|
|
56
|
+
items: {
|
|
57
|
+
type: "string",
|
|
58
|
+
minLength: 1
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
]
|
|
62
|
+
}
|
|
37
63
|
},
|
|
38
64
|
required: ["id", "version"],
|
|
39
65
|
additionalProperties: true
|
package/dist/index.cjs
CHANGED
|
@@ -62,8 +62,19 @@ var descriptor_schema_default = {
|
|
|
62
62
|
},
|
|
63
63
|
version: { $ref: "#/$defs/semver" },
|
|
64
64
|
providesFor: {
|
|
65
|
-
|
|
66
|
-
|
|
65
|
+
oneOf: [
|
|
66
|
+
{
|
|
67
|
+
type: "string",
|
|
68
|
+
minLength: 1
|
|
69
|
+
},
|
|
70
|
+
{
|
|
71
|
+
type: "array",
|
|
72
|
+
items: {
|
|
73
|
+
type: "string",
|
|
74
|
+
minLength: 1
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
]
|
|
67
78
|
},
|
|
68
79
|
capabilities: {
|
|
69
80
|
type: "array",
|
|
@@ -74,7 +85,22 @@ var descriptor_schema_default = {
|
|
|
74
85
|
},
|
|
75
86
|
dependencies: { $ref: "#/$defs/dependencyMap" },
|
|
76
87
|
disabled: { type: "boolean" },
|
|
77
|
-
location: { type: "string" }
|
|
88
|
+
location: { type: "string" },
|
|
89
|
+
defaultFor: {
|
|
90
|
+
oneOf: [
|
|
91
|
+
{
|
|
92
|
+
type: "string",
|
|
93
|
+
minLength: 1
|
|
94
|
+
},
|
|
95
|
+
{
|
|
96
|
+
type: "array",
|
|
97
|
+
items: {
|
|
98
|
+
type: "string",
|
|
99
|
+
minLength: 1
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
]
|
|
103
|
+
}
|
|
78
104
|
},
|
|
79
105
|
required: ["id", "version"],
|
|
80
106
|
additionalProperties: true
|
package/dist/index.js
CHANGED
package/dist/schema.cjs
CHANGED
|
@@ -47,8 +47,19 @@ var descriptor_schema_default = {
|
|
|
47
47
|
},
|
|
48
48
|
version: { $ref: "#/$defs/semver" },
|
|
49
49
|
providesFor: {
|
|
50
|
-
|
|
51
|
-
|
|
50
|
+
oneOf: [
|
|
51
|
+
{
|
|
52
|
+
type: "string",
|
|
53
|
+
minLength: 1
|
|
54
|
+
},
|
|
55
|
+
{
|
|
56
|
+
type: "array",
|
|
57
|
+
items: {
|
|
58
|
+
type: "string",
|
|
59
|
+
minLength: 1
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
]
|
|
52
63
|
},
|
|
53
64
|
capabilities: {
|
|
54
65
|
type: "array",
|
|
@@ -59,7 +70,22 @@ var descriptor_schema_default = {
|
|
|
59
70
|
},
|
|
60
71
|
dependencies: { $ref: "#/$defs/dependencyMap" },
|
|
61
72
|
disabled: { type: "boolean" },
|
|
62
|
-
location: { type: "string" }
|
|
73
|
+
location: { type: "string" },
|
|
74
|
+
defaultFor: {
|
|
75
|
+
oneOf: [
|
|
76
|
+
{
|
|
77
|
+
type: "string",
|
|
78
|
+
minLength: 1
|
|
79
|
+
},
|
|
80
|
+
{
|
|
81
|
+
type: "array",
|
|
82
|
+
items: {
|
|
83
|
+
type: "string",
|
|
84
|
+
minLength: 1
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
]
|
|
88
|
+
}
|
|
63
89
|
},
|
|
64
90
|
required: ["id", "version"],
|
|
65
91
|
additionalProperties: true
|
package/dist/schema.js
CHANGED
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@lorion-org/descriptor-discovery",
|
|
3
|
-
"version": "1.0.0-beta.
|
|
3
|
+
"version": "1.0.0-beta.2",
|
|
4
4
|
"description": "Node-based descriptor discovery and normalization helpers.",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"repository": {
|
|
@@ -22,6 +22,10 @@
|
|
|
22
22
|
"types": "./dist/index.d.ts",
|
|
23
23
|
"exports": {
|
|
24
24
|
".": {
|
|
25
|
+
"lorion-source": {
|
|
26
|
+
"types": "./src/index.ts",
|
|
27
|
+
"default": "./src/index.ts"
|
|
28
|
+
},
|
|
25
29
|
"import": {
|
|
26
30
|
"types": "./dist/index.d.ts",
|
|
27
31
|
"default": "./dist/index.js"
|
|
@@ -32,6 +36,10 @@
|
|
|
32
36
|
}
|
|
33
37
|
},
|
|
34
38
|
"./schema": {
|
|
39
|
+
"lorion-source": {
|
|
40
|
+
"types": "./src/schema.ts",
|
|
41
|
+
"default": "./src/schema.ts"
|
|
42
|
+
},
|
|
35
43
|
"import": {
|
|
36
44
|
"types": "./dist/schema.d.ts",
|
|
37
45
|
"default": "./dist/schema.js"
|
|
@@ -44,7 +52,10 @@
|
|
|
44
52
|
},
|
|
45
53
|
"files": [
|
|
46
54
|
"dist",
|
|
47
|
-
"LICENSE"
|
|
55
|
+
"LICENSE",
|
|
56
|
+
"src/**/*.ts",
|
|
57
|
+
"src/**/*.json",
|
|
58
|
+
"!src/**/*.spec.ts"
|
|
48
59
|
],
|
|
49
60
|
"keywords": [
|
|
50
61
|
"descriptor",
|
|
@@ -56,13 +67,13 @@
|
|
|
56
67
|
"node": "^20.19.0 || >=22.12.0"
|
|
57
68
|
},
|
|
58
69
|
"peerDependencies": {
|
|
59
|
-
"@lorion-org/composition-graph": "^1.0.0-beta.
|
|
70
|
+
"@lorion-org/composition-graph": "^1.0.0-beta.2"
|
|
60
71
|
},
|
|
61
72
|
"dependencies": {
|
|
62
73
|
"ajv": "^8.17.1"
|
|
63
74
|
},
|
|
64
75
|
"devDependencies": {
|
|
65
|
-
"@lorion-org/composition-graph": "1.0.0-beta.
|
|
76
|
+
"@lorion-org/composition-graph": "1.0.0-beta.2"
|
|
66
77
|
},
|
|
67
78
|
"scripts": {
|
|
68
79
|
"build": "tsup src/index.ts src/schema.ts --format esm,cjs --dts",
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
{
|
|
2
|
+
"$schema": "http://json-schema.org/draft-07/schema#",
|
|
3
|
+
"$id": "https://lorion.dev/schemas/descriptor.schema.json",
|
|
4
|
+
"$ref": "#/$defs/descriptor",
|
|
5
|
+
"$defs": {
|
|
6
|
+
"semver": {
|
|
7
|
+
"type": "string",
|
|
8
|
+
"pattern": "^(\\^|~)?\\d+\\.\\d+\\.\\d+$"
|
|
9
|
+
},
|
|
10
|
+
"dependencyMap": {
|
|
11
|
+
"type": "object",
|
|
12
|
+
"additionalProperties": { "$ref": "#/$defs/semver" }
|
|
13
|
+
},
|
|
14
|
+
"descriptor": {
|
|
15
|
+
"type": "object",
|
|
16
|
+
"properties": {
|
|
17
|
+
"id": {
|
|
18
|
+
"type": "string",
|
|
19
|
+
"minLength": 1
|
|
20
|
+
},
|
|
21
|
+
"version": { "$ref": "#/$defs/semver" },
|
|
22
|
+
"providesFor": {
|
|
23
|
+
"oneOf": [
|
|
24
|
+
{
|
|
25
|
+
"type": "string",
|
|
26
|
+
"minLength": 1
|
|
27
|
+
},
|
|
28
|
+
{
|
|
29
|
+
"type": "array",
|
|
30
|
+
"items": {
|
|
31
|
+
"type": "string",
|
|
32
|
+
"minLength": 1
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
]
|
|
36
|
+
},
|
|
37
|
+
"capabilities": {
|
|
38
|
+
"type": "array",
|
|
39
|
+
"items": {
|
|
40
|
+
"type": "string",
|
|
41
|
+
"minLength": 1
|
|
42
|
+
}
|
|
43
|
+
},
|
|
44
|
+
"dependencies": { "$ref": "#/$defs/dependencyMap" },
|
|
45
|
+
"disabled": { "type": "boolean" },
|
|
46
|
+
"location": { "type": "string" },
|
|
47
|
+
"defaultFor": {
|
|
48
|
+
"oneOf": [
|
|
49
|
+
{
|
|
50
|
+
"type": "string",
|
|
51
|
+
"minLength": 1
|
|
52
|
+
},
|
|
53
|
+
{
|
|
54
|
+
"type": "array",
|
|
55
|
+
"items": {
|
|
56
|
+
"type": "string",
|
|
57
|
+
"minLength": 1
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
]
|
|
61
|
+
}
|
|
62
|
+
},
|
|
63
|
+
"required": ["id", "version"],
|
|
64
|
+
"additionalProperties": true
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,336 @@
|
|
|
1
|
+
import { existsSync, readFileSync, readdirSync } from 'node:fs';
|
|
2
|
+
import { basename, dirname, join, resolve as resolvePath } from 'node:path';
|
|
3
|
+
|
|
4
|
+
import Ajv, { type ErrorObject, type Options as AjvOptions } from 'ajv';
|
|
5
|
+
import type { Descriptor } from '@lorion-org/composition-graph';
|
|
6
|
+
|
|
7
|
+
export { descriptorSchema, type JsonSchemaObject } from './schema';
|
|
8
|
+
|
|
9
|
+
export type RawDescriptor = Omit<Descriptor, 'id'> & {
|
|
10
|
+
id?: string;
|
|
11
|
+
};
|
|
12
|
+
|
|
13
|
+
export type DiscoveredDescriptor = {
|
|
14
|
+
id: string;
|
|
15
|
+
cwd: string;
|
|
16
|
+
descriptorPath: string;
|
|
17
|
+
descriptor: Descriptor;
|
|
18
|
+
};
|
|
19
|
+
|
|
20
|
+
export type DescriptorSchemaValidationTarget = {
|
|
21
|
+
descriptorPath: string;
|
|
22
|
+
};
|
|
23
|
+
|
|
24
|
+
export type DescriptorSchemaValidationErrorFormatter = (
|
|
25
|
+
target: DescriptorSchemaValidationTarget,
|
|
26
|
+
validationError: ErrorObject,
|
|
27
|
+
) => Error;
|
|
28
|
+
|
|
29
|
+
export type DescriptorValidationOptions = {
|
|
30
|
+
ajvOptions?: AjvOptions;
|
|
31
|
+
formatError?: DescriptorSchemaValidationErrorFormatter;
|
|
32
|
+
schema: object;
|
|
33
|
+
};
|
|
34
|
+
|
|
35
|
+
export type ExpandNestedDescriptorsInput = {
|
|
36
|
+
rawDescriptor: RawDescriptor & Record<string, unknown>;
|
|
37
|
+
fallbackId: string;
|
|
38
|
+
idField?: string;
|
|
39
|
+
nestedField?: string;
|
|
40
|
+
};
|
|
41
|
+
|
|
42
|
+
export type DiscoverDescriptorsInput = {
|
|
43
|
+
cwd?: string;
|
|
44
|
+
descriptorPaths?: string[];
|
|
45
|
+
roots?: string[];
|
|
46
|
+
descriptorFileName?: string;
|
|
47
|
+
idField?: string;
|
|
48
|
+
maxDepth?: number;
|
|
49
|
+
nestedField?: string;
|
|
50
|
+
validation?: false | DescriptorValidationOptions;
|
|
51
|
+
};
|
|
52
|
+
|
|
53
|
+
function formatDescriptorSchemaValidationError(
|
|
54
|
+
target: DescriptorSchemaValidationTarget,
|
|
55
|
+
validationError: ErrorObject,
|
|
56
|
+
): Error {
|
|
57
|
+
const jsonPath = validationError.instancePath || '/';
|
|
58
|
+
const ajvError = `${validationError.keyword}${validationError.message ? `: ${validationError.message}` : ''}`;
|
|
59
|
+
|
|
60
|
+
return new Error(
|
|
61
|
+
[
|
|
62
|
+
'Descriptor schema validation failed.',
|
|
63
|
+
`File: ${target.descriptorPath}`,
|
|
64
|
+
`JSON path: ${jsonPath}`,
|
|
65
|
+
`Schema error: ${ajvError}`,
|
|
66
|
+
].join('\n'),
|
|
67
|
+
);
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
function createDescriptorValidator(
|
|
71
|
+
options: false | DescriptorValidationOptions | undefined,
|
|
72
|
+
): ((target: DescriptorSchemaValidationTarget, descriptor: object) => void) | undefined {
|
|
73
|
+
if (!options) return undefined;
|
|
74
|
+
|
|
75
|
+
const ajv = new Ajv({
|
|
76
|
+
strict: false,
|
|
77
|
+
allErrors: false,
|
|
78
|
+
...options.ajvOptions,
|
|
79
|
+
});
|
|
80
|
+
const validate = ajv.compile(options.schema);
|
|
81
|
+
const formatError = options.formatError ?? formatDescriptorSchemaValidationError;
|
|
82
|
+
|
|
83
|
+
return (target, descriptor) => {
|
|
84
|
+
if (validate(descriptor)) return;
|
|
85
|
+
|
|
86
|
+
const validationError = validate.errors?.[0];
|
|
87
|
+
if (validationError) throw formatError(target, validationError);
|
|
88
|
+
|
|
89
|
+
throw new Error(`Descriptor schema validation failed: "${target.descriptorPath}"`);
|
|
90
|
+
};
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
function resolveDescriptorId(input: {
|
|
94
|
+
rawDescriptor: Record<string, unknown>;
|
|
95
|
+
idField: string;
|
|
96
|
+
fallbackId?: string;
|
|
97
|
+
label: string;
|
|
98
|
+
}): string {
|
|
99
|
+
const configuredId = input.rawDescriptor[input.idField];
|
|
100
|
+
|
|
101
|
+
if (typeof configuredId === 'string' && configuredId.trim()) {
|
|
102
|
+
return configuredId.trim();
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
if (typeof input.fallbackId === 'string' && input.fallbackId.trim()) {
|
|
106
|
+
return input.fallbackId.trim();
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
throw new Error(`${input.label} is missing a non-empty "${input.idField}" field`);
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
function getNestedDescriptors(input: {
|
|
113
|
+
rawDescriptor: Record<string, unknown>;
|
|
114
|
+
nestedField?: string;
|
|
115
|
+
parentId: string;
|
|
116
|
+
}): Array<RawDescriptor & Record<string, unknown>> {
|
|
117
|
+
if (!input.nestedField) return [];
|
|
118
|
+
|
|
119
|
+
const nestedValue = input.rawDescriptor[input.nestedField];
|
|
120
|
+
|
|
121
|
+
if (nestedValue === undefined) return [];
|
|
122
|
+
if (!Array.isArray(nestedValue))
|
|
123
|
+
throw new Error(`Descriptor "${input.parentId}" field "${input.nestedField}" must be an array`);
|
|
124
|
+
|
|
125
|
+
return nestedValue.map((entry, index) => {
|
|
126
|
+
if (typeof entry !== 'object' || entry === null || Array.isArray(entry)) {
|
|
127
|
+
throw new Error(
|
|
128
|
+
`Descriptor "${input.parentId}" field "${input.nestedField}" contains an invalid entry at index ${index}`,
|
|
129
|
+
);
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
return entry as RawDescriptor & Record<string, unknown>;
|
|
133
|
+
});
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
function normalizeDescriptor(input: {
|
|
137
|
+
rawDescriptor: RawDescriptor & Record<string, unknown>;
|
|
138
|
+
id: string;
|
|
139
|
+
nestedField?: string;
|
|
140
|
+
}): Descriptor {
|
|
141
|
+
const version =
|
|
142
|
+
typeof input.rawDescriptor.version === 'string' && input.rawDescriptor.version.trim()
|
|
143
|
+
? input.rawDescriptor.version
|
|
144
|
+
: '0.0.0';
|
|
145
|
+
|
|
146
|
+
if (input.nestedField) {
|
|
147
|
+
const descriptor = { ...input.rawDescriptor };
|
|
148
|
+
|
|
149
|
+
delete descriptor[input.nestedField];
|
|
150
|
+
|
|
151
|
+
return {
|
|
152
|
+
...descriptor,
|
|
153
|
+
id: input.id,
|
|
154
|
+
version,
|
|
155
|
+
};
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
return {
|
|
159
|
+
...input.rawDescriptor,
|
|
160
|
+
id: input.id,
|
|
161
|
+
version,
|
|
162
|
+
};
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
export function expandNestedDescriptors(input: ExpandNestedDescriptorsInput): Descriptor[] {
|
|
166
|
+
const idField: string = input.idField ?? 'id';
|
|
167
|
+
const rootId = resolveDescriptorId({
|
|
168
|
+
rawDescriptor: input.rawDescriptor,
|
|
169
|
+
idField,
|
|
170
|
+
fallbackId: input.fallbackId,
|
|
171
|
+
label: 'Descriptor',
|
|
172
|
+
});
|
|
173
|
+
const nestedDescriptors = getNestedDescriptors({
|
|
174
|
+
rawDescriptor: input.rawDescriptor,
|
|
175
|
+
parentId: rootId,
|
|
176
|
+
...(input.nestedField ? { nestedField: input.nestedField } : {}),
|
|
177
|
+
});
|
|
178
|
+
const descriptors: Descriptor[] = [
|
|
179
|
+
normalizeDescriptor({
|
|
180
|
+
rawDescriptor: input.rawDescriptor,
|
|
181
|
+
id: rootId,
|
|
182
|
+
...(input.nestedField ? { nestedField: input.nestedField } : {}),
|
|
183
|
+
}),
|
|
184
|
+
];
|
|
185
|
+
|
|
186
|
+
for (const nestedDescriptor of nestedDescriptors) {
|
|
187
|
+
const nestedId = resolveDescriptorId({
|
|
188
|
+
rawDescriptor: nestedDescriptor,
|
|
189
|
+
idField,
|
|
190
|
+
label: `Nested descriptor in "${rootId}"`,
|
|
191
|
+
});
|
|
192
|
+
const nestedChildren = getNestedDescriptors({
|
|
193
|
+
rawDescriptor: nestedDescriptor,
|
|
194
|
+
parentId: nestedId,
|
|
195
|
+
...(input.nestedField ? { nestedField: input.nestedField } : {}),
|
|
196
|
+
});
|
|
197
|
+
|
|
198
|
+
if (nestedChildren.length) {
|
|
199
|
+
throw new Error(`Nested descriptors are not supported inside descriptor "${nestedId}"`);
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
descriptors.push(
|
|
203
|
+
normalizeDescriptor({
|
|
204
|
+
rawDescriptor: nestedDescriptor,
|
|
205
|
+
id: nestedId,
|
|
206
|
+
...(input.nestedField ? { nestedField: input.nestedField } : {}),
|
|
207
|
+
}),
|
|
208
|
+
);
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
return descriptors;
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
function escapeRegex(value: string): string {
|
|
215
|
+
return value.replace(/[.+?^${}()|[\]\\]/g, '\\$&');
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
function createGlobSegmentRegex(segment: string): RegExp {
|
|
219
|
+
return new RegExp(`^${segment.split('*').map(escapeRegex).join('[^/\\\\]*')}$`);
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
function splitPattern(pattern: string): string[] {
|
|
223
|
+
return pattern.split(/[\\/]+/).filter(Boolean);
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
function expandDescriptorPathPattern(input: { cwd: string; pattern: string }): string[] {
|
|
227
|
+
const segments = splitPattern(input.pattern);
|
|
228
|
+
const visit = (currentDir: string, index: number): string[] => {
|
|
229
|
+
const segment = segments[index];
|
|
230
|
+
if (!segment) return [];
|
|
231
|
+
|
|
232
|
+
const isLast = index === segments.length - 1;
|
|
233
|
+
|
|
234
|
+
if (!segment.includes('*')) {
|
|
235
|
+
const nextPath = join(currentDir, segment);
|
|
236
|
+
|
|
237
|
+
if (isLast) return existsSync(nextPath) ? [nextPath] : [];
|
|
238
|
+
if (!existsSync(nextPath)) return [];
|
|
239
|
+
|
|
240
|
+
return visit(nextPath, index + 1);
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
if (!existsSync(currentDir)) return [];
|
|
244
|
+
|
|
245
|
+
const matcher = createGlobSegmentRegex(segment);
|
|
246
|
+
|
|
247
|
+
return readdirSync(currentDir, { withFileTypes: true })
|
|
248
|
+
.filter((entry) => entry.name !== 'node_modules' && matcher.test(entry.name))
|
|
249
|
+
.flatMap((entry) => {
|
|
250
|
+
const nextPath = join(currentDir, entry.name);
|
|
251
|
+
|
|
252
|
+
if (isLast) return entry.isFile() ? [nextPath] : [];
|
|
253
|
+
return entry.isDirectory() ? visit(nextPath, index + 1) : [];
|
|
254
|
+
});
|
|
255
|
+
};
|
|
256
|
+
|
|
257
|
+
return visit(resolvePath(input.cwd), 0);
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
function discoverDescriptorFiles(input: { cwd: string; descriptorPaths: string[] }): string[] {
|
|
261
|
+
return [
|
|
262
|
+
...new Set(
|
|
263
|
+
input.descriptorPaths.flatMap((pattern) =>
|
|
264
|
+
expandDescriptorPathPattern({
|
|
265
|
+
cwd: input.cwd,
|
|
266
|
+
pattern,
|
|
267
|
+
}),
|
|
268
|
+
),
|
|
269
|
+
),
|
|
270
|
+
].sort();
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
function discoverDescriptorFilesFromRoots(input: {
|
|
274
|
+
descriptorFileName: string;
|
|
275
|
+
maxDepth: number;
|
|
276
|
+
roots: string[];
|
|
277
|
+
}): string[] {
|
|
278
|
+
const discovered: string[] = [];
|
|
279
|
+
const visit = (root: string, depth: number): void => {
|
|
280
|
+
if (depth > input.maxDepth || !existsSync(root)) return;
|
|
281
|
+
|
|
282
|
+
for (const entry of readdirSync(root, { withFileTypes: true })) {
|
|
283
|
+
if (!entry.isDirectory() || entry.name === 'node_modules') continue;
|
|
284
|
+
|
|
285
|
+
const cwd: string = join(root, entry.name);
|
|
286
|
+
const descriptorPath: string = join(cwd, input.descriptorFileName);
|
|
287
|
+
|
|
288
|
+
if (existsSync(descriptorPath)) discovered.push(descriptorPath);
|
|
289
|
+
|
|
290
|
+
visit(cwd, depth + 1);
|
|
291
|
+
}
|
|
292
|
+
};
|
|
293
|
+
|
|
294
|
+
for (const root of input.roots) {
|
|
295
|
+
visit(resolvePath(root), 1);
|
|
296
|
+
}
|
|
297
|
+
|
|
298
|
+
return [...new Set(discovered)].sort();
|
|
299
|
+
}
|
|
300
|
+
|
|
301
|
+
export function discoverDescriptors(input: DiscoverDescriptorsInput): DiscoveredDescriptor[] {
|
|
302
|
+
const descriptorFileName: string = input.descriptorFileName ?? 'descriptor.json';
|
|
303
|
+
const idField: string = input.idField ?? 'id';
|
|
304
|
+
const maxDepth = input.maxDepth ?? 1;
|
|
305
|
+
const validateDescriptor = createDescriptorValidator(input.validation);
|
|
306
|
+
const descriptorPaths = input.descriptorPaths?.length
|
|
307
|
+
? discoverDescriptorFiles({
|
|
308
|
+
cwd: input.cwd ?? '',
|
|
309
|
+
descriptorPaths: input.descriptorPaths,
|
|
310
|
+
})
|
|
311
|
+
: discoverDescriptorFilesFromRoots({
|
|
312
|
+
descriptorFileName,
|
|
313
|
+
maxDepth,
|
|
314
|
+
roots: input.roots ?? [],
|
|
315
|
+
});
|
|
316
|
+
|
|
317
|
+
return descriptorPaths.flatMap((descriptorPath) => {
|
|
318
|
+
const cwd = dirname(descriptorPath);
|
|
319
|
+
const rawDescriptor = JSON.parse(readFileSync(descriptorPath, 'utf8')) as RawDescriptor &
|
|
320
|
+
Record<string, unknown>;
|
|
321
|
+
|
|
322
|
+
validateDescriptor?.({ descriptorPath }, rawDescriptor);
|
|
323
|
+
|
|
324
|
+
return expandNestedDescriptors({
|
|
325
|
+
rawDescriptor,
|
|
326
|
+
fallbackId: basename(cwd),
|
|
327
|
+
idField,
|
|
328
|
+
...(input.nestedField ? { nestedField: input.nestedField } : {}),
|
|
329
|
+
}).map((descriptor) => ({
|
|
330
|
+
id: descriptor.id,
|
|
331
|
+
cwd,
|
|
332
|
+
descriptorPath,
|
|
333
|
+
descriptor,
|
|
334
|
+
}));
|
|
335
|
+
});
|
|
336
|
+
}
|