@designos/compiler-cli 0.1.0 → 0.2.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/README.md +8 -2
- package/dist/cli.js +1449 -38
- package/dist/cli.js.map +1 -1
- package/package.json +9 -2
package/dist/cli.js
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
2
|
|
|
3
3
|
// src/run.ts
|
|
4
|
-
import { copyFile, lstat, mkdir, readFile, realpath, rename, rm, stat, writeFile } from "fs/promises";
|
|
4
|
+
import { copyFile, lstat, mkdir, readFile as readFile2, realpath as realpath2, rename, rm, stat as stat2, writeFile } from "fs/promises";
|
|
5
5
|
import { randomUUID } from "crypto";
|
|
6
|
-
import { dirname, isAbsolute, join, relative, resolve, sep } from "path";
|
|
6
|
+
import { dirname as dirname2, isAbsolute as isAbsolute2, join, relative as relative2, resolve as resolve2, sep as sep2 } from "path";
|
|
7
7
|
|
|
8
8
|
// ../compiler-core/dist/index.js
|
|
9
9
|
import {
|
|
@@ -1072,6 +1072,28 @@ function validateCompilerProject(project) {
|
|
|
1072
1072
|
const componentTypes = /* @__PURE__ */ new Set();
|
|
1073
1073
|
const componentNames = /* @__PURE__ */ new Set();
|
|
1074
1074
|
const assetOutputs = /* @__PURE__ */ new Map();
|
|
1075
|
+
if (project.styles !== void 0 && !Array.isArray(project.styles)) {
|
|
1076
|
+
diagnostics.push({ severity: "error", code: "COMPILER_PROJECT_STYLES_INVALID", message: "styles must be an array", path: "styles" });
|
|
1077
|
+
}
|
|
1078
|
+
for (const [styleIndex, style] of (Array.isArray(project.styles) ? project.styles : []).entries()) {
|
|
1079
|
+
const stylePath = `styles[${styleIndex}]`;
|
|
1080
|
+
if (!isSafeRelativePath(style.source)) {
|
|
1081
|
+
diagnostics.push({ severity: "error", code: "COMPILER_PROJECT_STYLE_SOURCE_PATH_INVALID", message: "Style source must be a safe path relative to the project root", path: `${stylePath}.source` });
|
|
1082
|
+
continue;
|
|
1083
|
+
}
|
|
1084
|
+
const outputPath = style.outputPath ?? style.source;
|
|
1085
|
+
if (!isSafeRelativePath(outputPath)) {
|
|
1086
|
+
diagnostics.push({ severity: "error", code: "COMPILER_PROJECT_STYLE_OUTPUT_PATH_INVALID", message: "Style outputPath must be a safe path relative to the generated output directory", path: `${stylePath}.outputPath` });
|
|
1087
|
+
continue;
|
|
1088
|
+
}
|
|
1089
|
+
const normalizedOutputPath = outputPath.toLowerCase();
|
|
1090
|
+
const existingSource = assetOutputs.get(normalizedOutputPath);
|
|
1091
|
+
if (existingSource && existingSource !== style.source) {
|
|
1092
|
+
diagnostics.push({ severity: "error", code: "COMPILER_PROJECT_STYLE_OUTPUT_COLLISION", message: `Different styles target the same output path: ${outputPath}`, path: `${stylePath}.outputPath` });
|
|
1093
|
+
} else {
|
|
1094
|
+
assetOutputs.set(normalizedOutputPath, style.source);
|
|
1095
|
+
}
|
|
1096
|
+
}
|
|
1075
1097
|
project.components.forEach((definition, index) => {
|
|
1076
1098
|
const prefix = `components[${index}]`;
|
|
1077
1099
|
const manifest = definition?.manifest;
|
|
@@ -1190,6 +1212,9 @@ async function compileProject(project, registry, options) {
|
|
|
1190
1212
|
const generatedPaths = /* @__PURE__ */ new Set();
|
|
1191
1213
|
const registryEntries = [];
|
|
1192
1214
|
const targetDefinition = registry.get(options.target);
|
|
1215
|
+
for (const style of project.styles ?? []) {
|
|
1216
|
+
assets.set(style.outputPath ?? style.source, style.source);
|
|
1217
|
+
}
|
|
1193
1218
|
for (const [index, definition] of project.components.entries()) {
|
|
1194
1219
|
let result;
|
|
1195
1220
|
if (definition.implementation.kind === "ir") {
|
|
@@ -1276,11 +1301,1341 @@ async function compileProject(project, registry, options) {
|
|
|
1276
1301
|
};
|
|
1277
1302
|
}
|
|
1278
1303
|
|
|
1304
|
+
// src/run.ts
|
|
1305
|
+
import { bladeTarget } from "@designos/compiler-blade";
|
|
1306
|
+
|
|
1307
|
+
// src/kit-validator.ts
|
|
1308
|
+
import { readFile, realpath, stat } from "fs/promises";
|
|
1309
|
+
import {
|
|
1310
|
+
dirname,
|
|
1311
|
+
extname,
|
|
1312
|
+
isAbsolute,
|
|
1313
|
+
relative,
|
|
1314
|
+
resolve,
|
|
1315
|
+
sep
|
|
1316
|
+
} from "path";
|
|
1317
|
+
import { parse as parseTypeScriptSource } from "@babel/parser";
|
|
1318
|
+
import Ajv2020 from "ajv/dist/2020.js";
|
|
1319
|
+
import addFormats from "ajv-formats";
|
|
1320
|
+
import semver from "semver";
|
|
1321
|
+
import {
|
|
1322
|
+
ComponentManifestSchema,
|
|
1323
|
+
validateComponentManifest
|
|
1324
|
+
} from "@designos/core";
|
|
1325
|
+
|
|
1326
|
+
// ../../schemas/designos.kit.schema.json
|
|
1327
|
+
var designos_kit_schema_default = {
|
|
1328
|
+
$schema: "https://json-schema.org/draft/2020-12/schema",
|
|
1329
|
+
$id: "https://raw.githubusercontent.com/relacloud/design-os/main/schemas/designos.kit.schema.json",
|
|
1330
|
+
title: "DesignOS Component Kit Manifest",
|
|
1331
|
+
description: "Root manifest for a version 1 DesignOS component kit.",
|
|
1332
|
+
type: "object",
|
|
1333
|
+
additionalProperties: false,
|
|
1334
|
+
required: [
|
|
1335
|
+
"schemaVersion",
|
|
1336
|
+
"id",
|
|
1337
|
+
"name",
|
|
1338
|
+
"version",
|
|
1339
|
+
"compilerProject",
|
|
1340
|
+
"designos",
|
|
1341
|
+
"components"
|
|
1342
|
+
],
|
|
1343
|
+
properties: {
|
|
1344
|
+
$schema: {
|
|
1345
|
+
type: "string",
|
|
1346
|
+
format: "uri"
|
|
1347
|
+
},
|
|
1348
|
+
schemaVersion: {
|
|
1349
|
+
const: "1",
|
|
1350
|
+
description: "Version of this kit-manifest contract; independent from compiler project schemaVersion."
|
|
1351
|
+
},
|
|
1352
|
+
id: {
|
|
1353
|
+
type: "string",
|
|
1354
|
+
minLength: 1,
|
|
1355
|
+
maxLength: 214,
|
|
1356
|
+
pattern: "^(?:@[a-z0-9][a-z0-9._-]*/)?[a-z0-9][a-z0-9._-]*$",
|
|
1357
|
+
description: "Exact package identifier; semantic validation checks equality with package.json.name."
|
|
1358
|
+
},
|
|
1359
|
+
name: {
|
|
1360
|
+
type: "string",
|
|
1361
|
+
minLength: 1,
|
|
1362
|
+
maxLength: 160
|
|
1363
|
+
},
|
|
1364
|
+
version: {
|
|
1365
|
+
$ref: "#/$defs/semver",
|
|
1366
|
+
description: "Strict SemVer 2.0.0 package version; this value is not a compatibility range."
|
|
1367
|
+
},
|
|
1368
|
+
description: {
|
|
1369
|
+
type: "string",
|
|
1370
|
+
maxLength: 2e3
|
|
1371
|
+
},
|
|
1372
|
+
license: {
|
|
1373
|
+
type: "string",
|
|
1374
|
+
minLength: 1,
|
|
1375
|
+
maxLength: 128
|
|
1376
|
+
},
|
|
1377
|
+
compilerProject: {
|
|
1378
|
+
$ref: "#/$defs/packageRelativePath",
|
|
1379
|
+
description: "Package-relative path to the compiler project; existence and real-path containment are semantic checks."
|
|
1380
|
+
},
|
|
1381
|
+
designos: {
|
|
1382
|
+
type: "object",
|
|
1383
|
+
additionalProperties: false,
|
|
1384
|
+
required: ["kit"],
|
|
1385
|
+
properties: {
|
|
1386
|
+
kit: {
|
|
1387
|
+
type: "string",
|
|
1388
|
+
minLength: 1,
|
|
1389
|
+
description: "Non-empty SemVer range for the kit contract. Schema validation checks only the string; semantic validation parses the range."
|
|
1390
|
+
},
|
|
1391
|
+
core: {
|
|
1392
|
+
type: "string",
|
|
1393
|
+
minLength: 1,
|
|
1394
|
+
description: "Optional non-empty SemVer range for DesignOS core; semantic validation parses the range."
|
|
1395
|
+
},
|
|
1396
|
+
compiler: {
|
|
1397
|
+
type: "string",
|
|
1398
|
+
minLength: 1,
|
|
1399
|
+
description: "Optional non-empty SemVer range for the compiler; semantic validation parses the range."
|
|
1400
|
+
},
|
|
1401
|
+
editor: {
|
|
1402
|
+
type: "string",
|
|
1403
|
+
minLength: 1,
|
|
1404
|
+
description: "Optional non-empty SemVer range for the editor; semantic validation parses the range."
|
|
1405
|
+
}
|
|
1406
|
+
}
|
|
1407
|
+
},
|
|
1408
|
+
components: {
|
|
1409
|
+
type: "array",
|
|
1410
|
+
minItems: 1,
|
|
1411
|
+
items: {
|
|
1412
|
+
$ref: "#/$defs/component"
|
|
1413
|
+
}
|
|
1414
|
+
},
|
|
1415
|
+
editor: {
|
|
1416
|
+
type: "object",
|
|
1417
|
+
additionalProperties: false,
|
|
1418
|
+
properties: {
|
|
1419
|
+
catalog: {
|
|
1420
|
+
$ref: "#/$defs/packageRelativePath"
|
|
1421
|
+
},
|
|
1422
|
+
previews: {
|
|
1423
|
+
type: "object",
|
|
1424
|
+
description: "Optional editor host adapters keyed by stable component type.",
|
|
1425
|
+
propertyNames: {
|
|
1426
|
+
minLength: 1
|
|
1427
|
+
},
|
|
1428
|
+
additionalProperties: {
|
|
1429
|
+
$ref: "#/$defs/packageRelativePath"
|
|
1430
|
+
}
|
|
1431
|
+
}
|
|
1432
|
+
},
|
|
1433
|
+
minProperties: 1
|
|
1434
|
+
},
|
|
1435
|
+
metadata: {
|
|
1436
|
+
type: "object",
|
|
1437
|
+
description: "Reserved for non-normative publisher-specific metadata.",
|
|
1438
|
+
additionalProperties: true
|
|
1439
|
+
}
|
|
1440
|
+
},
|
|
1441
|
+
$defs: {
|
|
1442
|
+
component: {
|
|
1443
|
+
type: "object",
|
|
1444
|
+
additionalProperties: false,
|
|
1445
|
+
required: ["type", "source", "exportName", "manifest"],
|
|
1446
|
+
properties: {
|
|
1447
|
+
type: {
|
|
1448
|
+
type: "string",
|
|
1449
|
+
minLength: 1,
|
|
1450
|
+
maxLength: 128,
|
|
1451
|
+
description: "Stable public component type; uniqueness and manifest agreement are semantic checks."
|
|
1452
|
+
},
|
|
1453
|
+
source: {
|
|
1454
|
+
$ref: "#/$defs/packageRelativePath"
|
|
1455
|
+
},
|
|
1456
|
+
exportName: {
|
|
1457
|
+
type: "string",
|
|
1458
|
+
pattern: "^[A-Za-z_$][A-Za-z0-9_$]*$",
|
|
1459
|
+
description: "Compiler export identifier; agreement with the compiler-project entry is a semantic check."
|
|
1460
|
+
},
|
|
1461
|
+
manifest: {
|
|
1462
|
+
$ref: "#/$defs/packageRelativePath"
|
|
1463
|
+
}
|
|
1464
|
+
}
|
|
1465
|
+
},
|
|
1466
|
+
packageRelativePath: {
|
|
1467
|
+
type: "string",
|
|
1468
|
+
minLength: 1,
|
|
1469
|
+
maxLength: 1024,
|
|
1470
|
+
pattern: "^(?!/)(?![A-Za-z]:)(?!.*(?:^|/)\\.\\.?/)(?!.*(?:^|/)\\.\\.?$)[^\\\\\\u0000]+$",
|
|
1471
|
+
description: "Package-relative path with basic lexical restrictions. Semantic validation must check normalized segments, existence, and real-path containment after symlink resolution."
|
|
1472
|
+
},
|
|
1473
|
+
semver: {
|
|
1474
|
+
type: "string",
|
|
1475
|
+
pattern: "^(0|[1-9][0-9]*)\\.(0|[1-9][0-9]*)\\.(0|[1-9][0-9]*)(?:-((?:0|[1-9][0-9]*|[0-9]*[A-Za-z-][0-9A-Za-z-]*)(?:\\.(?:0|[1-9][0-9]*|[0-9]*[A-Za-z-][0-9A-Za-z-]*))*))?(?:\\+([0-9A-Za-z-]+(?:\\.[0-9A-Za-z-]+)*))?$",
|
|
1476
|
+
description: "Strict SemVer 2.0.0 version syntax, excluding compatibility ranges."
|
|
1477
|
+
}
|
|
1478
|
+
}
|
|
1479
|
+
};
|
|
1480
|
+
|
|
1481
|
+
// ../core/package.json
|
|
1482
|
+
var package_default = {
|
|
1483
|
+
name: "@designos/core",
|
|
1484
|
+
version: "0.1.0",
|
|
1485
|
+
private: false,
|
|
1486
|
+
type: "module",
|
|
1487
|
+
exports: {
|
|
1488
|
+
".": {
|
|
1489
|
+
types: "./dist/index.d.ts",
|
|
1490
|
+
import: "./dist/index.js"
|
|
1491
|
+
}
|
|
1492
|
+
},
|
|
1493
|
+
scripts: {
|
|
1494
|
+
typecheck: "tsc --noEmit",
|
|
1495
|
+
build: "tsup",
|
|
1496
|
+
test: "vitest"
|
|
1497
|
+
},
|
|
1498
|
+
dependencies: {
|
|
1499
|
+
nanoid: "^5.1.6",
|
|
1500
|
+
zod: "^3.25.76"
|
|
1501
|
+
},
|
|
1502
|
+
devDependencies: {
|
|
1503
|
+
"@types/node": "^24.0.0",
|
|
1504
|
+
typescript: "^5.9.2",
|
|
1505
|
+
tsup: "^8.5.0",
|
|
1506
|
+
vitest: "^3.2.4"
|
|
1507
|
+
},
|
|
1508
|
+
files: [
|
|
1509
|
+
"dist",
|
|
1510
|
+
"README.md"
|
|
1511
|
+
],
|
|
1512
|
+
main: "./dist/index.js",
|
|
1513
|
+
types: "./dist/index.d.ts",
|
|
1514
|
+
publishConfig: {
|
|
1515
|
+
access: "public"
|
|
1516
|
+
},
|
|
1517
|
+
repository: {
|
|
1518
|
+
type: "git",
|
|
1519
|
+
url: "git+https://github.com/relacloud/design-os.git",
|
|
1520
|
+
directory: "packages/core"
|
|
1521
|
+
}
|
|
1522
|
+
};
|
|
1523
|
+
|
|
1524
|
+
// package.json
|
|
1525
|
+
var package_default2 = {
|
|
1526
|
+
name: "@designos/compiler-cli",
|
|
1527
|
+
version: "0.2.0",
|
|
1528
|
+
private: false,
|
|
1529
|
+
type: "module",
|
|
1530
|
+
bin: {
|
|
1531
|
+
designos: "./dist/cli.js"
|
|
1532
|
+
},
|
|
1533
|
+
files: [
|
|
1534
|
+
"dist",
|
|
1535
|
+
"README.md"
|
|
1536
|
+
],
|
|
1537
|
+
engines: {
|
|
1538
|
+
node: ">=20"
|
|
1539
|
+
},
|
|
1540
|
+
scripts: {
|
|
1541
|
+
build: "tsup",
|
|
1542
|
+
typecheck: "tsc --noEmit",
|
|
1543
|
+
test: "vitest run"
|
|
1544
|
+
},
|
|
1545
|
+
dependencies: {
|
|
1546
|
+
"@babel/parser": "^7.28.4",
|
|
1547
|
+
"@builder.io/mitosis": "0.14.1",
|
|
1548
|
+
"@designos/compiler-blade": "workspace:*",
|
|
1549
|
+
"@designos/compiler-core": "workspace:*",
|
|
1550
|
+
"@designos/core": "workspace:^",
|
|
1551
|
+
ajv: "^8.17.1",
|
|
1552
|
+
"ajv-formats": "^3.0.1",
|
|
1553
|
+
semver: "^7.7.2"
|
|
1554
|
+
},
|
|
1555
|
+
devDependencies: {
|
|
1556
|
+
"@types/node": "^24.0.0",
|
|
1557
|
+
"@types/semver": "^7.7.0",
|
|
1558
|
+
tsup: "^8.5.0",
|
|
1559
|
+
typescript: "^5.9.2",
|
|
1560
|
+
vitest: "^3.2.4"
|
|
1561
|
+
},
|
|
1562
|
+
main: "./dist/cli.js",
|
|
1563
|
+
publishConfig: {
|
|
1564
|
+
access: "public"
|
|
1565
|
+
},
|
|
1566
|
+
repository: {
|
|
1567
|
+
type: "git",
|
|
1568
|
+
url: "git+https://github.com/relacloud/design-os.git",
|
|
1569
|
+
directory: "packages/compiler-cli"
|
|
1570
|
+
}
|
|
1571
|
+
};
|
|
1572
|
+
|
|
1573
|
+
// src/kit-validator.ts
|
|
1574
|
+
var schemaValidator = (() => {
|
|
1575
|
+
const ajv = new Ajv2020({ allErrors: true, strict: true });
|
|
1576
|
+
addFormats(ajv);
|
|
1577
|
+
return ajv.compile(designos_kit_schema_default);
|
|
1578
|
+
})();
|
|
1579
|
+
var CURRENT_KIT_CONTRACT_VERSION = "1.0.0";
|
|
1580
|
+
var CURRENT_TOOL_VERSIONS = {
|
|
1581
|
+
kit: CURRENT_KIT_CONTRACT_VERSION,
|
|
1582
|
+
core: package_default.version,
|
|
1583
|
+
compiler: package_default2.version
|
|
1584
|
+
};
|
|
1585
|
+
function isRecord(value) {
|
|
1586
|
+
return Boolean(value) && typeof value === "object" && !Array.isArray(value);
|
|
1587
|
+
}
|
|
1588
|
+
function collectSourceImports(sourceText) {
|
|
1589
|
+
const ast = parseTypeScriptSource(sourceText, {
|
|
1590
|
+
sourceType: "module",
|
|
1591
|
+
plugins: ["typescript", "jsx"]
|
|
1592
|
+
});
|
|
1593
|
+
const importPaths = /* @__PURE__ */ new Set();
|
|
1594
|
+
const visit = (value) => {
|
|
1595
|
+
if (Array.isArray(value)) {
|
|
1596
|
+
for (const item of value) visit(item);
|
|
1597
|
+
return;
|
|
1598
|
+
}
|
|
1599
|
+
if (!isRecord(value)) return;
|
|
1600
|
+
const source = value.source;
|
|
1601
|
+
if ((value.type === "ImportDeclaration" || value.type === "ExportNamedDeclaration" || value.type === "ExportAllDeclaration") && isRecord(source) && typeof source.value === "string") {
|
|
1602
|
+
importPaths.add(source.value);
|
|
1603
|
+
}
|
|
1604
|
+
if (value.type === "ImportExpression" && isRecord(source) && typeof source.value === "string") {
|
|
1605
|
+
importPaths.add(source.value);
|
|
1606
|
+
}
|
|
1607
|
+
if (value.type === "CallExpression" && Array.isArray(value.arguments)) {
|
|
1608
|
+
const callee = value.callee;
|
|
1609
|
+
const isImport = isRecord(callee) && callee.type === "Import";
|
|
1610
|
+
const isRequire = isRecord(callee) && callee.type === "Identifier" && callee.name === "require";
|
|
1611
|
+
const firstArgument = value.arguments[0];
|
|
1612
|
+
if ((isImport || isRequire) && isRecord(firstArgument) && typeof firstArgument.value === "string") {
|
|
1613
|
+
importPaths.add(firstArgument.value);
|
|
1614
|
+
}
|
|
1615
|
+
}
|
|
1616
|
+
for (const [key, nested] of Object.entries(value)) {
|
|
1617
|
+
if ([
|
|
1618
|
+
"loc",
|
|
1619
|
+
"start",
|
|
1620
|
+
"end",
|
|
1621
|
+
"extra",
|
|
1622
|
+
"comments",
|
|
1623
|
+
"leadingComments",
|
|
1624
|
+
"trailingComments",
|
|
1625
|
+
"innerComments"
|
|
1626
|
+
].includes(key))
|
|
1627
|
+
continue;
|
|
1628
|
+
visit(nested);
|
|
1629
|
+
}
|
|
1630
|
+
};
|
|
1631
|
+
visit(ast.program);
|
|
1632
|
+
return [...importPaths];
|
|
1633
|
+
}
|
|
1634
|
+
function isWithin(root, candidate) {
|
|
1635
|
+
const pathFromRoot = relative(root, candidate);
|
|
1636
|
+
return pathFromRoot === "" || pathFromRoot !== ".." && !pathFromRoot.startsWith(`..${sep}`) && !isAbsolute(pathFromRoot);
|
|
1637
|
+
}
|
|
1638
|
+
function isSafeRelativePath2(value) {
|
|
1639
|
+
return typeof value === "string" && value.length > 0 && !value.startsWith("/") && !value.includes("\\") && !value.includes("\0") && !/^[A-Za-z]:/.test(value) && value.split("/").every(
|
|
1640
|
+
(segment) => segment.length > 0 && segment !== "." && segment !== ".."
|
|
1641
|
+
);
|
|
1642
|
+
}
|
|
1643
|
+
function addError(diagnostics, code, message, path) {
|
|
1644
|
+
diagnostics.push({
|
|
1645
|
+
severity: "error",
|
|
1646
|
+
code,
|
|
1647
|
+
message,
|
|
1648
|
+
...path ? { path } : {}
|
|
1649
|
+
});
|
|
1650
|
+
}
|
|
1651
|
+
async function readJsonFile(path, displayPath, diagnostics) {
|
|
1652
|
+
let text;
|
|
1653
|
+
try {
|
|
1654
|
+
text = await readFile(path, "utf8");
|
|
1655
|
+
} catch (cause) {
|
|
1656
|
+
addError(
|
|
1657
|
+
diagnostics,
|
|
1658
|
+
"KIT_FILE_READ_FAILED",
|
|
1659
|
+
`Unable to read ${displayPath}: ${cause instanceof Error ? cause.message : String(cause)}`,
|
|
1660
|
+
displayPath
|
|
1661
|
+
);
|
|
1662
|
+
return void 0;
|
|
1663
|
+
}
|
|
1664
|
+
try {
|
|
1665
|
+
return JSON.parse(text);
|
|
1666
|
+
} catch (cause) {
|
|
1667
|
+
addError(
|
|
1668
|
+
diagnostics,
|
|
1669
|
+
"KIT_JSON_INVALID",
|
|
1670
|
+
`Invalid JSON in ${displayPath}: ${cause instanceof Error ? cause.message : String(cause)}`,
|
|
1671
|
+
displayPath
|
|
1672
|
+
);
|
|
1673
|
+
return void 0;
|
|
1674
|
+
}
|
|
1675
|
+
}
|
|
1676
|
+
async function resolveRootFile(rootRealPath, pathValue, displayPath, diagnostics) {
|
|
1677
|
+
if (!isSafeRelativePath2(pathValue)) {
|
|
1678
|
+
addError(
|
|
1679
|
+
diagnostics,
|
|
1680
|
+
"KIT_PATH_INVALID",
|
|
1681
|
+
"Expected a normalized package-relative path without traversal, absolute segments, or backslashes.",
|
|
1682
|
+
displayPath
|
|
1683
|
+
);
|
|
1684
|
+
return void 0;
|
|
1685
|
+
}
|
|
1686
|
+
const candidate = resolve(rootRealPath, pathValue);
|
|
1687
|
+
if (!isWithin(rootRealPath, candidate)) {
|
|
1688
|
+
addError(
|
|
1689
|
+
diagnostics,
|
|
1690
|
+
"KIT_PATH_ESCAPES_ROOT",
|
|
1691
|
+
"Path resolves outside the kit package root.",
|
|
1692
|
+
displayPath
|
|
1693
|
+
);
|
|
1694
|
+
return void 0;
|
|
1695
|
+
}
|
|
1696
|
+
return resolveExistingFile(rootRealPath, candidate, displayPath, diagnostics);
|
|
1697
|
+
}
|
|
1698
|
+
async function resolveNestedFile(rootRealPath, baseDirectory, pathValue, displayPath, diagnostics) {
|
|
1699
|
+
if (typeof pathValue !== "string" || pathValue.length === 0 || pathValue.includes("\\") || pathValue.includes("\0") || pathValue.startsWith("/") || /^[A-Za-z]:/.test(pathValue)) {
|
|
1700
|
+
addError(
|
|
1701
|
+
diagnostics,
|
|
1702
|
+
"KIT_ADAPTER_PATH_INVALID",
|
|
1703
|
+
"Expected a relative adapter reference without an absolute path or backslashes.",
|
|
1704
|
+
displayPath
|
|
1705
|
+
);
|
|
1706
|
+
return void 0;
|
|
1707
|
+
}
|
|
1708
|
+
const candidate = resolve(baseDirectory, pathValue);
|
|
1709
|
+
if (!isWithin(rootRealPath, candidate)) {
|
|
1710
|
+
addError(
|
|
1711
|
+
diagnostics,
|
|
1712
|
+
"KIT_PATH_ESCAPES_ROOT",
|
|
1713
|
+
"Adapter reference resolves outside the kit package root.",
|
|
1714
|
+
displayPath
|
|
1715
|
+
);
|
|
1716
|
+
return void 0;
|
|
1717
|
+
}
|
|
1718
|
+
return resolveExistingFile(rootRealPath, candidate, displayPath, diagnostics);
|
|
1719
|
+
}
|
|
1720
|
+
async function resolveExistingFile(rootRealPath, candidate, displayPath, diagnostics) {
|
|
1721
|
+
let candidateRealPath;
|
|
1722
|
+
try {
|
|
1723
|
+
candidateRealPath = await realpath(candidate);
|
|
1724
|
+
} catch (cause) {
|
|
1725
|
+
addError(
|
|
1726
|
+
diagnostics,
|
|
1727
|
+
"KIT_FILE_NOT_FOUND",
|
|
1728
|
+
`Referenced file does not exist or cannot be resolved: ${cause instanceof Error ? cause.message : String(cause)}`,
|
|
1729
|
+
displayPath
|
|
1730
|
+
);
|
|
1731
|
+
return void 0;
|
|
1732
|
+
}
|
|
1733
|
+
if (!isWithin(rootRealPath, candidateRealPath)) {
|
|
1734
|
+
addError(
|
|
1735
|
+
diagnostics,
|
|
1736
|
+
"KIT_PATH_SYMLINK_ESCAPE",
|
|
1737
|
+
"Referenced path resolves through a symlink outside the kit package root.",
|
|
1738
|
+
displayPath
|
|
1739
|
+
);
|
|
1740
|
+
return void 0;
|
|
1741
|
+
}
|
|
1742
|
+
try {
|
|
1743
|
+
const info = await stat(candidateRealPath);
|
|
1744
|
+
if (!info.isFile()) {
|
|
1745
|
+
addError(
|
|
1746
|
+
diagnostics,
|
|
1747
|
+
"KIT_FILE_NOT_REGULAR",
|
|
1748
|
+
"Referenced path must resolve to a regular file.",
|
|
1749
|
+
displayPath
|
|
1750
|
+
);
|
|
1751
|
+
return void 0;
|
|
1752
|
+
}
|
|
1753
|
+
} catch (cause) {
|
|
1754
|
+
addError(
|
|
1755
|
+
diagnostics,
|
|
1756
|
+
"KIT_FILE_STAT_FAILED",
|
|
1757
|
+
`Unable to inspect referenced file: ${cause instanceof Error ? cause.message : String(cause)}`,
|
|
1758
|
+
displayPath
|
|
1759
|
+
);
|
|
1760
|
+
return void 0;
|
|
1761
|
+
}
|
|
1762
|
+
return candidateRealPath;
|
|
1763
|
+
}
|
|
1764
|
+
function validateCompatibility(manifest, diagnostics) {
|
|
1765
|
+
for (const [name, range] of Object.entries(manifest.designos)) {
|
|
1766
|
+
if (typeof range !== "string" || range.trim().length === 0 || semver.validRange(range) === null) {
|
|
1767
|
+
addError(
|
|
1768
|
+
diagnostics,
|
|
1769
|
+
"KIT_COMPATIBILITY_RANGE_INVALID",
|
|
1770
|
+
`Invalid SemVer compatibility range: ${JSON.stringify(range)}.`,
|
|
1771
|
+
`designos.${name}`
|
|
1772
|
+
);
|
|
1773
|
+
continue;
|
|
1774
|
+
}
|
|
1775
|
+
const currentVersion = CURRENT_TOOL_VERSIONS[name];
|
|
1776
|
+
if (!currentVersion && name === "editor") {
|
|
1777
|
+
diagnostics.push({
|
|
1778
|
+
severity: "info",
|
|
1779
|
+
code: "KIT_COMPATIBILITY_VERSION_UNAVAILABLE",
|
|
1780
|
+
path: `designos.${name}`,
|
|
1781
|
+
message: "The editor compatibility range is syntactically valid, but this CLI has no current editor version to compare against."
|
|
1782
|
+
});
|
|
1783
|
+
}
|
|
1784
|
+
if (currentVersion && !semver.satisfies(currentVersion, range)) {
|
|
1785
|
+
diagnostics.push({
|
|
1786
|
+
severity: "warning",
|
|
1787
|
+
code: "KIT_COMPATIBILITY_MISMATCH",
|
|
1788
|
+
path: `designos.${name}`,
|
|
1789
|
+
message: `Declared range ${JSON.stringify(range)} does not include the current ${name} version ${currentVersion}.`
|
|
1790
|
+
});
|
|
1791
|
+
}
|
|
1792
|
+
}
|
|
1793
|
+
}
|
|
1794
|
+
function convertCompilerDiagnostic(diagnostic) {
|
|
1795
|
+
return {
|
|
1796
|
+
severity: diagnostic.severity,
|
|
1797
|
+
code: diagnostic.code,
|
|
1798
|
+
message: diagnostic.message,
|
|
1799
|
+
...diagnostic.path ? { path: `compilerProject.${diagnostic.path}` } : {}
|
|
1800
|
+
};
|
|
1801
|
+
}
|
|
1802
|
+
function validateSemverPackageVersion(version, path, diagnostics) {
|
|
1803
|
+
if (typeof version !== "string" || semver.valid(version, { loose: false }) !== version) {
|
|
1804
|
+
addError(
|
|
1805
|
+
diagnostics,
|
|
1806
|
+
"KIT_PACKAGE_VERSION_INVALID",
|
|
1807
|
+
"Package version must be a strict SemVer 2.0.0 version.",
|
|
1808
|
+
path
|
|
1809
|
+
);
|
|
1810
|
+
}
|
|
1811
|
+
}
|
|
1812
|
+
async function validateEditorAdapters(rootRealPath, manifest, components, diagnostics) {
|
|
1813
|
+
const editor = manifest.editor;
|
|
1814
|
+
if (!editor) return;
|
|
1815
|
+
const componentsByType = new Map(
|
|
1816
|
+
components.map((component) => [component.definition.type, component])
|
|
1817
|
+
);
|
|
1818
|
+
const previewPaths = /* @__PURE__ */ new Map();
|
|
1819
|
+
for (const [type, pathValue] of Object.entries(editor.previews ?? {})) {
|
|
1820
|
+
if (!componentsByType.has(type)) {
|
|
1821
|
+
addError(
|
|
1822
|
+
diagnostics,
|
|
1823
|
+
"KIT_EDITOR_PREVIEW_TYPE_UNKNOWN",
|
|
1824
|
+
`Editor preview is declared for unknown component type ${JSON.stringify(type)}.`,
|
|
1825
|
+
`editor.previews.${type}`
|
|
1826
|
+
);
|
|
1827
|
+
}
|
|
1828
|
+
const previewPath = await resolveRootFile(
|
|
1829
|
+
rootRealPath,
|
|
1830
|
+
pathValue,
|
|
1831
|
+
`editor.previews.${type}`,
|
|
1832
|
+
diagnostics
|
|
1833
|
+
);
|
|
1834
|
+
if (previewPath) previewPaths.set(type, previewPath);
|
|
1835
|
+
}
|
|
1836
|
+
if (!editor.catalog) return;
|
|
1837
|
+
const catalogRealPath = await resolveRootFile(
|
|
1838
|
+
rootRealPath,
|
|
1839
|
+
editor.catalog,
|
|
1840
|
+
"editor.catalog",
|
|
1841
|
+
diagnostics
|
|
1842
|
+
);
|
|
1843
|
+
if (!catalogRealPath) return;
|
|
1844
|
+
const catalogValue = await readJsonFile(
|
|
1845
|
+
catalogRealPath,
|
|
1846
|
+
editor.catalog,
|
|
1847
|
+
diagnostics
|
|
1848
|
+
);
|
|
1849
|
+
if (!isRecord(catalogValue)) {
|
|
1850
|
+
addError(
|
|
1851
|
+
diagnostics,
|
|
1852
|
+
"KIT_EDITOR_CATALOG_INVALID",
|
|
1853
|
+
"Editor catalog must be a JSON object.",
|
|
1854
|
+
"editor.catalog"
|
|
1855
|
+
);
|
|
1856
|
+
return;
|
|
1857
|
+
}
|
|
1858
|
+
if (catalogValue.schemaVersion !== "1") {
|
|
1859
|
+
addError(
|
|
1860
|
+
diagnostics,
|
|
1861
|
+
"KIT_EDITOR_CATALOG_SCHEMA_VERSION_UNSUPPORTED",
|
|
1862
|
+
'Editor catalog schemaVersion must be "1".',
|
|
1863
|
+
"editor.catalog.schemaVersion"
|
|
1864
|
+
);
|
|
1865
|
+
}
|
|
1866
|
+
if (!Array.isArray(catalogValue.components)) {
|
|
1867
|
+
addError(
|
|
1868
|
+
diagnostics,
|
|
1869
|
+
"KIT_EDITOR_CATALOG_COMPONENTS_INVALID",
|
|
1870
|
+
"Editor catalog components must be an array.",
|
|
1871
|
+
"editor.catalog.components"
|
|
1872
|
+
);
|
|
1873
|
+
return;
|
|
1874
|
+
}
|
|
1875
|
+
const catalogBase = dirname(resolve(rootRealPath, editor.catalog));
|
|
1876
|
+
const seenTypes = /* @__PURE__ */ new Set();
|
|
1877
|
+
const catalogPreviewTypes = /* @__PURE__ */ new Set();
|
|
1878
|
+
for (const [index, value] of catalogValue.components.entries()) {
|
|
1879
|
+
const prefix = `editor.catalog.components[${index}]`;
|
|
1880
|
+
if (!isRecord(value) || typeof value.type !== "string" || typeof value.manifest !== "string") {
|
|
1881
|
+
addError(
|
|
1882
|
+
diagnostics,
|
|
1883
|
+
"KIT_EDITOR_CATALOG_ENTRY_INVALID",
|
|
1884
|
+
"Catalog entry requires string type and manifest fields.",
|
|
1885
|
+
prefix
|
|
1886
|
+
);
|
|
1887
|
+
continue;
|
|
1888
|
+
}
|
|
1889
|
+
const { type } = value;
|
|
1890
|
+
if (seenTypes.has(type))
|
|
1891
|
+
addError(
|
|
1892
|
+
diagnostics,
|
|
1893
|
+
"KIT_EDITOR_CATALOG_TYPE_DUPLICATE",
|
|
1894
|
+
`Duplicate editor catalog component type ${JSON.stringify(type)}.`,
|
|
1895
|
+
`${prefix}.type`
|
|
1896
|
+
);
|
|
1897
|
+
seenTypes.add(type);
|
|
1898
|
+
const kitComponent = componentsByType.get(type);
|
|
1899
|
+
if (!kitComponent) {
|
|
1900
|
+
addError(
|
|
1901
|
+
diagnostics,
|
|
1902
|
+
"KIT_EDITOR_CATALOG_TYPE_UNKNOWN",
|
|
1903
|
+
`Editor catalog references unknown component type ${JSON.stringify(type)}.`,
|
|
1904
|
+
`${prefix}.type`
|
|
1905
|
+
);
|
|
1906
|
+
}
|
|
1907
|
+
const catalogManifest = await resolveNestedFile(
|
|
1908
|
+
rootRealPath,
|
|
1909
|
+
catalogBase,
|
|
1910
|
+
value.manifest,
|
|
1911
|
+
`${prefix}.manifest`,
|
|
1912
|
+
diagnostics
|
|
1913
|
+
);
|
|
1914
|
+
if (catalogManifest && kitComponent?.manifestPath && catalogManifest !== kitComponent.manifestPath) {
|
|
1915
|
+
addError(
|
|
1916
|
+
diagnostics,
|
|
1917
|
+
"KIT_EDITOR_CATALOG_MANIFEST_MISMATCH",
|
|
1918
|
+
"Catalog manifest reference does not identify the kit inventory manifest for this component type.",
|
|
1919
|
+
`${prefix}.manifest`
|
|
1920
|
+
);
|
|
1921
|
+
}
|
|
1922
|
+
if (value.preview !== void 0) {
|
|
1923
|
+
if (typeof value.preview !== "string") {
|
|
1924
|
+
addError(
|
|
1925
|
+
diagnostics,
|
|
1926
|
+
"KIT_EDITOR_CATALOG_PREVIEW_INVALID",
|
|
1927
|
+
"Catalog preview reference must be a string.",
|
|
1928
|
+
`${prefix}.preview`
|
|
1929
|
+
);
|
|
1930
|
+
continue;
|
|
1931
|
+
}
|
|
1932
|
+
catalogPreviewTypes.add(type);
|
|
1933
|
+
const preview = await resolveNestedFile(
|
|
1934
|
+
rootRealPath,
|
|
1935
|
+
catalogBase,
|
|
1936
|
+
value.preview,
|
|
1937
|
+
`${prefix}.preview`,
|
|
1938
|
+
diagnostics
|
|
1939
|
+
);
|
|
1940
|
+
const declaredPreview = previewPaths.get(type);
|
|
1941
|
+
if (!declaredPreview) {
|
|
1942
|
+
addError(
|
|
1943
|
+
diagnostics,
|
|
1944
|
+
"KIT_EDITOR_PREVIEW_UNDECLARED",
|
|
1945
|
+
"Catalog preview must also be declared in designos.kit.json editor.previews.",
|
|
1946
|
+
`${prefix}.preview`
|
|
1947
|
+
);
|
|
1948
|
+
} else if (preview && preview !== declaredPreview) {
|
|
1949
|
+
addError(
|
|
1950
|
+
diagnostics,
|
|
1951
|
+
"KIT_EDITOR_PREVIEW_MISMATCH",
|
|
1952
|
+
"Catalog preview reference does not match editor.previews for this component type.",
|
|
1953
|
+
`${prefix}.preview`
|
|
1954
|
+
);
|
|
1955
|
+
}
|
|
1956
|
+
} else if (previewPaths.has(type)) {
|
|
1957
|
+
addError(
|
|
1958
|
+
diagnostics,
|
|
1959
|
+
"KIT_EDITOR_PREVIEW_MISSING_FROM_CATALOG",
|
|
1960
|
+
"The kit declares a preview for this component but the catalog entry omits it.",
|
|
1961
|
+
`${prefix}.preview`
|
|
1962
|
+
);
|
|
1963
|
+
}
|
|
1964
|
+
}
|
|
1965
|
+
for (const type of previewPaths.keys()) {
|
|
1966
|
+
if (!catalogPreviewTypes.has(type)) {
|
|
1967
|
+
addError(
|
|
1968
|
+
diagnostics,
|
|
1969
|
+
"KIT_EDITOR_PREVIEW_NOT_IN_CATALOG",
|
|
1970
|
+
`Declared editor preview for ${JSON.stringify(type)} is not referenced by the catalog.`,
|
|
1971
|
+
`editor.previews.${type}`
|
|
1972
|
+
);
|
|
1973
|
+
}
|
|
1974
|
+
}
|
|
1975
|
+
}
|
|
1976
|
+
async function validateComponentKit(kitRoot) {
|
|
1977
|
+
const diagnostics = [];
|
|
1978
|
+
let rootRealPath;
|
|
1979
|
+
try {
|
|
1980
|
+
rootRealPath = await realpath(resolve(kitRoot));
|
|
1981
|
+
const rootInfo = await stat(rootRealPath);
|
|
1982
|
+
if (!rootInfo.isDirectory()) throw new Error("Kit root is not a directory");
|
|
1983
|
+
} catch (cause) {
|
|
1984
|
+
addError(
|
|
1985
|
+
diagnostics,
|
|
1986
|
+
"KIT_ROOT_INVALID",
|
|
1987
|
+
`Unable to access kit root: ${cause instanceof Error ? cause.message : String(cause)}`,
|
|
1988
|
+
"."
|
|
1989
|
+
);
|
|
1990
|
+
return { valid: false, diagnostics };
|
|
1991
|
+
}
|
|
1992
|
+
const manifestCandidate = resolve(rootRealPath, "designos.kit.json");
|
|
1993
|
+
const manifestPath = await resolveExistingFile(
|
|
1994
|
+
rootRealPath,
|
|
1995
|
+
manifestCandidate,
|
|
1996
|
+
"designos.kit.json",
|
|
1997
|
+
diagnostics
|
|
1998
|
+
);
|
|
1999
|
+
if (!manifestPath) return { valid: false, diagnostics };
|
|
2000
|
+
const rawManifest = await readJsonFile(
|
|
2001
|
+
manifestPath,
|
|
2002
|
+
"designos.kit.json",
|
|
2003
|
+
diagnostics
|
|
2004
|
+
);
|
|
2005
|
+
if (rawManifest === void 0) return { valid: false, diagnostics };
|
|
2006
|
+
if (!schemaValidator(rawManifest)) {
|
|
2007
|
+
for (const error of schemaValidator.errors ?? []) {
|
|
2008
|
+
diagnostics.push({
|
|
2009
|
+
severity: "error",
|
|
2010
|
+
code: "KIT_MANIFEST_SCHEMA_INVALID",
|
|
2011
|
+
path: error.instancePath || "$",
|
|
2012
|
+
message: `${error.message ?? "Schema constraint failed"}${error.params ? ` (${JSON.stringify(error.params)})` : ""}`
|
|
2013
|
+
});
|
|
2014
|
+
}
|
|
2015
|
+
return { valid: false, diagnostics };
|
|
2016
|
+
}
|
|
2017
|
+
const manifest = rawManifest;
|
|
2018
|
+
validateCompatibility(manifest, diagnostics);
|
|
2019
|
+
const packageCandidate = resolve(rootRealPath, "package.json");
|
|
2020
|
+
const packagePath = await resolveExistingFile(
|
|
2021
|
+
rootRealPath,
|
|
2022
|
+
packageCandidate,
|
|
2023
|
+
"package.json",
|
|
2024
|
+
diagnostics
|
|
2025
|
+
);
|
|
2026
|
+
const packageValue = packagePath ? await readJsonFile(packagePath, "package.json", diagnostics) : void 0;
|
|
2027
|
+
if (!isRecord(packageValue)) {
|
|
2028
|
+
if (packageValue !== void 0)
|
|
2029
|
+
addError(
|
|
2030
|
+
diagnostics,
|
|
2031
|
+
"KIT_PACKAGE_JSON_INVALID",
|
|
2032
|
+
"Root package.json must be a JSON object.",
|
|
2033
|
+
"package.json"
|
|
2034
|
+
);
|
|
2035
|
+
} else {
|
|
2036
|
+
if (packageValue.name !== manifest.id) {
|
|
2037
|
+
addError(
|
|
2038
|
+
diagnostics,
|
|
2039
|
+
"KIT_PACKAGE_NAME_MISMATCH",
|
|
2040
|
+
`package.json name ${JSON.stringify(packageValue.name)} must exactly match kit id ${JSON.stringify(manifest.id)}.`,
|
|
2041
|
+
"package.json.name"
|
|
2042
|
+
);
|
|
2043
|
+
}
|
|
2044
|
+
if (packageValue.version !== manifest.version) {
|
|
2045
|
+
addError(
|
|
2046
|
+
diagnostics,
|
|
2047
|
+
"KIT_PACKAGE_VERSION_MISMATCH",
|
|
2048
|
+
`package.json version ${JSON.stringify(packageValue.version)} must exactly match kit version ${JSON.stringify(manifest.version)}.`,
|
|
2049
|
+
"package.json.version"
|
|
2050
|
+
);
|
|
2051
|
+
}
|
|
2052
|
+
validateSemverPackageVersion(
|
|
2053
|
+
packageValue.version,
|
|
2054
|
+
"package.json.version",
|
|
2055
|
+
diagnostics
|
|
2056
|
+
);
|
|
2057
|
+
}
|
|
2058
|
+
const rootComponents = [];
|
|
2059
|
+
const seenTypes = /* @__PURE__ */ new Set();
|
|
2060
|
+
const seenSources = /* @__PURE__ */ new Set();
|
|
2061
|
+
const seenManifests = /* @__PURE__ */ new Set();
|
|
2062
|
+
for (const [index, component] of manifest.components.entries()) {
|
|
2063
|
+
const prefix = `components[${index}]`;
|
|
2064
|
+
if (seenTypes.has(component.type))
|
|
2065
|
+
addError(
|
|
2066
|
+
diagnostics,
|
|
2067
|
+
"KIT_COMPONENT_TYPE_DUPLICATE",
|
|
2068
|
+
`Duplicate component type ${JSON.stringify(component.type)}.`,
|
|
2069
|
+
`${prefix}.type`
|
|
2070
|
+
);
|
|
2071
|
+
seenTypes.add(component.type);
|
|
2072
|
+
const sourcePath = await resolveRootFile(
|
|
2073
|
+
rootRealPath,
|
|
2074
|
+
component.source,
|
|
2075
|
+
`${prefix}.source`,
|
|
2076
|
+
diagnostics
|
|
2077
|
+
);
|
|
2078
|
+
const manifestPathValue = await resolveRootFile(
|
|
2079
|
+
rootRealPath,
|
|
2080
|
+
component.manifest,
|
|
2081
|
+
`${prefix}.manifest`,
|
|
2082
|
+
diagnostics
|
|
2083
|
+
);
|
|
2084
|
+
if (sourcePath) {
|
|
2085
|
+
if (seenSources.has(sourcePath))
|
|
2086
|
+
addError(
|
|
2087
|
+
diagnostics,
|
|
2088
|
+
"KIT_COMPONENT_SOURCE_DUPLICATE",
|
|
2089
|
+
"Multiple kit inventory entries resolve to the same source file.",
|
|
2090
|
+
`${prefix}.source`
|
|
2091
|
+
);
|
|
2092
|
+
seenSources.add(sourcePath);
|
|
2093
|
+
}
|
|
2094
|
+
if (manifestPathValue) {
|
|
2095
|
+
if (seenManifests.has(manifestPathValue))
|
|
2096
|
+
addError(
|
|
2097
|
+
diagnostics,
|
|
2098
|
+
"KIT_COMPONENT_MANIFEST_DUPLICATE",
|
|
2099
|
+
"Multiple kit inventory entries resolve to the same component manifest.",
|
|
2100
|
+
`${prefix}.manifest`
|
|
2101
|
+
);
|
|
2102
|
+
seenManifests.add(manifestPathValue);
|
|
2103
|
+
}
|
|
2104
|
+
rootComponents.push({
|
|
2105
|
+
definition: component,
|
|
2106
|
+
sourcePath,
|
|
2107
|
+
manifestPath: manifestPathValue
|
|
2108
|
+
});
|
|
2109
|
+
}
|
|
2110
|
+
const compilerProjectPath = await resolveRootFile(
|
|
2111
|
+
rootRealPath,
|
|
2112
|
+
manifest.compilerProject,
|
|
2113
|
+
"compilerProject",
|
|
2114
|
+
diagnostics
|
|
2115
|
+
);
|
|
2116
|
+
if (!compilerProjectPath) return { valid: false, diagnostics };
|
|
2117
|
+
const rawProject = await readJsonFile(
|
|
2118
|
+
compilerProjectPath,
|
|
2119
|
+
manifest.compilerProject,
|
|
2120
|
+
diagnostics
|
|
2121
|
+
);
|
|
2122
|
+
if (!isRecord(rawProject)) {
|
|
2123
|
+
if (rawProject !== void 0)
|
|
2124
|
+
addError(
|
|
2125
|
+
diagnostics,
|
|
2126
|
+
"KIT_COMPILER_PROJECT_INVALID",
|
|
2127
|
+
"Compiler project must be a JSON object.",
|
|
2128
|
+
"compilerProject"
|
|
2129
|
+
);
|
|
2130
|
+
return { valid: false, diagnostics };
|
|
2131
|
+
}
|
|
2132
|
+
if (rawProject.schemaVersion !== "2") {
|
|
2133
|
+
addError(
|
|
2134
|
+
diagnostics,
|
|
2135
|
+
"KIT_COMPILER_PROJECT_SCHEMA_VERSION_UNSUPPORTED",
|
|
2136
|
+
'Component-kit inventory requires a file-based compiler project with schemaVersion "2".',
|
|
2137
|
+
"compilerProject.schemaVersion"
|
|
2138
|
+
);
|
|
2139
|
+
return { valid: false, diagnostics };
|
|
2140
|
+
}
|
|
2141
|
+
const sourceProject = rawProject;
|
|
2142
|
+
if (!Array.isArray(sourceProject.components)) {
|
|
2143
|
+
addError(
|
|
2144
|
+
diagnostics,
|
|
2145
|
+
"KIT_COMPILER_PROJECT_COMPONENTS_INVALID",
|
|
2146
|
+
"Compiler project components must be an array.",
|
|
2147
|
+
"compilerProject.components"
|
|
2148
|
+
);
|
|
2149
|
+
return { valid: false, diagnostics };
|
|
2150
|
+
}
|
|
2151
|
+
const projectComponents = [];
|
|
2152
|
+
const seenProjectSources = /* @__PURE__ */ new Set();
|
|
2153
|
+
const seenProjectManifests = /* @__PURE__ */ new Set();
|
|
2154
|
+
const compilerComponents = [];
|
|
2155
|
+
const projectStyles = [];
|
|
2156
|
+
const declaredStylePaths = /* @__PURE__ */ new Set();
|
|
2157
|
+
const declaredAssetPaths = /* @__PURE__ */ new Set();
|
|
2158
|
+
const declaredOutputPaths = /* @__PURE__ */ new Map();
|
|
2159
|
+
if (sourceProject.styles !== void 0 && !Array.isArray(sourceProject.styles)) {
|
|
2160
|
+
addError(
|
|
2161
|
+
diagnostics,
|
|
2162
|
+
"KIT_COMPILER_PROJECT_STYLES_INVALID",
|
|
2163
|
+
"Compiler project styles must be an array.",
|
|
2164
|
+
"compilerProject.styles"
|
|
2165
|
+
);
|
|
2166
|
+
}
|
|
2167
|
+
for (const [index, style] of (Array.isArray(sourceProject.styles) ? sourceProject.styles : []).entries()) {
|
|
2168
|
+
const parsed = await validateProjectAsset(
|
|
2169
|
+
rootRealPath,
|
|
2170
|
+
style,
|
|
2171
|
+
`compilerProject.styles[${index}]`,
|
|
2172
|
+
diagnostics,
|
|
2173
|
+
declaredStylePaths,
|
|
2174
|
+
declaredOutputPaths
|
|
2175
|
+
);
|
|
2176
|
+
if (parsed) projectStyles.push(parsed);
|
|
2177
|
+
}
|
|
2178
|
+
for (const [index, entry] of sourceProject.components.entries()) {
|
|
2179
|
+
const prefix = `compilerProject.components[${index}]`;
|
|
2180
|
+
if (!isRecord(entry)) {
|
|
2181
|
+
addError(
|
|
2182
|
+
diagnostics,
|
|
2183
|
+
"KIT_COMPILER_PROJECT_COMPONENT_INVALID",
|
|
2184
|
+
"Compiler project component entry must be an object.",
|
|
2185
|
+
prefix
|
|
2186
|
+
);
|
|
2187
|
+
projectComponents.push({});
|
|
2188
|
+
continue;
|
|
2189
|
+
}
|
|
2190
|
+
const sourcePath = await resolveRootFile(
|
|
2191
|
+
rootRealPath,
|
|
2192
|
+
entry.source,
|
|
2193
|
+
`${prefix}.source`,
|
|
2194
|
+
diagnostics
|
|
2195
|
+
);
|
|
2196
|
+
const componentManifestPath = await resolveRootFile(
|
|
2197
|
+
rootRealPath,
|
|
2198
|
+
entry.manifest,
|
|
2199
|
+
`${prefix}.manifest`,
|
|
2200
|
+
diagnostics
|
|
2201
|
+
);
|
|
2202
|
+
if (sourcePath) {
|
|
2203
|
+
if (seenProjectSources.has(sourcePath))
|
|
2204
|
+
addError(
|
|
2205
|
+
diagnostics,
|
|
2206
|
+
"KIT_PROJECT_SOURCE_DUPLICATE",
|
|
2207
|
+
"Multiple compiler project entries resolve to the same source file.",
|
|
2208
|
+
`${prefix}.source`
|
|
2209
|
+
);
|
|
2210
|
+
seenProjectSources.add(sourcePath);
|
|
2211
|
+
}
|
|
2212
|
+
if (componentManifestPath) {
|
|
2213
|
+
if (seenProjectManifests.has(componentManifestPath))
|
|
2214
|
+
addError(
|
|
2215
|
+
diagnostics,
|
|
2216
|
+
"KIT_PROJECT_MANIFEST_DUPLICATE",
|
|
2217
|
+
"Multiple compiler project entries resolve to the same manifest file.",
|
|
2218
|
+
`${prefix}.manifest`
|
|
2219
|
+
);
|
|
2220
|
+
seenProjectManifests.add(componentManifestPath);
|
|
2221
|
+
}
|
|
2222
|
+
if (entry.exportName !== void 0 && typeof entry.exportName !== "string") {
|
|
2223
|
+
addError(
|
|
2224
|
+
diagnostics,
|
|
2225
|
+
"KIT_PROJECT_EXPORT_NAME_INVALID",
|
|
2226
|
+
"Compiler project exportName must be a string when provided.",
|
|
2227
|
+
`${prefix}.exportName`
|
|
2228
|
+
);
|
|
2229
|
+
}
|
|
2230
|
+
let manifestType;
|
|
2231
|
+
let parsedManifest;
|
|
2232
|
+
if (componentManifestPath) {
|
|
2233
|
+
const value = await readJsonFile(
|
|
2234
|
+
componentManifestPath,
|
|
2235
|
+
String(entry.manifest),
|
|
2236
|
+
diagnostics
|
|
2237
|
+
);
|
|
2238
|
+
if (value !== void 0) {
|
|
2239
|
+
const result = ComponentManifestSchema.safeParse(value);
|
|
2240
|
+
if (!result.success) {
|
|
2241
|
+
for (const issue of result.error.issues) {
|
|
2242
|
+
addError(
|
|
2243
|
+
diagnostics,
|
|
2244
|
+
"KIT_COMPONENT_MANIFEST_INVALID",
|
|
2245
|
+
`${issue.path.join(".") || "manifest"}: ${issue.message}`,
|
|
2246
|
+
`${prefix}.manifest`
|
|
2247
|
+
);
|
|
2248
|
+
}
|
|
2249
|
+
} else {
|
|
2250
|
+
parsedManifest = result.data;
|
|
2251
|
+
manifestType = parsedManifest.type;
|
|
2252
|
+
for (const issue of validateComponentManifest(parsedManifest).issues) {
|
|
2253
|
+
addError(
|
|
2254
|
+
diagnostics,
|
|
2255
|
+
"KIT_COMPONENT_MANIFEST_SEMANTIC_INVALID",
|
|
2256
|
+
issue.message,
|
|
2257
|
+
`${prefix}.manifest.${issue.path}`
|
|
2258
|
+
);
|
|
2259
|
+
}
|
|
2260
|
+
}
|
|
2261
|
+
}
|
|
2262
|
+
}
|
|
2263
|
+
let implementation;
|
|
2264
|
+
let sourceImports = [];
|
|
2265
|
+
if (sourcePath && typeof entry.source === "string") {
|
|
2266
|
+
if (!entry.source.endsWith(".lite.tsx")) {
|
|
2267
|
+
addError(
|
|
2268
|
+
diagnostics,
|
|
2269
|
+
"KIT_COMPONENT_SOURCE_EXTENSION_INVALID",
|
|
2270
|
+
"Compiler source files must use the .lite.tsx extension.",
|
|
2271
|
+
`${prefix}.source`
|
|
2272
|
+
);
|
|
2273
|
+
}
|
|
2274
|
+
try {
|
|
2275
|
+
const sourceText = await readFile(sourcePath, "utf8");
|
|
2276
|
+
try {
|
|
2277
|
+
sourceImports = collectSourceImports(sourceText);
|
|
2278
|
+
} catch (cause) {
|
|
2279
|
+
addError(
|
|
2280
|
+
diagnostics,
|
|
2281
|
+
"KIT_SOURCE_IMPORT_SCAN_FAILED",
|
|
2282
|
+
`Unable to inspect source imports: ${cause instanceof Error ? cause.message : String(cause)}`,
|
|
2283
|
+
`${prefix}.source`
|
|
2284
|
+
);
|
|
2285
|
+
}
|
|
2286
|
+
const component = parseMitosisSource(sourceText, {
|
|
2287
|
+
filePath: entry.source,
|
|
2288
|
+
enableBlocksSlots: true
|
|
2289
|
+
});
|
|
2290
|
+
implementation = { kind: "mitosis", component };
|
|
2291
|
+
if (typeof entry.exportName === "string" && component.name !== entry.exportName) {
|
|
2292
|
+
addError(
|
|
2293
|
+
diagnostics,
|
|
2294
|
+
"KIT_PROJECT_EXPORT_NAME_MISMATCH",
|
|
2295
|
+
`exportName ${JSON.stringify(entry.exportName)} does not match parsed component name ${JSON.stringify(component.name)}.`,
|
|
2296
|
+
`${prefix}.exportName`
|
|
2297
|
+
);
|
|
2298
|
+
}
|
|
2299
|
+
} catch (cause) {
|
|
2300
|
+
addError(
|
|
2301
|
+
diagnostics,
|
|
2302
|
+
"KIT_COMPONENT_SOURCE_INVALID",
|
|
2303
|
+
`Unable to parse compiler source: ${cause instanceof Error ? cause.message : String(cause)}`,
|
|
2304
|
+
`${prefix}.source`
|
|
2305
|
+
);
|
|
2306
|
+
}
|
|
2307
|
+
}
|
|
2308
|
+
const styles = [];
|
|
2309
|
+
if (entry.styles !== void 0 && !Array.isArray(entry.styles)) {
|
|
2310
|
+
addError(
|
|
2311
|
+
diagnostics,
|
|
2312
|
+
"KIT_PROJECT_STYLES_INVALID",
|
|
2313
|
+
"Component styles must be an array.",
|
|
2314
|
+
`${prefix}.styles`
|
|
2315
|
+
);
|
|
2316
|
+
}
|
|
2317
|
+
for (const [styleIndex, style] of (Array.isArray(entry.styles) ? entry.styles : []).entries()) {
|
|
2318
|
+
const parsed = await validateProjectAsset(
|
|
2319
|
+
rootRealPath,
|
|
2320
|
+
style,
|
|
2321
|
+
`${prefix}.styles[${styleIndex}]`,
|
|
2322
|
+
diagnostics,
|
|
2323
|
+
declaredStylePaths,
|
|
2324
|
+
declaredOutputPaths
|
|
2325
|
+
);
|
|
2326
|
+
if (parsed) styles.push(parsed);
|
|
2327
|
+
}
|
|
2328
|
+
const assets = [];
|
|
2329
|
+
if (entry.assets !== void 0 && !Array.isArray(entry.assets)) {
|
|
2330
|
+
addError(
|
|
2331
|
+
diagnostics,
|
|
2332
|
+
"KIT_PROJECT_ASSETS_INVALID",
|
|
2333
|
+
"Component assets must be an array.",
|
|
2334
|
+
`${prefix}.assets`
|
|
2335
|
+
);
|
|
2336
|
+
}
|
|
2337
|
+
for (const [assetIndex, asset] of (Array.isArray(entry.assets) ? entry.assets : []).entries()) {
|
|
2338
|
+
const parsed = await validateProjectAsset(
|
|
2339
|
+
rootRealPath,
|
|
2340
|
+
asset,
|
|
2341
|
+
`${prefix}.assets[${assetIndex}]`,
|
|
2342
|
+
diagnostics,
|
|
2343
|
+
declaredAssetPaths,
|
|
2344
|
+
declaredOutputPaths
|
|
2345
|
+
);
|
|
2346
|
+
if (parsed) assets.push(parsed);
|
|
2347
|
+
}
|
|
2348
|
+
if (sourcePath && implementation?.kind === "mitosis") {
|
|
2349
|
+
await validateLocalImports(
|
|
2350
|
+
rootRealPath,
|
|
2351
|
+
dirname(sourcePath),
|
|
2352
|
+
sourceImports,
|
|
2353
|
+
declaredStylePaths,
|
|
2354
|
+
declaredAssetPaths,
|
|
2355
|
+
prefix,
|
|
2356
|
+
diagnostics
|
|
2357
|
+
);
|
|
2358
|
+
}
|
|
2359
|
+
const outputDirectory = entry.outputDirectory;
|
|
2360
|
+
if (outputDirectory !== void 0 && typeof outputDirectory !== "string") {
|
|
2361
|
+
addError(
|
|
2362
|
+
diagnostics,
|
|
2363
|
+
"KIT_PROJECT_OUTPUT_DIRECTORY_INVALID",
|
|
2364
|
+
"outputDirectory must be a string when provided.",
|
|
2365
|
+
`${prefix}.outputDirectory`
|
|
2366
|
+
);
|
|
2367
|
+
}
|
|
2368
|
+
if (parsedManifest && implementation) {
|
|
2369
|
+
const sourceDirectory = typeof entry.source === "string" ? dirname(entry.source).split(sep).join("/") : ".";
|
|
2370
|
+
const component = {
|
|
2371
|
+
manifest: parsedManifest,
|
|
2372
|
+
implementation,
|
|
2373
|
+
outputDirectory: typeof outputDirectory === "string" ? outputDirectory : sourceDirectory === "." ? "components" : sourceDirectory,
|
|
2374
|
+
styles,
|
|
2375
|
+
assets
|
|
2376
|
+
};
|
|
2377
|
+
compilerComponents.push(component);
|
|
2378
|
+
}
|
|
2379
|
+
projectComponents.push({
|
|
2380
|
+
source: sourcePath,
|
|
2381
|
+
manifest: componentManifestPath,
|
|
2382
|
+
exportName: typeof entry.exportName === "string" ? entry.exportName : void 0,
|
|
2383
|
+
manifestType
|
|
2384
|
+
});
|
|
2385
|
+
}
|
|
2386
|
+
for (const [outputPath, pathOwner] of declaredOutputPaths) {
|
|
2387
|
+
const nestedOutput = [...declaredOutputPaths.keys()].find(
|
|
2388
|
+
(candidate) => candidate.startsWith(`${outputPath}/`)
|
|
2389
|
+
);
|
|
2390
|
+
if (nestedOutput) {
|
|
2391
|
+
addError(
|
|
2392
|
+
diagnostics,
|
|
2393
|
+
"KIT_OUTPUT_PATH_FILE_DIRECTORY_CONFLICT",
|
|
2394
|
+
`Output path ${JSON.stringify(outputPath)} is also used as a directory prefix for ${JSON.stringify(nestedOutput)}.`,
|
|
2395
|
+
`${pathOwner}.outputPath`
|
|
2396
|
+
);
|
|
2397
|
+
}
|
|
2398
|
+
}
|
|
2399
|
+
const rootComponentsByType = new Map(
|
|
2400
|
+
rootComponents.map((component) => [component.definition.type, component])
|
|
2401
|
+
);
|
|
2402
|
+
const projectTypes = /* @__PURE__ */ new Set();
|
|
2403
|
+
for (const [index, projectComponent] of projectComponents.entries()) {
|
|
2404
|
+
const type = projectComponent.manifestType;
|
|
2405
|
+
if (!type) continue;
|
|
2406
|
+
if (projectTypes.has(type))
|
|
2407
|
+
addError(
|
|
2408
|
+
diagnostics,
|
|
2409
|
+
"KIT_PROJECT_COMPONENT_TYPE_DUPLICATE",
|
|
2410
|
+
`Duplicate compiler project component type ${JSON.stringify(type)}.`,
|
|
2411
|
+
`compilerProject.components[${index}].manifest`
|
|
2412
|
+
);
|
|
2413
|
+
projectTypes.add(type);
|
|
2414
|
+
const kitComponent = rootComponentsByType.get(type);
|
|
2415
|
+
if (!kitComponent) {
|
|
2416
|
+
addError(
|
|
2417
|
+
diagnostics,
|
|
2418
|
+
"KIT_PROJECT_COMPONENT_NOT_IN_INVENTORY",
|
|
2419
|
+
`Compiler project component type ${JSON.stringify(type)} is missing from the kit inventory.`,
|
|
2420
|
+
`compilerProject.components[${index}]`
|
|
2421
|
+
);
|
|
2422
|
+
continue;
|
|
2423
|
+
}
|
|
2424
|
+
if (projectComponent.source && kitComponent.sourcePath && projectComponent.source !== kitComponent.sourcePath) {
|
|
2425
|
+
addError(
|
|
2426
|
+
diagnostics,
|
|
2427
|
+
"KIT_COMPONENT_SOURCE_MISMATCH",
|
|
2428
|
+
`Compiler project source for ${JSON.stringify(type)} does not match the package-relative inventory source.`,
|
|
2429
|
+
`compilerProject.components[${index}].source`
|
|
2430
|
+
);
|
|
2431
|
+
}
|
|
2432
|
+
if (projectComponent.manifest && kitComponent.manifestPath && projectComponent.manifest !== kitComponent.manifestPath) {
|
|
2433
|
+
addError(
|
|
2434
|
+
diagnostics,
|
|
2435
|
+
"KIT_COMPONENT_MANIFEST_MISMATCH",
|
|
2436
|
+
`Compiler project manifest for ${JSON.stringify(type)} does not match the package-relative inventory manifest.`,
|
|
2437
|
+
`compilerProject.components[${index}].manifest`
|
|
2438
|
+
);
|
|
2439
|
+
}
|
|
2440
|
+
if (projectComponent.exportName !== kitComponent.definition.exportName) {
|
|
2441
|
+
addError(
|
|
2442
|
+
diagnostics,
|
|
2443
|
+
"KIT_COMPONENT_EXPORT_NAME_MISMATCH",
|
|
2444
|
+
`Compiler project exportName for ${JSON.stringify(type)} must match the kit inventory.`,
|
|
2445
|
+
`compilerProject.components[${index}].exportName`
|
|
2446
|
+
);
|
|
2447
|
+
}
|
|
2448
|
+
}
|
|
2449
|
+
for (const [index, component] of rootComponents.entries()) {
|
|
2450
|
+
if (!projectTypes.has(component.definition.type)) {
|
|
2451
|
+
addError(
|
|
2452
|
+
diagnostics,
|
|
2453
|
+
"KIT_INVENTORY_COMPONENT_NOT_IN_PROJECT",
|
|
2454
|
+
`Kit inventory component type ${JSON.stringify(component.definition.type)} is missing from the compiler project.`,
|
|
2455
|
+
`components[${index}]`
|
|
2456
|
+
);
|
|
2457
|
+
}
|
|
2458
|
+
}
|
|
2459
|
+
if (projectComponents.length !== rootComponents.length) {
|
|
2460
|
+
addError(
|
|
2461
|
+
diagnostics,
|
|
2462
|
+
"KIT_INVENTORY_PROJECT_COMPONENT_COUNT_MISMATCH",
|
|
2463
|
+
`Kit inventory has ${rootComponents.length} components, but compiler project has ${projectComponents.length}.`,
|
|
2464
|
+
"components"
|
|
2465
|
+
);
|
|
2466
|
+
}
|
|
2467
|
+
const compilerProject = {
|
|
2468
|
+
schemaVersion: "1",
|
|
2469
|
+
components: compilerComponents,
|
|
2470
|
+
styles: projectStyles
|
|
2471
|
+
};
|
|
2472
|
+
for (const diagnostic of validateCompilerProject(compilerProject)) {
|
|
2473
|
+
diagnostics.push(convertCompilerDiagnostic(diagnostic));
|
|
2474
|
+
}
|
|
2475
|
+
await validateEditorAdapters(
|
|
2476
|
+
rootRealPath,
|
|
2477
|
+
manifest,
|
|
2478
|
+
rootComponents,
|
|
2479
|
+
diagnostics
|
|
2480
|
+
);
|
|
2481
|
+
return {
|
|
2482
|
+
valid: !diagnostics.some((diagnostic) => diagnostic.severity === "error"),
|
|
2483
|
+
diagnostics
|
|
2484
|
+
};
|
|
2485
|
+
}
|
|
2486
|
+
async function validateProjectAsset(rootRealPath, value, displayPath, diagnostics, declaredPaths, declaredOutputPaths) {
|
|
2487
|
+
if (!isRecord(value) || typeof value.source !== "string") {
|
|
2488
|
+
addError(
|
|
2489
|
+
diagnostics,
|
|
2490
|
+
"KIT_PROJECT_ASSET_INVALID",
|
|
2491
|
+
"Asset reference requires a project-relative source path.",
|
|
2492
|
+
displayPath
|
|
2493
|
+
);
|
|
2494
|
+
return void 0;
|
|
2495
|
+
}
|
|
2496
|
+
const sourcePath = await resolveRootFile(
|
|
2497
|
+
rootRealPath,
|
|
2498
|
+
value.source,
|
|
2499
|
+
`${displayPath}.source`,
|
|
2500
|
+
diagnostics
|
|
2501
|
+
);
|
|
2502
|
+
let outputPath;
|
|
2503
|
+
if (value.outputPath !== void 0) {
|
|
2504
|
+
if (!isSafeRelativePath2(value.outputPath)) {
|
|
2505
|
+
addError(
|
|
2506
|
+
diagnostics,
|
|
2507
|
+
"KIT_PROJECT_ASSET_OUTPUT_PATH_INVALID",
|
|
2508
|
+
"Asset outputPath must be a normalized relative path.",
|
|
2509
|
+
`${displayPath}.outputPath`
|
|
2510
|
+
);
|
|
2511
|
+
} else {
|
|
2512
|
+
outputPath = value.outputPath;
|
|
2513
|
+
}
|
|
2514
|
+
}
|
|
2515
|
+
if (!sourcePath) return void 0;
|
|
2516
|
+
declaredPaths?.add(sourcePath);
|
|
2517
|
+
const effectiveOutputPath = outputPath ?? value.source;
|
|
2518
|
+
const outputKey = effectiveOutputPath.toLowerCase();
|
|
2519
|
+
const existingOutput = declaredOutputPaths?.get(outputKey);
|
|
2520
|
+
if (existingOutput) {
|
|
2521
|
+
addError(
|
|
2522
|
+
diagnostics,
|
|
2523
|
+
"KIT_OUTPUT_PATH_DUPLICATE",
|
|
2524
|
+
`Output path ${JSON.stringify(effectiveOutputPath)} is already declared by ${existingOutput}.`,
|
|
2525
|
+
`${displayPath}.outputPath`
|
|
2526
|
+
);
|
|
2527
|
+
} else {
|
|
2528
|
+
declaredOutputPaths?.set(outputKey, displayPath);
|
|
2529
|
+
}
|
|
2530
|
+
return { source: value.source, ...outputPath ? { outputPath } : {} };
|
|
2531
|
+
}
|
|
2532
|
+
async function validateLocalImports(rootRealPath, sourceDirectory, imports, declaredStyles, declaredAssets, componentPath, diagnostics) {
|
|
2533
|
+
for (const [index, importEntry] of imports.entries()) {
|
|
2534
|
+
const importPath = importEntry;
|
|
2535
|
+
if (!importPath.startsWith(".")) {
|
|
2536
|
+
if (isAbsolute(importPath) || /^[A-Za-z]:/.test(importPath)) {
|
|
2537
|
+
addError(
|
|
2538
|
+
diagnostics,
|
|
2539
|
+
"KIT_SOURCE_IMPORT_ABSOLUTE_INVALID",
|
|
2540
|
+
"Local source imports must not use absolute filesystem paths.",
|
|
2541
|
+
`${componentPath}.source.imports[${index}]`
|
|
2542
|
+
);
|
|
2543
|
+
}
|
|
2544
|
+
continue;
|
|
2545
|
+
}
|
|
2546
|
+
const candidates = [importPath];
|
|
2547
|
+
if (!extname(importPath)) {
|
|
2548
|
+
candidates.push(
|
|
2549
|
+
`${importPath}.lite.tsx`,
|
|
2550
|
+
`${importPath}.tsx`,
|
|
2551
|
+
`${importPath}.ts`,
|
|
2552
|
+
`${importPath}.jsx`,
|
|
2553
|
+
`${importPath}.js`,
|
|
2554
|
+
`${importPath}.css`
|
|
2555
|
+
);
|
|
2556
|
+
}
|
|
2557
|
+
let resolvedImport;
|
|
2558
|
+
let escapedRoot = false;
|
|
2559
|
+
for (const candidatePath of candidates) {
|
|
2560
|
+
const candidate = resolve(sourceDirectory, candidatePath);
|
|
2561
|
+
if (!isWithin(rootRealPath, candidate)) {
|
|
2562
|
+
escapedRoot = true;
|
|
2563
|
+
addError(
|
|
2564
|
+
diagnostics,
|
|
2565
|
+
"KIT_SOURCE_IMPORT_ESCAPES_ROOT",
|
|
2566
|
+
"Local source import resolves outside the kit package root.",
|
|
2567
|
+
`${componentPath}.source.imports[${index}]`
|
|
2568
|
+
);
|
|
2569
|
+
break;
|
|
2570
|
+
}
|
|
2571
|
+
try {
|
|
2572
|
+
const candidateRealPath = await realpath(candidate);
|
|
2573
|
+
if (!isWithin(rootRealPath, candidateRealPath)) {
|
|
2574
|
+
escapedRoot = true;
|
|
2575
|
+
addError(
|
|
2576
|
+
diagnostics,
|
|
2577
|
+
"KIT_SOURCE_IMPORT_SYMLINK_ESCAPE",
|
|
2578
|
+
"Local source import resolves through a symlink outside the kit package root.",
|
|
2579
|
+
`${componentPath}.source.imports[${index}]`
|
|
2580
|
+
);
|
|
2581
|
+
break;
|
|
2582
|
+
}
|
|
2583
|
+
if ((await stat(candidateRealPath)).isFile()) {
|
|
2584
|
+
resolvedImport = candidateRealPath;
|
|
2585
|
+
break;
|
|
2586
|
+
}
|
|
2587
|
+
} catch {
|
|
2588
|
+
}
|
|
2589
|
+
}
|
|
2590
|
+
if (!resolvedImport) {
|
|
2591
|
+
if (!escapedRoot) {
|
|
2592
|
+
addError(
|
|
2593
|
+
diagnostics,
|
|
2594
|
+
"KIT_SOURCE_IMPORT_NOT_FOUND",
|
|
2595
|
+
`Local source import ${JSON.stringify(importPath)} does not resolve to a file inside the kit package.`,
|
|
2596
|
+
`${componentPath}.source.imports[${index}]`
|
|
2597
|
+
);
|
|
2598
|
+
}
|
|
2599
|
+
continue;
|
|
2600
|
+
}
|
|
2601
|
+
const extension = extname(resolvedImport).toLowerCase();
|
|
2602
|
+
if (extension === ".css" && !declaredStyles.has(resolvedImport)) {
|
|
2603
|
+
addError(
|
|
2604
|
+
diagnostics,
|
|
2605
|
+
"KIT_LOCAL_STYLE_UNDECLARED",
|
|
2606
|
+
`Imported stylesheet ${JSON.stringify(importPath)} must be declared in compiler-project styles.`,
|
|
2607
|
+
`${componentPath}.source.imports[${index}]`
|
|
2608
|
+
);
|
|
2609
|
+
}
|
|
2610
|
+
if ([
|
|
2611
|
+
".avif",
|
|
2612
|
+
".gif",
|
|
2613
|
+
".jpeg",
|
|
2614
|
+
".jpg",
|
|
2615
|
+
".png",
|
|
2616
|
+
".svg",
|
|
2617
|
+
".webp",
|
|
2618
|
+
".woff",
|
|
2619
|
+
".woff2",
|
|
2620
|
+
".ttf",
|
|
2621
|
+
".otf"
|
|
2622
|
+
].includes(extension) && !declaredAssets.has(resolvedImport)) {
|
|
2623
|
+
addError(
|
|
2624
|
+
diagnostics,
|
|
2625
|
+
"KIT_LOCAL_ASSET_UNDECLARED",
|
|
2626
|
+
`Imported asset ${JSON.stringify(importPath)} must be declared in compiler-project assets.`,
|
|
2627
|
+
`${componentPath}.source.imports[${index}]`
|
|
2628
|
+
);
|
|
2629
|
+
}
|
|
2630
|
+
}
|
|
2631
|
+
}
|
|
2632
|
+
|
|
1279
2633
|
// src/run.ts
|
|
1280
2634
|
var HELP = `DesignOS compiler CLI
|
|
1281
2635
|
|
|
1282
2636
|
Usage:
|
|
1283
2637
|
designos compile --target <target> [options]
|
|
2638
|
+
designos kit validate [--root <directory>]
|
|
1284
2639
|
|
|
1285
2640
|
Options:
|
|
1286
2641
|
--target <target> Registered target name (for example react or vue)
|
|
@@ -1290,6 +2645,48 @@ Options:
|
|
|
1290
2645
|
--target-option key=value Pass a target-specific option; repeatable, values parse as JSON
|
|
1291
2646
|
--help Show this help
|
|
1292
2647
|
`;
|
|
2648
|
+
var KIT_HELP = `DesignOS component-kit validator
|
|
2649
|
+
|
|
2650
|
+
Usage:
|
|
2651
|
+
designos kit validate [--root <directory>]
|
|
2652
|
+
|
|
2653
|
+
Options:
|
|
2654
|
+
--root <directory> Kit package root (default: current directory)
|
|
2655
|
+
--help Show this help
|
|
2656
|
+
|
|
2657
|
+
Validation is read-only and does not compile a target or write artifacts.
|
|
2658
|
+
`;
|
|
2659
|
+
function parseKitArguments(args) {
|
|
2660
|
+
if (args.length === 0 || args[0] === "--help" || args[0] === "help") return { help: true };
|
|
2661
|
+
if (args[0] !== "validate") throw new Error(`Unknown kit command: ${args[0]}
|
|
2662
|
+
|
|
2663
|
+
${KIT_HELP}`);
|
|
2664
|
+
const options = {};
|
|
2665
|
+
for (let index = 1; index < args.length; index += 1) {
|
|
2666
|
+
const flag = args[index];
|
|
2667
|
+
if (flag === "--help") return { help: true };
|
|
2668
|
+
if (flag !== "--root") throw new Error(`Unknown option: ${flag}
|
|
2669
|
+
|
|
2670
|
+
${KIT_HELP}`);
|
|
2671
|
+
const value = args[index + 1];
|
|
2672
|
+
if (!value || value.startsWith("--")) throw new Error(`Missing value for ${flag}`);
|
|
2673
|
+
options.root = value;
|
|
2674
|
+
index += 1;
|
|
2675
|
+
}
|
|
2676
|
+
return options;
|
|
2677
|
+
}
|
|
2678
|
+
async function runKitCommand(args, output, cwd) {
|
|
2679
|
+
const parsed = parseKitArguments(args);
|
|
2680
|
+
if ("help" in parsed) {
|
|
2681
|
+
output.log(KIT_HELP);
|
|
2682
|
+
return 0;
|
|
2683
|
+
}
|
|
2684
|
+
const result = await validateComponentKit(resolve2(cwd, parsed.root ?? "."));
|
|
2685
|
+
printDiagnostics(output, result.diagnostics);
|
|
2686
|
+
if (!result.valid) return 1;
|
|
2687
|
+
output.log("Component kit validation passed. No target compilation or output generation was run.");
|
|
2688
|
+
return 0;
|
|
2689
|
+
}
|
|
1293
2690
|
function parseTargetOption(value) {
|
|
1294
2691
|
const separator = value.indexOf("=");
|
|
1295
2692
|
if (separator < 1) throw new Error(`Invalid --target-option ${JSON.stringify(value)}; expected key=value`);
|
|
@@ -1334,16 +2731,16 @@ ${HELP}`);
|
|
|
1334
2731
|
if (!options.target) throw new Error("Missing required --target option");
|
|
1335
2732
|
return options;
|
|
1336
2733
|
}
|
|
1337
|
-
function
|
|
1338
|
-
const pathFromRoot =
|
|
1339
|
-
return pathFromRoot === "" || pathFromRoot !== ".." && !pathFromRoot.startsWith(`..${
|
|
2734
|
+
function isWithin2(root, candidate) {
|
|
2735
|
+
const pathFromRoot = relative2(root, candidate);
|
|
2736
|
+
return pathFromRoot === "" || pathFromRoot !== ".." && !pathFromRoot.startsWith(`..${sep2}`) && !isAbsolute2(pathFromRoot);
|
|
1340
2737
|
}
|
|
1341
|
-
function
|
|
2738
|
+
function isSafeRelativePath3(value) {
|
|
1342
2739
|
return value.length > 0 && !value.startsWith("/") && !value.includes("\\") && !/^[A-Za-z]:/.test(value) && value.split("/").every((segment) => segment.length > 0 && segment !== "." && segment !== "..");
|
|
1343
2740
|
}
|
|
1344
2741
|
async function ensureDirectoryWithoutSymlinks(root, relativeDirectory) {
|
|
1345
2742
|
let current = root;
|
|
1346
|
-
for (const segment of relativeDirectory ? relativeDirectory.split(
|
|
2743
|
+
for (const segment of relativeDirectory ? relativeDirectory.split(sep2).filter(Boolean) : []) {
|
|
1347
2744
|
current = join(current, segment);
|
|
1348
2745
|
try {
|
|
1349
2746
|
const entry = await lstat(current);
|
|
@@ -1358,28 +2755,28 @@ async function ensureDirectoryWithoutSymlinks(root, relativeDirectory) {
|
|
|
1358
2755
|
return current;
|
|
1359
2756
|
}
|
|
1360
2757
|
async function prepareOutputRoot(projectRoot, outputArgument) {
|
|
1361
|
-
const distPath =
|
|
1362
|
-
const requestedOutput =
|
|
1363
|
-
if (requestedOutput === distPath || !
|
|
2758
|
+
const distPath = resolve2(projectRoot, "dist");
|
|
2759
|
+
const requestedOutput = resolve2(projectRoot, outputArgument);
|
|
2760
|
+
if (requestedOutput === distPath || !isWithin2(distPath, requestedOutput)) {
|
|
1364
2761
|
throw new Error("--out-dir must be a dedicated subdirectory inside the project dist directory");
|
|
1365
2762
|
}
|
|
1366
2763
|
await mkdir(distPath, { recursive: true });
|
|
1367
|
-
const rootRealPath = await
|
|
1368
|
-
const distRealPath = await
|
|
1369
|
-
if (!
|
|
1370
|
-
const relativeOutput =
|
|
1371
|
-
if (!relativeOutput || relativeOutput === ".." || relativeOutput.startsWith(`..${
|
|
2764
|
+
const rootRealPath = await realpath2(projectRoot);
|
|
2765
|
+
const distRealPath = await realpath2(distPath);
|
|
2766
|
+
if (!isWithin2(rootRealPath, distRealPath)) throw new Error("The project dist directory resolves outside the project root");
|
|
2767
|
+
const relativeOutput = relative2(distPath, requestedOutput);
|
|
2768
|
+
if (!relativeOutput || relativeOutput === ".." || relativeOutput.startsWith(`..${sep2}`) || isAbsolute2(relativeOutput)) {
|
|
1372
2769
|
throw new Error("--out-dir must resolve to a subdirectory inside dist");
|
|
1373
2770
|
}
|
|
1374
2771
|
return ensureDirectoryWithoutSymlinks(distRealPath, relativeOutput);
|
|
1375
2772
|
}
|
|
1376
2773
|
async function resolveProjectAsset(projectRoot, projectRootReal, source) {
|
|
1377
|
-
if (!
|
|
1378
|
-
const candidate =
|
|
1379
|
-
if (!
|
|
1380
|
-
const sourceRealPath = await
|
|
1381
|
-
if (!
|
|
1382
|
-
const info = await
|
|
2774
|
+
if (!isSafeRelativePath3(source)) throw new Error(`Asset source is not a safe project-relative path: ${source}`);
|
|
2775
|
+
const candidate = resolve2(projectRoot, source);
|
|
2776
|
+
if (!isWithin2(projectRoot, candidate)) throw new Error(`Asset source escapes project root: ${source}`);
|
|
2777
|
+
const sourceRealPath = await realpath2(candidate);
|
|
2778
|
+
if (!isWithin2(projectRootReal, sourceRealPath)) throw new Error(`Asset source resolves outside project root: ${source}`);
|
|
2779
|
+
const info = await stat2(sourceRealPath);
|
|
1383
2780
|
if (!info.isFile()) throw new Error(`Asset source is not a file: ${source}`);
|
|
1384
2781
|
return sourceRealPath;
|
|
1385
2782
|
}
|
|
@@ -1390,6 +2787,14 @@ async function loadCompilerProject(config, projectRoot, projectRootReal) {
|
|
|
1390
2787
|
if (schemaVersion !== "2") throw new Error(`Unsupported project schemaVersion ${JSON.stringify(schemaVersion)}; expected "1" (IR) or "2" (TSX source)`);
|
|
1391
2788
|
const sourceProject = config;
|
|
1392
2789
|
if (!Array.isArray(sourceProject.components)) throw new Error("Source project components must be an array");
|
|
2790
|
+
if (!Array.isArray(sourceProject.styles ?? [])) throw new Error("Source project styles must be an array");
|
|
2791
|
+
const styles = await Promise.all((sourceProject.styles ?? []).map(async (style, index) => {
|
|
2792
|
+
if (!style || typeof style.source !== "string" || style.outputPath !== void 0 && typeof style.outputPath !== "string") {
|
|
2793
|
+
throw new Error(`styles[${index}] requires a project-relative source and optional outputPath`);
|
|
2794
|
+
}
|
|
2795
|
+
await resolveProjectAsset(projectRoot, projectRootReal, style.source);
|
|
2796
|
+
return style;
|
|
2797
|
+
}));
|
|
1393
2798
|
const components = await Promise.all(sourceProject.components.map(async (entry, index) => {
|
|
1394
2799
|
const prefix = `components[${index}]`;
|
|
1395
2800
|
if (!entry || typeof entry.source !== "string" || typeof entry.manifest !== "string") {
|
|
@@ -1402,11 +2807,11 @@ async function loadCompilerProject(config, projectRoot, projectRootReal) {
|
|
|
1402
2807
|
const manifestPath = await resolveProjectAsset(projectRoot, projectRootReal, entry.manifest);
|
|
1403
2808
|
let manifest;
|
|
1404
2809
|
try {
|
|
1405
|
-
manifest = JSON.parse(await
|
|
2810
|
+
manifest = JSON.parse(await readFile2(manifestPath, "utf8"));
|
|
1406
2811
|
} catch (cause) {
|
|
1407
2812
|
throw new Error(`Unable to read JSON manifest ${entry.manifest}: ${cause instanceof Error ? cause.message : String(cause)}`, { cause });
|
|
1408
2813
|
}
|
|
1409
|
-
const source = await
|
|
2814
|
+
const source = await readFile2(sourcePath, "utf8");
|
|
1410
2815
|
const implementation = parseMitosisSource(source, { filePath: entry.source, enableBlocksSlots: true });
|
|
1411
2816
|
if (entry.exportName && implementation.name !== entry.exportName) {
|
|
1412
2817
|
throw new Error(`${prefix}.exportName is ${JSON.stringify(entry.exportName)}, but Mitosis parsed ${JSON.stringify(implementation.name)} from ${entry.source}`);
|
|
@@ -1414,7 +2819,7 @@ async function loadCompilerProject(config, projectRoot, projectRootReal) {
|
|
|
1414
2819
|
if (!Array.isArray(entry.styles ?? []) || !Array.isArray(entry.assets ?? [])) {
|
|
1415
2820
|
throw new Error(`${prefix}.styles and ${prefix}.assets must be arrays of project-relative file references`);
|
|
1416
2821
|
}
|
|
1417
|
-
const sourceDirectory =
|
|
2822
|
+
const sourceDirectory = dirname2(entry.source).split(sep2).join("/");
|
|
1418
2823
|
return {
|
|
1419
2824
|
manifest,
|
|
1420
2825
|
implementation: { kind: "mitosis", component: implementation },
|
|
@@ -1423,12 +2828,12 @@ async function loadCompilerProject(config, projectRoot, projectRootReal) {
|
|
|
1423
2828
|
assets: entry.assets ?? []
|
|
1424
2829
|
};
|
|
1425
2830
|
}));
|
|
1426
|
-
return { schemaVersion: "1", components };
|
|
2831
|
+
return { schemaVersion: "1", components, styles };
|
|
1427
2832
|
}
|
|
1428
2833
|
async function writeArtifact(outputRoot, relativePath, contents) {
|
|
1429
|
-
if (!
|
|
2834
|
+
if (!isSafeRelativePath3(relativePath)) throw new Error(`Unsafe generated output path: ${relativePath}`);
|
|
1430
2835
|
const parts = relativePath.split("/");
|
|
1431
|
-
const directory = parts.slice(0, -1).join(
|
|
2836
|
+
const directory = parts.slice(0, -1).join(sep2);
|
|
1432
2837
|
const parent = await ensureDirectoryWithoutSymlinks(outputRoot, directory);
|
|
1433
2838
|
const destination = join(parent, parts.at(-1));
|
|
1434
2839
|
try {
|
|
@@ -1457,32 +2862,38 @@ function printDiagnostics(output, diagnostics) {
|
|
|
1457
2862
|
}
|
|
1458
2863
|
async function runCli(args, output = console, cwd = process.cwd()) {
|
|
1459
2864
|
try {
|
|
2865
|
+
if (args[0] === "kit") return await runKitCommand(args.slice(1), output, cwd);
|
|
1460
2866
|
const parsed = parseArguments(args);
|
|
1461
2867
|
if ("help" in parsed) {
|
|
1462
2868
|
output.log(HELP);
|
|
1463
2869
|
return 0;
|
|
1464
2870
|
}
|
|
1465
|
-
const projectRoot =
|
|
1466
|
-
const projectRootReal = await
|
|
1467
|
-
const configPath =
|
|
1468
|
-
const configText = await
|
|
2871
|
+
const projectRoot = resolve2(cwd, parsed.projectRoot ?? ".");
|
|
2872
|
+
const projectRootReal = await realpath2(projectRoot);
|
|
2873
|
+
const configPath = resolve2(projectRoot, parsed.config);
|
|
2874
|
+
const configText = await readFile2(configPath, "utf8");
|
|
1469
2875
|
const projectConfig = JSON.parse(configText);
|
|
1470
2876
|
const project = await loadCompilerProject(projectConfig, projectRoot, projectRootReal);
|
|
1471
2877
|
const registry = createMitosisCompiler();
|
|
2878
|
+
registry.register(bladeTarget);
|
|
1472
2879
|
if (!registry.names().includes(parsed.target)) {
|
|
1473
2880
|
throw new Error(`Unknown compiler target ${JSON.stringify(parsed.target)}. Registered targets: ${registry.names().join(", ")}`);
|
|
1474
2881
|
}
|
|
1475
2882
|
const target = parsed.target;
|
|
1476
|
-
const
|
|
2883
|
+
const targetProject = target === "blade" ? {
|
|
2884
|
+
...project,
|
|
2885
|
+
components: project.components.map((component) => ({ ...component, outputDirectory: "components" }))
|
|
2886
|
+
} : project;
|
|
2887
|
+
const validation = validateCompilerProject(targetProject);
|
|
1477
2888
|
printDiagnostics(output, validation);
|
|
1478
2889
|
if (validation.some((diagnostic) => diagnostic.severity === "error")) return 1;
|
|
1479
|
-
const result = await compileProject(
|
|
2890
|
+
const result = await compileProject(targetProject, registry, { target, targetOptions: parsed.targetOptions });
|
|
1480
2891
|
printDiagnostics(output, result.diagnostics);
|
|
1481
2892
|
if (result.diagnostics.some((diagnostic) => diagnostic.severity === "error")) return 1;
|
|
1482
|
-
const manifests = Object.fromEntries(
|
|
2893
|
+
const manifests = Object.fromEntries(targetProject.components.map(({ manifest }) => [manifest.type, manifest]));
|
|
1483
2894
|
const manifestRegistryFile = {
|
|
1484
2895
|
kind: "registry",
|
|
1485
|
-
path: "manifests.json",
|
|
2896
|
+
path: target === "blade" ? "registry.json" : "manifests.json",
|
|
1486
2897
|
contents: `${JSON.stringify(manifests, null, 2)}
|
|
1487
2898
|
`
|
|
1488
2899
|
};
|
|
@@ -1515,8 +2926,8 @@ async function runCli(args, output = console, cwd = process.cwd()) {
|
|
|
1515
2926
|
for (const [outputPath, sourcePath] of assetSources) {
|
|
1516
2927
|
let temporary = "";
|
|
1517
2928
|
try {
|
|
1518
|
-
if (!
|
|
1519
|
-
const relativeParent =
|
|
2929
|
+
if (!isSafeRelativePath3(outputPath)) throw new Error(`Unsafe asset output path: ${outputPath}`);
|
|
2930
|
+
const relativeParent = dirname2(outputPath).split(sep2).join("/");
|
|
1520
2931
|
const parent = await ensureDirectoryWithoutSymlinks(outputRoot, relativeParent === "." ? "" : relativeParent);
|
|
1521
2932
|
const destination = join(parent, outputPath.split("/").at(-1));
|
|
1522
2933
|
try {
|
|
@@ -1533,7 +2944,7 @@ async function runCli(args, output = console, cwd = process.cwd()) {
|
|
|
1533
2944
|
throw cause;
|
|
1534
2945
|
}
|
|
1535
2946
|
}
|
|
1536
|
-
output.log(`Compiled ${project.components.length} components for ${target}; wrote ${outputFiles.length} generated files and ${result.assets.length} assets to ${
|
|
2947
|
+
output.log(`Compiled ${project.components.length} components for ${target}; wrote ${outputFiles.length} generated files and ${result.assets.length} assets to ${relative2(projectRoot, outputRoot)}.`);
|
|
1537
2948
|
return 0;
|
|
1538
2949
|
} catch (cause) {
|
|
1539
2950
|
output.error(cause instanceof Error ? cause.message : String(cause));
|