@joycostudio/chopper 0.3.0 → 0.4.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
@@ -103,7 +103,7 @@ function outputMimeType(encoding) {
103
103
 
104
104
  // package.json
105
105
  var package_default = {
106
- version: "0.3.0"};
106
+ version: "0.4.0"};
107
107
 
108
108
  // src/codecs/manifest.ts
109
109
  var CODEC_MANIFEST_HASH = "2046d60deb0f9f224743f4404f3c6e8ba5399f81b2811fa50466e93fce3bd322";
@@ -1148,7 +1148,7 @@ async function runConfig(options = {}) {
1148
1148
  const mode = options.mode ?? "write";
1149
1149
  const loaded = options.config ? success(options.config) : await loadConfig(resolve(cwd, options.configPath ?? "chopper.config.ts"));
1150
1150
  if (!loaded.ok) return loaded;
1151
- const planned = await planAssets(loaded.data, cwd, options.outputDirectory);
1151
+ const planned = await planAssets(loaded.data, cwd);
1152
1152
  if (!planned.ok) return planned;
1153
1153
  const chopper2 = options.chopper ?? createNodeChopper();
1154
1154
  const files = await mapConcurrent(
@@ -1156,11 +1156,18 @@ async function runConfig(options = {}) {
1156
1156
  loaded.data.concurrency ?? 2,
1157
1157
  (asset) => executeAsset(asset, mode, cwd, chopper2)
1158
1158
  );
1159
- return success({ mode, files, totals: summarize(files) });
1159
+ return success({ mode, output: relative(cwd, resolve(cwd, loaded.data.output)), files, totals: summarize(files) });
1160
1160
  }
1161
- async function planAssets(config, cwd, outputDirectory) {
1161
+ async function planAssets(config, cwd) {
1162
1162
  const planned = [];
1163
- const outputRoot = outputDirectory ? resolve(cwd, outputDirectory) : void 0;
1163
+ if (typeof config.output !== "string" || !config.output) {
1164
+ return failure("invalid_recipe", "Config output directory is required");
1165
+ }
1166
+ const outputRoot = resolve(cwd, config.output);
1167
+ const relativeRoot = relative(cwd, outputRoot);
1168
+ if (isAbsolute(relativeRoot) || relativeRoot === ".." || relativeRoot.startsWith(`..${sep}`)) {
1169
+ return failure("invalid_recipe", `Output directory must be inside the project: ${config.output}`);
1170
+ }
1164
1171
  for (const rule of config.rules) {
1165
1172
  const valid = validateRecipe(rule.recipe);
1166
1173
  if (!valid.ok) return valid;
@@ -1170,14 +1177,14 @@ async function planAssets(config, cwd, outputDirectory) {
1170
1177
  onlyFiles: true
1171
1178
  });
1172
1179
  for (const match of matches.sort()) {
1173
- const configuredOutput = resolveOutput(rule, match, cwd);
1174
- const relativeOutput = relative(cwd, configuredOutput);
1175
- if (outputRoot && (isAbsolute(relativeOutput) || relativeOutput === ".." || relativeOutput.startsWith(`..${sep}`))) {
1176
- return failure("invalid_recipe", `Cannot stage output outside the project: ${configuredOutput}`);
1180
+ const output = resolveOutput(rule, match, outputRoot);
1181
+ const relativeOutput = relative(outputRoot, output);
1182
+ if (isAbsolute(relativeOutput) || relativeOutput === ".." || relativeOutput.startsWith(`..${sep}`)) {
1183
+ return failure("invalid_recipe", `Output path must stay inside ${config.output}: ${rule.path}`);
1177
1184
  }
1178
1185
  planned.push({
1179
1186
  input: resolve(cwd, match),
1180
- output: outputRoot ? resolve(outputRoot, relativeOutput) : configuredOutput,
1187
+ output,
1181
1188
  rule
1182
1189
  });
1183
1190
  }
@@ -1240,14 +1247,13 @@ async function executeAsset(asset, mode, cwd, chopper2) {
1240
1247
  };
1241
1248
  }
1242
1249
  }
1243
- function resolveOutput(rule, input, cwd) {
1244
- if (rule.output.mode === "replace") return resolve(cwd, input);
1250
+ function resolveOutput(rule, input, outputRoot) {
1245
1251
  const extension = rule.recipe.kind === "glb" ? ".glb" : outputExtension(rule.recipe.encode);
1246
1252
  const inputDirectory = dirname(input) === "." ? "" : dirname(input);
1247
1253
  const name = basename(input, extname(input));
1248
1254
  return resolve(
1249
- cwd,
1250
- rule.output.path.replaceAll("{dir}", inputDirectory).replaceAll("{name}", name).replaceAll("{ext}", extension.slice(1))
1255
+ outputRoot,
1256
+ (rule.path ?? "{dir}/{name}.{ext}").replaceAll("{dir}", inputDirectory).replaceAll("{name}", name).replaceAll("{ext}", extension.slice(1))
1251
1257
  );
1252
1258
  }
1253
1259
  async function writeAtomic(path, bytes) {