@joycostudio/chopper 0.7.0 → 0.8.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.7.0"};
229
+ version: "0.8.0"};
230
230
 
231
231
  // src/codecs/manifest.ts
232
232
  var CODEC_MANIFEST_HASH = "2046d60deb0f9f224743f4404f3c6e8ba5399f81b2811fa50466e93fce3bd322";
@@ -1363,19 +1363,27 @@ function createNodeChopper(options = {}) {
1363
1363
  }
1364
1364
 
1365
1365
  // src/config/validation.ts
1366
- var DEFAULT_OUTPUT = ".chopper-output";
1366
+ var DEFAULT_BUILD_DIRECTORY = ".chopper";
1367
+ var DEFAULT_OUTPUT_DIRECTORY = "compressed";
1367
1368
  function validateConfig(config) {
1368
1369
  if (!config || typeof config !== "object" || Array.isArray(config)) {
1369
1370
  return failure("invalid_recipe", "Config must be an object");
1370
1371
  }
1371
1372
  const candidate = config;
1372
- const unknownField = unknownFieldIssue(candidate, ["output", "outputDirectory", "rules", "concurrency"], "config");
1373
+ const unknownField = unknownFieldIssue(
1374
+ candidate,
1375
+ ["buildDirectory", "outputDirectory", "rules", "concurrency"],
1376
+ "config"
1377
+ );
1373
1378
  if (unknownField) return failure("invalid_recipe", unknownField);
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");
1379
+ if (candidate.buildDirectory !== void 0 && (typeof candidate.buildDirectory !== "string" || !candidate.buildDirectory)) {
1380
+ return failure("invalid_recipe", "Config buildDirectory must be a non-empty string");
1376
1381
  }
1377
- if (candidate.output !== void 0 && !isProjectRelativePath(candidate.output, true)) {
1378
- return failure("invalid_recipe", `Output directory must be a project-relative POSIX path: ${candidate.output}`);
1382
+ if (candidate.buildDirectory !== void 0 && !isProjectRelativePath(candidate.buildDirectory, true)) {
1383
+ return failure(
1384
+ "invalid_recipe",
1385
+ `Build directory must be a project-relative POSIX path: ${candidate.buildDirectory}`
1386
+ );
1379
1387
  }
1380
1388
  if (candidate.outputDirectory !== void 0 && !isSafePathSegment(candidate.outputDirectory)) {
1381
1389
  return failure(
@@ -1394,7 +1402,7 @@ function validateConfig(config) {
1394
1402
  return failure("invalid_recipe", `Config rule ${index} must be an object`);
1395
1403
  }
1396
1404
  const entry = rule;
1397
- const ruleUnknownField = unknownFieldIssue(entry, ["include", "exclude", "recipe", "path"], `config rule ${index}`);
1405
+ const ruleUnknownField = unknownFieldIssue(entry, ["include", "exclude", "recipe"], `config rule ${index}`);
1398
1406
  if (ruleUnknownField) return failure("invalid_recipe", ruleUnknownField);
1399
1407
  if (!isStringArray(entry.include)) {
1400
1408
  return failure("invalid_recipe", `Config rule ${index} include must be an array of glob patterns`);
@@ -1408,19 +1416,14 @@ function validateConfig(config) {
1408
1416
  if (invalidGlob) {
1409
1417
  return failure("invalid_recipe", `Config rule ${index} glob must be project-relative: ${invalidGlob}`);
1410
1418
  }
1411
- if (entry.path !== void 0 && (typeof entry.path !== "string" || !entry.path)) {
1412
- return failure("invalid_recipe", `Config rule ${index} path must be a non-empty string`);
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
- }
1420
1419
  const recipe = validateRecipe(entry.recipe);
1421
1420
  if (!recipe.ok) return recipe;
1422
1421
  }
1423
- return success({ ...config, output: candidate.output ?? DEFAULT_OUTPUT });
1422
+ return success({
1423
+ ...config,
1424
+ buildDirectory: candidate.buildDirectory ?? DEFAULT_BUILD_DIRECTORY,
1425
+ outputDirectory: candidate.outputDirectory ?? DEFAULT_OUTPUT_DIRECTORY
1426
+ });
1424
1427
  }
1425
1428
  function normalizeProjectPath(path) {
1426
1429
  if (typeof path !== "string") return void 0;
@@ -1486,15 +1489,14 @@ function createConfigAssetPlanner(config) {
1486
1489
  if (!normalizedInput) {
1487
1490
  return failure("invalid_recipe", `Asset input must be a project-relative POSIX path: ${input}`);
1488
1491
  }
1489
- if (prepared.data.outputDirectory && isGeneratedOutput(normalizedInput, prepared.data.outputDirectory)) {
1492
+ if (isGeneratedOutput(normalizedInput, prepared.data.outputDirectory)) {
1490
1493
  return success({ kind: "output", input: normalizedInput });
1491
1494
  }
1492
1495
  const planned = /* @__PURE__ */ new Map();
1493
1496
  for (const { rule, matches } of prepared.data.rules) {
1494
1497
  if (!matches(normalizedInput)) continue;
1495
1498
  const output = resolveOutput(rule, normalizedInput, prepared.data.outputDirectory);
1496
- if (!output.ok) return output;
1497
- planned.set(output.data, { input: normalizedInput, output: output.data, rule });
1499
+ planned.set(output, { input: normalizedInput, output, rule });
1498
1500
  }
1499
1501
  const outputs = [...planned.values()];
1500
1502
  return outputs.length ? success({ kind: "source", input: normalizedInput, outputs }) : success({ kind: "unmatched", input: normalizedInput });
@@ -1544,19 +1546,7 @@ function resolveOutput(rule, input, outputDirectory) {
1544
1546
  const dot = base.lastIndexOf(".");
1545
1547
  const name = dot <= 0 ? base : base.slice(0, dot);
1546
1548
  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
- }
1554
- const rendered = template.replaceAll("{dir}/", directory ? `${directory}/` : "").replaceAll("{dir}", directory).replaceAll("{name}", name).replaceAll("{ext}", extension.slice(1));
1555
- if (/\{[^{}]+\}/.test(rendered)) {
1556
- return failure("invalid_recipe", `Output path contains an unsupported placeholder: ${template}`);
1557
- }
1558
- const output = normalizeProjectPath(rendered);
1559
- return output ? success(output) : failure("invalid_recipe", `Output path must stay inside the configured output directory: ${template}`);
1549
+ return `${directory ? `${directory}/` : ""}${outputDirectory}/${name}${extension}`;
1560
1550
  }
1561
1551
  function isGeneratedOutput(input, outputDirectory) {
1562
1552
  return input.split("/").slice(0, -1).includes(outputDirectory);
@@ -1564,8 +1554,8 @@ function isGeneratedOutput(input, outputDirectory) {
1564
1554
 
1565
1555
  // src/config/index.ts
1566
1556
  function createChopper2(config) {
1567
- const { output = DEFAULT_OUTPUT, ...rest } = config;
1568
- const defined = { output, ...rest };
1557
+ const { buildDirectory = DEFAULT_BUILD_DIRECTORY, outputDirectory = DEFAULT_OUTPUT_DIRECTORY, ...rest } = config;
1558
+ const defined = { buildDirectory, outputDirectory, ...rest };
1569
1559
  const planner = createConfigAssetPlanner(defined);
1570
1560
  Object.defineProperties(defined, {
1571
1561
  route: { enumerable: false, value: planner.route },
@@ -1608,17 +1598,22 @@ async function runConfig(options = {}) {
1608
1598
  config.concurrency ?? 2,
1609
1599
  (asset) => executeAsset(asset, mode, cwd, chopper2)
1610
1600
  );
1611
- return success({ mode, output: relative(cwd, resolve(cwd, config.output)), files, totals: summarize(files) });
1601
+ return success({
1602
+ mode,
1603
+ buildDirectory: relative(cwd, resolve(cwd, config.buildDirectory)),
1604
+ files,
1605
+ totals: summarize(files)
1606
+ });
1612
1607
  }
1613
1608
  async function planAssets(config, cwd) {
1614
1609
  const validated = validateConfig(config);
1615
1610
  if (!validated.ok) return validated;
1616
1611
  const configured = validated.data;
1617
1612
  const planned = [];
1618
- const outputRoot = resolve(cwd, configured.output);
1619
- const relativeRoot = relative(cwd, outputRoot);
1613
+ const buildRoot = resolve(cwd, configured.buildDirectory);
1614
+ const relativeRoot = relative(cwd, buildRoot);
1620
1615
  if (isAbsolute(relativeRoot) || relativeRoot === ".." || relativeRoot.startsWith(`..${sep}`)) {
1621
- return failure("invalid_recipe", `Output directory must be inside the project: ${configured.output}`);
1616
+ return failure("invalid_recipe", `Build directory must be inside the project: ${configured.buildDirectory}`);
1622
1617
  }
1623
1618
  const planner = createChopper2(configured);
1624
1619
  const discoveryPatterns = configured.rules.flatMap(
@@ -1632,13 +1627,12 @@ async function planAssets(config, cwd) {
1632
1627
  planned.push(
1633
1628
  ...routed.data.outputs.map(({ input, output, rule }) => ({
1634
1629
  input: resolve(cwd, input),
1635
- output: resolve(outputRoot, output),
1630
+ output: resolve(buildRoot, output),
1636
1631
  rule
1637
1632
  }))
1638
1633
  );
1639
1634
  }
1640
1635
  const resolved = /* @__PURE__ */ new Map();
1641
- const inputs = new Set(planned.map(({ input }) => input));
1642
1636
  for (const asset of planned) {
1643
1637
  const owner = resolved.get(asset.output);
1644
1638
  if (owner && owner.input !== asset.input) {
@@ -1647,9 +1641,6 @@ async function planAssets(config, cwd) {
1647
1641
  `Output collision: ${relative(cwd, owner.input)} and ${relative(cwd, asset.input)} both write ${relative(cwd, asset.output)}`
1648
1642
  );
1649
1643
  }
1650
- if (asset.output !== asset.input && inputs.has(asset.output)) {
1651
- return failure("invalid_recipe", `Output loop: ${relative(cwd, asset.output)} is also a matched input`);
1652
- }
1653
1644
  resolved.set(asset.output, asset);
1654
1645
  }
1655
1646
  return success([...resolved.values()]);
@@ -1808,7 +1799,7 @@ var COMMANDS = {
1808
1799
  "Options:",
1809
1800
  CONFIG_OPTION,
1810
1801
  "",
1811
- "Optimizes each matched asset and writes it into the config output directory",
1802
+ "Optimizes each matched asset and writes it into the config build directory",
1812
1803
  "through a temporary sibling and an atomic rename. Prints the batch report as",
1813
1804
  "JSON and exits 1 when any asset fails."
1814
1805
  ].join("\n"),