@joycostudio/chopper 0.8.1 → 0.9.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.8.1"};
229
+ version: "0.9.0"};
230
230
 
231
231
  // src/codecs/manifest.ts
232
232
  var CODEC_MANIFEST_HASH = "1a43d902a113a7dcfa7801bcbd90998e401898dbad555f913e1e7f4352d9c63c";
@@ -1648,7 +1648,11 @@ function validateConfig(config) {
1648
1648
  return failure("invalid_recipe", `Config rule ${index} must be an object`);
1649
1649
  }
1650
1650
  const entry = rule;
1651
- const ruleUnknownField = unknownFieldIssue(entry, ["include", "exclude", "recipe"], `config rule ${index}`);
1651
+ const ruleUnknownField = unknownFieldIssue(
1652
+ entry,
1653
+ ["include", "exclude", "recipe", "filename"],
1654
+ `config rule ${index}`
1655
+ );
1652
1656
  if (ruleUnknownField) return failure("invalid_recipe", ruleUnknownField);
1653
1657
  if (!isStringArray(entry.include)) {
1654
1658
  return failure("invalid_recipe", `Config rule ${index} include must be an array of glob patterns`);
@@ -1662,6 +1666,9 @@ function validateConfig(config) {
1662
1666
  if (invalidGlob) {
1663
1667
  return failure("invalid_recipe", `Config rule ${index} glob must be project-relative: ${invalidGlob}`);
1664
1668
  }
1669
+ if (entry.filename !== void 0 && typeof entry.filename !== "function") {
1670
+ return failure("invalid_recipe", `Config rule ${index} filename must be a function`);
1671
+ }
1665
1672
  const recipe = validateRecipe(entry.recipe);
1666
1673
  if (!recipe.ok) return recipe;
1667
1674
  }
@@ -1742,7 +1749,8 @@ function createConfigAssetPlanner(config) {
1742
1749
  for (const { rule, matches } of prepared.data.rules) {
1743
1750
  if (!matches(normalizedInput)) continue;
1744
1751
  const output = resolveOutput(rule, normalizedInput, prepared.data.outputDirectory);
1745
- planned.set(output, { input: normalizedInput, output, rule });
1752
+ if (!output.ok) return output;
1753
+ planned.set(output.data, { input: normalizedInput, output: output.data, rule });
1746
1754
  }
1747
1755
  const outputs = [...planned.values()];
1748
1756
  return outputs.length ? success({ kind: "source", input: normalizedInput, outputs }) : success({ kind: "unmatched", input: normalizedInput });
@@ -1792,23 +1800,25 @@ function resolveOutput(rule, input, outputDirectory) {
1792
1800
  const dot = base.lastIndexOf(".");
1793
1801
  const name = dot <= 0 ? base : base.slice(0, dot);
1794
1802
  const extension = rule.recipe.kind === "glb" ? ".glb" : outputExtension(rule.recipe.encode);
1795
- return `${directory ? `${directory}/` : ""}${outputDirectory}/${name}${extension}`;
1803
+ let filename = name;
1804
+ if (rule.filename) {
1805
+ try {
1806
+ filename = rule.filename({ input, directory, name, recipe: rule.recipe });
1807
+ } catch (cause) {
1808
+ return failure("invalid_recipe", `Config rule filename threw for ${input}`, cause);
1809
+ }
1810
+ if (!isSafePathSegment(filename)) {
1811
+ return failure(
1812
+ "invalid_recipe",
1813
+ `Config rule filename must return exactly one safe path segment for ${input}: ${String(filename)}`
1814
+ );
1815
+ }
1816
+ }
1817
+ return success(`${directory ? `${directory}/` : ""}${outputDirectory}/${filename}${extension}`);
1796
1818
  }
1797
1819
  function isGeneratedOutput(input, outputDirectory) {
1798
1820
  return input.split("/").slice(0, -1).includes(outputDirectory);
1799
1821
  }
1800
-
1801
- // src/config/index.ts
1802
- function createChopper2(config) {
1803
- const { buildDirectory = DEFAULT_BUILD_DIRECTORY, outputDirectory = DEFAULT_OUTPUT_DIRECTORY, ...rest } = config;
1804
- const defined = { buildDirectory, outputDirectory, ...rest };
1805
- const planner = createConfigAssetPlanner(defined);
1806
- Object.defineProperties(defined, {
1807
- route: { enumerable: false, value: planner.route },
1808
- resolve: { enumerable: false, value: planner.resolve }
1809
- });
1810
- return defined;
1811
- }
1812
1822
  async function loadConfig(path = "chopper.config.ts") {
1813
1823
  const absolutePath = resolve(path);
1814
1824
  try {
@@ -1861,7 +1871,7 @@ async function planAssets(config, cwd) {
1861
1871
  if (isAbsolute(relativeRoot) || relativeRoot === ".." || relativeRoot.startsWith(`..${sep}`)) {
1862
1872
  return failure("invalid_recipe", `Build directory must be inside the project: ${configured.buildDirectory}`);
1863
1873
  }
1864
- const planner = createChopper2(configured);
1874
+ const planner = createConfigAssetPlanner(configured);
1865
1875
  const discoveryPatterns = configured.rules.flatMap(
1866
1876
  ({ include }) => include.filter((pattern) => !pattern.startsWith("!") || pattern.startsWith("!("))
1867
1877
  );