@joycostudio/chopper 0.6.0 → 0.7.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/dist/cli/index.js CHANGED
@@ -226,7 +226,7 @@ function latin1(bytes) {
226
226
 
227
227
  // package.json
228
228
  var package_default = {
229
- version: "0.6.0"};
229
+ version: "0.7.0"};
230
230
 
231
231
  // src/codecs/manifest.ts
232
232
  var CODEC_MANIFEST_HASH = "2046d60deb0f9f224743f4404f3c6e8ba5399f81b2811fa50466e93fce3bd322";
@@ -1363,19 +1363,26 @@ function createNodeChopper(options = {}) {
1363
1363
  }
1364
1364
 
1365
1365
  // src/config/validation.ts
1366
+ var DEFAULT_OUTPUT = ".chopper-output";
1366
1367
  function validateConfig(config) {
1367
1368
  if (!config || typeof config !== "object" || Array.isArray(config)) {
1368
1369
  return failure("invalid_recipe", "Config must be an object");
1369
1370
  }
1370
1371
  const candidate = config;
1371
- const unknownField = unknownFieldIssue(candidate, ["output", "rules", "concurrency"], "config");
1372
+ const unknownField = unknownFieldIssue(candidate, ["output", "outputDirectory", "rules", "concurrency"], "config");
1372
1373
  if (unknownField) return failure("invalid_recipe", unknownField);
1373
- if (typeof candidate.output !== "string" || !candidate.output) {
1374
- return failure("invalid_recipe", "Config output directory is required");
1374
+ if (candidate.output !== void 0 && (typeof candidate.output !== "string" || !candidate.output)) {
1375
+ return failure("invalid_recipe", "Config output must be a non-empty string");
1375
1376
  }
1376
- if (!isProjectRelativePath(candidate.output, true)) {
1377
+ if (candidate.output !== void 0 && !isProjectRelativePath(candidate.output, true)) {
1377
1378
  return failure("invalid_recipe", `Output directory must be a project-relative POSIX path: ${candidate.output}`);
1378
1379
  }
1380
+ if (candidate.outputDirectory !== void 0 && !isSafePathSegment(candidate.outputDirectory)) {
1381
+ return failure(
1382
+ "invalid_recipe",
1383
+ `Config outputDirectory must be exactly one safe path segment: ${candidate.outputDirectory}`
1384
+ );
1385
+ }
1379
1386
  if (!Array.isArray(candidate.rules)) {
1380
1387
  return failure("invalid_recipe", "Config rules must be an array");
1381
1388
  }
@@ -1404,10 +1411,16 @@ function validateConfig(config) {
1404
1411
  if (entry.path !== void 0 && (typeof entry.path !== "string" || !entry.path)) {
1405
1412
  return failure("invalid_recipe", `Config rule ${index} path must be a non-empty string`);
1406
1413
  }
1414
+ if (candidate.outputDirectory !== void 0 && entry.path !== void 0) {
1415
+ return failure(
1416
+ "invalid_recipe",
1417
+ `Config rule ${index} path cannot be combined with outputDirectory; authoritative sibling output routing determines the output path`
1418
+ );
1419
+ }
1407
1420
  const recipe = validateRecipe(entry.recipe);
1408
1421
  if (!recipe.ok) return recipe;
1409
1422
  }
1410
- return success(config);
1423
+ return success({ ...config, output: candidate.output ?? DEFAULT_OUTPUT });
1411
1424
  }
1412
1425
  function normalizeProjectPath(path) {
1413
1426
  if (typeof path !== "string") return void 0;
@@ -1427,6 +1440,9 @@ function normalizeProjectPath(path) {
1427
1440
  function isStringArray(value) {
1428
1441
  return Array.isArray(value) && value.every((item) => typeof item === "string" && item.length > 0);
1429
1442
  }
1443
+ function isSafePathSegment(value) {
1444
+ return typeof value === "string" && value.length > 0 && value !== "." && value !== ".." && !value.includes("/") && !value.includes("\\") && !value.includes("\0") && !value.includes(":");
1445
+ }
1430
1446
  function isProjectRelativeGlob(pattern) {
1431
1447
  const path = pattern.startsWith("!") && !pattern.startsWith("!(") ? pattern.slice(1) : pattern;
1432
1448
  return path.length > 0 && !path.includes("\0") && !path.startsWith("/") && !/^[A-Za-z]:\//.test(path) && !path.split("/").includes("..");
@@ -1463,28 +1479,41 @@ var GLOB_OPTIONS = {
1463
1479
  };
1464
1480
  function createConfigAssetPlanner(config) {
1465
1481
  let prepared;
1466
- return (input) => {
1482
+ const route = (input) => {
1467
1483
  prepared ??= prepareRules(config);
1468
1484
  if (!prepared.ok) return prepared;
1469
1485
  const normalizedInput = normalizeProjectPath(input);
1470
1486
  if (!normalizedInput) {
1471
1487
  return failure("invalid_recipe", `Asset input must be a project-relative POSIX path: ${input}`);
1472
1488
  }
1489
+ if (prepared.data.outputDirectory && isGeneratedOutput(normalizedInput, prepared.data.outputDirectory)) {
1490
+ return success({ kind: "output", input: normalizedInput });
1491
+ }
1473
1492
  const planned = /* @__PURE__ */ new Map();
1474
- for (const { rule, matches } of prepared.data) {
1493
+ for (const { rule, matches } of prepared.data.rules) {
1475
1494
  if (!matches(normalizedInput)) continue;
1476
- const output = resolveOutput(rule, normalizedInput);
1495
+ const output = resolveOutput(rule, normalizedInput, prepared.data.outputDirectory);
1477
1496
  if (!output.ok) return output;
1478
1497
  planned.set(output.data, { input: normalizedInput, output: output.data, rule });
1479
1498
  }
1480
- return success([...planned.values()]);
1499
+ const outputs = [...planned.values()];
1500
+ return outputs.length ? success({ kind: "source", input: normalizedInput, outputs }) : success({ kind: "unmatched", input: normalizedInput });
1481
1501
  };
1502
+ const resolve5 = (input) => {
1503
+ const routed = route(input);
1504
+ if (!routed.ok) return routed;
1505
+ return success(routed.data.kind === "source" ? routed.data.outputs : []);
1506
+ };
1507
+ return { route, resolve: resolve5 };
1482
1508
  }
1483
1509
  function prepareRules(config) {
1484
1510
  const validated = validateConfig(config);
1485
1511
  if (!validated.ok) return validated;
1486
1512
  try {
1487
- return success(validated.data.rules.map((rule) => ({ rule, matches: compileRuleMatcher(rule) })));
1513
+ return success({
1514
+ outputDirectory: validated.data.outputDirectory,
1515
+ rules: validated.data.rules.map((rule) => ({ rule, matches: compileRuleMatcher(rule) }))
1516
+ });
1488
1517
  } catch (cause) {
1489
1518
  return failure("invalid_recipe", "Config contains an invalid glob pattern", cause);
1490
1519
  }
@@ -1508,17 +1537,20 @@ function expandDirectoryPattern(pattern) {
1508
1537
  const withoutTrailingSlash = normalized.endsWith("/") ? normalized.slice(0, -1) : normalized;
1509
1538
  return withoutTrailingSlash.endsWith("*") ? withoutTrailingSlash : `${withoutTrailingSlash}/**`;
1510
1539
  }
1511
- function resolveOutput(rule, input) {
1512
- const template = rule.path ?? "{dir}/{name}.{ext}";
1513
- if (template.includes("\\") || template.startsWith("/") || /^[A-Za-z]:\//.test(template)) {
1514
- return failure("invalid_recipe", `Output path must stay inside the configured output directory: ${template}`);
1515
- }
1540
+ function resolveOutput(rule, input, outputDirectory) {
1516
1541
  const slash = input.lastIndexOf("/");
1517
1542
  const directory = slash < 0 ? "" : input.slice(0, slash);
1518
1543
  const base = slash < 0 ? input : input.slice(slash + 1);
1519
1544
  const dot = base.lastIndexOf(".");
1520
1545
  const name = dot <= 0 ? base : base.slice(0, dot);
1521
1546
  const extension = rule.recipe.kind === "glb" ? ".glb" : outputExtension(rule.recipe.encode);
1547
+ if (outputDirectory) {
1548
+ return success(`${directory ? `${directory}/` : ""}${outputDirectory}/${name}${extension}`);
1549
+ }
1550
+ const template = rule.path ?? "{dir}/{name}.{ext}";
1551
+ if (template.includes("\\") || template.startsWith("/") || /^[A-Za-z]:\//.test(template)) {
1552
+ return failure("invalid_recipe", `Output path must stay inside the configured output directory: ${template}`);
1553
+ }
1522
1554
  const rendered = template.replaceAll("{dir}/", directory ? `${directory}/` : "").replaceAll("{dir}", directory).replaceAll("{name}", name).replaceAll("{ext}", extension.slice(1));
1523
1555
  if (/\{[^{}]+\}/.test(rendered)) {
1524
1556
  return failure("invalid_recipe", `Output path contains an unsupported placeholder: ${template}`);
@@ -1526,13 +1558,18 @@ function resolveOutput(rule, input) {
1526
1558
  const output = normalizeProjectPath(rendered);
1527
1559
  return output ? success(output) : failure("invalid_recipe", `Output path must stay inside the configured output directory: ${template}`);
1528
1560
  }
1561
+ function isGeneratedOutput(input, outputDirectory) {
1562
+ return input.split("/").slice(0, -1).includes(outputDirectory);
1563
+ }
1529
1564
 
1530
1565
  // src/config/index.ts
1531
1566
  function createChopper2(config) {
1532
- const defined = { ...config };
1533
- Object.defineProperty(defined, "resolve", {
1534
- enumerable: false,
1535
- value: createConfigAssetPlanner(defined)
1567
+ const { output = DEFAULT_OUTPUT, ...rest } = config;
1568
+ const defined = { output, ...rest };
1569
+ const planner = createConfigAssetPlanner(defined);
1570
+ Object.defineProperties(defined, {
1571
+ route: { enumerable: false, value: planner.route },
1572
+ resolve: { enumerable: false, value: planner.resolve }
1536
1573
  });
1537
1574
  return defined;
1538
1575
  }
@@ -1560,36 +1597,40 @@ async function runConfig(options = {}) {
1560
1597
  const mode = options.mode ?? "write";
1561
1598
  const loaded = options.config ? success(options.config) : await loadConfig(resolve(cwd, options.configPath ?? "chopper.config.ts"));
1562
1599
  if (!loaded.ok) return loaded;
1563
- const planned = await planAssets(loaded.data, cwd);
1600
+ const validated = validateConfig(loaded.data);
1601
+ if (!validated.ok) return validated;
1602
+ const config = validated.data;
1603
+ const planned = await planAssets(config, cwd);
1564
1604
  if (!planned.ok) return planned;
1565
1605
  const chopper2 = options.chopper ?? createNodeChopper();
1566
1606
  const files = await mapConcurrent(
1567
1607
  planned.data,
1568
- loaded.data.concurrency ?? 2,
1608
+ config.concurrency ?? 2,
1569
1609
  (asset) => executeAsset(asset, mode, cwd, chopper2)
1570
1610
  );
1571
- return success({ mode, output: relative(cwd, resolve(cwd, loaded.data.output)), files, totals: summarize(files) });
1611
+ return success({ mode, output: relative(cwd, resolve(cwd, config.output)), files, totals: summarize(files) });
1572
1612
  }
1573
1613
  async function planAssets(config, cwd) {
1574
1614
  const validated = validateConfig(config);
1575
1615
  if (!validated.ok) return validated;
1576
- config = validated.data;
1616
+ const configured = validated.data;
1577
1617
  const planned = [];
1578
- const outputRoot = resolve(cwd, config.output);
1618
+ const outputRoot = resolve(cwd, configured.output);
1579
1619
  const relativeRoot = relative(cwd, outputRoot);
1580
1620
  if (isAbsolute(relativeRoot) || relativeRoot === ".." || relativeRoot.startsWith(`..${sep}`)) {
1581
- return failure("invalid_recipe", `Output directory must be inside the project: ${config.output}`);
1621
+ return failure("invalid_recipe", `Output directory must be inside the project: ${configured.output}`);
1582
1622
  }
1583
- const planner = createChopper2(config);
1584
- const discoveryPatterns = config.rules.flatMap(
1623
+ const planner = createChopper2(configured);
1624
+ const discoveryPatterns = configured.rules.flatMap(
1585
1625
  ({ include }) => include.filter((pattern) => !pattern.startsWith("!") || pattern.startsWith("!("))
1586
1626
  );
1587
1627
  const matches = await glob(discoveryPatterns, { cwd, onlyFiles: true });
1588
1628
  for (const match of matches.sort()) {
1589
- const result = planner.resolve(match);
1590
- if (!result.ok) return result;
1629
+ const routed = planner.route(match);
1630
+ if (!routed.ok) return routed;
1631
+ if (routed.data.kind !== "source") continue;
1591
1632
  planned.push(
1592
- ...result.data.map(({ input, output, rule }) => ({
1633
+ ...routed.data.outputs.map(({ input, output, rule }) => ({
1593
1634
  input: resolve(cwd, input),
1594
1635
  output: resolve(outputRoot, output),
1595
1636
  rule