@joycostudio/chopper 0.5.4 → 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/README.md +65 -1
- package/codecs/manifest.json +1 -1
- package/dist/browser/{chunk-G4APMK7P.js → chunk-33SSR3TA.js} +3 -3
- package/dist/browser/{chunk-G4APMK7P.js.map → chunk-33SSR3TA.js.map} +1 -1
- package/dist/browser/{in-process-5DUPLJNY.js → in-process-KCP5E5RP.js} +3 -3
- package/dist/browser/{in-process-5DUPLJNY.js.map → in-process-KCP5E5RP.js.map} +1 -1
- package/dist/browser/index.js +2 -2
- package/dist/browser/worker.js +4 -2
- package/dist/browser/worker.js.map +1 -1
- package/dist/cli/index.js +217 -42
- package/dist/cli/index.js.map +1 -1
- package/dist/config/index.d.ts +68 -7
- package/dist/config/index.js +416 -2
- package/dist/config/index.js.map +1 -1
- package/dist/index.js +1 -1
- package/dist/index.js.map +1 -1
- package/dist/node/index.d.ts +16 -4
- package/dist/node/index.js +219 -42
- package/dist/node/index.js.map +1 -1
- package/package.json +4 -2
package/dist/node/index.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { createHash } from 'crypto';
|
|
2
2
|
import { readFile, mkdir, mkdtemp, writeFile, rename, rm, readdir, copyFile } from 'fs/promises';
|
|
3
|
-
import { join, resolve, dirname, relative, isAbsolute, sep
|
|
3
|
+
import { join, resolve, dirname, relative, isAbsolute, sep } from 'path';
|
|
4
4
|
import avifEncoderFactory from '@jsquash/avif/codec/enc/avif_enc.js';
|
|
5
5
|
import { defaultOptions as defaultOptions$1 } from '@jsquash/avif/meta.js';
|
|
6
6
|
import jpegDecoderFactory from '@jsquash/jpeg/codec/dec/mozjpeg_dec.js';
|
|
@@ -20,6 +20,7 @@ import { MeshoptDecoder, MeshoptEncoder } from 'meshoptimizer';
|
|
|
20
20
|
import { draco, meshopt, resample, prune, dedup, listTextureSlots } from '@gltf-transform/functions';
|
|
21
21
|
import { createJiti } from 'jiti';
|
|
22
22
|
import { glob } from 'tinyglobby';
|
|
23
|
+
import picomatch from 'picomatch';
|
|
23
24
|
|
|
24
25
|
// src/node/image-engine.ts
|
|
25
26
|
|
|
@@ -430,7 +431,7 @@ function latin1(bytes) {
|
|
|
430
431
|
|
|
431
432
|
// package.json
|
|
432
433
|
var package_default = {
|
|
433
|
-
version: "0.
|
|
434
|
+
version: "0.7.0"};
|
|
434
435
|
|
|
435
436
|
// src/codecs/manifest.ts
|
|
436
437
|
var CODEC_MANIFEST_HASH = "2046d60deb0f9f224743f4404f3c6e8ba5399f81b2811fa50466e93fce3bd322";
|
|
@@ -1323,13 +1324,28 @@ async function optimizeFile({
|
|
|
1323
1324
|
return failure("io_failed", `Could not optimize ${input}`, cause);
|
|
1324
1325
|
}
|
|
1325
1326
|
}
|
|
1327
|
+
|
|
1328
|
+
// src/config/validation.ts
|
|
1329
|
+
var DEFAULT_OUTPUT = ".chopper-output";
|
|
1326
1330
|
function validateConfig(config) {
|
|
1327
1331
|
if (!config || typeof config !== "object" || Array.isArray(config)) {
|
|
1328
1332
|
return failure("invalid_recipe", "Config must be an object");
|
|
1329
1333
|
}
|
|
1330
1334
|
const candidate = config;
|
|
1331
|
-
const unknownField = unknownFieldIssue(candidate, ["output", "rules", "concurrency"], "config");
|
|
1335
|
+
const unknownField = unknownFieldIssue(candidate, ["output", "outputDirectory", "rules", "concurrency"], "config");
|
|
1332
1336
|
if (unknownField) return failure("invalid_recipe", unknownField);
|
|
1337
|
+
if (candidate.output !== void 0 && (typeof candidate.output !== "string" || !candidate.output)) {
|
|
1338
|
+
return failure("invalid_recipe", "Config output must be a non-empty string");
|
|
1339
|
+
}
|
|
1340
|
+
if (candidate.output !== void 0 && !isProjectRelativePath(candidate.output, true)) {
|
|
1341
|
+
return failure("invalid_recipe", `Output directory must be a project-relative POSIX path: ${candidate.output}`);
|
|
1342
|
+
}
|
|
1343
|
+
if (candidate.outputDirectory !== void 0 && !isSafePathSegment(candidate.outputDirectory)) {
|
|
1344
|
+
return failure(
|
|
1345
|
+
"invalid_recipe",
|
|
1346
|
+
`Config outputDirectory must be exactly one safe path segment: ${candidate.outputDirectory}`
|
|
1347
|
+
);
|
|
1348
|
+
}
|
|
1333
1349
|
if (!Array.isArray(candidate.rules)) {
|
|
1334
1350
|
return failure("invalid_recipe", "Config rules must be an array");
|
|
1335
1351
|
}
|
|
@@ -1343,11 +1359,79 @@ function validateConfig(config) {
|
|
|
1343
1359
|
const entry = rule;
|
|
1344
1360
|
const ruleUnknownField = unknownFieldIssue(entry, ["include", "exclude", "recipe", "path"], `config rule ${index}`);
|
|
1345
1361
|
if (ruleUnknownField) return failure("invalid_recipe", ruleUnknownField);
|
|
1362
|
+
if (!isStringArray(entry.include)) {
|
|
1363
|
+
return failure("invalid_recipe", `Config rule ${index} include must be an array of glob patterns`);
|
|
1364
|
+
}
|
|
1365
|
+
if (entry.exclude !== void 0 && !isStringArray(entry.exclude)) {
|
|
1366
|
+
return failure("invalid_recipe", `Config rule ${index} exclude must be an array of glob patterns`);
|
|
1367
|
+
}
|
|
1368
|
+
const invalidGlob = [...entry.include, ...entry.exclude ?? []].find(
|
|
1369
|
+
(pattern) => !isProjectRelativeGlob(pattern)
|
|
1370
|
+
);
|
|
1371
|
+
if (invalidGlob) {
|
|
1372
|
+
return failure("invalid_recipe", `Config rule ${index} glob must be project-relative: ${invalidGlob}`);
|
|
1373
|
+
}
|
|
1374
|
+
if (entry.path !== void 0 && (typeof entry.path !== "string" || !entry.path)) {
|
|
1375
|
+
return failure("invalid_recipe", `Config rule ${index} path must be a non-empty string`);
|
|
1376
|
+
}
|
|
1377
|
+
if (candidate.outputDirectory !== void 0 && entry.path !== void 0) {
|
|
1378
|
+
return failure(
|
|
1379
|
+
"invalid_recipe",
|
|
1380
|
+
`Config rule ${index} path cannot be combined with outputDirectory; authoritative sibling output routing determines the output path`
|
|
1381
|
+
);
|
|
1382
|
+
}
|
|
1346
1383
|
const recipe = validateRecipe(entry.recipe);
|
|
1347
1384
|
if (!recipe.ok) return recipe;
|
|
1348
1385
|
}
|
|
1349
|
-
return success(config);
|
|
1386
|
+
return success({ ...config, output: candidate.output ?? DEFAULT_OUTPUT });
|
|
1387
|
+
}
|
|
1388
|
+
function normalizeProjectPath(path) {
|
|
1389
|
+
if (typeof path !== "string") return void 0;
|
|
1390
|
+
if (!isProjectRelativePath(path, false)) return void 0;
|
|
1391
|
+
const parts = [];
|
|
1392
|
+
for (const part of path.split("/")) {
|
|
1393
|
+
if (!part || part === ".") continue;
|
|
1394
|
+
if (part === "..") {
|
|
1395
|
+
if (!parts.length) return void 0;
|
|
1396
|
+
parts.pop();
|
|
1397
|
+
} else {
|
|
1398
|
+
parts.push(part);
|
|
1399
|
+
}
|
|
1400
|
+
}
|
|
1401
|
+
return parts.join("/") || void 0;
|
|
1402
|
+
}
|
|
1403
|
+
function isStringArray(value) {
|
|
1404
|
+
return Array.isArray(value) && value.every((item) => typeof item === "string" && item.length > 0);
|
|
1405
|
+
}
|
|
1406
|
+
function isSafePathSegment(value) {
|
|
1407
|
+
return typeof value === "string" && value.length > 0 && value !== "." && value !== ".." && !value.includes("/") && !value.includes("\\") && !value.includes("\0") && !value.includes(":");
|
|
1408
|
+
}
|
|
1409
|
+
function isProjectRelativeGlob(pattern) {
|
|
1410
|
+
const path = pattern.startsWith("!") && !pattern.startsWith("!(") ? pattern.slice(1) : pattern;
|
|
1411
|
+
return path.length > 0 && !path.includes("\0") && !path.startsWith("/") && !/^[A-Za-z]:\//.test(path) && !path.split("/").includes("..");
|
|
1350
1412
|
}
|
|
1413
|
+
function isProjectRelativePath(path, allowCurrentDirectory) {
|
|
1414
|
+
if (!path || path.includes("\\") || path.includes("\0") || path.startsWith("/") || /^[A-Za-z]:\//.test(path)) {
|
|
1415
|
+
return false;
|
|
1416
|
+
}
|
|
1417
|
+
const normalized = normalizeSegments(path);
|
|
1418
|
+
return normalized !== void 0 && (allowCurrentDirectory || normalized !== "");
|
|
1419
|
+
}
|
|
1420
|
+
function normalizeSegments(path) {
|
|
1421
|
+
const parts = [];
|
|
1422
|
+
for (const part of path.split("/")) {
|
|
1423
|
+
if (!part || part === ".") continue;
|
|
1424
|
+
if (part === "..") {
|
|
1425
|
+
if (!parts.length) return void 0;
|
|
1426
|
+
parts.pop();
|
|
1427
|
+
} else {
|
|
1428
|
+
parts.push(part);
|
|
1429
|
+
}
|
|
1430
|
+
}
|
|
1431
|
+
return parts.join("/");
|
|
1432
|
+
}
|
|
1433
|
+
|
|
1434
|
+
// src/node/config.ts
|
|
1351
1435
|
async function loadConfig(path = "chopper.config.ts") {
|
|
1352
1436
|
const absolutePath = resolve(path);
|
|
1353
1437
|
try {
|
|
@@ -1365,54 +1449,156 @@ async function loadConfig(path = "chopper.config.ts") {
|
|
|
1365
1449
|
return failure("io_failed", `Could not load ${path}`, cause);
|
|
1366
1450
|
}
|
|
1367
1451
|
}
|
|
1452
|
+
var GLOB_OPTIONS = {
|
|
1453
|
+
dot: false,
|
|
1454
|
+
nobrace: false,
|
|
1455
|
+
nocase: false,
|
|
1456
|
+
noextglob: false,
|
|
1457
|
+
noglobstar: false,
|
|
1458
|
+
posix: true
|
|
1459
|
+
};
|
|
1460
|
+
function createConfigAssetPlanner(config) {
|
|
1461
|
+
let prepared;
|
|
1462
|
+
const route = (input) => {
|
|
1463
|
+
prepared ??= prepareRules(config);
|
|
1464
|
+
if (!prepared.ok) return prepared;
|
|
1465
|
+
const normalizedInput = normalizeProjectPath(input);
|
|
1466
|
+
if (!normalizedInput) {
|
|
1467
|
+
return failure("invalid_recipe", `Asset input must be a project-relative POSIX path: ${input}`);
|
|
1468
|
+
}
|
|
1469
|
+
if (prepared.data.outputDirectory && isGeneratedOutput(normalizedInput, prepared.data.outputDirectory)) {
|
|
1470
|
+
return success({ kind: "output", input: normalizedInput });
|
|
1471
|
+
}
|
|
1472
|
+
const planned = /* @__PURE__ */ new Map();
|
|
1473
|
+
for (const { rule, matches } of prepared.data.rules) {
|
|
1474
|
+
if (!matches(normalizedInput)) continue;
|
|
1475
|
+
const output = resolveOutput(rule, normalizedInput, prepared.data.outputDirectory);
|
|
1476
|
+
if (!output.ok) return output;
|
|
1477
|
+
planned.set(output.data, { input: normalizedInput, output: output.data, rule });
|
|
1478
|
+
}
|
|
1479
|
+
const outputs = [...planned.values()];
|
|
1480
|
+
return outputs.length ? success({ kind: "source", input: normalizedInput, outputs }) : success({ kind: "unmatched", input: normalizedInput });
|
|
1481
|
+
};
|
|
1482
|
+
const resolve5 = (input) => {
|
|
1483
|
+
const routed = route(input);
|
|
1484
|
+
if (!routed.ok) return routed;
|
|
1485
|
+
return success(routed.data.kind === "source" ? routed.data.outputs : []);
|
|
1486
|
+
};
|
|
1487
|
+
return { route, resolve: resolve5 };
|
|
1488
|
+
}
|
|
1489
|
+
function prepareRules(config) {
|
|
1490
|
+
const validated = validateConfig(config);
|
|
1491
|
+
if (!validated.ok) return validated;
|
|
1492
|
+
try {
|
|
1493
|
+
return success({
|
|
1494
|
+
outputDirectory: validated.data.outputDirectory,
|
|
1495
|
+
rules: validated.data.rules.map((rule) => ({ rule, matches: compileRuleMatcher(rule) }))
|
|
1496
|
+
});
|
|
1497
|
+
} catch (cause) {
|
|
1498
|
+
return failure("invalid_recipe", "Config contains an invalid glob pattern", cause);
|
|
1499
|
+
}
|
|
1500
|
+
}
|
|
1501
|
+
function compileRuleMatcher(rule) {
|
|
1502
|
+
const includePatterns = [];
|
|
1503
|
+
const ignorePatterns = (rule.exclude ?? []).filter((pattern) => !pattern.startsWith("!") || pattern.startsWith("!("));
|
|
1504
|
+
for (const pattern of rule.include) {
|
|
1505
|
+
if (pattern.startsWith("!") && !pattern.startsWith("!(")) {
|
|
1506
|
+
if (!pattern.startsWith("!!") || pattern.startsWith("!!(")) ignorePatterns.push(pattern.slice(1));
|
|
1507
|
+
} else {
|
|
1508
|
+
includePatterns.push(pattern);
|
|
1509
|
+
}
|
|
1510
|
+
}
|
|
1511
|
+
const include = picomatch(includePatterns.map(expandDirectoryPattern), GLOB_OPTIONS);
|
|
1512
|
+
const ignore = picomatch(ignorePatterns.map(expandDirectoryPattern), GLOB_OPTIONS);
|
|
1513
|
+
return (input) => include(input) && !ignore(input);
|
|
1514
|
+
}
|
|
1515
|
+
function expandDirectoryPattern(pattern) {
|
|
1516
|
+
const normalized = pattern.split("/").filter((segment) => segment && segment !== ".").join("/") || "**";
|
|
1517
|
+
const withoutTrailingSlash = normalized.endsWith("/") ? normalized.slice(0, -1) : normalized;
|
|
1518
|
+
return withoutTrailingSlash.endsWith("*") ? withoutTrailingSlash : `${withoutTrailingSlash}/**`;
|
|
1519
|
+
}
|
|
1520
|
+
function resolveOutput(rule, input, outputDirectory) {
|
|
1521
|
+
const slash = input.lastIndexOf("/");
|
|
1522
|
+
const directory = slash < 0 ? "" : input.slice(0, slash);
|
|
1523
|
+
const base = slash < 0 ? input : input.slice(slash + 1);
|
|
1524
|
+
const dot = base.lastIndexOf(".");
|
|
1525
|
+
const name = dot <= 0 ? base : base.slice(0, dot);
|
|
1526
|
+
const extension = rule.recipe.kind === "glb" ? ".glb" : outputExtension(rule.recipe.encode);
|
|
1527
|
+
if (outputDirectory) {
|
|
1528
|
+
return success(`${directory ? `${directory}/` : ""}${outputDirectory}/${name}${extension}`);
|
|
1529
|
+
}
|
|
1530
|
+
const template = rule.path ?? "{dir}/{name}.{ext}";
|
|
1531
|
+
if (template.includes("\\") || template.startsWith("/") || /^[A-Za-z]:\//.test(template)) {
|
|
1532
|
+
return failure("invalid_recipe", `Output path must stay inside the configured output directory: ${template}`);
|
|
1533
|
+
}
|
|
1534
|
+
const rendered = template.replaceAll("{dir}/", directory ? `${directory}/` : "").replaceAll("{dir}", directory).replaceAll("{name}", name).replaceAll("{ext}", extension.slice(1));
|
|
1535
|
+
if (/\{[^{}]+\}/.test(rendered)) {
|
|
1536
|
+
return failure("invalid_recipe", `Output path contains an unsupported placeholder: ${template}`);
|
|
1537
|
+
}
|
|
1538
|
+
const output = normalizeProjectPath(rendered);
|
|
1539
|
+
return output ? success(output) : failure("invalid_recipe", `Output path must stay inside the configured output directory: ${template}`);
|
|
1540
|
+
}
|
|
1541
|
+
function isGeneratedOutput(input, outputDirectory) {
|
|
1542
|
+
return input.split("/").slice(0, -1).includes(outputDirectory);
|
|
1543
|
+
}
|
|
1544
|
+
|
|
1545
|
+
// src/config/index.ts
|
|
1546
|
+
function createChopper2(config) {
|
|
1547
|
+
const { output = DEFAULT_OUTPUT, ...rest } = config;
|
|
1548
|
+
const defined = { output, ...rest };
|
|
1549
|
+
const planner = createConfigAssetPlanner(defined);
|
|
1550
|
+
Object.defineProperties(defined, {
|
|
1551
|
+
route: { enumerable: false, value: planner.route },
|
|
1552
|
+
resolve: { enumerable: false, value: planner.resolve }
|
|
1553
|
+
});
|
|
1554
|
+
return defined;
|
|
1555
|
+
}
|
|
1556
|
+
|
|
1557
|
+
// src/node/batch.ts
|
|
1368
1558
|
async function runConfig(options = {}) {
|
|
1369
1559
|
const cwd = resolve(options.cwd ?? process.cwd());
|
|
1370
1560
|
const mode = options.mode ?? "write";
|
|
1371
1561
|
const loaded = options.config ? success(options.config) : await loadConfig(resolve(cwd, options.configPath ?? "chopper.config.ts"));
|
|
1372
1562
|
if (!loaded.ok) return loaded;
|
|
1373
|
-
const
|
|
1563
|
+
const validated = validateConfig(loaded.data);
|
|
1564
|
+
if (!validated.ok) return validated;
|
|
1565
|
+
const config = validated.data;
|
|
1566
|
+
const planned = await planAssets(config, cwd);
|
|
1374
1567
|
if (!planned.ok) return planned;
|
|
1375
1568
|
const chopper2 = options.chopper ?? createNodeChopper();
|
|
1376
1569
|
const files = await mapConcurrent(
|
|
1377
1570
|
planned.data,
|
|
1378
|
-
|
|
1571
|
+
config.concurrency ?? 2,
|
|
1379
1572
|
(asset) => executeAsset(asset, mode, cwd, chopper2)
|
|
1380
1573
|
);
|
|
1381
|
-
return success({ mode, output: relative(cwd, resolve(cwd,
|
|
1574
|
+
return success({ mode, output: relative(cwd, resolve(cwd, config.output)), files, totals: summarize(files) });
|
|
1382
1575
|
}
|
|
1383
1576
|
async function planAssets(config, cwd) {
|
|
1384
1577
|
const validated = validateConfig(config);
|
|
1385
1578
|
if (!validated.ok) return validated;
|
|
1386
|
-
|
|
1579
|
+
const configured = validated.data;
|
|
1387
1580
|
const planned = [];
|
|
1388
|
-
|
|
1389
|
-
return failure("invalid_recipe", "Config output directory is required");
|
|
1390
|
-
}
|
|
1391
|
-
const outputRoot = resolve(cwd, config.output);
|
|
1581
|
+
const outputRoot = resolve(cwd, configured.output);
|
|
1392
1582
|
const relativeRoot = relative(cwd, outputRoot);
|
|
1393
1583
|
if (isAbsolute(relativeRoot) || relativeRoot === ".." || relativeRoot.startsWith(`..${sep}`)) {
|
|
1394
|
-
return failure("invalid_recipe", `Output directory must be inside the project: ${
|
|
1395
|
-
}
|
|
1396
|
-
|
|
1397
|
-
|
|
1398
|
-
|
|
1399
|
-
|
|
1400
|
-
|
|
1401
|
-
|
|
1402
|
-
|
|
1403
|
-
|
|
1404
|
-
|
|
1405
|
-
|
|
1406
|
-
|
|
1407
|
-
|
|
1408
|
-
|
|
1409
|
-
}
|
|
1410
|
-
planned.push({
|
|
1411
|
-
input: resolve(cwd, match),
|
|
1412
|
-
output,
|
|
1584
|
+
return failure("invalid_recipe", `Output directory must be inside the project: ${configured.output}`);
|
|
1585
|
+
}
|
|
1586
|
+
const planner = createChopper2(configured);
|
|
1587
|
+
const discoveryPatterns = configured.rules.flatMap(
|
|
1588
|
+
({ include }) => include.filter((pattern) => !pattern.startsWith("!") || pattern.startsWith("!("))
|
|
1589
|
+
);
|
|
1590
|
+
const matches = await glob(discoveryPatterns, { cwd, onlyFiles: true });
|
|
1591
|
+
for (const match of matches.sort()) {
|
|
1592
|
+
const routed = planner.route(match);
|
|
1593
|
+
if (!routed.ok) return routed;
|
|
1594
|
+
if (routed.data.kind !== "source") continue;
|
|
1595
|
+
planned.push(
|
|
1596
|
+
...routed.data.outputs.map(({ input, output, rule }) => ({
|
|
1597
|
+
input: resolve(cwd, input),
|
|
1598
|
+
output: resolve(outputRoot, output),
|
|
1413
1599
|
rule
|
|
1414
|
-
})
|
|
1415
|
-
|
|
1600
|
+
}))
|
|
1601
|
+
);
|
|
1416
1602
|
}
|
|
1417
1603
|
const resolved = /* @__PURE__ */ new Map();
|
|
1418
1604
|
const inputs = new Set(planned.map(({ input }) => input));
|
|
@@ -1473,15 +1659,6 @@ async function executeAsset(asset, mode, cwd, chopper2) {
|
|
|
1473
1659
|
};
|
|
1474
1660
|
}
|
|
1475
1661
|
}
|
|
1476
|
-
function resolveOutput(rule, input, outputRoot) {
|
|
1477
|
-
const extension = rule.recipe.kind === "glb" ? ".glb" : outputExtension(rule.recipe.encode);
|
|
1478
|
-
const inputDirectory = dirname(input) === "." ? "" : dirname(input);
|
|
1479
|
-
const name = basename(input, extname(input));
|
|
1480
|
-
return resolve(
|
|
1481
|
-
outputRoot,
|
|
1482
|
-
(rule.path ?? "{dir}/{name}.{ext}").replaceAll("{dir}", inputDirectory).replaceAll("{name}", name).replaceAll("{ext}", extension.slice(1))
|
|
1483
|
-
);
|
|
1484
|
-
}
|
|
1485
1662
|
async function writeAtomic(path, bytes) {
|
|
1486
1663
|
await mkdir(dirname(path), { recursive: true });
|
|
1487
1664
|
const temporaryDirectory = await mkdtemp(join(dirname(path), ".chopper-"));
|