@lorion-org/descriptor-discovery 1.0.0-beta.1 → 1.0.0-beta.3
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/README.md +3 -3
- package/dist/{chunk-PIMQRGRQ.js → chunk-MUTMPDXM.js} +29 -3
- package/dist/index.cjs +39 -5
- package/dist/index.d.cts +4 -1
- package/dist/index.d.ts +4 -1
- package/dist/index.js +9 -2
- 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 +350 -0
- package/src/schema.ts +5 -0
package/README.md
CHANGED
|
@@ -44,7 +44,7 @@ layer-extensions/
|
|
|
44
44
|
import { discoverDescriptors } from '@lorion-org/descriptor-discovery';
|
|
45
45
|
|
|
46
46
|
const discovered = discoverDescriptors({
|
|
47
|
-
cwd: '
|
|
47
|
+
cwd: '.',
|
|
48
48
|
descriptorPaths: ['layer-extensions/*/extension.json'],
|
|
49
49
|
});
|
|
50
50
|
|
|
@@ -61,7 +61,7 @@ id when the descriptor does not define one.
|
|
|
61
61
|
import { discoverDescriptors } from '@lorion-org/descriptor-discovery';
|
|
62
62
|
|
|
63
63
|
const discovered = discoverDescriptors({
|
|
64
|
-
cwd: '
|
|
64
|
+
cwd: '.',
|
|
65
65
|
descriptorPaths: ['layer-extensions/*/extension.json'],
|
|
66
66
|
nestedField: 'bundles',
|
|
67
67
|
});
|
|
@@ -79,7 +79,7 @@ import { createDescriptorCatalog } from '@lorion-org/composition-graph';
|
|
|
79
79
|
import { discoverDescriptors } from '@lorion-org/descriptor-discovery';
|
|
80
80
|
|
|
81
81
|
const discovered = discoverDescriptors({
|
|
82
|
-
cwd: '
|
|
82
|
+
cwd: '.',
|
|
83
83
|
descriptorPaths: ['layer-extensions/*/extension.json'],
|
|
84
84
|
nestedField: 'bundles',
|
|
85
85
|
});
|
|
@@ -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
|
@@ -32,7 +32,8 @@ var index_exports = {};
|
|
|
32
32
|
__export(index_exports, {
|
|
33
33
|
descriptorSchema: () => descriptorSchema,
|
|
34
34
|
discoverDescriptors: () => discoverDescriptors,
|
|
35
|
-
expandNestedDescriptors: () => expandNestedDescriptors
|
|
35
|
+
expandNestedDescriptors: () => expandNestedDescriptors,
|
|
36
|
+
requirePackageName: () => requirePackageName
|
|
36
37
|
});
|
|
37
38
|
module.exports = __toCommonJS(index_exports);
|
|
38
39
|
var import_node_fs = require("fs");
|
|
@@ -62,8 +63,19 @@ var descriptor_schema_default = {
|
|
|
62
63
|
},
|
|
63
64
|
version: { $ref: "#/$defs/semver" },
|
|
64
65
|
providesFor: {
|
|
65
|
-
|
|
66
|
-
|
|
66
|
+
oneOf: [
|
|
67
|
+
{
|
|
68
|
+
type: "string",
|
|
69
|
+
minLength: 1
|
|
70
|
+
},
|
|
71
|
+
{
|
|
72
|
+
type: "array",
|
|
73
|
+
items: {
|
|
74
|
+
type: "string",
|
|
75
|
+
minLength: 1
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
]
|
|
67
79
|
},
|
|
68
80
|
capabilities: {
|
|
69
81
|
type: "array",
|
|
@@ -74,7 +86,22 @@ var descriptor_schema_default = {
|
|
|
74
86
|
},
|
|
75
87
|
dependencies: { $ref: "#/$defs/dependencyMap" },
|
|
76
88
|
disabled: { type: "boolean" },
|
|
77
|
-
location: { type: "string" }
|
|
89
|
+
location: { type: "string" },
|
|
90
|
+
defaultFor: {
|
|
91
|
+
oneOf: [
|
|
92
|
+
{
|
|
93
|
+
type: "string",
|
|
94
|
+
minLength: 1
|
|
95
|
+
},
|
|
96
|
+
{
|
|
97
|
+
type: "array",
|
|
98
|
+
items: {
|
|
99
|
+
type: "string",
|
|
100
|
+
minLength: 1
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
]
|
|
104
|
+
}
|
|
78
105
|
},
|
|
79
106
|
required: ["id", "version"],
|
|
80
107
|
additionalProperties: true
|
|
@@ -86,6 +113,12 @@ var descriptor_schema_default = {
|
|
|
86
113
|
var descriptorSchema = descriptor_schema_default;
|
|
87
114
|
|
|
88
115
|
// src/index.ts
|
|
116
|
+
function requirePackageName(packageJson, packageJsonPath) {
|
|
117
|
+
if (typeof packageJson.name !== "string") {
|
|
118
|
+
throw new Error(`Capability package is missing "name": ${packageJsonPath}`);
|
|
119
|
+
}
|
|
120
|
+
return packageJson.name;
|
|
121
|
+
}
|
|
89
122
|
function formatDescriptorSchemaValidationError(target, validationError) {
|
|
90
123
|
const jsonPath = validationError.instancePath || "/";
|
|
91
124
|
const ajvError = `${validationError.keyword}${validationError.message ? `: ${validationError.message}` : ""}`;
|
|
@@ -294,5 +327,6 @@ function discoverDescriptors(input) {
|
|
|
294
327
|
0 && (module.exports = {
|
|
295
328
|
descriptorSchema,
|
|
296
329
|
discoverDescriptors,
|
|
297
|
-
expandNestedDescriptors
|
|
330
|
+
expandNestedDescriptors,
|
|
331
|
+
requirePackageName
|
|
298
332
|
});
|
package/dist/index.d.cts
CHANGED
|
@@ -2,6 +2,9 @@ import { ErrorObject, Options } from 'ajv';
|
|
|
2
2
|
import { Descriptor } from '@lorion-org/composition-graph';
|
|
3
3
|
export { JsonSchemaObject, descriptorSchema } from './schema.cjs';
|
|
4
4
|
|
|
5
|
+
declare function requirePackageName(packageJson: {
|
|
6
|
+
name?: unknown;
|
|
7
|
+
}, packageJsonPath: string): string;
|
|
5
8
|
type RawDescriptor = Omit<Descriptor, 'id'> & {
|
|
6
9
|
id?: string;
|
|
7
10
|
};
|
|
@@ -39,4 +42,4 @@ type DiscoverDescriptorsInput = {
|
|
|
39
42
|
declare function expandNestedDescriptors(input: ExpandNestedDescriptorsInput): Descriptor[];
|
|
40
43
|
declare function discoverDescriptors(input: DiscoverDescriptorsInput): DiscoveredDescriptor[];
|
|
41
44
|
|
|
42
|
-
export { type DescriptorSchemaValidationErrorFormatter, type DescriptorSchemaValidationTarget, type DescriptorValidationOptions, type DiscoverDescriptorsInput, type DiscoveredDescriptor, type ExpandNestedDescriptorsInput, type RawDescriptor, discoverDescriptors, expandNestedDescriptors };
|
|
45
|
+
export { type DescriptorSchemaValidationErrorFormatter, type DescriptorSchemaValidationTarget, type DescriptorValidationOptions, type DiscoverDescriptorsInput, type DiscoveredDescriptor, type ExpandNestedDescriptorsInput, type RawDescriptor, discoverDescriptors, expandNestedDescriptors, requirePackageName };
|
package/dist/index.d.ts
CHANGED
|
@@ -2,6 +2,9 @@ import { ErrorObject, Options } from 'ajv';
|
|
|
2
2
|
import { Descriptor } from '@lorion-org/composition-graph';
|
|
3
3
|
export { JsonSchemaObject, descriptorSchema } from './schema.js';
|
|
4
4
|
|
|
5
|
+
declare function requirePackageName(packageJson: {
|
|
6
|
+
name?: unknown;
|
|
7
|
+
}, packageJsonPath: string): string;
|
|
5
8
|
type RawDescriptor = Omit<Descriptor, 'id'> & {
|
|
6
9
|
id?: string;
|
|
7
10
|
};
|
|
@@ -39,4 +42,4 @@ type DiscoverDescriptorsInput = {
|
|
|
39
42
|
declare function expandNestedDescriptors(input: ExpandNestedDescriptorsInput): Descriptor[];
|
|
40
43
|
declare function discoverDescriptors(input: DiscoverDescriptorsInput): DiscoveredDescriptor[];
|
|
41
44
|
|
|
42
|
-
export { type DescriptorSchemaValidationErrorFormatter, type DescriptorSchemaValidationTarget, type DescriptorValidationOptions, type DiscoverDescriptorsInput, type DiscoveredDescriptor, type ExpandNestedDescriptorsInput, type RawDescriptor, discoverDescriptors, expandNestedDescriptors };
|
|
45
|
+
export { type DescriptorSchemaValidationErrorFormatter, type DescriptorSchemaValidationTarget, type DescriptorValidationOptions, type DiscoverDescriptorsInput, type DiscoveredDescriptor, type ExpandNestedDescriptorsInput, type RawDescriptor, discoverDescriptors, expandNestedDescriptors, requirePackageName };
|
package/dist/index.js
CHANGED
|
@@ -1,11 +1,17 @@
|
|
|
1
1
|
import {
|
|
2
2
|
descriptorSchema
|
|
3
|
-
} from "./chunk-
|
|
3
|
+
} from "./chunk-MUTMPDXM.js";
|
|
4
4
|
|
|
5
5
|
// src/index.ts
|
|
6
6
|
import { existsSync, readFileSync, readdirSync } from "fs";
|
|
7
7
|
import { basename, dirname, join, resolve as resolvePath } from "path";
|
|
8
8
|
import Ajv from "ajv";
|
|
9
|
+
function requirePackageName(packageJson, packageJsonPath) {
|
|
10
|
+
if (typeof packageJson.name !== "string") {
|
|
11
|
+
throw new Error(`Capability package is missing "name": ${packageJsonPath}`);
|
|
12
|
+
}
|
|
13
|
+
return packageJson.name;
|
|
14
|
+
}
|
|
9
15
|
function formatDescriptorSchemaValidationError(target, validationError) {
|
|
10
16
|
const jsonPath = validationError.instancePath || "/";
|
|
11
17
|
const ajvError = `${validationError.keyword}${validationError.message ? `: ${validationError.message}` : ""}`;
|
|
@@ -213,5 +219,6 @@ function discoverDescriptors(input) {
|
|
|
213
219
|
export {
|
|
214
220
|
descriptorSchema,
|
|
215
221
|
discoverDescriptors,
|
|
216
|
-
expandNestedDescriptors
|
|
222
|
+
expandNestedDescriptors,
|
|
223
|
+
requirePackageName
|
|
217
224
|
};
|
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.3",
|
|
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,350 @@
|
|
|
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
|
+
// A capability lives on disk as a package: its descriptor (capability.json) beside
|
|
10
|
+
// a package.json. Validating that the package declares a `name` — with one shared
|
|
11
|
+
// error message — belongs here next to descriptor discovery, so each host does not
|
|
12
|
+
// reimplement it. Takes an already-parsed package.json to avoid re-reading it.
|
|
13
|
+
export function requirePackageName(
|
|
14
|
+
packageJson: { name?: unknown },
|
|
15
|
+
packageJsonPath: string,
|
|
16
|
+
): string {
|
|
17
|
+
if (typeof packageJson.name !== 'string') {
|
|
18
|
+
throw new Error(`Capability package is missing "name": ${packageJsonPath}`);
|
|
19
|
+
}
|
|
20
|
+
return packageJson.name;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export type RawDescriptor = Omit<Descriptor, 'id'> & {
|
|
24
|
+
id?: string;
|
|
25
|
+
};
|
|
26
|
+
|
|
27
|
+
export type DiscoveredDescriptor = {
|
|
28
|
+
id: string;
|
|
29
|
+
cwd: string;
|
|
30
|
+
descriptorPath: string;
|
|
31
|
+
descriptor: Descriptor;
|
|
32
|
+
};
|
|
33
|
+
|
|
34
|
+
export type DescriptorSchemaValidationTarget = {
|
|
35
|
+
descriptorPath: string;
|
|
36
|
+
};
|
|
37
|
+
|
|
38
|
+
export type DescriptorSchemaValidationErrorFormatter = (
|
|
39
|
+
target: DescriptorSchemaValidationTarget,
|
|
40
|
+
validationError: ErrorObject,
|
|
41
|
+
) => Error;
|
|
42
|
+
|
|
43
|
+
export type DescriptorValidationOptions = {
|
|
44
|
+
ajvOptions?: AjvOptions;
|
|
45
|
+
formatError?: DescriptorSchemaValidationErrorFormatter;
|
|
46
|
+
schema: object;
|
|
47
|
+
};
|
|
48
|
+
|
|
49
|
+
export type ExpandNestedDescriptorsInput = {
|
|
50
|
+
rawDescriptor: RawDescriptor & Record<string, unknown>;
|
|
51
|
+
fallbackId: string;
|
|
52
|
+
idField?: string;
|
|
53
|
+
nestedField?: string;
|
|
54
|
+
};
|
|
55
|
+
|
|
56
|
+
export type DiscoverDescriptorsInput = {
|
|
57
|
+
cwd?: string;
|
|
58
|
+
descriptorPaths?: string[];
|
|
59
|
+
roots?: string[];
|
|
60
|
+
descriptorFileName?: string;
|
|
61
|
+
idField?: string;
|
|
62
|
+
maxDepth?: number;
|
|
63
|
+
nestedField?: string;
|
|
64
|
+
validation?: false | DescriptorValidationOptions;
|
|
65
|
+
};
|
|
66
|
+
|
|
67
|
+
function formatDescriptorSchemaValidationError(
|
|
68
|
+
target: DescriptorSchemaValidationTarget,
|
|
69
|
+
validationError: ErrorObject,
|
|
70
|
+
): Error {
|
|
71
|
+
const jsonPath = validationError.instancePath || '/';
|
|
72
|
+
const ajvError = `${validationError.keyword}${validationError.message ? `: ${validationError.message}` : ''}`;
|
|
73
|
+
|
|
74
|
+
return new Error(
|
|
75
|
+
[
|
|
76
|
+
'Descriptor schema validation failed.',
|
|
77
|
+
`File: ${target.descriptorPath}`,
|
|
78
|
+
`JSON path: ${jsonPath}`,
|
|
79
|
+
`Schema error: ${ajvError}`,
|
|
80
|
+
].join('\n'),
|
|
81
|
+
);
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
function createDescriptorValidator(
|
|
85
|
+
options: false | DescriptorValidationOptions | undefined,
|
|
86
|
+
): ((target: DescriptorSchemaValidationTarget, descriptor: object) => void) | undefined {
|
|
87
|
+
if (!options) return undefined;
|
|
88
|
+
|
|
89
|
+
const ajv = new Ajv({
|
|
90
|
+
strict: false,
|
|
91
|
+
allErrors: false,
|
|
92
|
+
...options.ajvOptions,
|
|
93
|
+
});
|
|
94
|
+
const validate = ajv.compile(options.schema);
|
|
95
|
+
const formatError = options.formatError ?? formatDescriptorSchemaValidationError;
|
|
96
|
+
|
|
97
|
+
return (target, descriptor) => {
|
|
98
|
+
if (validate(descriptor)) return;
|
|
99
|
+
|
|
100
|
+
const validationError = validate.errors?.[0];
|
|
101
|
+
if (validationError) throw formatError(target, validationError);
|
|
102
|
+
|
|
103
|
+
throw new Error(`Descriptor schema validation failed: "${target.descriptorPath}"`);
|
|
104
|
+
};
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
function resolveDescriptorId(input: {
|
|
108
|
+
rawDescriptor: Record<string, unknown>;
|
|
109
|
+
idField: string;
|
|
110
|
+
fallbackId?: string;
|
|
111
|
+
label: string;
|
|
112
|
+
}): string {
|
|
113
|
+
const configuredId = input.rawDescriptor[input.idField];
|
|
114
|
+
|
|
115
|
+
if (typeof configuredId === 'string' && configuredId.trim()) {
|
|
116
|
+
return configuredId.trim();
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
if (typeof input.fallbackId === 'string' && input.fallbackId.trim()) {
|
|
120
|
+
return input.fallbackId.trim();
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
throw new Error(`${input.label} is missing a non-empty "${input.idField}" field`);
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
function getNestedDescriptors(input: {
|
|
127
|
+
rawDescriptor: Record<string, unknown>;
|
|
128
|
+
nestedField?: string;
|
|
129
|
+
parentId: string;
|
|
130
|
+
}): Array<RawDescriptor & Record<string, unknown>> {
|
|
131
|
+
if (!input.nestedField) return [];
|
|
132
|
+
|
|
133
|
+
const nestedValue = input.rawDescriptor[input.nestedField];
|
|
134
|
+
|
|
135
|
+
if (nestedValue === undefined) return [];
|
|
136
|
+
if (!Array.isArray(nestedValue))
|
|
137
|
+
throw new Error(`Descriptor "${input.parentId}" field "${input.nestedField}" must be an array`);
|
|
138
|
+
|
|
139
|
+
return nestedValue.map((entry, index) => {
|
|
140
|
+
if (typeof entry !== 'object' || entry === null || Array.isArray(entry)) {
|
|
141
|
+
throw new Error(
|
|
142
|
+
`Descriptor "${input.parentId}" field "${input.nestedField}" contains an invalid entry at index ${index}`,
|
|
143
|
+
);
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
return entry as RawDescriptor & Record<string, unknown>;
|
|
147
|
+
});
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
function normalizeDescriptor(input: {
|
|
151
|
+
rawDescriptor: RawDescriptor & Record<string, unknown>;
|
|
152
|
+
id: string;
|
|
153
|
+
nestedField?: string;
|
|
154
|
+
}): Descriptor {
|
|
155
|
+
const version =
|
|
156
|
+
typeof input.rawDescriptor.version === 'string' && input.rawDescriptor.version.trim()
|
|
157
|
+
? input.rawDescriptor.version
|
|
158
|
+
: '0.0.0';
|
|
159
|
+
|
|
160
|
+
if (input.nestedField) {
|
|
161
|
+
const descriptor = { ...input.rawDescriptor };
|
|
162
|
+
|
|
163
|
+
delete descriptor[input.nestedField];
|
|
164
|
+
|
|
165
|
+
return {
|
|
166
|
+
...descriptor,
|
|
167
|
+
id: input.id,
|
|
168
|
+
version,
|
|
169
|
+
};
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
return {
|
|
173
|
+
...input.rawDescriptor,
|
|
174
|
+
id: input.id,
|
|
175
|
+
version,
|
|
176
|
+
};
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
export function expandNestedDescriptors(input: ExpandNestedDescriptorsInput): Descriptor[] {
|
|
180
|
+
const idField: string = input.idField ?? 'id';
|
|
181
|
+
const rootId = resolveDescriptorId({
|
|
182
|
+
rawDescriptor: input.rawDescriptor,
|
|
183
|
+
idField,
|
|
184
|
+
fallbackId: input.fallbackId,
|
|
185
|
+
label: 'Descriptor',
|
|
186
|
+
});
|
|
187
|
+
const nestedDescriptors = getNestedDescriptors({
|
|
188
|
+
rawDescriptor: input.rawDescriptor,
|
|
189
|
+
parentId: rootId,
|
|
190
|
+
...(input.nestedField ? { nestedField: input.nestedField } : {}),
|
|
191
|
+
});
|
|
192
|
+
const descriptors: Descriptor[] = [
|
|
193
|
+
normalizeDescriptor({
|
|
194
|
+
rawDescriptor: input.rawDescriptor,
|
|
195
|
+
id: rootId,
|
|
196
|
+
...(input.nestedField ? { nestedField: input.nestedField } : {}),
|
|
197
|
+
}),
|
|
198
|
+
];
|
|
199
|
+
|
|
200
|
+
for (const nestedDescriptor of nestedDescriptors) {
|
|
201
|
+
const nestedId = resolveDescriptorId({
|
|
202
|
+
rawDescriptor: nestedDescriptor,
|
|
203
|
+
idField,
|
|
204
|
+
label: `Nested descriptor in "${rootId}"`,
|
|
205
|
+
});
|
|
206
|
+
const nestedChildren = getNestedDescriptors({
|
|
207
|
+
rawDescriptor: nestedDescriptor,
|
|
208
|
+
parentId: nestedId,
|
|
209
|
+
...(input.nestedField ? { nestedField: input.nestedField } : {}),
|
|
210
|
+
});
|
|
211
|
+
|
|
212
|
+
if (nestedChildren.length) {
|
|
213
|
+
throw new Error(`Nested descriptors are not supported inside descriptor "${nestedId}"`);
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
descriptors.push(
|
|
217
|
+
normalizeDescriptor({
|
|
218
|
+
rawDescriptor: nestedDescriptor,
|
|
219
|
+
id: nestedId,
|
|
220
|
+
...(input.nestedField ? { nestedField: input.nestedField } : {}),
|
|
221
|
+
}),
|
|
222
|
+
);
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
return descriptors;
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
function escapeRegex(value: string): string {
|
|
229
|
+
return value.replace(/[.+?^${}()|[\]\\]/g, '\\$&');
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
function createGlobSegmentRegex(segment: string): RegExp {
|
|
233
|
+
return new RegExp(`^${segment.split('*').map(escapeRegex).join('[^/\\\\]*')}$`);
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
function splitPattern(pattern: string): string[] {
|
|
237
|
+
return pattern.split(/[\\/]+/).filter(Boolean);
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
function expandDescriptorPathPattern(input: { cwd: string; pattern: string }): string[] {
|
|
241
|
+
const segments = splitPattern(input.pattern);
|
|
242
|
+
const visit = (currentDir: string, index: number): string[] => {
|
|
243
|
+
const segment = segments[index];
|
|
244
|
+
if (!segment) return [];
|
|
245
|
+
|
|
246
|
+
const isLast = index === segments.length - 1;
|
|
247
|
+
|
|
248
|
+
if (!segment.includes('*')) {
|
|
249
|
+
const nextPath = join(currentDir, segment);
|
|
250
|
+
|
|
251
|
+
if (isLast) return existsSync(nextPath) ? [nextPath] : [];
|
|
252
|
+
if (!existsSync(nextPath)) return [];
|
|
253
|
+
|
|
254
|
+
return visit(nextPath, index + 1);
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
if (!existsSync(currentDir)) return [];
|
|
258
|
+
|
|
259
|
+
const matcher = createGlobSegmentRegex(segment);
|
|
260
|
+
|
|
261
|
+
return readdirSync(currentDir, { withFileTypes: true })
|
|
262
|
+
.filter((entry) => entry.name !== 'node_modules' && matcher.test(entry.name))
|
|
263
|
+
.flatMap((entry) => {
|
|
264
|
+
const nextPath = join(currentDir, entry.name);
|
|
265
|
+
|
|
266
|
+
if (isLast) return entry.isFile() ? [nextPath] : [];
|
|
267
|
+
return entry.isDirectory() ? visit(nextPath, index + 1) : [];
|
|
268
|
+
});
|
|
269
|
+
};
|
|
270
|
+
|
|
271
|
+
return visit(resolvePath(input.cwd), 0);
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
function discoverDescriptorFiles(input: { cwd: string; descriptorPaths: string[] }): string[] {
|
|
275
|
+
return [
|
|
276
|
+
...new Set(
|
|
277
|
+
input.descriptorPaths.flatMap((pattern) =>
|
|
278
|
+
expandDescriptorPathPattern({
|
|
279
|
+
cwd: input.cwd,
|
|
280
|
+
pattern,
|
|
281
|
+
}),
|
|
282
|
+
),
|
|
283
|
+
),
|
|
284
|
+
].sort();
|
|
285
|
+
}
|
|
286
|
+
|
|
287
|
+
function discoverDescriptorFilesFromRoots(input: {
|
|
288
|
+
descriptorFileName: string;
|
|
289
|
+
maxDepth: number;
|
|
290
|
+
roots: string[];
|
|
291
|
+
}): string[] {
|
|
292
|
+
const discovered: string[] = [];
|
|
293
|
+
const visit = (root: string, depth: number): void => {
|
|
294
|
+
if (depth > input.maxDepth || !existsSync(root)) return;
|
|
295
|
+
|
|
296
|
+
for (const entry of readdirSync(root, { withFileTypes: true })) {
|
|
297
|
+
if (!entry.isDirectory() || entry.name === 'node_modules') continue;
|
|
298
|
+
|
|
299
|
+
const cwd: string = join(root, entry.name);
|
|
300
|
+
const descriptorPath: string = join(cwd, input.descriptorFileName);
|
|
301
|
+
|
|
302
|
+
if (existsSync(descriptorPath)) discovered.push(descriptorPath);
|
|
303
|
+
|
|
304
|
+
visit(cwd, depth + 1);
|
|
305
|
+
}
|
|
306
|
+
};
|
|
307
|
+
|
|
308
|
+
for (const root of input.roots) {
|
|
309
|
+
visit(resolvePath(root), 1);
|
|
310
|
+
}
|
|
311
|
+
|
|
312
|
+
return [...new Set(discovered)].sort();
|
|
313
|
+
}
|
|
314
|
+
|
|
315
|
+
export function discoverDescriptors(input: DiscoverDescriptorsInput): DiscoveredDescriptor[] {
|
|
316
|
+
const descriptorFileName: string = input.descriptorFileName ?? 'descriptor.json';
|
|
317
|
+
const idField: string = input.idField ?? 'id';
|
|
318
|
+
const maxDepth = input.maxDepth ?? 1;
|
|
319
|
+
const validateDescriptor = createDescriptorValidator(input.validation);
|
|
320
|
+
const descriptorPaths = input.descriptorPaths?.length
|
|
321
|
+
? discoverDescriptorFiles({
|
|
322
|
+
cwd: input.cwd ?? '',
|
|
323
|
+
descriptorPaths: input.descriptorPaths,
|
|
324
|
+
})
|
|
325
|
+
: discoverDescriptorFilesFromRoots({
|
|
326
|
+
descriptorFileName,
|
|
327
|
+
maxDepth,
|
|
328
|
+
roots: input.roots ?? [],
|
|
329
|
+
});
|
|
330
|
+
|
|
331
|
+
return descriptorPaths.flatMap((descriptorPath) => {
|
|
332
|
+
const cwd = dirname(descriptorPath);
|
|
333
|
+
const rawDescriptor = JSON.parse(readFileSync(descriptorPath, 'utf8')) as RawDescriptor &
|
|
334
|
+
Record<string, unknown>;
|
|
335
|
+
|
|
336
|
+
validateDescriptor?.({ descriptorPath }, rawDescriptor);
|
|
337
|
+
|
|
338
|
+
return expandNestedDescriptors({
|
|
339
|
+
rawDescriptor,
|
|
340
|
+
fallbackId: basename(cwd),
|
|
341
|
+
idField,
|
|
342
|
+
...(input.nestedField ? { nestedField: input.nestedField } : {}),
|
|
343
|
+
}).map((descriptor) => ({
|
|
344
|
+
id: descriptor.id,
|
|
345
|
+
cwd,
|
|
346
|
+
descriptorPath,
|
|
347
|
+
descriptor,
|
|
348
|
+
}));
|
|
349
|
+
});
|
|
350
|
+
}
|